Skip to content

Commit 428692f

Browse files
Basic i2c
1 parent e0be60d commit 428692f

File tree

6 files changed

+124
-5
lines changed

6 files changed

+124
-5
lines changed

Controllers/LEDStripController/LEDStripController.cpp

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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+
89117
void 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

98127
void 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

107137
std::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+
}

Controllers/LEDStripController/LEDStripController.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define LED_STRIP_H
99

1010
#include "RGBController.h"
11+
#include "i2c_smbus.h"
1112
#include "serial_port.h"
1213
#include "net_port.h"
1314
#include <vector>
@@ -29,7 +30,8 @@ enum
2930
{
3031
LED_PROTOCOL_KEYBOARD_VISUALIZER,
3132
LED_PROTOCOL_ADALIGHT,
32-
LED_PROTOCOL_TPM2
33+
LED_PROTOCOL_TPM2,
34+
LED_PROTOCOL_BASIC_I2C
3335
};
3436

3537
struct LEDStripDevice
@@ -49,6 +51,7 @@ class LEDStripController
4951

5052
void Initialize(char* ledstring, led_protocol proto);
5153

54+
void InitializeI2C(char* i2cname);
5255
void InitializeSerial(char* portname, int baud);
5356
void InitializeUDP(char* clientname, char* port);
5457

@@ -60,6 +63,7 @@ class LEDStripController
6063
void SetLEDsKeyboardVisualizer(std::vector<RGBColor> colors);
6164
void SetLEDsAdalight(std::vector<RGBColor> colors);
6265
void SetLEDsTPM2(std::vector<RGBColor> colors);
66+
void SetLEDsBasicI2C(std::vector<RGBColor> colors);
6367

6468
int num_leds;
6569

@@ -71,6 +75,8 @@ class LEDStripController
7175
std::string client_name;
7276
serial_port *serialport;
7377
net_port *udpport;
78+
i2c_smbus_interface *i2cport;
79+
unsigned char i2c_addr;
7480
led_protocol protocol;
7581
};
7682

Controllers/LEDStripController/LEDStripControllerDetect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ void DetectLEDStripControllers(std::vector<RGBController*> &rgb_controllers)
7474
{
7575
dev.protocol = LED_PROTOCOL_TPM2;
7676
}
77+
else if(protocol_string == "basic_i2c")
78+
{
79+
dev.protocol = LED_PROTOCOL_BASIC_I2C;
80+
}
7781
}
7882

7983
std::string value = dev.port + "," + std::to_string(dev.baud) + "," + std::to_string(dev.num_leds);

qt/OpenRGBSerialSettingsPage/OpenRGBSerialSettingsEntry.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ OpenRGBSerialSettingsEntry::OpenRGBSerialSettingsEntry(QWidget *parent) :
1212
ui->ProtocolComboBox->addItem("Keyboard Visualizer");
1313
ui->ProtocolComboBox->addItem("Adalight");
1414
ui->ProtocolComboBox->addItem("TPM2");
15+
ui->ProtocolComboBox->addItem("Basic I2C");
1516
}
1617

1718
OpenRGBSerialSettingsEntry::~OpenRGBSerialSettingsEntry()
@@ -26,3 +27,16 @@ void OpenRGBSerialSettingsEntry::changeEvent(QEvent *event)
2627
ui->retranslateUi(this);
2728
}
2829
}
30+
31+
void Ui::OpenRGBSerialSettingsEntry::on_ProtocolComboBox_currentIndexChanged(int index)
32+
{
33+
if(index == 3)
34+
{
35+
ui->BaudLabel->setText("Address:");
36+
}
37+
else
38+
{
39+
ui->BaudLabel->setText("Baud:");
40+
}
41+
}
42+

qt/OpenRGBSerialSettingsPage/OpenRGBSerialSettingsEntry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Ui::OpenRGBSerialSettingsEntry : public QWidget
1515
private slots:
1616
void changeEvent(QEvent *event);
1717

18+
void on_ProtocolComboBox_currentIndexChanged(int index);
19+
1820
public:
1921
explicit OpenRGBSerialSettingsEntry(QWidget *parent = nullptr);
2022
~OpenRGBSerialSettingsEntry();

qt/OpenRGBSerialSettingsPage/OpenRGBSerialSettingsPage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ OpenRGBSerialSettingsPage::OpenRGBSerialSettingsPage(QWidget *parent) :
6262
{
6363
entry->ui->ProtocolComboBox->setCurrentIndex(2);
6464
}
65+
else if(protocol_string == "basic_i2c")
66+
{
67+
entry->ui->ProtocolComboBox->setCurrentIndex(3);
68+
}
6569
}
6670

6771
entries.push_back(entry);
@@ -154,6 +158,9 @@ void Ui::OpenRGBSerialSettingsPage::on_SaveSerialConfigurationButton_clicked()
154158
case 2:
155159
ledstrip_settings["devices"][device_idx]["protocol"] = "tpm2";
156160
break;
161+
case 3:
162+
ledstrip_settings["devices"][device_idx]["protocol"] = "basic_i2c";
163+
break;
157164
}
158165
}
159166

0 commit comments

Comments
 (0)