Interim SPI-LED update: general LED logic appplied, LED-ranges, dynamic LED array alloc

This commit is contained in:
2019-10-21 02:40:27 +03:00
parent 9cd0d924bb
commit 78e1562b48
4 changed files with 201 additions and 46 deletions

View File

@@ -17,6 +17,7 @@ GIT: https://github.com/anklimov/lighthub
e-mail anklimov@gmail.com
*/
#pragma once
#include "options.h"
#include "abstractout.h"
@@ -73,7 +74,7 @@ e-mail anklimov@gmail.com
#define CMD_FAN 0xd
#define CMD_DRY 0xe
#define CMD_SET 0xf
#define CMD_HIGH 0x10
#define CMD_HIGH 0x10 //AC fan leve
#define CMD_MED 0x11
#define CMD_LOW 0x12
//#define CMD_CURTEMP 0xf
@@ -176,7 +177,7 @@ class Item
inline int Toggle(){return Ctrl(CMD_TOGGLE);};
int Poll();
int SendStatus(int sendFlags);
int isActive();
protected:
short cmd2changeActivity(int lastActivity, short defaultCmd = CMD_SET);
int VacomSetFan (int8_t val, int8_t cmd=0);
@@ -184,7 +185,6 @@ class Item
int modbusDimmerSet(int addr, uint16_t _reg, int _regType, int _mask, uint16_t value);
int modbusDimmerSet(uint16_t value);
void mb_fail();
int isActive();
void Parse();
int checkModbusDimmer();
int checkModbusDimmer(int data);

View File

@@ -1566,6 +1566,7 @@ for (short i = 0; i < 6; i++) {
mac[i] = EEPROM.read(i);
if (mac[i] != 0 && mac[i] != 0xff) isMacValid = true;
}
if (!isMacValid) {
debugSerial<<F("No MAC configured: set firmware's MAC\n");

View File

@@ -7,15 +7,19 @@
#include "FastLED.h"
#include "item.h"
#define NUM_LEDS 15
#define NUM_LEDS 43
#define DATA_PIN 4
static CRGB leds[NUM_LEDS];
//static CRGB leds[NUM_LEDS];
static CRGB *leds = NULL;
static int driverStatus = CST_UNKNOWN;
int out_SPILed::Setup()
{
Serial.println("SPI-LED Init");
leds = new CRGB [NUM_LEDS]; //Allocate dynamic memory for LED objects
FastLED.addLeds<TM1809, DATA_PIN, BRG>(leds, NUM_LEDS);
driverStatus = CST_INITIALIZED;
return 1;
@@ -27,6 +31,7 @@ Serial.println("SPI-LED De-Init");
//FastLED.addLeds<TM1809, DATA_PIN, BRG>(leds, NUM_LEDS);
FastLED.clear(true);
driverStatus = CST_UNKNOWN;
delete [] leds;
return 1;
}
@@ -37,61 +42,206 @@ return driverStatus;
int out_SPILed::isActive()
{
CHstore st;
st.aslong = item->getVal(); //Restore old params
debugSerial<< F(" val:")<<st.v<<endl;
return st.v;
}
int out_SPILed::Poll()
{
//FastLED.show();
return 1;
};
int out_SPILed::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixCode, char* subItem)
int out_SPILed::PixelCtrl(CHstore *st, short cmd, int from, int to, bool show, bool rgb)
{
int from=0, to=NUM_LEDS-1;
if (subItem)
{ //Just single LED to control
from=atoi(subItem);
to=from;
//debugSerial<<F("cmd: ")<<cmd<<endl;
CRGB pixel;
if (!rgb)
{
int Saturation = map(st->s, 0, 100, 0, 255);
int Value = map(st->v, 0, 100, 0, 255);
int Hue = map(st->h, 0, 365, 0, 255);
pixel = CHSV(Hue, Saturation, Value);
debugSerial<<F("hsv: ")<<st->h<<F(",")<<st->s<<F(",")<<st->v<<endl;
}
debugSerial<<from<<F("-")<<to<<F(" cmd=")<<cmd<<endl;
if (to>NUM_LEDS-1) to=NUM_LEDS-1;
if (from<0) from=0;
for (int i=from;i<=to;i++)
{
// debugSerial<<F(".");
switch(cmd)
{
switch (cmd) {
case CMD_ON:
debugSerial<<F("Ch: ")<<i<<F(" White")<<endl;
leds[i] = CRGB::White;
if (!leds[i].r && !leds[i].g &&!leds[i].b) leds[i] = CRGB::White;
// FastLED.show();
break;
case CMD_OFF:
debugSerial<<F("Ch: ")<<i<<F(" Black")<<endl;
//pixel = leds[i];
leds[i] = CRGB::Black;
//FastLED.show();
//leds[i] = pixel;
break;
case CMD_NUM:
switch (suffixCode)
default:
if (rgb)
{
leds[i] = CRGB(st->r,st->g,st->b);
}
else
{
leds[i] = pixel;
}
// case S_POWER:
// case S_VOL:
//leds[n].setBrightness(Parameters[0]);
// break;
case S_SET:
case S_HSV:
debugSerial<<F("HSV: ")<<i<<F(" :")<<Parameters[0]<<Parameters[1]<<Parameters[2]<<endl;
leds[i] = CHSV(Parameters[0],Parameters[1],Parameters[2]);
break;
case S_RGB:
debugSerial<<F("RGB: ")<<i<<F(" :")<<Parameters[0]<<Parameters[1]<<Parameters[2]<<endl;
leds[i] = CRGB(Parameters[0],Parameters[1],Parameters[2]);
break;
}
}
}
} //for
if (show)
{
FastLED.show();
debugSerial<<F("Show")<<endl;
}
return 1;
}
int out_SPILed::Ctrl(short cmd, short n, int * Parameters, boolean send, int suffixCode, char* subItem)
{
int chActive = item->isActive();
bool toExecute = (chActive>0);
CHstore st;
if (cmd>0 && !suffixCode) suffixCode=S_CMD; //if some known command find, but w/o correct suffix - got it
int from=0, to=NUM_LEDS-1; //All LEDs on the strip by default
// retrive LEDs range from suffix
if (subItem)
{ //Just single LED to control todo - range
// debugSerial<<F("Range:")<<subItem<<endl;
if (sscanf(subItem,"%d-%d",&from,&to) == 1) to=from;
}
debugSerial<<from<<F("-")<<to<<F(" cmd=")<<cmd<<endl;
switch(suffixCode)
{
case S_NOTFOUND:
// turn on and set
toExecute = true;
case S_SET:
case S_HSV:
st.aslong = item->getVal(); //Restore old params
switch (n) //How many parameters are passed?
{
case 1:
st.v = Parameters[0]; //Volume only
if (!st.hsv_flag)
{
st.h = 0; //Filling by default
st.s = 0;
st.hsv_flag = 1; // Mark stored vals as HSV
}
break;
case 2: // Just hue and saturation
st.h = Parameters[0];
st.s = Parameters[1];
st.hsv_flag = 1;
break;
case 3: //complete triplet
st.h = Parameters[0];
st.s = Parameters[1];
st.v = Parameters[2];
st.hsv_flag = 1;
}
PixelCtrl(&st,0,from,to,toExecute);
if (!subItem) //Whole strip
{
item->setVal(st.aslong); //Store
if (toExecute)
{
if (chActive>0 && !st.v) item->setCmd(CMD_OFF);
if (chActive==0 && st.v) item->setCmd(CMD_ON);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED);
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
}
return 1;
//break;
case S_RGB:
st.r = Parameters[0];
st.g = Parameters[1];
st.b = Parameters[2];
st.w = 0;
st.hsv_flag = 0;
PixelCtrl(&st,0,from,to,toExecute,true);
//item->setVal(st.aslong); //Store
if (toExecute)
{
if (chActive>0 && !st.aslong) item->setCmd(CMD_OFF);
if (chActive==0 && st.aslong) item->setCmd(CMD_ON);
item->SendStatus(SEND_COMMAND | SEND_PARAMETERS | SEND_DEFFERED);
}
else item->SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
return 1;
//break;
case S_CMD:
switch (cmd)
{
case CMD_ON:
/*
if (chActive>0 && send)
{
SendStatus(SEND_COMMAND);
return 1;
}
*/
//retrive stored values
st.aslong = item->getVal();
if (subItem) // LED range, not whole strip
PixelCtrl(&st,CMD_ON,from,to);
else //whole strip
{
if (st.aslong && (st.v<MIN_VOLUME)) st.v=INIT_VOLUME;
item->setVal(st.aslong);
if (st.aslong ) //Stored smthng
{
if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS);
debugSerial<<F("Restored: ")<<st.h<<F(",")<<st.s<<F(",")<<st.v<<endl;
}
else
{
debugSerial<<st.aslong<<F(": No stored values - default\n");
st.h = 100;
st.s = 0;
st.v = 100;
st.hsv_flag=1;
// Store
item->setVal(st.aslong);
if (send) item->SendStatus(SEND_COMMAND | SEND_PARAMETERS );
}
PixelCtrl(&st,0,from,to);
}
return 1;
case CMD_OFF:
if (subItem) // LED range, not whole strip
PixelCtrl(&st,CMD_OFF,from,to);
else
{
st.v=0;
PixelCtrl(&st,0,from,to);
if (send) item->SendStatus(SEND_COMMAND);
// if (send) item->publishTopic(item->itemArr->name,"OFF","/cmd");
}
return 1;
} //switch cmd
break;
} //switch suffix
debugSerial<<F("Unknown cmd")<<endl;
return 0;
}
#endif

View File

@@ -2,6 +2,7 @@
#pragma once
#ifndef SPILED_DISABLE
#include <abstractout.h>
#include <item.h>
class out_SPILed : public abstractOut {
public:
@@ -13,7 +14,10 @@ public:
int Status() override;
int isActive() override;
int Ctrl(short cmd, short n=0, int * Parameters=NULL, boolean send=true, int suffixCode=0, char* subItem=NULL) override;
int PixelCtrl(CHstore *st, short cmd, int from =0 , int to = 1024, bool show = 1, bool rgb = 0);
int numLeds;
int pin;
protected:
};
#endif