Skip to content

Instantly share code, notes, and snippets.

@JokerCatz
Created December 1, 2018 05:06
Show Gist options
  • Save JokerCatz/b39143861480ef174069bc5c718b142c to your computer and use it in GitHub Desktop.
Save JokerCatz/b39143861480ef174069bc5c718b142c to your computer and use it in GitHub Desktop.
rpi-rgb-led-matrix unix socket server
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "led-matrix-c.h"
struct RGBLedMatrix *matrix;
struct RGBLedMatrixOptions options;
struct LedCanvas *canvas;
char const *socket_path = "./tmp/socket";
int main(int argc, char **argv){
// for socket srv
struct sockaddr_un addr;
char buf[1024]; // 64width x 64height x 3color = 1 frame
int fd, cl, rc;
// for led
struct RGBLedMatrixOptions options;
struct LedCanvas *canvas;
int width, height, r = 0, g = 0, b = 0;
memset(&options, 0, sizeof(options));
options.rows = 64;
options.cols = 64;
options.chain_length = 1;
options.led_rgb_sequence = "BRG";
options.multiplexing = 1; // 32row x 2 = 64row
options.pwm_lsb_nanoseconds = 260;
matrix = led_matrix_create_from_options(&options, &argc, &argv);
if(matrix == NULL){
return 1;
}
canvas = led_matrix_create_offscreen_canvas(matrix);
led_canvas_get_size(canvas, &width, &height);
fprintf(stdout, "Size: %dx%d. Hardware gpio mapping: %s\n", width, height, options.hardware_mapping);
led_canvas_clear(canvas);
///////////socket
if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1){
fprintf(stdout, "socket error\n");
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
unlink(socket_path);
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1){
fprintf(stdout, "bind error\n");
return 1;
}
chmod(socket_path, 0777);
if(listen(fd, 5) == -1){
fprintf(stdout, "listen error\n");
return 1;
}
fprintf(stdout, "start loop\n");
int flag = 0 , chr_i = 0 , ilen = sizeof(buf);
while(true){
fprintf(stdout, "do loop\n");
if((cl = accept(fd, NULL, NULL)) == -1){
fprintf(stdout, "accept error\n");
continue;
}
while((rc = read(cl, buf, sizeof(buf))) > 0){
for(chr_i = 0 ; chr_i < ilen ; chr_i++){
// cmd(111) is_last(1) val(1111)
int per_chr = buf[chr_i];
if(per_chr == 0){
break;
}
int val = per_chr & 0x0F; // 00001111
bool is_last = (per_chr & 0x10) > 0; // 00010000
int cmd = per_chr >> 5; // 11100000
switch(cmd){
case 0x1 : // R
//fprintf(stdout , "R,");
r = is_last ? val | r : val << 4 ;
break;
case 0x2 : // G
//fprintf(stdout , "G,");
g = is_last ? val | g : val << 4 ;
break;
case 0x3 : // B (final)
//fprintf(stdout , "B,");
if(is_last){
b = val | b;
//fprintf(stdout, "(%d , %d) : (%d , %d , %d)" , flag % height, flag / width, r, g, b);
led_canvas_set_pixel(canvas, flag % height, flag / width, r, g, b);
flag++;
}else{
b = val << 4;
}
break;
case 0x4 : // start frame
fprintf(stdout, "<");
flag = 0;
led_canvas_clear(canvas);
break;
case 0x5 : // end frame
fprintf(stdout, ">\n");
canvas = led_matrix_swap_on_vsync(matrix, canvas);
flag = 0;
break;
default :
fprintf(stdout, "u(%d,%d)\n" , buf[chr_i] , per_chr);
break;
}
}
}
if(rc == -1){
fprintf(stdout, "read\n");
}else if(rc == 0){
fprintf(stdout, "EOF\n");
close(cl);
}
}
led_matrix_delete(matrix); // close screen
///////////socket
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment