diff --git a/src/hal/bk7231/hal_flashVars_bk7231.c b/src/hal/bk7231/hal_flashVars_bk7231.c
index 71c2b9d13..55ec3f766 100644
--- a/src/hal/bk7231/hal_flashVars_bk7231.c
+++ b/src/hal/bk7231/hal_flashVars_bk7231.c
@@ -23,14 +23,19 @@
#include "../../logging/logging.h"
-
+#define MAX_RETAIN_CHANNELS 12
typedef struct flash_vars_structure
{
+ // offset 0
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
- unsigned char _align[3];
+ // offset 4
+ short savedValues[MAX_RETAIN_CHANNELS];
+ // offset 28
+ unsigned char rgb[3];
unsigned char len; // length of the whole structure (i.e. 2+2+1 = 5) MUST NOT BE 255
+ // size 32
} FLASH_VARS_STRUCTURE;
extern FLASH_VARS_STRUCTURE flash_vars;
@@ -518,6 +523,29 @@ void HAL_FlashVars_IncreaseBootCount(){
data.boot_count - data.boot_success_count );
#endif
}
+void HAL_FlashVars_SaveChannel(int index, int value) {
+#ifndef DISABLE_FLASH_VARS_VARS
+ FLASH_VARS_STRUCTURE data;
+
+
+ if(index < 0 || index >= MAX_RETAIN_CHANNELS){
+ ADDLOG_INFO(LOG_FEATURE_CFG, "####### Flash Save Can't Save Channel %d as %d (not enough space in array) #######", index,value);
+ return;
+ }
+
+ flash_vars_init();
+ flash_vars.savedValues[index] = value;
+ ADDLOG_INFO(LOG_FEATURE_CFG, "####### Flash Save Channel %d as %d #######", index,value);
+ flash_vars_write();
+
+ flash_vars_read(&data);
+ ADDLOG_DEBUG(LOG_FEATURE_CFG, "re-read - offset %d, boot count %d, boot success %d, bootfailures %d",
+ flash_vars_offset,
+ data.boot_count,
+ data.boot_success_count,
+ data.boot_count - data.boot_success_count );
+#endif
+}
// call once started (>30s?)
void HAL_FlashVars_SaveBootComplete(){
@@ -550,6 +578,13 @@ int HAL_FlashVars_GetBootFailures(){
int HAL_FlashVars_GetBootCount(){
return flash_vars.boot_count;
}
+int HAL_FlashVars_GetChannelValue(int ch){
+ if(ch < 0 || ch >= MAX_RETAIN_CHANNELS){
+ ADDLOG_INFO(LOG_FEATURE_CFG, "####### Flash Save Can't Get Channel %d (not enough space in array) #######", ch);
+ return 0;
+ }
+ return flash_vars.savedValues[ch];
+}
#endif
diff --git a/src/hal/bl602/hal_flashVars_bl602.c b/src/hal/bl602/hal_flashVars_bl602.c
index 158cdeae4..b11e66fa2 100644
--- a/src/hal/bl602/hal_flashVars_bl602.c
+++ b/src/hal/bl602/hal_flashVars_bl602.c
@@ -78,6 +78,12 @@ void HAL_FlashVars_IncreaseBootCount(){
}
+void HAL_FlashVars_SaveChannel(int index, int value) {
+
+}
+int HAL_FlashVars_GetChannelValue(int ch) {
+ return 0;
+}
#endif // PLATFORM_BL602
diff --git a/src/hal/hal_flashVars.h b/src/hal/hal_flashVars.h
index ffb4e6be5..336dec7a4 100644
--- a/src/hal/hal_flashVars.h
+++ b/src/hal/hal_flashVars.h
@@ -9,4 +9,7 @@ void HAL_FlashVars_IncreaseBootCount();
void HAL_FlashVars_SaveBootComplete();
// call to return the number of boots since a HAL_FlashVars_SaveBootComplete
int HAL_FlashVars_GetBootFailures();
-int HAL_FlashVars_GetBootCount();
\ No newline at end of file
+int HAL_FlashVars_GetBootCount();
+void HAL_FlashVars_SaveChannel(int index, int value);
+int HAL_FlashVars_GetChannelValue(int ch);
+
diff --git a/src/hal/xr809/hal_flashVars_xr809.c b/src/hal/xr809/hal_flashVars_xr809.c
index c9e625eb8..a53fc2a4c 100644
--- a/src/hal/xr809/hal_flashVars_xr809.c
+++ b/src/hal/xr809/hal_flashVars_xr809.c
@@ -15,6 +15,13 @@ int HAL_FlashVars_GetBootFailures(){
}
void HAL_FlashVars_IncreaseBootCount(){
}
+void HAL_FlashVars_SaveChannel(int index, int value) {
+
+}
+int HAL_FlashVars_GetChannelValue(int ch) {
+ return 0;
+}
+
diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c
index 078ffdff3..038e3ad58 100644
--- a/src/httpserver/http_fns.c
+++ b/src/httpserver/http_fns.c
@@ -298,7 +298,7 @@ int http_fn_index(http_request_t *request) {
int bFirst = true;
hprintf128(request,"
");
for(i = 0; i < CHANNEL_MAX; i++) {
- if(CHANNEL_IsUsed(i)) {
+ if(CHANNEL_IsInUse(i)) {
int value = CHANNEL_Get(i);
if(bFirst == false) {
hprintf128(request,", ");
@@ -1388,10 +1388,11 @@ int http_fn_cfg_startup(http_request_t *request) {
hprintf128(request,"Here you can set pin start values");
hprintf128(request,"For relays, simply use 1 or 0
");
- //hprintf128(request,"For 'remember last power state', use -1 as a special value
");
+ hprintf128(request,"For 'remember last power state', use -1 as a special value
");
hprintf128(request,"For dimmers, range is 0 to 100
");
hprintf128(request,"For custom values, you can set any number you want to
");
hprintf128(request,"Remember that you can also use short startup command to run commands like led_baseColor #FF0000 and led_enableAll 1 etc
");
+ hprintf128(request,"Remembering last state of LED driver is not yet fully supported, please wait!
");
if( http_getArg(request->url,"idx",tmpA,sizeof(tmpA))) {
channelIndex = atoi(tmpA);
diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c
index ca3916656..e6dca0d60 100644
--- a/src/mqtt/new_mqtt.c
+++ b/src/mqtt/new_mqtt.c
@@ -681,7 +681,7 @@ int MQTT_RunEverySecondUpdate() {
int g_sent_thisFrame = 0;
while(g_publishItemIndex < CHANNEL_MAX) {
- if(CHANNEL_IsUsed(g_publishItemIndex)) {
+ if(CHANNEL_IsInUse(g_publishItemIndex)) {
MQTT_ChannelPublish(g_publishItemIndex);
g_sent_thisFrame++;
if(g_sent_thisFrame>=2){
diff --git a/src/new_pins.c b/src/new_pins.c
index 088b821dd..ddcba4cb1 100644
--- a/src/new_pins.c
+++ b/src/new_pins.c
@@ -195,27 +195,7 @@ void CHANNEL_SetType(int ch, int type) {
int CHANNEL_GetType(int ch) {
return g_cfg.pins.channelTypes[ch];
}
-bool CHANNEL_IsInUse(int ch) {
- int i;
- for(i = 0; i < PLATFORM_GPIO_MAX; i++){
- if(g_cfg.pins.channels[i] == ch) {
- switch(g_cfg.pins.roles[i])
- {
- case IOR_LED:
- case IOR_LED_n:
- case IOR_Relay:
- case IOR_Relay_n:
- case IOR_PWM:
- return true;
- break;
- default:
- break;
- }
- }
- }
- return false;
-}
void CHANNEL_SetAll(int iVal, int iFlags) {
int i;
@@ -479,6 +459,13 @@ 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);
+ }
+
}
void CFG_ApplyChannelStartValues() {
int i;
@@ -487,7 +474,7 @@ void CFG_ApplyChannelStartValues() {
iValue = g_cfg.startChannelValues[i];
if(iValue == -1) {
-
+ g_channelValues[i] = HAL_FlashVars_GetChannelValue(i);
} else {
g_channelValues[i] = iValue;
}
@@ -613,17 +600,24 @@ bool CHANNEL_Check(int ch) {
return 0;
}
-
-int CHANNEL_IsUsed(int ch) {
+bool CHANNEL_IsInUse(int ch) {
int i;
- for (i = 0; i < PLATFORM_GPIO_MAX; i++){
- if (g_cfg.pins.channels[i] == ch){
- return true;
+
+ if(g_cfg.pins.channelTypes[ch] != ChType_Default){
+ return true;
+ }
+
+ for(i = 0; i < PLATFORM_GPIO_MAX; i++){
+ if(g_cfg.pins.roles[i] != IOR_None){
+ if(g_cfg.pins.channels[i] == ch) {
+ return true;
+ }
}
}
return false;
}
+
int CHANNEL_GetRoleForOutputChannel(int ch){
int i;
for (i = 0; i < PLATFORM_GPIO_MAX; i++){
diff --git a/src/new_pins.h b/src/new_pins.h
index 94c43ce30..1995ad870 100644
--- a/src/new_pins.h
+++ b/src/new_pins.h
@@ -157,7 +157,6 @@ void CHANNEL_Add(int ch, int iVal);
void CHANNEL_AddClamped(int ch, int iVal, int min, int max);
int CHANNEL_Get(int ch);
int CHANNEL_GetRoleForOutputChannel(int ch);
-int CHANNEL_IsUsed(int ch);
// See: enum ChannelType
void CHANNEL_SetType(int ch, int type);
int CHANNEL_GetType(int ch);
@@ -165,6 +164,7 @@ int CHANNEL_GetType(int ch);
void CHANNEL_SetAll(int iVal, int iFlags);
void CHANNEL_SetStateOnly(int iVal);
int CHANNEL_HasChannelPinWithRole(int ch, int iorType);
+bool CHANNEL_IsInUse(int ch);
int PIN_GetPWMIndexForPinIndex(int pin);
diff --git a/windowsTest_msvc2008.vcproj b/windowsTest_msvc2008.vcproj
index da39175f2..cb8a3c985 100644
--- a/windowsTest_msvc2008.vcproj
+++ b/windowsTest_msvc2008.vcproj
@@ -1,1134 +1,1134 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-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