Created
December 11, 2020 15:43
-
-
Save goran-mahovlic/2adedd4644d302e1faff17e4fad2cc01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C library headers | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
// Linux headers | |
#include <fcntl.h> // Contains file controls like O_RDWR | |
#include <errno.h> // Error integer and strerror() function | |
#include <termios.h> // Contains POSIX terminal control definitions | |
#include <unistd.h> // write(), read(), close() | |
#define HEADER 0xff | |
#define FOOTER 0xfe | |
long serialTransmitDelay = 100000; | |
int randomise(int min, int max){ | |
int randomNo = rand() % (max + 1 - min) + min; | |
return randomNo; | |
} | |
int main() { | |
// Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device) | |
int serial_port = open("/dev/ttyUSB0", O_RDWR); | |
// Create new termios struc, we call it 'tty' for convention | |
struct termios tty; | |
// Read in existing settings, and handle any error | |
if(tcgetattr(serial_port, &tty) != 0) { | |
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno)); | |
return 1; | |
} | |
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common) | |
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) | |
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size | |
tty.c_cflag |= CS8; // 8 bits per byte (most common) | |
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common) | |
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) | |
tty.c_lflag &= ~ICANON; | |
tty.c_lflag &= ~ECHO; // Disable echo | |
tty.c_lflag &= ~ECHOE; // Disable erasure | |
tty.c_lflag &= ~ECHONL; // Disable new-line echo | |
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP | |
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl | |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes | |
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) | |
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed | |
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX) | |
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX) | |
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received. | |
tty.c_cc[VMIN] = 0; | |
// Set in/out baud rate to be 9600 | |
cfsetispeed(&tty, B57600); | |
cfsetospeed(&tty, B57600); | |
// Save tty settings, also checking for error | |
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { | |
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno)); | |
return 1; | |
} | |
// Write to serial port HEAD COMM FIR LAST FIR LAST TYPE VAL | |
// Set all panels and all LEDS to 0xDE brightness | |
unsigned char msg1[] = { HEADER, 0x01, 0x00, 0xfd, 0x00, 0xfd, 0x00, 0xfd, FOOTER }; | |
// Set all panels and all LEDS to 0 | |
unsigned char msg2[] = { HEADER, 0x01, 0x00, 0xfd, 0x00, 0xfd, 0x00, 0x00, FOOTER }; | |
// Set second led on last panel to 0xDE brightness | |
unsigned char msg3[] = { HEADER, 0x01, 0xfd, 0xfd, 0x01, 0x01, 0x00, 0x80, FOOTER }; | |
// Set second led on last panel to 0 brightness | |
unsigned char msg4[] = { HEADER, 0x01, 0xfd, 0xfd, 0x01, 0x01, 0x00, 0x00, FOOTER }; | |
// Set second led on first panel to 0xDE brightness | |
unsigned char msg5[] = { HEADER, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x80, FOOTER }; | |
// Set second led on first panel to 0 brightness | |
unsigned char msg6[] = { HEADER, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, FOOTER }; | |
// Set second and third led on last panel on low brightness | |
unsigned char msg7[] = { HEADER, 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x20, FOOTER }; | |
// Set second and third led panel to 0 brightness | |
unsigned char msg8[] = { HEADER, 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, FOOTER }; | |
// Set all panels and all LEDS to RED full brightness | |
unsigned char msg9[] = { HEADER, 0x02, 0x00, 0xfd, 0x00, 0xfd, 0x00, 0xfd, 0x00, 0x00, FOOTER }; | |
// Set all panels and all LEDS to 0 | |
unsigned char msg10[] = { HEADER, 0x01, 0x00, 0xfd, 0x00, 0xfd, 0x00, 0x00, FOOTER }; | |
// Set frist panel LED 1 and 2 to group 02 | |
unsigned char msg11[] = { HEADER, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, FOOTER }; | |
// Set frist panel LED 3 to group 04 | |
unsigned char msg12[] = { HEADER, 0x03, 0x00, 0x00, 0x02, 0x02, 0x00, 0x04, FOOTER }; | |
// Set last panel LED 1-30 to group 02 | |
unsigned char msg13[] = { HEADER, 0x03, 0xfd, 0xfd, 0x00, 0x1e, 0x00, 0x02, FOOTER }; | |
// Set last panel LED 31-60 and 3 to group 04 | |
unsigned char msg14[] = { HEADER, 0x03, 0xfd, 0xfd, 0x1f, 0x3c, 0x00, 0x04, FOOTER }; | |
// Set last panel LED 61-100 and 3 to group 08 | |
unsigned char msg20[] = { HEADER, 0x03, 0xfd, 0xfd, 0x3d, 0x64, 0x00, 0x08, FOOTER }; | |
// Set group 04 to Blue | |
unsigned char msg15[] = { HEADER, 0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, FOOTER }; | |
// Set group 02 to Red | |
unsigned char msg16[] = { HEADER, 0x05, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, FOOTER }; | |
// Set group 04 to Red | |
unsigned char msg17[] = { HEADER, 0x05, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00, FOOTER }; | |
// Set group 02 to Blue | |
unsigned char msg18[] = { HEADER, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, FOOTER }; | |
// Set group 01 to Red | |
unsigned char msg19[] = { HEADER, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, FOOTER }; | |
// Set group 01 to Green | |
unsigned char msg21[] = { HEADER, 0x05, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, FOOTER }; | |
// Set group 01 to Blue | |
unsigned char msg22[] = { HEADER, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, FOOTER }; | |
// Set group 08 to Blue | |
unsigned char msg23[] = { HEADER, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00, 0x02, FOOTER }; | |
// Set group 08 to Green | |
unsigned char msg24[] = { HEADER, 0x05, 0x00, 0x08, 0x00, 0x00, 0x02, 0x00, FOOTER }; | |
while(1){ | |
//for (int i=0;i<1000;i++){ | |
// if (i==500){ | |
/* | |
printf("Set all panels and all LEDS to highest brightness\n\r"); | |
write(serial_port, msg1, 9); | |
sleep(1); | |
printf("Set all panels and all LEDS to 0\n\r"); | |
write(serial_port, msg2, 9); | |
sleep(1); | |
printf("Set second led on last panel to half brightness\n\r"); | |
write(serial_port, msg3, 9); | |
sleep(1); | |
printf("Set second led on last panel to 0 brightness\n\r"); | |
write(serial_port, msg4, 9); | |
sleep(1); | |
printf("Set second led on first panel to half brightness\n\r"); | |
write(serial_port, msg5, 9); | |
sleep(1); | |
printf("Set second led on first panel to 0 brightness\n\r"); | |
write(serial_port, msg6, 9); | |
sleep(1); | |
printf("Set second and third led on last panel to half brightness\n\r"); | |
write(serial_port, msg7, 9); | |
sleep(1); | |
printf("Set second and third led panel to 0 brightness\n\r"); | |
write(serial_port, msg8, 9); | |
sleep(1); | |
printf("Set all panels and all LEDS to RED full brightness\n\r"); | |
write(serial_port, msg9, 11); | |
sleep(1); | |
printf("Set all panels and all LEDS to 0\n\r"); | |
write(serial_port, msg10, 9); | |
sleep(1); | |
*/ | |
// printf("Set group 01 to Blue\n\r"); | |
int magic0,magic1,magic2,magic3; | |
int order = randomise(3,5); | |
if (order == 3){ | |
magic1 = randomise(0,100); | |
magic2 = randomise(0,100); | |
magic0 = randomise(0,100); | |
magic3 = randomise(1,16); | |
} | |
else if (order == 5){ | |
magic0 = randomise(1,16); | |
magic1 = randomise(0,20); | |
magic2 = randomise(0,20); | |
magic3 = randomise(0,20); | |
} | |
else if (order == 4){ | |
order = 5; | |
magic1 = randomise(0,20); | |
magic2 = randomise(0,20); | |
magic3 = randomise(0,20); | |
magic0 = randomise(1,16); | |
} | |
unsigned char msg[] = { HEADER, order, randomise(0,100), magic0, randomise(0,100), magic1, magic2, magic3, FOOTER }; | |
write(serial_port, msg, 9); | |
usleep(50000); | |
// write(serial_port, &msg[0], 1);s | |
// write(serial_port, &msg[1], 1); | |
// write(serial_port, &msg[2], 1); | |
// } | |
// else{ | |
// write(serial_port, &msg[2], 1); | |
// } | |
// usleep(1000); | |
} | |
//write(serial_port, &msg[1], 1); | |
//usleep(1); | |
// } | |
// Allocate memory for read buffer, set size according to your needs | |
// char read_buf [256]; | |
// Normally you wouldn't do this memset() call, but since we will just receive | |
// ASCII data for this example, we'll set everything to 0 so we can | |
// call printf() easily. | |
// memset(&read_buf, '\0', sizeof(read_buf); | |
// Read bytes. The behaviour of read() (e.g. does it block?, | |
// how long does it block for?) depends on the configuration | |
// settings above, specifically VMIN and VTIME | |
// int num_bytes = read(serial_port, &read_buf, sizeof(read_buf)); | |
// n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error. | |
// if (num_bytes < 0) { | |
// printf("Error reading: %s", strerror(errno)); | |
// return 1; | |
// } | |
// Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and | |
// print it to the screen like this!) | |
// printf("Read %i bytes. Received message: %s", num_bytes, read_buf); | |
close(serial_port); | |
return 0; // success | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment