Skip to content

Commit

Permalink
KP18058: LED current setting, PWM dimming (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
miegl authored Aug 21, 2023
1 parent ab07ba4 commit 323d4e9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Do not add anything here, as it will overwritten with next rebuild.
| [Script constants](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/constants.md) (29 total) | Every console command that takes an integer argument supports certain constant expansion. |
| [Channel Types](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/channelTypes.md) (40 total) | Channel types are often not required and don't have to be configured, but in some cases they are required for better device control from OpenBeken web panel. Channel types describes the kind of value stored in channel, for example, if you have a Tuya Fan Controller with 3 speeds control, you can set the channel type to LowMidHigh and it will display the correct UI radiobutton on OpenBeken panel.<br>Some channels have '_div10' or '_div100' sufixes. This is for TuyaMCU. This is needed because TuyaMCU sends values as integers, so it sends, for example, 215 for 21.5C temperature, and we store it internally as 215 and only convert to float for display. |
| [FAQ](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/faq.md) (27 total) | Here is a detailed list of questions you may ask. Some information from docs is repeated here. |
| [Console/Script commands](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md) (278 total) | There are multiple console commands that allow you to automate your devices. Commands can be entered manually in command line, can be send by HTTP (just like in Tasmota), can be send by MQTT and also can be scripted. |
| [Console/Script commands](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md) (279 total) | There are multiple console commands that allow you to automate your devices. Commands can be entered manually in command line, can be send by HTTP (just like in Tasmota), can be send by MQTT and also can be scripted. |
| [Command Examples](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commandExamples.md) (10 total) | Here you can find some examples of console commands usage |
| [Autoexec.bat examples (configs)](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md) (16 total) | Here you can find examples of autoexec.bat configs. The autoexec.bat file can be created in Web Application, under LittleFS tab, and is run every time device reboots (unless device enters safe mode/AP mode). The autoexec.bat file allows you to create more advanced configs, setup TuyaMCU mappings, etc |
| [MQTT Topics](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/mqttTopics.md) (25 total) | MQTT topic names and content for incoming and ougoing OBK MQTT publishes |
| [Script examples](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/scriptExamples.md) (5 total) | Scripts can be put in autoexec.bat and then they will start automatically on reboot, you can also put script in other LittleFS file and use startScript [fileName] [Label] command to run them. From the firmware point of view, scripts and autoexecs are basically the same thing. There is, however, a little bit more advanced system of execution for scripts which can be written in a form of scripts threads that run over time, can have delays within then, conditional checks and jumps. |
| [Console/Script commands [Extended Edition]](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands-extended.md) (278 total) | More details on commands. |
| [Console/Script commands [Extended Edition]](https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands-extended.md) (279 total) | More details on commands. |
5 changes: 3 additions & 2 deletions docs/commands-extended.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ Do not add anything here, as it will overwritten with next rebuild.
| IREnable | [Str][1or0] | Enable/disable aspects of IR. IREnable RXTX 0/1 - enable Rx whilst Tx. IREnable [protocolname] 0/1 - enable/disable a specified protocol.<br/><br/>See also [IREnable on forum](https://www.elektroda.com/rtvforum/find.php?q=IREnable). | File: driver/drv_ir.cpp<br/>Function: IR_Enable |
| IRSend | [PROT-ADDR-CMD-REP] | Sends IR commands in the form PROT-ADDR-CMD-REP, e.g. NEC-1-1A-0.<br/><br/>See also [IRSend on forum](https://www.elektroda.com/rtvforum/find.php?q=IRSend). | File: driver/drv_ir.cpp<br/>Function: IR_Send_Cmd |
| json_test | cmnd_json_test | .<br/><br/>See also [json_test on forum](https://www.elektroda.com/rtvforum/find.php?q=json_test). | File: cmnds/cmd_test.c<br/>Function: NULL); |
| KP18058_Map | [Ch0][Ch1][Ch2][Ch3][Ch4] | Maps KP18058_Map RGBCW values to given indices of KP18058 channels. This is because KP18058 channels order is not the same for some devices. Some devices are using RGBCW order and some are using GBRCW, etc, etc. Example usage: KP18058_Map 0 1 2 3 4.<br/><br/>See also [KP18058_Map on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_Map). | File: driver/drv_sm2235.c<br/>Function: KP18058_Map |
| KP18058_RGBCW | [HexColor] | Don't use it. It's for direct access of KP18058 driver. You don't need it because LED driver automatically calls it, so just use led_basecolor_rgb.<br/><br/>See also [KP18058_RGBCW on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_RGBCW). | File: driver/drv_bp5758d.c<br/>Function: KP18058_RGBCW |
| KP18058_Current | [RGBLimit][CWLimit] | Sets the maximum current for LED driver. Values 0-31. Example usage: KP18058_Current 14 30.<br/><br/>See also [KP18058_Current on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_Current). | File: driver/drv_kp18058.c<br/>Function: KP18058_Current |
| KP18058_Map | [Ch0][Ch1][Ch2][Ch3][Ch4] | Maps KP18058_Map RGBCW values to given indices of KP18058 channels. This is because KP18058 channels order is not the same for some devices. Some devices are using RGBCW order and some are using GBRCW, etc, etc. Example usage: KP18058_Map 0 1 2 3 4.<br/><br/>See also [KP18058_Map on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_Map). | File: driver/drv_kp18058.c<br/>Function: KP18058_Map |
| KP18058_RGBCW | [HexColor] | Don't use it. It's for direct access of KP18058 driver. You don't need it because LED driver automatically calls it, so just use led_basecolor_rgb.<br/><br/>See also [KP18058_RGBCW on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_RGBCW). | File: driver/drv_kp18058.c<br/>Function: KP18058_RGBCW |
| lcd_clear | | Clears the LCD.<br/><br/>See also [lcd_clear on forum](https://www.elektroda.com/rtvforum/find.php?q=lcd_clear). | File: i2c/drv_i2c_lcd_pcf8574t.c<br/>Function: DRV_I2C_LCD_PCF8574_Clear |
| lcd_clearAndGoto | | Clears LCD and go to pos.<br/><br/>See also [lcd_clearAndGoto on forum](https://www.elektroda.com/rtvforum/find.php?q=lcd_clearAndGoto). | File: i2c/drv_i2c_lcd_pcf8574t.c<br/>Function: DRV_I2C_LCD_PCF8574_ClearAndGoTo |
| lcd_goto | | Go to position on LCD.<br/><br/>See also [lcd_goto on forum](https://www.elektroda.com/rtvforum/find.php?q=lcd_goto). | File: i2c/drv_i2c_lcd_pcf8574t.c<br/>Function: DRV_I2C_LCD_PCF8574_GoTo |
Expand Down
1 change: 1 addition & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Do not add anything here, as it will overwritten with next rebuild.
| IREnable | [Str][1or0] | Enable/disable aspects of IR. IREnable RXTX 0/1 - enable Rx whilst Tx. IREnable [protocolname] 0/1 - enable/disable a specified protocol.<br/><br/>See also [IREnable on forum](https://www.elektroda.com/rtvforum/find.php?q=IREnable). |
| IRSend | [PROT-ADDR-CMD-REP] | Sends IR commands in the form PROT-ADDR-CMD-REP, e.g. NEC-1-1A-0.<br/><br/>See also [IRSend on forum](https://www.elektroda.com/rtvforum/find.php?q=IRSend). |
| json_test | cmnd_json_test | .<br/><br/>See also [json_test on forum](https://www.elektroda.com/rtvforum/find.php?q=json_test). |
| KP18058_Current | [RGBLimit][CWLimit] | Sets the maximum current for LED driver. Values 0-31. Example usage: KP18058_Current 14 30.<br/><br/>See also [KP18058_Current on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_Current). |
| KP18058_Map | [Ch0][Ch1][Ch2][Ch3][Ch4] | Maps KP18058_Map RGBCW values to given indices of KP18058 channels. This is because KP18058 channels order is not the same for some devices. Some devices are using RGBCW order and some are using GBRCW, etc, etc. Example usage: KP18058_Map 0 1 2 3 4.<br/><br/>See also [KP18058_Map on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_Map). |
| KP18058_RGBCW | [HexColor] | Don't use it. It's for direct access of KP18058 driver. You don't need it because LED driver automatically calls it, so just use led_basecolor_rgb.<br/><br/>See also [KP18058_RGBCW on forum](https://www.elektroda.com/rtvforum/find.php?q=KP18058_RGBCW). |
| lcd_clear | | Clears the LCD.<br/><br/>See also [lcd_clear on forum](https://www.elektroda.com/rtvforum/find.php?q=lcd_clear). |
Expand Down
13 changes: 11 additions & 2 deletions docs/json/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,21 @@
"requires": "",
"examples": ""
},
{
"name": "KP18058_Current",
"args": "[RGBLimit][CWLimit]",
"descr": "Sets the maximum current for LED driver. Values 0-31. Example usage: KP18058_Current 14 30",
"fn": "KP18058_Current",
"file": "driver/drv_kp18058.c",
"requires": "",
"examples": ""
},
{
"name": "KP18058_Map",
"args": "[Ch0][Ch1][Ch2][Ch3][Ch4]",
"descr": "Maps KP18058_Map RGBCW values to given indices of KP18058 channels. This is because KP18058 channels order is not the same for some devices. Some devices are using RGBCW order and some are using GBRCW, etc, etc. Example usage: KP18058_Map 0 1 2 3 4",
"fn": "KP18058_Map",
"file": "driver/drv_sm2235.c",
"file": "driver/drv_kp18058.c",
"requires": "",
"examples": ""
},
Expand All @@ -733,7 +742,7 @@
"args": "[HexColor]",
"descr": "Don't use it. It's for direct access of KP18058 driver. You don't need it because LED driver automatically calls it, so just use led_basecolor_rgb",
"fn": "KP18058_RGBCW",
"file": "driver/drv_bp5758d.c",
"file": "driver/drv_kp18058.c",
"requires": "",
"examples": ""
},
Expand Down
65 changes: 40 additions & 25 deletions src/driver/drv_kp18058.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@


static softI2C_t g_softI2C;
static int g_current_RGB = 14;
static int g_current_CW = 30;

byte CountBytes(byte b) {
byte GetParityBit(byte b) {
byte sum;
int i;

Expand All @@ -31,51 +33,66 @@ byte CountBytes(byte b) {

void KP18058_Write(float *rgbcw) {
bool bAllZero = true;
int i;


for (int i = 0; i < 5; i++) {
if (rgbcw[i] > 0.01f) {
bAllZero = false;
}
}

// RGB current
byte byte2 = (rgbcw[0] || rgbcw[1] || rgbcw[2]) ? g_current_RGB : 1;
byte2 = byte2 << 1;
byte2 |= GetParityBit(byte2);

// Bit 7: RGB PWM, Bit 6: Unknown, Bit 5-1: CW current
byte byte3 = (1 << 7) | (1 << 6) | (g_current_CW << 1);
byte3 |= GetParityBit(byte3);

if (bAllZero) {
Soft_I2C_Start(&g_softI2C, 0x81);
Soft_I2C_WriteByte(&g_softI2C, 0x00);
Soft_I2C_WriteByte(&g_softI2C, 0x03);
Soft_I2C_WriteByte(&g_softI2C, 0x7D);
Soft_I2C_WriteByte(&g_softI2C, byte3);
for (int i = 0; i < 10; i++) {
Soft_I2C_WriteByte(&g_softI2C, 0x00);
}
}
else {
//FILE *f = fopen("dimmerTest.txt", "a");
Soft_I2C_Start(&g_softI2C, 0xE1);
Soft_I2C_WriteByte(&g_softI2C, 0x00);
Soft_I2C_WriteByte(&g_softI2C, 0x03);
Soft_I2C_WriteByte(&g_softI2C, 0x7D);
Soft_I2C_WriteByte(&g_softI2C, byte2);
Soft_I2C_WriteByte(&g_softI2C, byte3);
for (int i = 0; i < 5; i++) {
float useVal = rgbcw[g_cfg.ledRemap.ar[i]];
unsigned short cur_col_12 = MAP(useVal, 0, 255.0f, 0, 1023.0f);
unsigned short cur_col_10 = MAP(useVal, 0, 255.0f, 0, 1023.0f);
byte a, b;
a = cur_col_12 & 0x1F;
b = (cur_col_12 >> 5) & 0x1F;
a = cur_col_10 & 0x1F;
b = (cur_col_10 >> 5) & 0x1F;
a = a << 1;
b = b << 1;
a |= CountBytes(a);
b |= CountBytes(b);
a |= GetParityBit(a);
b |= GetParityBit(b);
Soft_I2C_WriteByte(&g_softI2C, b);
Soft_I2C_WriteByte(&g_softI2C, a);

//fprintf(f, "0x%02X 0x%02X ", b, a);
}
//fprintf(f, "\n");
//fclose(f);
}
Soft_I2C_Stop(&g_softI2C);
}

commandResult_t KP18058_Current(const void *context, const char *cmd, const char *args, int flags) {
Tokenizer_TokenizeString(args, 0);

if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 2)) {
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

g_current_RGB = Tokenizer_GetArgIntegerRange(0, 0, 31);
g_current_CW = Tokenizer_GetArgIntegerRange(1, 0, 31);

return CMD_RES_OK;
}

// startDriver KP18058
// KP18058_Map 0 1 2 3 4
// KP18058_RGBCW FF00000000
Expand All @@ -89,22 +106,20 @@ void KP18058_Init() {
g_softI2C.pin_data = PIN_FindPinIndexForRole(IOR_KP18058_DAT, g_softI2C.pin_data);

Soft_I2C_PreInit(&g_softI2C);
#if 0
for (float f = 0; f < 255; f += 0.25f) {
float rgbcw[5] = { 0 };
rgbcw[1] = f;
KP18058_Write(rgbcw);
}
#endif

//cmddetail:{"name":"KP18058_RGBCW","args":"[HexColor]",
//cmddetail:"descr":"Don't use it. It's for direct access of KP18058 driver. You don't need it because LED driver automatically calls it, so just use led_basecolor_rgb",
//cmddetail:"fn":"KP18058_RGBCW","file":"driver/drv_bp5758d.c","requires":"",
//cmddetail:"fn":"KP18058_RGBCW","file":"driver/drv_kp18058.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("KP18058_RGBCW", CMD_LEDDriver_WriteRGBCW, NULL);
//cmddetail:{"name":"KP18058_Map","args":"[Ch0][Ch1][Ch2][Ch3][Ch4]",
//cmddetail:"descr":"Maps KP18058_Map RGBCW values to given indices of KP18058 channels. This is because KP18058 channels order is not the same for some devices. Some devices are using RGBCW order and some are using GBRCW, etc, etc. Example usage: KP18058_Map 0 1 2 3 4",
//cmddetail:"fn":"KP18058_Map","file":"driver/drv_sm2235.c","requires":"",
//cmddetail:"fn":"KP18058_Map","file":"driver/drv_kp18058.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("KP18058_Map", CMD_LEDDriver_Map, NULL);
//cmddetail:{"name":"KP18058_Current","args":"[RGBLimit][CWLimit]",
//cmddetail:"descr":"Sets the maximum current for LED driver. Values 0-31. Example usage: KP18058_Current 14 30",
//cmddetail:"fn":"KP18058_Current","file":"driver/drv_kp18058.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("KP18058_Current", KP18058_Current, NULL);
}

0 comments on commit 323d4e9

Please sign in to comment.