Poll() refactoring, PWM fix, Hum preparation

This commit is contained in:
Климов Андрей Николаевич
2022-04-15 00:05:44 +03:00
parent b3af9865c7
commit 4c80f435e5
21 changed files with 76 additions and 66 deletions

View File

@@ -0,0 +1,3 @@
export PORT=cu.usbmodem14101
echo . | stty -f /dev/$PORT speed 1200
../tools/mac/tool-bossac/bossac -U false -p $PORT -i -e -w -v -b firmware.bin -R

View File

@@ -9,7 +9,7 @@ class abstractCh {
public:
abstractCh(){};
virtual ~abstractCh(){};
virtual int Poll(short cause) = 0;
virtual int Poll(short cause) {return 0;}
virtual int Setup() =0; //Should initialize hardware and reserve resources
virtual int Anounce () {return 0;};
virtual int Stop() {return 0;}; //Should free resources

View File

@@ -57,6 +57,10 @@ e-mail anklimov@gmail.com
#include "modules/out_elevator.h"
#endif
#ifdef HUMIDIFIER_ENABLE
#include "modules/out_humidifier.h"
#endif
short modbusBusy = 0;
bool isPendedModbusWrites = false;
@@ -207,6 +211,13 @@ void Item::Parse() {
// debugSerial<<F("AC driver created")<<endl;
break;
#endif
#ifdef HUMIDIFIER_ENABLE
case CH_HUMIDIFIER:
driver = new out_humidifier (this);
break;
#endif
#ifndef COUNTER_DISABLE
case CH_COUNTER:
driver = new out_counter (this);
@@ -1341,7 +1352,8 @@ if (timestampObj)
itemCmd st(ST_UINT32,cmd);
st.Int(remain);
if (!(remain % 1000))
//if (!(remain % 1000))
if (cause == POLLING_1S)
{
SendStatusImmediate(st,SEND_DELAYED);
debugSerial<< remain/1000 << F(" sec remaining") << endl;
@@ -1365,28 +1377,14 @@ switch (cause)
case CH_MODBUS:
checkModbusDimmer();
sendDelayedStatus();
return INTERVAL_SLOW_POLLING;
return true;
break;
case CH_VC:
checkFM();
sendDelayedStatus();
return INTERVAL_SLOW_POLLING;
return true;
break;
/*
case CH_VCTEMP:
checkHeatRetry();
sendDelayedStatus();
return INTERVAL_SLOW_POLLING;
break; */
#endif
/* case CH_RGB: //All channels with slider generate too many updates
case CH_RGBW:
case CH_DIMMER:
case CH_PWM:
case CH_VCTEMP:
case CH_THERMO:
case CH_GROUP:*/
default:
sendDelayedStatus();
}
@@ -1397,7 +1395,7 @@ switch (cause)
return driver->Poll(cause);
}
return 0;
return false;
}
void Item::sendDelayedStatus()

View File

@@ -60,6 +60,7 @@ e-mail anklimov@gmail.com
#define CH_MULTIVENT 18
#define CH_ELEVATOR 19
#define CH_COUNTER 20
#define CH_HUMIDIFIER 21
//#define CHANNEL_TYPES 13
@@ -74,6 +75,7 @@ e-mail anklimov@gmail.com
#define POLLING_SLOW 1
#define POLLING_FAST 2
#define POLLING_INT 3
#define POLLING_1S 4
#define I_TYPE 0 //Type of item

View File

@@ -2268,6 +2268,15 @@ infoSerial<<F("\n(+)MULTIVENT");
infoSerial<<F("\n(-)MULTIVENT");
#endif
infoSerial<<endl;
#ifdef HUMIDIFIER_ENABLE
infoSerial<<F("\n(+)HUMIDIFIER");
#endif
#ifdef ELEVATOR_ENABLE
infoSerial<<F("\n(+)ELEVATOR");
#endif
// WDT_Disable( WDT ) ;
#if defined(__SAM3X8E__)
@@ -2564,17 +2573,21 @@ configLocked++;
configLocked--;
}
// POLLINT_FAST - as often AS possible every item
// POLLING_1S - once per second every item
// POLLING_SLOW - just one item every 1S (Note: item::Poll() should return true if some action done - it will postpone next SLOW POLLING)
void pollingLoop(void) {
if (!items) return;
// FAST POLLINT - as often AS possible every item
if (!items) return;
bool secExpired = isTimeOver(timerPollingCheck,millis(),INTERVAL_SLOW_POLLING);
if (secExpired) timerPollingCheck = millis();
configLocked++;
aJsonObject * item = items->child;
while (items && item)
if (item->type == aJson_Array && aJson.getArraySize(item)>1) {
Item it(item);
if (it.isValid()) {
it.Poll(POLLING_FAST);
it.Poll((secExpired)?POLLING_1S:POLLING_FAST);
} //isValid
yield();
item = item->next;
@@ -2583,8 +2596,8 @@ configLocked--;
// SLOW POLLING
boolean done = false;
if (lanStatus == RETAINING_COLLECTING) return;
//if (millis() > timerPollingCheck)
if (isTimeOver(timerPollingCheck,millis(),INTERVAL_SLOW_POLLING))
if (secExpired)
{
while (pollingItem && !done) {
if (pollingItem->type == aJson_Array) {
@@ -2592,7 +2605,7 @@ configLocked--;
uint32_t ret = it.Poll(POLLING_SLOW);
if (ret)
{
timerPollingCheck = millis();// + ret; //INTERVAL_CHECK_MODBUS;
////// timerPollingCheck = millis();// + ret; //INTERVAL_CHECK_MODBUS;
done = true;
}
}//if

View File

@@ -242,16 +242,14 @@ return (power & 1);
int out_AC::Poll(short cause)
{
if (cause!=POLLING_SLOW) return 0;
if (cause!=POLLING_SLOW) return false;
//long now = millis();
//if (now - prevPolling > INTERVAL_AC_POLLING) {
if (isTimeOver(prevPolling,millis(),INTERVAL_AC_POLLING)) {
prevPolling = millisNZ();
debugSerial.println(F("Polling"));
SendData(qstn, sizeof(qstn)/sizeof(byte)); //Опрос кондиционера
}
///delay(100);
if(AC_Serial.available() >= 37){ //was 0
AC_Serial.readBytes(data, 37);
while(AC_Serial.available()){
@@ -263,10 +261,9 @@ if (cause!=POLLING_SLOW) return 0;
InsertData(data, 37);
}
}
return INTERVAL_SLOW_POLLING;
return true;
};
//int out_AC::Ctrl(short cmd, short n, int * Parameters, int suffixCode, char* subItem)
int out_AC::Ctrl(itemCmd cmd, char* subItem , bool toExecute)
{
//char s_mode[10];

View File

@@ -38,6 +38,7 @@ return driverStatus;
int out_counter::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
if (!item) return 0;

View File

@@ -31,12 +31,6 @@ int out_dmx::Status()
return driverStatus;
}
int out_dmx::Poll(short cause)
{
return 0;
};
int out_dmx::getChanType()
{
if (item)

View File

@@ -12,7 +12,6 @@ public:
out_dmx(Item * _item):colorChannel(_item){};
int Setup() override;
int Poll(short cause) override;
int Stop() override;
int Status() override;

View File

@@ -451,6 +451,7 @@ int out_Modbus::sendModbus(char * paramName, int32_t value, uint8_t regType)
int out_Modbus::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
bool lineInitialized = false;
if (modbusBusy || (Status() != CST_INITIALIZED)) return 0;

View File

@@ -101,6 +101,7 @@ return st.getPercents255();
*/
int out_Motor::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
int curPos = -1;
int targetPos = -1;
int dif;

View File

@@ -160,6 +160,7 @@ return (item->getCmd()!=CMD_OFF);
int out_pid::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
if (store && store->pid && (Status() == CST_INITIALIZED) && item && (item->getCmd()!=CMD_OFF))
{
//double prevOut=store->output;

View File

@@ -85,12 +85,6 @@ return driverStatus;
}
int out_pwm::Poll(short cause)
{
return 0;
};
int out_pwm::getChanType()
{
if (item)

View File

@@ -12,7 +12,6 @@ public:
out_pwm(Item * _item):colorChannel(_item){};
int Setup() override;
int Poll(short cause) override;
int Stop() override;
int Status() override;

View File

@@ -7,7 +7,7 @@
#include "item.h"
#include "main.h"
#include "dmx.h"
#include "utils.h"
static int driverStatus = CST_UNKNOWN;
void out_relay::getConfig()
@@ -44,7 +44,7 @@ if (item) item->setExt(0);
//if (item->getCmd()) item->setFlag(SEND_COMMAND);
//if (item->itemVal) item->setFlag(SEND_PARAMETERS);
driverStatus = CST_INITIALIZED;
if (!item->isActive())
if (item->isActive()>0) ///????
{
item->setExt(millisNZ());
}
@@ -63,7 +63,7 @@ int out_relay::Status()
{
return driverStatus;
}
/*
const char action_P[] PROGMEM = "action";
const char cooling_P[] PROGMEM = "cooling";
const char heating_P[] PROGMEM = "heating";
@@ -71,7 +71,7 @@ const char drying_P[] PROGMEM = "drying";
const char idle_P[] PROGMEM = "idle";
const char fan_P[] PROGMEM = "fan";
const char off_P[] PROGMEM = "off";
*/
void out_relay::relay(bool state)
{
@@ -111,14 +111,9 @@ debugSerial << F("pub action ") << publishTopic(item->itemArr->name,val,subtopic
}
bool getPinVal(uint8_t pin)
{
return (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));
}
int out_relay::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
if (!item) return 0;
itemCmd st;
st.loadItem(item);

View File

@@ -89,12 +89,6 @@ return driverStatus;
}
int out_SPILed::Poll(short cause)
{
return 0;
};
int out_SPILed::getChanType()
{

View File

@@ -17,7 +17,6 @@ public:
out_SPILed(Item * _item):colorChannel(_item){getConfig();};
int Setup() override;
int Poll(short cause) override;
int Stop() override;
int Status() override;
int getChanType() override;

View File

@@ -287,6 +287,7 @@ bufB[0]=0;
int out_UARTbridge::Poll(short cause)
{
if (cause==POLLING_SLOW) return 0;
uint8_t chA;
uint8_t chB;

View File

@@ -41,6 +41,16 @@ const char statsval_P[] PROGMEM = "uptime,freeheap";
const char uptime_P[] PROGMEM = "uptime";
const char freeheap_P[] PROGMEM = "freeheap";
const char CMDTOPIC_P[] PROGMEM = CMDTOPIC;
const char action_P[] PROGMEM = "action";
const char cooling_P[] PROGMEM = "cooling";
const char heating_P[] PROGMEM = "heating";
const char drying_P[] PROGMEM = "drying";
const char idle_P[] PROGMEM = "idle";
const char fan_P[] PROGMEM = "fan";
const char off_P[] PROGMEM = "off";
const char on_P[] PROGMEM = "on";
/*
{"name": "Спальня LED",

View File

@@ -581,7 +581,7 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m
return ((currTime>endTime) && (currTime <timestamp)) ||
((timestamp<endTime) && ((currTime>endTime) || (currTime <timestamp)));
}
//millis() - tmr1 >= MY_PERIOD
@@ -721,10 +721,11 @@ itemCmd mapInt(int32_t arg, aJsonObject* map)
return _itemCmd.Int(arg);
}
//Same as millis() but never return 0 or -1
unsigned long millisNZ(uint8_t shift)
{
unsigned long now = millis()>>shift;
if (!now) now=1;
if (!now || !(now+1)) now=1;
return now;
}
@@ -774,5 +775,11 @@ serialParamType str2SerialParam(char * str)
debugSerial<< F("Default serial mode N81 used");
return static_cast<serialParamType> (SERIAL_8N1);
}
bool getPinVal(uint8_t pin)
{
return (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));
}
#pragma message(VAR_NAME_VALUE(debugSerial))
#pragma message(VAR_NAME_VALUE(SERIAL_BAUD))

View File

@@ -75,3 +75,4 @@ itemCmd mapInt(int32_t arg, aJsonObject* map);
unsigned long millisNZ(uint8_t shift=0);
serialParamType str2SerialParam(char * str);
String toString(const IPAddress& address);
bool getPinVal(uint8_t pin);