55\*---------------------------------------------------------*/
66
77#include " LEDStripController.h"
8+ #include " ResourceManager.h"
89
910#include < fstream>
1011#include < iostream>
@@ -26,13 +27,16 @@ void LEDStripController::Initialize(char* ledstring, led_protocol proto)
2627 LPSTR source = NULL ;
2728 LPSTR udpport_baud = NULL ;
2829 LPSTR next = NULL ;
29-
30+
3031 // Set the protocol
3132 protocol = proto;
3233
3334 // Assume serial device unless a different protocol is specified
3435 bool serial = TRUE ;
35-
36+
37+ // Default i2c address out of range
38+ i2c_addr = 255 ;
39+
3640 source = strtok_s (ledstring, " ," , &next);
3741
3842 // Check if we are setting up a Keyboard Visualizer UDP protocol device
@@ -41,7 +45,7 @@ void LEDStripController::Initialize(char* ledstring, led_protocol proto)
4145 source = source + 4 ;
4246 serial = FALSE ;
4347 }
44-
48+
4549 // Check for either the UDP port or the serial baud rate
4650 if (strlen (next))
4751 {
@@ -56,7 +60,13 @@ void LEDStripController::Initialize(char* ledstring, led_protocol proto)
5660
5761 if (serial)
5862 {
59- if (udpport_baud == NULL )
63+ if (protocol == LED_PROTOCOL_BASIC_I2C)
64+ {
65+ // I2C uses the baud field for address
66+ i2c_addr = atoi (udpport_baud);
67+ InitializeI2C (source);
68+ }
69+ else if (udpport_baud == NULL )
6070 {
6171 // Initialize with default baud rate
6272 InitializeSerial (source, 115200 );
@@ -86,13 +96,32 @@ void LEDStripController::Initialize(char* ledstring, led_protocol proto)
8696 }
8797}
8898
99+ void LEDStripController::InitializeI2C (char * i2cname)
100+ {
101+ for (unsigned int i2c_idx = 0 ; i2c_idx < ResourceManager::get ()->GetI2CBusses ().size (); i2c_idx++)
102+ {
103+ if (ResourceManager::get ()->GetI2CBusses ()[i2c_idx]->device_name == std::string (i2cname))
104+ {
105+ if (i2c_addr < 128 )
106+ {
107+ i2cport = ResourceManager::get ()->GetI2CBusses ()[i2c_idx];
108+ break ;
109+ }
110+ }
111+ }
112+
113+ serialport = NULL ;
114+ udpport = NULL ;
115+ }
116+
89117void LEDStripController::InitializeSerial (char * portname, int baud)
90118{
91119 portname = strtok (portname, " \r " );
92120 port_name = portname;
93121 baud_rate = baud;
94122 serialport = new serial_port (port_name.c_str (), baud_rate);
95123 udpport = NULL ;
124+ i2cport = NULL ;
96125}
97126
98127void LEDStripController::InitializeUDP (char * clientname, char * port)
@@ -102,6 +131,7 @@ void LEDStripController::InitializeUDP(char * clientname, char * port)
102131
103132 udpport = new net_port (client_name.c_str (), port_name.c_str ());
104133 serialport = NULL ;
134+ i2cport = NULL ;
105135}
106136
107137std::string LEDStripController::GetLocation ()
@@ -114,6 +144,10 @@ std::string LEDStripController::GetLocation()
114144 {
115145 return (" UDP: " + client_name + " :" + port_name);
116146 }
147+ else if (i2cport != NULL )
148+ {
149+ return (" I2C: " + std::string (i2cport->device_name ) + " , Address " + std::to_string (i2c_addr));
150+ }
117151 else
118152 {
119153 return (" " );
@@ -140,6 +174,10 @@ void LEDStripController::SetLEDs(std::vector<RGBColor> colors)
140174 case LED_PROTOCOL_TPM2:
141175 SetLEDsTPM2 (colors);
142176 break ;
177+
178+ case LED_PROTOCOL_BASIC_I2C:
179+ SetLEDsBasicI2C (colors);
180+ break ;
143181 }
144182}
145183
@@ -318,3 +356,51 @@ void LEDStripController::SetLEDsTPM2(std::vector<RGBColor> colors)
318356
319357 delete[] serial_buf;
320358}
359+
360+ void LEDStripController::SetLEDsBasicI2C (std::vector<RGBColor> colors)
361+ {
362+ unsigned char serial_buf[30 ];
363+
364+ /* -------------------------------------------------------------*\
365+ | Basic I2C Protocol |
366+ | |
367+ | Packet size: At most 32 bytes (SMBus block size) |
368+ | |
369+ | Packet is in RGBRGBRGB... format, also provide start index |
370+ \*-------------------------------------------------------------*/
371+
372+ unsigned char index = 0 ;
373+ unsigned char offset = 0 ;
374+
375+ for (unsigned int color_idx = 0 ; color_idx < colors.size (); color_idx++)
376+ {
377+ serial_buf[index + 0 ] = RGBGetRValue (colors[color_idx]);
378+ serial_buf[index + 1 ] = RGBGetGValue (colors[color_idx]);
379+ serial_buf[index + 2 ] = RGBGetBValue (colors[color_idx]);
380+
381+ index += 3 ;
382+
383+ if (index >= 30 )
384+ {
385+ if (i2cport != NULL )
386+ {
387+ i2cport->i2c_smbus_write_i2c_block_data (i2c_addr, offset, 30 , serial_buf);
388+ offset += 30 ;
389+ index = 0 ;
390+ }
391+ }
392+ }
393+
394+ if (index > 0 )
395+ {
396+ if (i2cport != NULL )
397+ {
398+ i2cport->i2c_smbus_write_i2c_block_data (i2c_addr, offset, index, serial_buf);
399+ }
400+ }
401+
402+ if (i2cport != NULL )
403+ {
404+ i2cport->i2c_smbus_write_byte (i2c_addr, 0xFF );
405+ }
406+ }
0 commit comments