@@ -1451,6 +1451,12 @@ void CHANNEL_Set_Ex(int ch, int iVal, int iFlags, int ausemovingaverage) {
14511451 if (bSilent == 0 ) {
14521452 addLogAdv (LOG_INFO , LOG_FEATURE_GENERAL , "CHANNEL_Set channel %i has changed to %i (flags %i)\n\r" , ch , iVal , iFlags );
14531453 }
1454+ #ifdef ENABLE_BL_MOVINGAVG
1455+ //addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL, "CHANNEL_Set debug channel %i has changed to %i (flags %i)\n\r", ch, iVal, iFlags);
1456+ if (ausemovingaverage ) {
1457+ iVal = XJ_MovingAverage_int (prevValue , iVal );
1458+ }
1459+ #endif
14541460 g_channelValues [ch ] = iVal ;
14551461
14561462 Channel_OnChanged (ch , prevValue , iFlags );
@@ -2477,6 +2483,47 @@ static commandResult_t showgpi(const void* context, const char* cmd, const char*
24772483 return CMD_RES_OK ;
24782484}
24792485
2486+ #ifdef ENABLE_BL_MOVINGAVG
2487+ static int movingavg_cnt = 0 ;
2488+ static commandResult_t CMD_setMovingAvg (const void * context , const char * cmd ,
2489+ const char * args , int cmdFlags ) {
2490+ Tokenizer_TokenizeString (args , 0 );
2491+ if (Tokenizer_CheckArgsCountAndPrintWarning (cmd , 1 )) {
2492+ return CMD_RES_NOT_ENOUGH_ARGUMENTS ;
2493+ }
2494+
2495+ int fval = Tokenizer_GetArgInteger (0 );
2496+ if (fval < 0 ) {
2497+ ADDLOG_ERROR (LOG_FEATURE_ENERGYMETER , "%s" ,
2498+ CMD_GetResultString (CMD_RES_BAD_ARGUMENT ));
2499+ return CMD_RES_BAD_ARGUMENT ;
2500+ }
2501+ addLogAdv (LOG_INFO , LOG_FEATURE_ENERGYMETER , "MovingAvg=%i" , fval );
2502+ movingavg_cnt = fval ;
2503+ return CMD_RES_OK ;
2504+ }
2505+
2506+ float XJ_MovingAverage_float (float aprevvalue , float aactvalue ) {
2507+ //if aprevvalue not set, return actual
2508+ if (aprevvalue <= 0 ) return aactvalue ;
2509+ if (movingavg_cnt <= 1 ) return aactvalue ;
2510+ //if aprevvalue set, calculate simple average value
2511+ float res = (((movingavg_cnt - 1 ) * aprevvalue + aactvalue ) / movingavg_cnt );
2512+ //addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %.2f a: %.2f r: %.2f", aprevvalue, aactvalue, res);
2513+ return res ;
2514+ }
2515+
2516+ int XJ_MovingAverage_int (int aprevvalue , int aactvalue ) {
2517+ //if aprevvalue not set, return actual
2518+ if (aprevvalue <= 0 ) return aactvalue ;
2519+ if (movingavg_cnt <= 1 ) return aactvalue ;
2520+ //if aprevvalue set, calculate simple average value
2521+ int res = (((movingavg_cnt - 1 ) * aprevvalue + aactvalue ) / movingavg_cnt );
2522+ //addLogAdv(LOG_DEBUG, LOG_FEATURE_ENERGYMETER, "MovAvg p: %i a: %i r: %i", aprevvalue, aactvalue, res);
2523+ return res ;
2524+ }
2525+ #endif
2526+
24802527void PIN_AddCommands (void )
24812528{
24822529 //cmddetail:{"name":"showgpi","args":"NULL",
@@ -2504,6 +2551,13 @@ void PIN_AddCommands(void)
25042551 //cmddetail:"fn":"CMD_setButtonHoldRepeat","file":"new_pins.c","requires":"",
25052552 //cmddetail:"examples":""}
25062553 CMD_RegisterCommand ("setButtonHoldRepeat" , CMD_setButtonHoldRepeat , NULL );
2554+ #ifdef ENABLE_BL_MOVINGAVG
2555+ //cmddetail:{"name":"setMovingAvg","args":"MovingAvg",
2556+ //cmddetail:"descr":"Moving average value for power and current. <=1 disable, >=2 count of avg values. The setting is temporary and need to be set at startup.",
2557+ //cmddetail:"fn":"NULL);","file":"new_pins.c","requires":"",
2558+ //cmddetail:"examples":""}
2559+ CMD_RegisterCommand ("setMovingAvg" , CMD_setMovingAvg , NULL );
2560+ #endif
25072561#if ALLOW_SSID2
25082562 //cmddetail:{"name":"setStartupSSIDChannel","args":"[Value]",
25092563 //cmddetail:"descr":"Sets retain channel number to store last used SSID, 0..MAX_RETAIN_CHANNELS-1, -1 to disable. Suggested channel number is 7 (MAXMAX_RETAIN_CHANNELS-5)",
0 commit comments