@@ -1329,145 +1329,145 @@ void CmndEnergyConfig(void) {
13291329 * USE_ENERGY_MARGIN_DETECTION and USE_ENERGY_POWER_LIMIT
13301330\*********************************************************************************************/
13311331
1332+ void ResponseAppendMargin (uint16_t * start, uint32_t step_size = 0 );
1333+ void ResponseAppendMargin (uint16_t * start, uint32_t step_size) {
1334+ if (Energy->phase_count > 1 ) {
1335+ if (0 == step_size) {
1336+ step_size = sizeof (tPhase) / sizeof (uint16_t );
1337+ }
1338+ for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1339+ ResponseAppend_P (PSTR (" %c%d" ), (i)?' ,' :' [' , start[i * step_size]);
1340+ }
1341+ ResponseAppend_P (PSTR (" ]" ));
1342+ } else {
1343+ ResponseAppend_P (PSTR (" %d" ), start[0 ]);
1344+ }
1345+ }
1346+
13321347void EnergyMarginStatus (void ) {
1348+ /*
13331349 Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERDELTA "\":["));
13341350 for (uint32_t i = 0; i < Energy->phase_count; i++) {
13351351 ResponseAppend_P(PSTR("%s%d"), (i>0)?",":"", Energy->Settings.power_delta[i]);
13361352 }
1337- ResponseAppend_P (PSTR (" ],\" " D_CMND_POWERLOW " \" :[" ));
1338- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1339- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].min_power );
1340- }
1341- ResponseAppend_P (PSTR (" ],\" " D_CMND_POWERHIGH " \" :[" ));
1342- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1343- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].max_power );
1344- }
1345- ResponseAppend_P (PSTR (" ],\" " D_CMND_VOLTAGELOW " \" :[" ));
1346- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1347- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].min_voltage );
1348- }
1349- ResponseAppend_P (PSTR (" ],\" " D_CMND_VOLTAGEHIGH " \" :[" ));
1350- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1351- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].max_voltage );
1352- }
1353- ResponseAppend_P (PSTR (" ],\" " D_CMND_CURRENTLOW " \" :[" ));
1354- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1355- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].min_current );
1356- }
1357- ResponseAppend_P (PSTR (" ],\" " D_CMND_CURRENTHIGH " \" :[" ));
1358- for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1359- ResponseAppend_P (PSTR (" %s%d" ), (i>0 )?" ," :" " , Energy->Settings .phase [i].max_current );
1353+ ResponseAppend_P(PSTR("],\"" D_CMND_POWERLOW "\":"));
1354+ */
1355+ Response_P (PSTR (" {\" " D_CMND_STATUS D_STATUS9_MARGIN " \" :{\" " D_CMND_POWERDELTA " \" :" ));
1356+ ResponseAppendMargin (&Energy->Settings .power_delta [0 ], 1 );
1357+ ResponseAppend_P (PSTR (" ,\" " D_CMND_POWERLOW " \" :" ));
1358+
1359+ ResponseAppendMargin (&Energy->Settings .phase [0 ].min_power );
1360+ ResponseAppend_P (PSTR (" ,\" " D_CMND_POWERHIGH " \" :" ));
1361+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_power );
1362+ ResponseAppend_P (PSTR (" ,\" " D_CMND_VOLTAGELOW " \" :" ));
1363+ ResponseAppendMargin (&Energy->Settings .phase [0 ].min_voltage );
1364+ ResponseAppend_P (PSTR (" ,\" " D_CMND_VOLTAGEHIGH " \" :" ));
1365+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_voltage );
1366+ ResponseAppend_P (PSTR (" ,\" " D_CMND_CURRENTLOW " \" :" ));
1367+ ResponseAppendMargin (&Energy->Settings .phase [0 ].min_current );
1368+ ResponseAppend_P (PSTR (" ,\" " D_CMND_CURRENTHIGH " \" :" ));
1369+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_current );
1370+ ResponseAppend_P (PSTR (" ,\" " D_CMND_MAXPOWER " \" :" ));
1371+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_power_limit );
1372+ ResponseAppend_P (PSTR (" ,\" " D_CMND_MAXPOWERHOLD " \" :" ));
1373+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_power_limit_hold );
1374+ ResponseAppend_P (PSTR (" ,\" " D_CMND_MAXPOWERWINDOW " \" :" ));
1375+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_power_limit_window );
1376+ ResponseAppend_P (PSTR (" ,\" " D_CMND_MAXENERGY " \" :" ));
1377+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_energy );
1378+ ResponseAppend_P (PSTR (" ,\" " D_CMND_MAXENERGYSTART " \" :" ));
1379+ ResponseAppendMargin (&Energy->Settings .phase [0 ].max_energy_start );
1380+ ResponseJsonEndEnd ();
1381+ }
1382+
1383+ bool ResponseCmndEnergyMargin (uint16_t * start, uint32_t max_value, uint32_t default_value = 1 , uint32_t step_size = 0 );
1384+ bool ResponseCmndEnergyMargin (uint16_t * start, uint32_t max_value, uint32_t default_value, uint32_t step_size) {
1385+ bool value_changed = false ;
1386+ if ((XdrvMailbox.index >= 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1387+ if (0 == step_size) {
1388+ step_size = sizeof (tPhase) / sizeof (uint16_t );
1389+ }
1390+ if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= max_value)) {
1391+ if (0 == XdrvMailbox.index ) {
1392+ // PowerLow0 10
1393+ for (uint32_t i = 0 ; i < Energy->phase_count ; i++) {
1394+ start[i * step_size] = (1 == XdrvMailbox.payload ) ? default_value : XdrvMailbox.payload ;
1395+ }
1396+ } else {
1397+ // PowerLow 10 = PowerLow1 10 .. PowerLow8 10
1398+ start[(XdrvMailbox.index -1 ) * step_size] = (1 == XdrvMailbox.payload ) ? default_value : XdrvMailbox.payload ;
1399+ }
1400+ value_changed = true ;
1401+ }
1402+ if (0 == XdrvMailbox.index ) {
1403+ // PowerLow0, PowerLow0 10
1404+ ResponseCmnd ();
1405+ ResponseAppendMargin (start, step_size);
1406+ ResponseJsonEnd ();
1407+ } else {
1408+ // PowerLow = PowerLow1 .. PowerLow8
1409+ ResponseCmndIdxNumber (start[(XdrvMailbox.index -1 ) * step_size]);
1410+ }
13601411 }
1361- ResponseAppend_P ( PSTR ( " ]}} " )) ;
1412+ return value_changed ;
13621413}
13631414
13641415void CmndPowerDelta (void ) {
1416+ /*
13651417 if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
13661418 if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 32000)) {
13671419 Energy->Settings.power_delta[XdrvMailbox.index -1] = XdrvMailbox.payload;
13681420 }
13691421 ResponseCmndIdxNumber(Energy->Settings.power_delta[XdrvMailbox.index -1]);
13701422 }
1423+ */
1424+ ResponseCmndEnergyMargin (&Energy->Settings .power_delta [0 ], 32000 , 1 , 1 );
13711425}
13721426
13731427void CmndPowerLow (void ) {
1374- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1375- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1376- Energy->Settings .phase [XdrvMailbox.index -1 ].min_power = XdrvMailbox.payload ;
1377- }
1378- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].min_power );
1379- }
1428+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].min_power , 6000 );
13801429}
13811430
13821431void CmndPowerHigh (void ) {
1383- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1384- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1385- Energy->Settings .phase [XdrvMailbox.index -1 ].max_power = XdrvMailbox.payload ;
1386- }
1387- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_power );
1388- }
1432+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_power , 6000 );
13891433}
13901434
13911435void CmndVoltageLow (void ) {
1392- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1393- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 500 )) {
1394- Energy->Settings .phase [XdrvMailbox.index -1 ].min_voltage = XdrvMailbox.payload ;
1395- }
1396- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].min_voltage );
1397- }
1436+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].min_voltage , 500 );
13981437}
13991438
14001439void CmndVoltageHigh (void ) {
1401- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1402- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 500 )) {
1403- Energy->Settings .phase [XdrvMailbox.index -1 ].max_voltage = XdrvMailbox.payload ;
1404- }
1405- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_voltage );
1406- }
1440+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_voltage , 500 );
14071441}
14081442
14091443void CmndCurrentLow (void ) {
1410- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1411- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 25000 )) {
1412- Energy->Settings .phase [XdrvMailbox.index -1 ].min_current = XdrvMailbox.payload ;
1413- }
1414- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].min_current );
1415- }
1444+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].min_current , 25000 );
14161445}
14171446
14181447void CmndCurrentHigh (void ) {
1419- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1420- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 25000 )) {
1421- Energy->Settings .phase [XdrvMailbox.index -1 ].max_current = XdrvMailbox.payload ;
1422- }
1423- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_current );
1424- }
1448+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_current , 25000 );
14251449}
14261450
14271451void CmndMaxPower (void ) {
1428- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1429- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1430- Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit = XdrvMailbox.payload ;
1431- }
1432- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit );
1433- }
1452+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_power_limit , 6000 );
14341453}
14351454
14361455void CmndMaxPowerHold (void ) {
1437- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1438- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1439- Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit_hold = (1 == XdrvMailbox.payload ) ? MAX_POWER_HOLD : XdrvMailbox.payload ;
1440- }
1441- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit_hold );
1442- }
1456+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_power_limit_hold , 6000 , MAX_POWER_HOLD);
14431457}
14441458
14451459void CmndMaxPowerWindow (void ) {
1446- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1447- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1448- Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit_window = (1 == XdrvMailbox.payload ) ? MAX_POWER_WINDOW : XdrvMailbox.payload ;
1449- }
1450- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_power_limit_window );
1451- }
1460+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_power_limit_window , 6000 , MAX_POWER_WINDOW);
14521461}
14531462
14541463void CmndMaxEnergy (void ) {
1455- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1456- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 6000 )) {
1457- Energy->Settings .phase [XdrvMailbox.index -1 ].max_energy = XdrvMailbox.payload ;
1458- Energy->max_energy_state [XdrvMailbox.index -1 ] = 3 ;
1459- }
1460- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_energy );
1464+ if (ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_energy , 6000 )) {
1465+ Energy->max_energy_state [XdrvMailbox.index -1 ] = 3 ;
14611466 }
14621467}
14631468
14641469void CmndMaxEnergyStart (void ) {
1465- if ((XdrvMailbox.index > 0 ) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
1466- if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload < 24 )) {
1467- Energy->Settings .phase [XdrvMailbox.index -1 ].max_energy_start = XdrvMailbox.payload ;
1468- }
1469- ResponseCmndIdxNumber (Energy->Settings .phase [XdrvMailbox.index -1 ].max_energy_start );
1470- }
1470+ ResponseCmndEnergyMargin (&Energy->Settings .phase [0 ].max_energy_start , 23 );
14711471}
14721472
14731473/* *******************************************************************************************/
0 commit comments