Add LEDStrip interface from KeyboardVisualizer

This commit is contained in:
Adam Honse 2019-06-20 20:44:12 -05:00
parent 8f96f9535d
commit 9c5f592618
9 changed files with 595 additions and 1 deletions

105
OpenAuraSDK/LEDStrip.cpp Normal file
View file

@ -0,0 +1,105 @@
/*---------------------------------------------------------*\
| Processing Code for Generic LED Strip Interface |
| |
| Adam Honse (calcprogrammer1@gmail.com), 12/11/2016 |
\*---------------------------------------------------------*/
#include "LEDStrip.h"
#include <fstream>
#include <iostream>
#include <string>
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<unsigned int> 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);
}
}

52
OpenAuraSDK/LEDStrip.h Normal file
View file

@ -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 <vector>
#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<unsigned int> 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

View file

@ -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 <vector>
#include <stdio.h>
@ -22,7 +23,6 @@
#include <tchar.h>
#include <regex>
#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
}

View file

@ -184,6 +184,7 @@
<ClInclude Include="i2c_smbus_i801.h" />
<ClInclude Include="i2c_smbus_nuvoton_nct6793d.h" />
<ClInclude Include="i2c_smbus_piix4.h" />
<ClInclude Include="LEDStrip.h" />
<ClInclude Include="OpenAuraSDK.h" />
<ClInclude Include="OpenAuraSDKDialog.h" />
<ClInclude Include="resource.h" />
@ -191,6 +192,8 @@
<ClInclude Include="RGBController_AorusGPU.h" />
<ClInclude Include="RGBController_Aura.h" />
<ClInclude Include="RGBController_Corsair.h" />
<ClInclude Include="RGBController_LEDStrip.h" />
<ClInclude Include="serial_port.h" />
<ClInclude Include="wmi.h" />
</ItemGroup>
<ItemGroup>
@ -202,11 +205,14 @@
<ClCompile Include="i2c_smbus_i801.cpp" />
<ClCompile Include="i2c_smbus_nuvoton_nct6793d.cpp" />
<ClCompile Include="i2c_smbus_piix4.cpp" />
<ClCompile Include="LEDStrip.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="OpenAuraSDKDialog.cpp" />
<ClCompile Include="RGBController_AorusGPU.cpp" />
<ClCompile Include="RGBController_Aura.cpp" />
<ClCompile Include="RGBController_Corsair.cpp" />
<ClCompile Include="RGBController_LEDStrip.cpp" />
<ClCompile Include="serial_port.cpp" />
<ClCompile Include="wmi.cpp" />
<ClCompile Include="OpenAuraSDK.cpp" />
</ItemGroup>

View file

@ -60,6 +60,15 @@
<ClInclude Include="RGBController_AorusGPU.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RGBController_LEDStrip.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="serial_port.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="LEDStrip.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="OpenAuraSDK.cpp">
@ -107,6 +116,15 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RGBController_LEDStrip.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LEDStrip.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="serial_port.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.rc">

View file

@ -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<int> 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);
}

View file

@ -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<RGBColor> colors;
LEDStrip* strip;
};

219
OpenAuraSDK/serial_port.cpp Normal file
View file

@ -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 <name> at baud rate <baud>
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 <name> without changing stored baud rate
bool serial_port::serial_open(const char * name)
{
return serial_open(name, baud_rate);
}
//open
// Opens the serial port <name> at baud rate <baud>
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 <length> bytes from the serial port into <buffer>
// Returns the number of bytes actually read
// If less than <length> 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 <length> bytes to the serial port from <buffer>
// Returns the number of bytes actually written
// Does not check for null-termination, so if <length> is
// greater than the number of bytes in <buffer>, it will read
// past <buffer> 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
}

73
OpenAuraSDK/serial_port.h Normal file
View file

@ -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 <string.h>
#include <stdio.h>
#include <windows.h>
//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