Skip to content

Commit

Permalink
bl602 flash vars save pins, save pins on configure change
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Jun 8, 2022
1 parent 2dbe483 commit 69e2ac2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
26 changes: 20 additions & 6 deletions src/hal/bl602/hal_flashVars_bl602.c
Expand Up @@ -9,20 +9,23 @@
void BL602_InitEasyFlashIfNeeded();

#define EASYFLASH_MY_BOOTCOUNTS "myBtCnts"
#define BL602_SAVED_CHANNELS_MAX 8

typedef struct bl602_bootCounts_s {
unsigned short boot_count; // number of times the device has booted
unsigned short boot_success_count; // if a device boots completely (>30s), will equal boot_success_count
short channelStates[BL602_SAVED_CHANNELS_MAX];
} bl602_bootCounts_t;

bl602_bootCounts_t g_bootCounts;

static bl602_bootCounts_t g_bootCounts;
static int g_loaded = 0;

static int BL602_ReadFlashVars(void *target, int dataLen){
int readLen;

BL602_InitEasyFlashIfNeeded();

g_loaded = 1;
ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_ReadFlashVars: will read %d bytes", dataLen);
readLen = ef_get_env_blob(EASYFLASH_MY_BOOTCOUNTS, target, dataLen , NULL);
ADDLOG_DEBUG(LOG_FEATURE_CFG, "BL602_ReadFlashVars: really loaded %d bytes", readLen);
Expand Down Expand Up @@ -68,8 +71,7 @@ int HAL_FlashVars_GetBootFailures(){
}
void HAL_FlashVars_IncreaseBootCount(){
// defaults - in case read fails
g_bootCounts.boot_count = 0;
g_bootCounts.boot_success_count = 0;
memset(&g_bootCounts,0,sizeof(g_bootCounts));
// read saved
BL602_ReadFlashVars(&g_bootCounts,sizeof(g_bootCounts));
g_bootCounts.boot_count++;
Expand All @@ -79,10 +81,22 @@ void HAL_FlashVars_IncreaseBootCount(){


void HAL_FlashVars_SaveChannel(int index, int value) {

if(index<0||index>=BL602_SAVED_CHANNELS_MAX)
return;
if(g_loaded==0) {
BL602_ReadFlashVars(&g_bootCounts,sizeof(g_bootCounts));
}
g_bootCounts.channelStates[index] = value;
// save after increase
BL602_SaveFlashVars(&g_bootCounts,sizeof(g_bootCounts));
}
int HAL_FlashVars_GetChannelValue(int ch) {
return 0;
if(ch<0||ch>=BL602_SAVED_CHANNELS_MAX)
return 0;
if(g_loaded==0) {
BL602_ReadFlashVars(&g_bootCounts,sizeof(g_bootCounts));
}
return g_bootCounts.channelStates[ch];
}

#endif // PLATFORM_BL602
Expand Down
3 changes: 2 additions & 1 deletion src/httpserver/http_fns.c
Expand Up @@ -1401,7 +1401,8 @@ int http_fn_cfg_startup(http_request_t *request) {


CFG_SetChannelStartupValue(channelIndex,newValue);

// also save current value if marked as saved
Channel_SaveInFlashIfNeeded(channelIndex);
hprintf128(request,"<h5>Setting channel %i start value to %i<h5>",channelIndex,newValue);

}
Expand Down
16 changes: 9 additions & 7 deletions src/new_pins.c
Expand Up @@ -411,7 +411,12 @@ void PIN_SetPinRoleForPinIndex(int index, int role) {
void PIN_SetGenericDoubleClickCallback(void (*cb)(int pinIndex)) {
g_doubleClickCallback = cb;
}

void Channel_SaveInFlashIfNeeded(int ch) {
// save, if marked as save value in flash (-1)
if(g_cfg.startChannelValues[ch] == -1) {
HAL_FlashVars_SaveChannel(ch,g_channelValues[ch]);
}
}
static void Channel_OnChanged(int ch, int prevValue, int iFlags) {
int i;
int iVal;
Expand Down Expand Up @@ -461,12 +466,9 @@ static void Channel_OnChanged(int ch, int prevValue, int iFlags) {
}
EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_CHANNEL0 + ch, prevValue, iVal);

addLogAdv(LOG_ERROR, LOG_FEATURE_GENERAL,"CHANNEL_OnChanged: Channel index %i startChannelValues %i\n\r",ch,g_cfg.startChannelValues[ch]);
// save, if marked as save value in flash (-1)
if(g_cfg.startChannelValues[ch] == -1) {
HAL_FlashVars_SaveChannel(ch,iVal);
}

//addLogAdv(LOG_ERROR, LOG_FEATURE_GENERAL,"CHANNEL_OnChanged: Channel index %i startChannelValues %i\n\r",ch,g_cfg.startChannelValues[ch]);

Channel_SaveInFlashIfNeeded(ch);
}
void CFG_ApplyChannelStartValues() {
int i;
Expand Down
1 change: 1 addition & 0 deletions src/new_pins.h
Expand Up @@ -165,6 +165,7 @@ void CHANNEL_SetAll(int iVal, int iFlags);
void CHANNEL_SetStateOnly(int iVal);
int CHANNEL_HasChannelPinWithRole(int ch, int iorType);
bool CHANNEL_IsInUse(int ch);
void Channel_SaveInFlashIfNeeded(int ch);

int PIN_GetPWMIndexForPinIndex(int pin);

Expand Down

0 comments on commit 69e2ac2

Please sign in to comment.