diff --git a/.gitignore b/.gitignore index 553ebd940d06590ce62057ea7be8631770e14671..353e92a86c7b9e2f91cd2c580e3046d239609df2 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ tags .DS_Store .directory *.debug -Makefile* *.prl *.app moc_*.cpp diff --git a/test_anim/.vscode/sftp.json b/test_anim/.vscode/sftp.json index 1dd67f01adeeb6c5f3b0102a1070ff4011828f43..6c105d6a6cedaf28354587329c54a4ae64c12496 100644 --- a/test_anim/.vscode/sftp.json +++ b/test_anim/.vscode/sftp.json @@ -4,7 +4,7 @@ "protocol": "sftp", "port": 22, "username": "pi", - "password": "kiskutya", + "privateKeyPath": "c:\\Users\\Tomi\\.ssh\\id_rsa", "remotePath": "/home/pi/Documents/minimatrixrpi/test_anim", "uploadOnSave": true } diff --git a/test_anim/Makefile b/test_anim/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..c5bbd3654376f336fd37fe3d846cb0f8438a5ef2 --- /dev/null +++ b/test_anim/Makefile @@ -0,0 +1,36 @@ +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 + diff --git a/test_anim/matrix_server.cpp b/test_anim/matrix_server.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccbe2278ee453f7522dbf1fc4057f1ad5f1cc610 --- /dev/null +++ b/test_anim/matrix_server.cpp @@ -0,0 +1,107 @@ +// 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; +} diff --git a/test_anim/matrix_server.h b/test_anim/matrix_server.h new file mode 100644 index 0000000000000000000000000000000000000000..a12a4f227c9cfaeb9fc85b6a2f01ed0e981676dc --- /dev/null +++ b/test_anim/matrix_server.h @@ -0,0 +1,28 @@ +#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 diff --git a/test_anim/minimatrixrpi.cpp b/test_anim/minimatrixrpi.cpp index 3cc9b0a6e0f3d7b86ebcb05da7d41267feff8be7..1f2a1e79d9fa1f5b6e44d111653af95e41c7c67d 100644 --- a/test_anim/minimatrixrpi.cpp +++ b/test_anim/minimatrixrpi.cpp @@ -3,21 +3,33 @@ #include "spiframe.h" #include "spisender.h" #include "color.hpp" +#include "matrix_server.h" #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) { - Animation anim; - anim.openFile("asdasd.qp4"); - MxFrame frame; + MatrixServer server; SpiSender spisender; - while (!anim.eof()) + printf("Started\n"); + while (1) { - frame = anim.nextFrame(); - spisender.sendFrame(SpiFrame(frame)); - usleep(1000*frame.length); + if (server.receive()) + { + 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; }