From fb643945713371bc72eff718eb8dd30f72c7194f Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Fri, 19 Jul 2019 13:52:52 +0300 Subject: [PATCH] driver status, setup, stop --- lighthub/abstractch.h | 8 +++++++- lighthub/item.cpp | 29 +++++++++++++++++++++++++++-- lighthub/item.h | 1 + lighthub/main.cpp | 5 +++-- lighthub/modules/out_spiled.cpp | 21 +++++++++++++++++++-- lighthub/modules/out_spiled.h | 4 +++- 6 files changed, 60 insertions(+), 8 deletions(-) diff --git a/lighthub/abstractch.h b/lighthub/abstractch.h index bcf27a5..ed7efae 100644 --- a/lighthub/abstractch.h +++ b/lighthub/abstractch.h @@ -1,13 +1,19 @@ #pragma once #include "Arduino.h" +#define CST_UNKNOWN 0 +#define CST_INITIALIZED 1 + class abstractCh { public: abstractCh(){}; virtual ~abstractCh(){}; virtual int Poll() = 0; - virtual int Setup() =0; + virtual int Setup() =0; //Should initialize hardware and reserve resources virtual int Anounce () {}; + virtual int Stop() {}; //Should free resources + virtual int Status() {return CST_UNKNOWN;} + protected: virtual int publishTopic(char* topic, long value, char* subtopic = NULL); diff --git a/lighthub/item.cpp b/lighthub/item.cpp index f6ca397..7302998 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -135,15 +135,33 @@ void Item::Parse() { { case CH_SPILED: driver = new out_SPILed (this); + debugSerial<name << F(" T:") << itemType << F(" =") << getArg() << endl; } } +boolean Item::Setup() +{ + +if (driver) + { + if (driver->Status()) driver->Stop(); + driver->Setup(); + return true; + } +else return false; +} + Item::~Item() { - if (driver) delete driver; + if (driver) + { + delete driver; + debugSerial<Ctrl(cmd, n, Parameters, send, suffixCode, subItem); + if (driver) return driver->Ctrl(cmd, n, Parameters, send, suffixCode, subItem); int iaddr = getArg(); HSVstore st; @@ -1222,6 +1240,13 @@ int Item::checkModbusDimmer(int data) { } int Item::Poll() { + + if (driver && driver->Status()) + { + driver->Poll(); + return INTERVAL_POLLING; //TODO refactoring + } + // Legacy polling switch (itemType) { case CH_MODBUS: checkModbusDimmer(); diff --git a/lighthub/item.h b/lighthub/item.h index 8924203..57c5463 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -129,6 +129,7 @@ class Item ~Item(); boolean isValid (); + boolean Setup(); virtual int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL); virtual int Ctrl(char * payload, boolean send=true, char * subItem=NULL); diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 1576698..7a561c9 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -907,7 +907,8 @@ void applyConfig() { while (items && item) if (item->type == aJson_Array && aJson.getArraySize(item)>1) { Item it(item); - if (it.isValid()) { + if (it.isValid() && !it.Setup()) { + //Legacy Setup short inverse = 0; int pin=it.getArg(); if (pin<0) {pin=-pin; inverse = 1;} @@ -1162,7 +1163,7 @@ void saveFlash(short n, char *str) { if (len>MAXFLASHSTR-1) len=MAXFLASHSTR-1; for(int i=0;i(leds, NUM_LEDS); +driverStatus = CST_INITIALIZED; return 1; } +int out_SPILed::Stop() +{ +Serial.println("SPI-LED De-Init"); +FastLED.addLeds(leds, NUM_LEDS); +driverStatus = CST_UNKNOWN; +return 1; +} + +int out_SPILed::Status() +{ +return driverStatus; +} + + int out_SPILed::Poll() { FastLED.show(); @@ -32,7 +49,7 @@ if (subItem) from=atoi(subItem); to=from; } - +debugSerial<