Skip to content

Commit

Permalink
Merge pull request #1 from janvdb-eu/feature/import_code
Browse files Browse the repository at this point in the history
Feature/import code
  • Loading branch information
janvdbergh authored Oct 25, 2017
2 parents cd57c2d + 1b2cf94 commit bf5994f
Show file tree
Hide file tree
Showing 9 changed files with 140,292 additions and 0 deletions.
125 changes: 125 additions & 0 deletions LedMatrixDisplay.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <iostream>
#include <stdio.h>
#include "LedMatrixDisplay.h"

const int DIGIT_START_X = 7;
const int DIGIT_START_Y = 12;
const char *BDF_SMALL_FONT_FILE = "fonts/6x9.bdf";
const char *BDF_LARGE_FONT_FILE = "fonts/9x18.bdf";

using namespace rgb_matrix;

LedMatrixDisplay::LedMatrixDisplay() : _rgbMatrix(0), _frameCanvas(0), _color(255, 255, 255), _smallFont(0), _largeFont(0) {}

LedMatrixDisplay::~LedMatrixDisplay() {
delete _rgbMatrix;
delete _smallFont;
}

bool LedMatrixDisplay::Initialize(int argc, char *argv[]) {
RGBMatrix::Options options;
options.hardware_mapping = "adafruit-hat-pwm";
options.rows = 32;
options.chain_length = 2;
options.parallel = 1;

RuntimeOptions runtimeOptions;
runtimeOptions.gpio_slowdown = 2;
_rgbMatrix = rgb_matrix::CreateMatrixFromFlags(&argc, &argv, &options, &runtimeOptions);

if (_rgbMatrix == 0) {
std::cerr << "Could not initialize matrix" << std::endl;
rgb_matrix::PrintMatrixFlags(stderr);
return false;
}

_frameCanvas = _rgbMatrix->CreateFrameCanvas();

_smallFont = new Font();
if (!_smallFont->LoadFont(BDF_SMALL_FONT_FILE)) {
std::cerr << "Could not load font " << BDF_SMALL_FONT_FILE << std::endl;
return false;
}

_largeFont = new Font();
if (!_largeFont->LoadFont(BDF_LARGE_FONT_FILE)) {
std::cerr << "Could not load font " << BDF_LARGE_FONT_FILE << std::endl;
return false;
}

return true;
}

void LedMatrixDisplay::SetColor(uint8_t r, uint8_t g, uint8_t b) {
_color = Color(r, g, b);
}

void LedMatrixDisplay::Clear() {
_frameCanvas->Clear();
}

void LedMatrixDisplay::DrawPixel(int x, int y) {
_frameCanvas->SetPixel(x, y, _color.r, _color.g, _color.b);
}

void LedMatrixDisplay::DrawRectangle(int x, int y, int width, int height) {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
_frameCanvas->SetPixel(x + i, y + j, _color.r, _color.g, _color.b);
}
}
}

void LedMatrixDisplay::DrawDigit(int position, int digit) {
int startX = DIGIT_START_X + position * 12;
if (position >= 2) {
startX += 4;
}

// top
if (digit==0 || digit==2 || digit==3 || digit==5 || digit==6 || digit==7 || digit==8 || digit==9) {
DrawRectangle(startX, DIGIT_START_Y, 10, 2);
}

// middle
if (digit==2 || digit==3 || digit==4 || digit==5 || digit==6 || digit==8 || digit==9) {
DrawRectangle(startX, DIGIT_START_Y + 8, 10, 2);
}

// bottom
if (digit==0 || digit==2 || digit==3 || digit==5 || digit==6 || digit==8 || digit==9) {
DrawRectangle(startX, DIGIT_START_Y + 16, 10, 2);
}

// left top
if (digit==0 || digit==4 || digit==5 || digit==6 || digit==8 || digit==9) {
DrawRectangle(startX, DIGIT_START_Y, 2, 10);
}

// right top
if (digit==0 || digit==1 || digit==2 || digit==3 || digit==4 || digit==7 || digit==8 || digit==9) {
DrawRectangle(startX + 8, DIGIT_START_Y, 2, 10);
}

// left bottom
if (digit==0 || digit==2 || digit==6 || digit==8) {
DrawRectangle(startX, DIGIT_START_Y + 8, 2, 10);
}

// right bottom
if (digit==0 || digit==1 || digit==3 || digit==4 || digit==5 || digit==6 || digit==7 || digit==8 || digit==9) {
DrawRectangle(startX + 8, DIGIT_START_Y + 8, 2, 10);
}
}

void LedMatrixDisplay::DrawSmallText(int x, int y, std::string text) {
DrawText(_frameCanvas, *_smallFont, x, y, _color, text.c_str());
}

void LedMatrixDisplay::DrawLargeText(int x, int y, std::string text) {
DrawText(_frameCanvas, *_largeFont, x, y, _color, text.c_str());
}

void LedMatrixDisplay::Show() {
_frameCanvas = _rgbMatrix->SwapOnVSync(_frameCanvas);
}
32 changes: 32 additions & 0 deletions LedMatrixDisplay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DISPLAYSERVER_LEDMATRIXDISPLAY_H
#define DISPLAYSERVER_LEDMATRIXDISPLAY_H

#include <led-matrix.h>
#include <graphics.h>

class LedMatrixDisplay {
public:
LedMatrixDisplay();
virtual ~LedMatrixDisplay();

bool Initialize(int argc, char **argv);

void SetColor(uint8_t r, uint8_t g, uint8_t b);

void Clear();
void DrawPixel(int x, int y);
void DrawRectangle(int x, int y, int width, int height);
void DrawDigit(int position, int digit);
void DrawSmallText(int x, int y, std::string text);
void DrawLargeText(int x, int y, std::string text);

void Show();
private:
rgb_matrix::RGBMatrix* _rgbMatrix;
rgb_matrix::FrameCanvas *_frameCanvas;
rgb_matrix::Color _color;
rgb_matrix::Font* _smallFont;
rgb_matrix::Font* _largeFont;
};

#endif //DISPLAYSERVER_LEDMATRIXDISPLAY_H
17 changes: 17 additions & 0 deletions Main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <unistd.h>

#include "LedMatrixDisplay.h"
#include "NetworkServer.h"

int main(int argc, char* argv[]) {
LedMatrixDisplay ledMatrixDisplay;
if (!ledMatrixDisplay.Initialize(argc, argv)) {
return 1;
}

NetworkServer networkServer(ledMatrixDisplay);
networkServer.RunServer(1236);

return 0;
}
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CFLAGS=-Wall -O3 -g -std=c++11
CXXFLAGS=$(CFLAGS)
LDFLAGS+=-lrgbmatrix -lrt -lm -lpthread

all : bin/displayserver

bin/displayserver : obj/Main.o obj/LedMatrixDisplay.o obj/NetworkServer.o
mkdir -p bin
$(CXX) $^ -o $@ $(LDFLAGS)

obj/%.o : %.cc
mkdir -p obj
$(CXX) -I/usr/local/include/rpi-rgb-led-matrix $(CXXFLAGS) -c -o $@ $<

clean:
rm -rf obj bin
125 changes: 125 additions & 0 deletions NetworkServer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <regex>

#include "NetworkServer.h"

std::basic_regex<char> REGEX_CLEAR("^clear\\s*");
std::basic_regex<char> REGEX_COLOR("^color (\\d+) (\\d+) (\\d+)\\s*");
std::basic_regex<char> REGEX_SHOW("^show\\s*");
std::basic_regex<char> REGEX_PIXEL("^pixel (\\d+) (\\d+)\\s*");
std::basic_regex<char> REGEX_RECTANGLE("^rectangle (\\d+) (\\d+) (\\d+) (\\d+)\\s*");
std::basic_regex<char> REGEX_DIGIT("^digit (\\d+) (\\d+)\\s*");
std::basic_regex<char> REGEX_SMALL_TEXT("^smalltext (\\d+) (\\d+) \"([^\"]*)\"\\s*");
std::basic_regex<char> REGEX_LARGE_TEXT("^largetext (-?\\d+) (-?\\d+) \"([^\"]*)\"\\s*");

void NetworkServer::RunServer(int port) {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
perror("Error opening server socket");
return;
}

struct sockaddr_in serverAddress;
bzero((char *) &serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(port);
if (bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
perror("Error on binding");
return;
}
listen(serverSocket, 0);

while(true) {
handleClientConnection(serverSocket);
}

close(serverSocket);
}

void NetworkServer::handleClientConnection(int serverSocket) {
struct sockaddr_in clientAddress;
socklen_t clientAddressLength = sizeof(clientAddress);
int clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, &clientAddressLength);
if (clientSocket < 0) {
perror("Error on accept");
return;
}

char buffer[256];
bzero(buffer, 256);

int bytesRead = read(clientSocket, buffer, 255);
while (bytesRead > 0) {
buffer[bytesRead] = '\0';
_messages = _messages + std::string(buffer);
handleMessage();

bytesRead = read(clientSocket, buffer, 255);
}
if (bytesRead < 0) {
perror("Error reading from socket");
}
close(clientSocket);
}

void NetworkServer::handleMessage() {
size_t index = _messages.find('\n');
while(index != std::string::npos) {
std::string message = _messages.substr(0, index);
std::cout << message << std::endl;
_messages = _messages.substr(index + 1);
index = _messages.find('\n');

std::smatch match;
if (regex_match(message, REGEX_CLEAR)) {
_ledMatrixDisplay.Clear();
} else if (regex_match(message, REGEX_SHOW)) {
_ledMatrixDisplay.Show();
} else if (regex_match(message, match, REGEX_COLOR)) {
int r = atoi(match[1].str().c_str());
int g = atoi(match[2].str().c_str());
int b = atoi(match[3].str().c_str());

_ledMatrixDisplay.SetColor(r, g, b);
} else if (regex_match(message, match, REGEX_PIXEL)) {
int x = atoi(match[1].str().c_str());
int y = atoi(match[2].str().c_str());

_ledMatrixDisplay.DrawPixel(x, y);
} else if (regex_match(message, match, REGEX_RECTANGLE)) {
int x = atoi(match[1].str().c_str());
int y = atoi(match[2].str().c_str());
int width = atoi(match[3].str().c_str());
int height = atoi(match[4].str().c_str());

_ledMatrixDisplay.DrawRectangle(x, y, width, height);
} else if (regex_match(message, match, REGEX_DIGIT)) {
int position = atoi(match[1].str().c_str());
int digit = atoi(match[2].str().c_str());

_ledMatrixDisplay.DrawDigit(position, digit);
} else if (regex_match(message, match, REGEX_SMALL_TEXT)) {
int x = atoi(match[1].str().c_str());
int y = atoi(match[2].str().c_str());
std::string text = match[3].str();

_ledMatrixDisplay.DrawSmallText(x, y, text);
} else if (regex_match(message, match, REGEX_LARGE_TEXT)) {
int x = atoi(match[1].str().c_str());
int y = atoi(match[2].str().c_str());
std::string text = match[3].str();

_ledMatrixDisplay.DrawLargeText(x, y, text);
} else {
std::cerr << "Invalid message **" << message << "**" << std::endl;
}
}
}
20 changes: 20 additions & 0 deletions NetworkServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DISPLAYSERVER_NETWORK_SERVER_H
#define DISPLAYSERVER_NETWORK_SERVER_H

#include <string>
#include "LedMatrixDisplay.h"

class NetworkServer {
public:
NetworkServer(LedMatrixDisplay& ledMatrixDisplay): _ledMatrixDisplay(ledMatrixDisplay), _messages("") {}

void RunServer(int port);
private:
LedMatrixDisplay& _ledMatrixDisplay;
std::string _messages;

void handleClientConnection(int serverSocket);
void handleMessage();
};

#endif //DISPLAYSERVER_NETWORK_SERVER_H
Loading

0 comments on commit bf5994f

Please sign in to comment.