diff --git a/OpenAuraSDK/LEDStrip.cpp b/OpenAuraSDK/LEDStrip.cpp new file mode 100644 index 000000000..db4222e23 --- /dev/null +++ b/OpenAuraSDK/LEDStrip.cpp @@ -0,0 +1,105 @@ +/*---------------------------------------------------------*\ +| Processing Code for Generic LED Strip Interface | +| | +| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 | +\*---------------------------------------------------------*/ + +#include "LEDStrip.h" + +#include +#include +#include + +LEDStrip::LEDStrip() +{ + num_leds = 30; +} + + +LEDStrip::~LEDStrip() +{ +} + +void LEDStrip::Initialize(char* ledstring, int matrix_size, int matrix_pos, int sections, int rotate_x, bool mirror_x, bool mirror_y, bool single_color) +{ + InitializeSerial(ledstring , 115200); +} + +void LEDStrip::InitializeSerial(char* portname, int baud) +{ + portname = strtok(portname, "\r"); + strcpy(port_name, portname); + baud_rate = baud; + serialport = new serial_port(port_name, baud_rate); +} + +void LEDStrip::InitializeUDP(char * clientname, char * port) +{ + strcpy(client_name, clientname); + strcpy(port_name, port); + + //udpport = new net_port(client_name, port_name); + serialport = NULL; +} + +void LEDStrip::InitializeEspurna(char * clientname, char * port, char * apikey) +{ + strcpy(client_name, clientname); + strcpy(port_name, port); + strcpy(espurna_apikey, apikey); + //tcpport = new net_port; + serialport = NULL; +} + +char* LEDStrip::GetLEDString() +{ + return(led_string); +} + +void LEDStrip::SetNumLEDs(int numleds) +{ + num_leds = numleds; +} + +void LEDStrip::SetLEDs(std::vector colors) +{ + if (serialport != NULL ) + { + unsigned char *serial_buf; + + serial_buf = new unsigned char[(num_leds * 3) + 3]; + + serial_buf[0] = 0xAA; + + for (int idx = 0; idx < (num_leds * 3); idx += 3) + { + int pixel_idx = idx / 3; + unsigned int color = colors[pixel_idx]; + serial_buf[idx + 1] = GetRValue(color); + serial_buf[idx + 2] = GetGValue(color); + serial_buf[idx + 3] = GetBValue(color); + } + + unsigned short sum = 0; + + for (int i = 0; i < (num_leds * 3) + 1; i++) + { + sum += serial_buf[i]; + } + + serial_buf[(num_leds * 3) + 1] = sum >> 8; + serial_buf[(num_leds * 3) + 2] = sum & 0x00FF; + + if (serialport != NULL) + { + serialport->serial_write((char *)serial_buf, (num_leds * 3) + 3); + serialport->serial_flush_tx(); + } + + delete[] serial_buf; + } + else + { + //SetLEDsEspurna(pixels); + } +} \ No newline at end of file diff --git a/OpenAuraSDK/LEDStrip.h b/OpenAuraSDK/LEDStrip.h new file mode 100644 index 000000000..a4c1da44f --- /dev/null +++ b/OpenAuraSDK/LEDStrip.h @@ -0,0 +1,52 @@ +/*---------------------------------------------------------*\ +| Definitions for Generic LED Strip Interface | +| | +| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 | +\*---------------------------------------------------------*/ + +#ifndef LED_STRIP_H +#define LED_STRIP_H + + +#include "serial_port.h" +#include + + +#ifndef TRUE +#define TRUE true +#define FALSE false +#endif + +class LEDStrip +{ +public: + LEDStrip(); + ~LEDStrip(); + + void Initialize(char* ledstring, int matrix_size, int matrix_pos, int sections, int rotate_x, bool mirror_x, bool mirror_y, bool single_color); + void InitializeHuePlus(char * ledstring); + void InitializeSerial(char* portname, int baud); + void InitializeUDP(char* clientname, char* port); + void InitializeEspurna(char* clientname, char* port, char * apikey); + char* GetLEDString(); + void SetNumLEDs(int numleds); + void SetLEDs(std::vector colors); + +private: + int baud_rate; + int num_leds; + int fans; + int channel; + const int hueSize = 125; + + int * LEDStripXIndex; + int * LEDStripYIndex; + + char led_string[1024]; + char port_name[128]; + char client_name[1024]; + char espurna_apikey[128]; + serial_port *serialport; +}; + +#endif diff --git a/OpenAuraSDK/OpenAuraSDK.cpp b/OpenAuraSDK/OpenAuraSDK.cpp index 1c91fabbc..555b03c6b 100644 --- a/OpenAuraSDK/OpenAuraSDK.cpp +++ b/OpenAuraSDK/OpenAuraSDK.cpp @@ -13,6 +13,7 @@ #include "RGBController_Corsair.h" #include "RGBController_OpenRazer.h" #include "RGBController_AorusGPU.h" +#include "RGBController_LEDStrip.h" #include "i2c_smbus.h" #include #include @@ -22,7 +23,6 @@ #include #include -#include "I2CDetectDialog.h" #include "i2c_smbus_piix4.h" #include "i2c_smbus_i801.h" #include "i2c_smbus_nuvoton_nct6793d.h" @@ -691,4 +691,11 @@ void DetectRGBControllers(void) rgb_controllers.push_back(aorus_rgb); #endif + //This is for testing LED strips +#if 0 + RGBController_LEDStrip* ledstrip_rgb = new RGBController_LEDStrip(); + + rgb_controllers.push_back(ledstrip_rgb); +#endif + } diff --git a/OpenAuraSDK/OpenAuraSDK.vcxproj b/OpenAuraSDK/OpenAuraSDK.vcxproj index 6082f647b..b933aac34 100644 --- a/OpenAuraSDK/OpenAuraSDK.vcxproj +++ b/OpenAuraSDK/OpenAuraSDK.vcxproj @@ -184,6 +184,7 @@ + @@ -191,6 +192,8 @@ + + @@ -202,11 +205,14 @@ + + + diff --git a/OpenAuraSDK/OpenAuraSDK.vcxproj.filters b/OpenAuraSDK/OpenAuraSDK.vcxproj.filters index a5d1bafc2..14cd49586 100644 --- a/OpenAuraSDK/OpenAuraSDK.vcxproj.filters +++ b/OpenAuraSDK/OpenAuraSDK.vcxproj.filters @@ -60,6 +60,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -107,6 +116,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + diff --git a/OpenAuraSDK/RGBController_LEDStrip.cpp b/OpenAuraSDK/RGBController_LEDStrip.cpp new file mode 100644 index 000000000..2360c8c90 --- /dev/null +++ b/OpenAuraSDK/RGBController_LEDStrip.cpp @@ -0,0 +1,85 @@ +/*-----------------------------------------*\ +| RGBController_LEDStrip.cpp | +| | +| Generic RGB Interface for OpenAuraSDK | +| KeyboardVisualizer LED strip interface | +| | +| Adam Honse (CalcProgrammer1) 6/20/2019 | +\*-----------------------------------------*/ + +#include "RGBController_LEDStrip.h" + + +RGBController_LEDStrip::RGBController_LEDStrip() +{ + strip = new LEDStrip(); + + strip->Initialize((char *)"COM1", 0, 0, 0, 0, 0, 0, 0); + strip->SetNumLEDs(15); + + name = "LED Strip"; + + mode led_mode; + led_mode.name = "Custom"; + modes.push_back(led_mode); + + for (int i = 0; i < 15; i++) + { + colors.push_back(0x00000000); + led new_led; + new_led.name = "LED Strip"; + leds.push_back(new_led); + } + + zone led_zone; + led_zone.name = "LED Strip"; + std::vector led_zone_map; + for (int i = 0; i < 15; i++) + { + led_zone_map.push_back(i); + } + led_zone.map.push_back(led_zone_map); + zones.push_back(led_zone); +} + +int RGBController_LEDStrip::GetMode() +{ + return 0; +} + +void RGBController_LEDStrip::SetMode(int mode) +{ + +} + +void RGBController_LEDStrip::SetCustomMode() +{ + +} + +void RGBController_LEDStrip::SetAllLEDs(RGBColor color) +{ + for (int i = 0; i < colors.size(); i++) + { + colors[i] = color; + } + + strip->SetLEDs(colors); +} + +void RGBController_LEDStrip::SetAllZoneLEDs(int zone, RGBColor color) +{ + for (int i = 0; i < colors.size(); i++) + { + colors[i] = color; + } + + strip->SetLEDs(colors); +} + +void RGBController_LEDStrip::SetLED(int led, RGBColor color) +{ + colors[led] = color; + + strip->SetLEDs(colors); +} \ No newline at end of file diff --git a/OpenAuraSDK/RGBController_LEDStrip.h b/OpenAuraSDK/RGBController_LEDStrip.h new file mode 100644 index 000000000..568be05ff --- /dev/null +++ b/OpenAuraSDK/RGBController_LEDStrip.h @@ -0,0 +1,29 @@ +/*-----------------------------------------*\ +| RGBController_LEDStrip.h | +| | +| Generic RGB Interface for OpenAuraSDK | +| KeyboardVisualizer LED strip interface | +| | +| Adam Honse (CalcProgrammer1) 6/20/2019 | +\*-----------------------------------------*/ + +#pragma once +#include "RGBController.h" +#include "serial_port.h" +#include "LEDStrip.h" + +class RGBController_LEDStrip : public RGBController +{ +public: + RGBController_LEDStrip(); + int GetMode(); + void SetMode(int mode); + void SetCustomMode(); + void SetAllLEDs(RGBColor color); + void SetAllZoneLEDs(int zone, RGBColor color); + void SetLED(int led, RGBColor color); + +private: + std::vector colors; + LEDStrip* strip; +}; \ No newline at end of file diff --git a/OpenAuraSDK/serial_port.cpp b/OpenAuraSDK/serial_port.cpp new file mode 100644 index 000000000..d4eb7da9d --- /dev/null +++ b/OpenAuraSDK/serial_port.cpp @@ -0,0 +1,219 @@ +/*---------------------------------------------------------*\ +| Cross Platform Serial COM Library for Windows and Linux | +| This library provides access to serial ports with a | +| common API for both Windows and Linux systems. It | +| features read and write as well as tx/rx buffer flush. | +| | +| Adam Honse (calcprogrammer1@gmail.com), 1/21/2013 | +\*---------------------------------------------------------*/ + +#include "serial_port.h" + +//serial_port (constructor) +// The default constructor does not initialize the serial port +serial_port::serial_port() +{ + //Set a default baud rate + baud_rate = 9600; +} + +//serial_port (constructor) +// When created with port information, the constructor +// will automatically open port at baud rate +serial_port::serial_port(const char * name, unsigned int baud) +{ + serial_open(name, baud); +} + +//~serial_port (destructor) +// Closes the port before destroying the object +serial_port::~serial_port() +{ + serial_close(); +} + +//open +// Opens the serial port using stored information +// Sets the baud rate to the stored baud rate +// 8 data bits, no parity, one stop bit +bool serial_port::serial_open() +{ +// printf("SerialPort: Opening serial port %s at baud rate %d.\n", port_name, baud_rate); + + #ifdef WIN32 + file_descriptor = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + + if((int)file_descriptor < 0) + { +// printf("SerialPort: Port %s could not be opened: %d.\n", port_name, file_descriptor); + return false; + } + + SetupComm(file_descriptor, 1, 128); + GetCommState(file_descriptor, &dcb); + + dcb.BaudRate = baud_rate; //Set baud rate + dcb.ByteSize = 8; //8 data bits + dcb.Parity = NOPARITY; //Parity = none + dcb.StopBits = ONESTOPBIT; //One stop bit + dcb.fAbortOnError = TRUE; //Abort on error + dcb.fOutX = FALSE; //XON/XOFF off for transmit + dcb.fInX = FALSE; //XON/XOFF off for receive + dcb.fOutxCtsFlow = FALSE; //Turn off CTS flow control + dcb.fRtsControl = RTS_CONTROL_DISABLE; //Options DISABLE, ENABLE, HANDSHAKE + dcb.fOutxDsrFlow = FALSE; //Turn off DSR flow control + dcb.fDtrControl = DTR_CONTROL_DISABLE; //Disable DTR control + + SetCommState(file_descriptor, &dcb); + + COMMTIMEOUTS timeouts = {0}; + timeouts.ReadIntervalTimeout = 50; + timeouts.ReadTotalTimeoutConstant=50; + timeouts.ReadTotalTimeoutMultiplier=10; + timeouts.WriteTotalTimeoutConstant=50; + timeouts.WriteTotalTimeoutMultiplier=10; + SetCommTimeouts(file_descriptor, &timeouts); + + #else + + file_descriptor = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY); + + if(file_descriptor < 0) + { +// printf("SerialPort: Port %s could not be opened: %d.\n", port_name, file_descriptor); + return false; + } + + struct termios2 options; + ioctl(file_descriptor, TCGETS2, &options); + options.c_cflag &= ~CBAUD; + options.c_cflag |= BOTHER; + options.c_ispeed = baud_rate; + options.c_ospeed = baud_rate; + ioctl(file_descriptor, TCSETS2, &options); + + //serial_struct ss; + //int closestSpeed; + //ioctl(file_descriptor, TIOCGSERIAL, &ss); + //ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST | ASYNCB_LOW_LATENCY; + //ss.custom_divisor = (ss.baud_base + (baud_rate / 2)) / baud_rate; + //if(ss.custom_divisor == 0) + //{ + // closestSpeed = baud_rate; + //} + //else + //{ + // closestSpeed = ss.baud_base / ss.custom_divisor; + //} + + //if((float)closestSpeed < ((float)baud_rate * (98.0f/100.0f)) || (float)closestSpeed > ((float)baud_rate * (102.0f/100.0f))) + //{ +// printf("SerialPort: Cannot set %s to %d. Closest possible speed is %d.\n", port_name, baud_rate, closestSpeed); + //} + //else + //{ +// printf("SerialPort: %s speed set to %d.\n", port_name, baud_rate); + //} + + //fcntl(file_descriptor, F_SETFL, 0); + #endif + +// printf("SerialPort: Serial port %s opened successfully.\n", port_name); + return true; +} + +//open +// Opens the serial port without changing stored baud rate +bool serial_port::serial_open(const char * name) +{ + return serial_open(name, baud_rate); +} + +//open +// Opens the serial port at baud rate +bool serial_port::serial_open(const char* name, unsigned int baud) +{ + strcpy(port_name, name); + baud_rate = baud; + return serial_open(); +} + +//close +// Closes the serial port +void serial_port::serial_close() +{ +// printf("SerialPort: Closing port %s.\n", port_name); + #ifdef WIN32 + + #else + + close(file_descriptor); + #endif +} + +// read +// Reads bytes from the serial port into +// Returns the number of bytes actually read +// If less than bytes are available, it will read all +// available bytes +int serial_port::serial_read(char * buffer, int length) +{ + #ifdef WIN32 + DWORD bytesread; + ReadFile(file_descriptor, buffer, length, &bytesread, NULL); + + #else + + int bytesread; + bytesread = read(file_descriptor, buffer, length); + #endif + + //printf("SerialPort: Read %d bytes on port %s.\n", bytesread, port_name); + return bytesread; +} + +//write +// Writes bytes to the serial port from +// Returns the number of bytes actually written +// Does not check for null-termination, so if is +// greater than the number of bytes in , it will read +// past and may cause a segfault +int serial_port::serial_write(char * buffer, int length) +{ + #ifdef WIN32 + DWORD byteswritten; + WriteFile(file_descriptor, buffer, length, &byteswritten, NULL); + + #else + + int byteswritten; + byteswritten = write(file_descriptor, buffer, length); + #endif + + //printf("SerialPort: Wrote %d bytes on port %s.\n", byteswritten, port_name); + return byteswritten; +} + +//flush +void serial_port::serial_flush_rx() +{ + #ifdef WIN32 + PurgeComm(file_descriptor, PURGE_RXABORT | PURGE_RXCLEAR); + + #else + + tcflush(file_descriptor, TCIFLUSH); + #endif +} + +void serial_port::serial_flush_tx() +{ + + #ifdef WIN32 + PurgeComm(file_descriptor, PURGE_TXABORT | PURGE_TXCLEAR); + + #else + + tcflush(file_descriptor, TCOFLUSH); + #endif +} diff --git a/OpenAuraSDK/serial_port.h b/OpenAuraSDK/serial_port.h new file mode 100644 index 000000000..43dcf32a3 --- /dev/null +++ b/OpenAuraSDK/serial_port.h @@ -0,0 +1,73 @@ +/*---------------------------------------------------------*\ +| Cross Platform Serial COM Library for Windows and Linux | +| This library provides access to serial ports with a | +| common API for both Windows and Linux systems. It | +| features read and write as well as tx/rx buffer flush. | +| | +| Adam Honse (calcprogrammer1@gmail.com), 1/21/2013 | +\*---------------------------------------------------------*/ + +#ifndef SERIAL_PORT_H +#define SERIAL_PORT_H + +#include +#include + + +#include + + + +//Serial Port Class +//The reason for this class is that serial ports are treated differently +//on Windows and Linux. By creating a class, those differences can be +//made invisible to the program and make cross-platform usage easy + +class serial_port +{ + public: + serial_port(); + serial_port(const char * name, unsigned int baud); + + ~serial_port(); + + //Function to open the port + bool serial_open(); + bool serial_open(const char* name); + bool serial_open(const char* name, unsigned int baud); + + //Function to close the port + void serial_close(); + + //Functions for controlling baud rate + void serial_set_baud(unsigned int baud); + int serial_get_baud(); + + //Function to read data from the port buffer + int serial_read(char * buffer, int length); + + //Function to write data to the serial port + int serial_write(char * buffer, int length); + + //Functions to flush the serial port rx and tx buffers + void serial_flush_rx(); + void serial_flush_tx(); + + //Function to list the number of available bytes + int serial_available(); + + private: + char port_name[1024]; + unsigned int baud_rate; + + #ifdef WIN32 + HANDLE file_descriptor; + DCB dcb; + + #else + + int file_descriptor; + #endif +}; + +#endif