Skip to content

Commit

Permalink
IMPORTANT all led remaps are now saved automatically in flash..
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Feb 14, 2023
1 parent 8c391db commit 9095b2f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 43 deletions.
10 changes: 3 additions & 7 deletions src/driver/drv_bp1658cj.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void BP1658CJ_Write(float *rgbcw) {

for(i = 0; i < 5; i++){
// convert 0-255 to 0-1023
//cur_col_10[i] = rgbcw[g_channelOrder[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_channelOrder[i]], 0, 255.0f, 0, 1023.0f);
//cur_col_10[i] = rgbcw[g_cfg.ledRemap.ar[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_cfg.ledRemap.ar[i]], 0, 255.0f, 0, 1023.0f);
}
//ADDLOG_DEBUG(LOG_FEATURE_CMD, "Writing to Lamp (hex): #%02X%02X%02X%02X%02X", cur_col_10[0], cur_col_10[1], cur_col_10[2], cur_col_10[3], cur_col_10[4]);
// If we receive 0 for all channels, we'll assume that the lightbulb is off, and activate BP1658CJ's sleep mode ([0x80] ).
Expand Down Expand Up @@ -62,11 +62,7 @@ void BP1658CJ_Write(float *rgbcw) {
// BP1658CJ_RGBCW FF00000000
void BP1658CJ_Init() {
// default map
g_channelOrder[0] = 1;
g_channelOrder[1] = 0;
g_channelOrder[2] = 2;
g_channelOrder[3] = 3;
g_channelOrder[4] = 4;
CFG_SetDefaultLEDRemap(1, 0, 2, 3, 4);

g_i2c_pin_clk = PIN_FindPinIndexForRole(IOR_BP1658CJ_CLK,g_i2c_pin_clk);
g_i2c_pin_data = PIN_FindPinIndexForRole(IOR_BP1658CJ_DAT,g_i2c_pin_data);
Expand Down
10 changes: 4 additions & 6 deletions src/driver/drv_bp5758d.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ void BP5758D_Write(float *rgbcw) {

for(i = 0; i < 5; i++){
// convert 0-255 to 0-1023
//cur_col_10[i] = rgbcw[g_channelOrder[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_channelOrder[i]], 0, 255.0f, 0, 1023.0f);
//cur_col_10[i] = rgbcw[g_cfg.ledRemap.ar[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_cfg.ledRemap.ar[i]], 0, 255.0f, 0, 1023.0f);

}

Expand Down Expand Up @@ -149,10 +149,8 @@ static commandResult_t BP5758D_Current(const void *context, const char *cmd, con
// backlog startDriver BP5758D; BP5758D_Current 14;
void BP5758D_Init() {
int i;
// default map
for (i = 0; i < 5; i++) {
g_channelOrder[i] = i;
}
// default setting (applied only if none was applied earlier)
CFG_SetDefaultLEDRemap(0, 1, 2, 3, 4);

g_i2c_pin_clk = PIN_FindPinIndexForRole(IOR_BP5758D_CLK,g_i2c_pin_clk);
g_i2c_pin_data = PIN_FindPinIndexForRole(IOR_BP5758D_DAT,g_i2c_pin_data);
Expand Down
1 change: 0 additions & 1 deletion src/driver/drv_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ void WEMO_AppendInformationToHTTPIndexPage(http_request_t* request);

extern int g_i2c_pin_clk;
extern int g_i2c_pin_data;
extern byte g_channelOrder[5];
void Soft_I2C_SetLow(uint8_t pin);
void Soft_I2C_SetHigh(uint8_t pin);
bool Soft_I2C_PreInit(void);
Expand Down
51 changes: 27 additions & 24 deletions src/driver/drv_sm2135.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ int g_i2c_pin_data = 1;

static int g_current_setting_cw = SM2135_20MA;
static int g_current_setting_rgb = SM2135_20MA;
// Mapping between RGBCW to current SM2135 channels
byte g_channelOrder[5] = { 2, 1, 0, 4, 3 };


void usleep(int r) //delay function do 10*r nops, because rtos_delay_milliseconds is too much
{
Expand Down Expand Up @@ -149,7 +148,7 @@ void SM2135_Write(float *rgbcw) {
if(CFG_HasFlag(OBK_FLAG_SM2135_SEPARATE_MODES)) {
bRGB = 0;
for(i = 0; i < 3; i++){
if(rgbcw[g_channelOrder[i]]!=0) {
if(rgbcw[g_cfg.ledRemap.ar[i]]!=0) {
bRGB = 1;
break;
}
Expand All @@ -158,9 +157,9 @@ void SM2135_Write(float *rgbcw) {
Soft_I2C_Start(SM2135_ADDR_MC);
Soft_I2C_WriteByte(g_current_setting_rgb);
Soft_I2C_WriteByte(SM2135_RGB);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[0]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[1]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[2]]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.r]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.g]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.b]);
Soft_I2C_Stop();
} else {
Soft_I2C_Start(SM2135_ADDR_MC);
Expand All @@ -170,20 +169,20 @@ void SM2135_Write(float *rgbcw) {
usleep(SM2135_DELAY);

Soft_I2C_Start(SM2135_ADDR_C);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[3]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[4]]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.c]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.w]);
Soft_I2C_Stop();

}
} else {
Soft_I2C_Start(SM2135_ADDR_MC);
Soft_I2C_WriteByte(g_current_setting_rgb);
Soft_I2C_WriteByte(SM2135_RGB);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[0]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[1]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[2]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[3]]);
Soft_I2C_WriteByte(rgbcw[g_channelOrder[4]]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.r]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.g]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.b]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.c]);
Soft_I2C_WriteByte(rgbcw[g_cfg.ledRemap.w]);
Soft_I2C_Stop();
}
}
Expand Down Expand Up @@ -234,23 +233,25 @@ commandResult_t CMD_LEDDriver_WriteRGBCW(const void *context, const char *cmd, c
// SM2135_Map 2 1 0 4 3

commandResult_t CMD_LEDDriver_Map(const void *context, const char *cmd, const char *args, int flags){

int r, g, b, c, w;
Tokenizer_TokenizeString(args,0);

if(Tokenizer_GetArgsCount()==0) {
ADDLOG_DEBUG(LOG_FEATURE_CMD, "Current map is %i %i %i %i %i",
(int)g_channelOrder[0],(int)g_channelOrder[1],(int)g_channelOrder[2],(int)g_channelOrder[3],(int)g_channelOrder[4]);
ADDLOG_INFO(LOG_FEATURE_CMD, "Current map is %i %i %i %i %i",
(int)g_cfg.ledRemap.r,(int)g_cfg.ledRemap.g,(int)g_cfg.ledRemap.b,(int)g_cfg.ledRemap.c,(int)g_cfg.ledRemap.w);
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}

g_channelOrder[0] = Tokenizer_GetArgIntegerRange(0, 0, 4);
g_channelOrder[1] = Tokenizer_GetArgIntegerRange(1, 0, 4);
g_channelOrder[2] = Tokenizer_GetArgIntegerRange(2, 0, 4);
g_channelOrder[3] = Tokenizer_GetArgIntegerRange(3, 0, 4);
g_channelOrder[4] = Tokenizer_GetArgIntegerRange(4, 0, 4);
r = Tokenizer_GetArgIntegerRange(0, 0, 4);
g = Tokenizer_GetArgIntegerRange(1, 0, 4);
b = Tokenizer_GetArgIntegerRange(2, 0, 4);
c = Tokenizer_GetArgIntegerRange(3, 0, 4);
w = Tokenizer_GetArgIntegerRange(4, 0, 4);

CFG_SetLEDRemap(r, g, b, c, w);

ADDLOG_DEBUG(LOG_FEATURE_CMD, "New map is %i %i %i %i %i",
(int)g_channelOrder[0],(int)g_channelOrder[1],(int)g_channelOrder[2],(int)g_channelOrder[3],(int)g_channelOrder[4]);
ADDLOG_INFO(LOG_FEATURE_CMD, "New map is %i %i %i %i %i",
(int)g_cfg.ledRemap.r,(int)g_cfg.ledRemap.g,(int)g_cfg.ledRemap.b,(int)g_cfg.ledRemap.c,(int)g_cfg.ledRemap.w);

return CMD_RES_OK;
}
Expand Down Expand Up @@ -281,10 +282,12 @@ static commandResult_t SM2135_Current(const void *context, const char *cmd, cons
// CMD_LEDDriver_WriteRGBCW FF00000000
void SM2135_Init() {

// default setting (applied only if none was applied earlier)
CFG_SetDefaultLEDRemap(2, 1, 0, 4, 3);

g_i2c_pin_clk = PIN_FindPinIndexForRole(IOR_SM2135_CLK,g_i2c_pin_clk);
g_i2c_pin_data = PIN_FindPinIndexForRole(IOR_SM2135_DAT,g_i2c_pin_data);


Soft_I2C_PreInit();

//cmddetail:{"name":"SM2135_RGBCW","args":"[HexColor]",
Expand Down
7 changes: 5 additions & 2 deletions src/driver/drv_sm2235.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void SM2235_Write(float *rgbcw) {

for (i = 0; i < 5; i++) {
// convert 0-255 to 0-1023
//cur_col_10[i] = rgbcw[g_channelOrder[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_channelOrder[i]], 0, 255.0f, 0, 1023.0f);
//cur_col_10[i] = rgbcw[g_cfg.ledRemap.ar[i]] * 4;
cur_col_10[i] = MAP(rgbcw[g_cfg.ledRemap.ar[i]], 0, 255.0f, 0, 1023.0f);
}

#define SM2235_FIRST_BYTE(x) ((x >> 8) & 0xFF)
Expand Down Expand Up @@ -80,6 +80,9 @@ static commandResult_t SM2235_Current(const void *context, const char *cmd, cons
// SM2235_RGBCW FF00000000
void SM2235_Init() {

// default setting (applied only if none was applied earlier)
CFG_SetDefaultLEDRemap(2, 1, 0, 4, 3);

g_i2c_pin_clk = PIN_FindPinIndexForRole(IOR_SM2235_CLK,g_i2c_pin_clk);
g_i2c_pin_data = PIN_FindPinIndexForRole(IOR_SM2235_DAT,g_i2c_pin_data);

Expand Down
15 changes: 15 additions & 0 deletions src/new_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ void CFG_SetDefaultConfig() {
g_cfg_pendingChanges++;
}

void CFG_SetLEDRemap(int r, int g, int b, int c, int w) {
if (g_cfg.ledRemap.r != r || g_cfg.ledRemap.g != g || g_cfg.ledRemap.b != b || g_cfg.ledRemap.c != c || g_cfg.ledRemap.w != w) {
g_cfg.ledRemap.r = r;
g_cfg.ledRemap.g = g;
g_cfg.ledRemap.b = b;
g_cfg.ledRemap.c = c;
g_cfg.ledRemap.w = w;
CFG_MarkAsDirty();
}
}
void CFG_SetDefaultLEDRemap(int r, int g, int b, int c, int w) {
if (g_cfg.ledRemap.r == 0 && g_cfg.ledRemap.g == 0 && g_cfg.ledRemap.b == 0 && g_cfg.ledRemap.c == 0 && g_cfg.ledRemap.w == 0) {
CFG_SetLEDRemap(r, g, b, c, w);
}
}
const char *CFG_GetWebappRoot(){
return g_cfg.webappRoot;
}
Expand Down
2 changes: 2 additions & 0 deletions src/new_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void CFG_SetMQTTUserName(const char *s);
void CFG_SetMQTTGroupTopic(const char *s);
void CFG_SetMQTTPass(const char *s);
const char *CFG_GetWebappRoot();
void CFG_SetLEDRemap(int r, int g, int b, int c, int w);
void CFG_SetDefaultLEDRemap(int r, int g, int b, int c, int w);
int CFG_SetWebappRoot(const char *s);
void CFG_InitAndLoad();
//void CFG_ApplyStartChannelValues();
Expand Down
26 changes: 24 additions & 2 deletions src/new_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ typedef struct led_corr_s { // LED gamma correction and calibration data block

#define MAGIC_LED_CORR_SIZE 24

typedef struct ledRemap_s {
//unsigned short r : 3;
//unsigned short g : 3;
//unsigned short b : 3;
//unsigned short c : 3;
//unsigned short w : 3;
// I want to be able to easily access indices with []
union {
struct {
byte r;
byte g;
byte b;
byte c;
byte w;
};
byte ar[5];
};
} ledRemap_t;

#define MAGIC_LED_REMAP_SIZE 5

//
// Main config structure (less than 2KB)
//
Expand All @@ -262,7 +283,6 @@ typedef struct mainConfig_s {
// 0x4
int version;
int genericFlags;
// unused
int genericFlags2;
unsigned short changeCounter;
unsigned short otaCounter;
Expand Down Expand Up @@ -312,7 +332,8 @@ typedef struct mainConfig_s {

// offset 0x000004BC
unsigned long LFS_Size; // szie of LFS volume. it's aligned against the end of OTA
byte unusedSectorAB[124];
byte unusedSectorAB[119];
ledRemap_t ledRemap;
led_corr_t led_corr;
// alternate topic name for receiving MQTT commands
// offset 0x00000554
Expand Down Expand Up @@ -383,6 +404,7 @@ void Channel_SaveInFlashIfNeeded(int ch);
int CHANNEL_FindMaxValueForChannel(int ch);
// cmd_channels.c
const char *CHANNEL_GetLabel(int ch);
//ledRemap_t *CFG_GetLEDRemap();

void get_Relay_PWM_Count(int* relayCount, int* pwmCount, int* dInputCount);
int h_isChannelPWM(int tg_ch);
Expand Down
6 changes: 5 additions & 1 deletion src/win_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ int __cdecl main(int argc, char **argv)
printf("sizeof(double) = %d\n", (int)sizeof(double));
printf("sizeof(long double) = %d\n", (int)sizeof(long double));
printf("sizeof(led_corr_t) = %d\n", (int)sizeof(led_corr_t));


if (sizeof(ledRemap_t) != MAGIC_LED_REMAP_SIZE) {
printf("sizeof(ledRemap_t) != MAGIC_LED_REMAP_SIZE!: %i\n", sizeof(ledRemap_t));
system("pause");
}
if (sizeof(led_corr_t) != MAGIC_LED_CORR_SIZE) {
printf("sizeof(led_corr_t) != MAGIC_LED_CORR_SIZE!: %i\n", sizeof(led_corr_t));
system("pause");
Expand Down

0 comments on commit 9095b2f

Please sign in to comment.