Skip to content
Snippets Groups Projects
Commit d808720f authored by ftomi's avatar ftomi
Browse files

Matrix server added

parent 616116d8
No related branches found
No related tags found
No related merge requests found
...@@ -23,7 +23,6 @@ tags ...@@ -23,7 +23,6 @@ tags
.DS_Store .DS_Store
.directory .directory
*.debug *.debug
Makefile*
*.prl *.prl
*.app *.app
moc_*.cpp moc_*.cpp
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"protocol": "sftp", "protocol": "sftp",
"port": 22, "port": 22,
"username": "pi", "username": "pi",
"password": "kiskutya", "privateKeyPath": "c:\\Users\\Tomi\\.ssh\\id_rsa",
"remotePath": "/home/pi/Documents/minimatrixrpi/test_anim", "remotePath": "/home/pi/Documents/minimatrixrpi/test_anim",
"uploadOnSave": true "uploadOnSave": true
} }
CC = g++
# -g or nothing
DEBUG = -g
PROJECT = minimatrixrpi
COMPONENTS = animation mxframe spiframe spisender e131 matrix_server
COMPONENTS_O = $(addsuffix .o,$(COMPONENTS))
all: $(PROJECT)
%.o: %.cpp %.h
$(CC) -c $(DEBUG) $<
test_server.o: test_server.cpp
$(CC) -c $(DEBUG) $<
test_server: test_server.o e131.o
$(CC) $^ -o $@
test_client.o: test_client.cpp
$(CC) -c $(DEBUG) $<
test_client: test_client.o e131.o
$(CC) $^ -o $@
$(PROJECT).o: $(PROJECT).cpp
$(CC) -c $(DEBUG) $<
$(PROJECT): $(PROJECT).o $(COMPONENTS_O)
$(CC) $^ -lwiringPi $(DEBUG) -o $@
chmod +x $(PROJECT)
clean:
rm -f $(PROJECT) test_server test_client *.o
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdint.h>
#include "matrix_server.h"
MatrixServer::MatrixServer()
{
buffer = (uint8_t*)malloc(BUFFER_SIZE);
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if (bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
}
MatrixServer::~MatrixServer()
{
free(buffer);
}
bool MatrixServer::receive()
{
size_t len = sizeof(cliaddr); //len is value/resuslt
datagram_size = recvfrom(sockfd, (char *)buffer, BUFFER_SIZE,
MSG_WAITALL, (struct sockaddr *)&cliaddr,
&len);
buffer[datagram_size] = '\0';
return parseDatagram(buffer, datagram_size);
}
bool MatrixServer::parseDatagram(uint8_t* buffer, size_t len)
{
if (len != 1250)
return false;
// Protocol type must be 2.
if (buffer[0] != 2)
return false;
frame = MxFrame();
int k = 0;
int x = 0;
int y = 0;
for (size_t i = 2; i < len; i++)
{
uint8_t color = buffer[i];
uint8_t colorH = (color >> 4) & 0x07;
uint8_t colorL = color & 0x07;
if (k % 3 == 0)
{
frame.pixels[y][x].r = colorH;
frame.pixels[y][x].g = colorL;
}
else if (k % 3 == 1)
{
frame.pixels[y][x].g = colorH;
frame.pixels[y][x].b = colorL;
// Next 4 bits corresponds to the next pixel
x++;
if (x >= 32)
{
x = 0;
y++;
}
}
else if (k % 3 == 2)
{
frame.pixels[y][x].b = colorH;
// Next 4 bits corresponds to the next pixel
x++;
if (x >= 32)
{
x = 0;
y++;
}
frame.pixels[y][x].r = colorL;
}
k = (k + 2) % 3;
}
return true;
}
MxFrame MatrixServer::getFrame()
{
return frame;
}
#ifndef MATRIX_SERVER_H
#define MATRIX_SERVER_H
#include <stdint.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "mxframe.h"
class MatrixServer
{
private:
const int BUFFER_SIZE = 2000;
const int PORT = 10000;
int sockfd = -1;
uint8_t* buffer;
size_t datagram_size;
struct sockaddr_in servaddr, cliaddr;
MxFrame frame;
bool parseDatagram(uint8_t* buffer, size_t len);
public:
MatrixServer();
~MatrixServer();
bool receive();
MxFrame getFrame();
};
#endif // MATRIX_SERVER_H
\ No newline at end of file
...@@ -3,21 +3,33 @@ ...@@ -3,21 +3,33 @@
#include "spiframe.h" #include "spiframe.h"
#include "spisender.h" #include "spisender.h"
#include "color.hpp" #include "color.hpp"
#include "matrix_server.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Animation anim; MatrixServer server;
anim.openFile("asdasd.qp4");
MxFrame frame;
SpiSender spisender; SpiSender spisender;
while (!anim.eof()) printf("Started\n");
while (1)
{ {
frame = anim.nextFrame(); if (server.receive())
spisender.sendFrame(SpiFrame(frame)); {
usleep(1000*frame.length); spisender.sendFrame(SpiFrame(server.getFrame()));
}
} }
// Animation anim;
// anim.openFile("bss.qp4");
// MxFrame frame;
// SpiSender spisender;
// while (!anim.eof())
// {
// frame = anim.nextFrame();
// spisender.sendFrame(SpiFrame(frame));
// usleep(1000*frame.length);
// }
// spisender.sendFrame(SpiFrame(MxFrame()));
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment