Refactoring #4 (untested)

This commit is contained in:
2020-11-17 20:13:06 +03:00
parent 963a934f51
commit 14ff55fd59
15 changed files with 311 additions and 211 deletions

View File

@@ -45,27 +45,44 @@ return 0;
int out_dmx::getChanType()
{
if (item) return item->itemType;
if (item)
{
switch (numArgs)
{
case 3:
return CH_RGB;
case 4:
return CH_RGBW;
default:
return item->itemType;
}
return 0;
}
}
int out_dmx::PixelCtrl(itemCmd cmd)
int out_dmx::PixelCtrl(itemCmd cmd, char* subItem, bool show)
//int out_dmx::PixelCtrl(itemCmd cmd)
{
if (!item) return 0;
int iaddr = item->getArg(0);
itemCmd st(ST_RGB);
if (!item || !show) return 0;
short cType=getChanType();
if (cType==CH_DIMMER) //Single channel
{
DmxWrite(iaddr, cmd.getPercents255());
return 1;
}
itemCmd st(ST_RGB,CMD_VOID);
st.assignFrom(cmd);
switch (getChanType())
{ case CH_DIMMER:
DmxWrite(iaddr + 3, cmd.getPercents255());
break;
switch (cType)
{
case CH_RGBW:
DmxWrite(iaddr + 3, st.param.w);
DmxWrite(getChannelAddr(3), st.param.w);
case CH_RGB:
DmxWrite(iaddr, st.param.r);
DmxWrite(iaddr + 1, st.param.g);
DmxWrite(iaddr + 2, st.param.b);
DmxWrite(getChannelAddr(1), st.param.g);
DmxWrite(getChannelAddr(2), st.param.b);
break;
default: ;
}

View File

@@ -5,19 +5,21 @@
#include <abstractout.h>
#include <item.h>
#include "colorchannel.h"
class out_dmx : public abstractOut {
class out_dmx : public colorChannel {
public:
out_dmx(Item * _item):abstractOut(_item){};
out_dmx(Item * _item):colorChannel(_item){};
int Setup() override;
int Poll(short cause) override;
int Stop() override;
int Status() override;
int isActive() override;
int getChanType() override;
int Ctrl(itemCmd cmd, char* subItem=NULL) override;
int PixelCtrl(itemCmd cmd);
// int Ctrl(itemCmd cmd, char* subItem=NULL) override;
// int PixelCtrl(itemCmd cmd) override;
virtual int PixelCtrl(itemCmd cmd, char* subItem=NULL, bool show=true ) override;
protected:
};

View File

@@ -342,7 +342,7 @@ int out_Modbus::Ctrl(itemCmd cmd, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
itemCmd st(ST_UINT32);
itemCmd st(ST_UINT32,CMD_VOID);
int suffixCode = cmd.getSuffix();
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it

View File

@@ -216,7 +216,7 @@ int out_Motor::Ctrl(itemCmd cmd, char* subItem)
int chActive = item->isActive();
bool toExecute = (chActive>0);
int suffixCode = cmd.getSuffix();
itemCmd st(ST_PERCENTS);
itemCmd st(ST_PERCENTS,CMD_VOID);
if (cmd.isCommand() && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
item->setFlag(ACTION_NEEDED);

View File

@@ -9,9 +9,50 @@
static int driverStatus = CST_UNKNOWN;
#if defined(ARDUINO_ARCH_ESP32)
void analogWrite(int pin, int val)
{
//TBD
}
#endif
int out_pwm::Setup()
{
debugSerial<<F("PWM-Out Init")<<endl;
if (!item || iaddr) return 0;
switch (getChanType())
{
case CH_RGBW:
pinMode(getChannelAddr(3), OUTPUT);
case CH_RGB:
pinMode(getChannelAddr(0), OUTPUT);
pinMode(getChannelAddr(1), OUTPUT);
pinMode(getChannelAddr(2), OUTPUT);
break;
default:
pinMode(iaddr, OUTPUT);
}
//timer 0 for pin 13 and 4
//timer 1 for pin 12 and 11
//timer 2 for pin 10 and 9
//timer 3 for pin 5 and 3 and 2
//timer 4 for pin 8 and 7 and 6
//prescaler = 1 ---> PWM frequency is 31000 Hz
//prescaler = 2 ---> PWM frequency is 4000 Hz
//prescaler = 3 ---> PWM frequency is 490 Hz (default value)
//prescaler = 4 ---> PWM frequency is 120 Hz
//prescaler = 5 ---> PWM frequency is 30 Hz
//prescaler = 6 ---> PWM frequency is <20 Hz
#if defined(__AVR_ATmega2560__)
int tval = 7; // this is 111 in binary and is used as an eraser
TCCR4B &= ~tval; // this operation (AND plus NOT), set the three bits in TCCR2B to 0
TCCR3B &= ~tval;
tval = 2;
TCCR4B |= tval;
TCCR3B |= tval;
#endif
driverStatus = CST_INITIALIZED;
return 1;
}
@@ -19,6 +60,19 @@ return 1;
int out_pwm::Stop()
{
debugSerial<<F("PWM-Out stop")<<endl;
switch (getChanType())
{
case CH_RGBW:
pinMode(getChannelAddr(3), INPUT);
case CH_RGB:
pinMode(getChannelAddr(0), INPUT);
pinMode(getChannelAddr(1), INPUT);
pinMode(getChannelAddr(2), INPUT);
break;
default:
pinMode(iaddr, INPUT);
}
driverStatus = CST_UNKNOWN;
return 1;
}
@@ -43,23 +97,49 @@ return 0;
int out_pwm::getChanType()
{
if (item) return item->itemType;
if (item)
{
switch (numArgs)
{
case 3:
return CH_RGB;
case 4:
return CH_RGBW;
default:
return item->itemType;
}
}
return 0;
}
int out_pwm::PixelCtrl(itemCmd cmd)
int out_pwm::PixelCtrl(itemCmd cmd, char* subItem, bool show)
{
if (!item) return 0;
int iaddr = item->getArg(0);
itemCmd st(ST_RGB);
if (!item || !iaddr || !show) return 0;
bool inverse = (item->getArg()<0);
short cType = getChanType();
if (cType=CH_PWM)
{ short k;
analogWrite(iaddr, k=cmd.getPercents255(inverse));
debugSerial<<F("Pin:")<<iaddr<<F("=")<<k<<endl;
return 1;
}
itemCmd st(ST_RGB,CMD_VOID);
st.assignFrom(cmd);
switch (getChanType())
{ case CH_PWM:
// DmxWrite(iaddr + 3, cmd.getPercents255());
break;
switch (cType)
{
case CH_RGBW:
analogWrite(getChannelAddr(3), st.param.w);
case CH_RGB:
analogWrite(iaddr, st.param.r);
analogWrite(getChannelAddr(1), st.param.g);
analogWrite(getChannelAddr(2), st.param.b);
break;
default: ;
}
default: ;
}
return 1;
}

View File

@@ -7,7 +7,7 @@
#include <item.h>
#include "colorchannel.h"
class out_dmx : public colorChannel {
class out_pwm : public colorChannel {
public:
out_pwm(Item * _item):colorChannel(_item){};
@@ -18,9 +18,9 @@ public:
int isActive() override;
int getChanType() override;
//int Ctrl(itemCmd cmd, char* subItem=NULL) override;
//int PixelCtrl(itemCmd cmd) override;
int PixelCtrl(itemCmd cmd, char* subItem=NULL, bool show=true ) override;
protected:
short numChannels;
};
#endif

View File

@@ -109,7 +109,7 @@ int out_SPILed::getChanType()
return CH_RGBW;
}
int PixelCtrl(itemCmd cmd, char* subItem=NULL, bool show );
int out_SPILed::PixelCtrl(itemCmd cmd, char* subItem, bool show )
//int out_SPILed::PixelCtrl(itemCmd cmd, int from, int to, bool show)
{
@@ -123,7 +123,7 @@ int from=0, to=numLeds-1; //All LEDs on the strip by default
debugSerial<<from<<F("-")<<to<<F(" cmd=")<<cmd.getCmd()<<endl;
itemCmd st(ST_RGB);
itemCmd st(ST_RGB,CMD_VOID);
#ifdef ADAFRUIT_LED
uint32_t pixel;

View File

@@ -12,7 +12,7 @@
#include "FastLED.h"
#endif
class out_SPILed : public abstractOut {
class out_SPILed : public colorChannel {
public:
out_SPILed(Item * _item):colorChannel(_item){getConfig();};