ESP persist fix, closing files, RGBWW

This commit is contained in:
2021-11-01 01:52:40 +03:00
parent 8dd0b57f72
commit 3b92723ee3
11 changed files with 90 additions and 52 deletions

View File

@@ -25,6 +25,7 @@ int colorChannel::getDefaultStorageType()
{
case 3:
case 4:
case 5:
return ST_HSV255;
case 1:
return ST_PERCENTS255;

View File

@@ -1,4 +1,5 @@
#include "config.h"
#include "main.h"
String systemConfig::getMACString()
{
@@ -28,6 +29,7 @@ bool systemConfig::isValidSysConf()
stream->close();
return false;
}
stream->close();
return true;
};
@@ -289,4 +291,13 @@ bool systemConfig::getLoadHTTPConfig()
return false;
}
String systemConfig::getETAG()
{
return "123";
}
bool systemConfig::setETAG(String etag)
{
debugSerial<<F("ETAG:")<<etag<<endl;
return 1;
}

View File

@@ -54,5 +54,8 @@ class systemConfig {
bool setSaveSuccedConfig(bool);
bool getLoadHTTPConfig();
bool setLoadHTTPConfig(bool);
String getETAG();
bool setETAG(String etag);
//bool Save();
};

View File

@@ -91,7 +91,8 @@ NRFFlashStorage EEPROM;
int flashStream::available()
{
if (!fs) return 0;
if (textMode && peek()==EOFchar) return 0;
//if (textMode && (peek()==EOFchar)) {debugSerial<<"X";return 0;} //Not working for esp8266
if (fs.position()>=streamSize) return 0;
return fs.available();
};
@@ -111,8 +112,8 @@ NRFFlashStorage EEPROM;
debugSerial<<(F(" Res:"))<<res<<endl;
return res;
};
void flashStream::close() {fs.close(); };
void flashStream::flush() {fs.flush(); };
void flashStream::close() {fs.close(); debugSerial<<filename<<" Closed\n";};
void flashStream::flush() {fs.flush(); debugSerial<<filename<<" Flushed\n";};
size_t flashStream::write(uint8_t ch)
{
return fs.write(ch);
@@ -162,38 +163,33 @@ NRFFlashStorage EEPROM;
else if (_filename == "/config.bin") return open (FN_CONFIG_BIN,mode);
else return 0;
};
/*
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
{
pos = 0;
startPos = _startPos;
streamSize = _size;
return 1;
};
*/
unsigned int flashStream::seek(unsigned int _pos)
{ pos=min(_pos, streamSize);
debugSerial<<F("Seek:")<<pos<<endl;
return pos;
};
int flashStream::available() {
//debugSerial<<pos<<"%"<<streamSize<<";";
int flashStream::available()
{
if (textMode && peek()==EOFchar) return 0;
return (pos<streamSize);
};
int flashStream::read()
{
int ch = peek();
pos++;
//debugSerial<<"<"<<(char)ch;
return ch;
};
int flashStream::peek()
{
if (pos<streamSize)
return EEPROM.read(startPos+pos);
else return -1;
};
void flashStream::flush() {
#if defined(ESP8266) || defined(ESP32)
if (EEPROM.commitReset())
@@ -204,7 +200,6 @@ NRFFlashStorage EEPROM;
size_t flashStream::write(uint8_t ch)
{
//debugSerial<<">"<<(char)ch;
#if defined(__AVR__)
EEPROM.update(startPos+pos++,(char)ch);
return 1;
@@ -236,7 +231,7 @@ NRFFlashStorage EEPROM;
void flashStream::close()
{
flush();
putEOF();
}

View File

@@ -35,13 +35,14 @@ public:
};
virtual int open(String _filename, char mode) override;
virtual int available() override;
virtual int read();
virtual int peek();
virtual int read() override;
virtual int peek() override;
virtual unsigned int seek (unsigned int _pos = 0) override;
virtual void close() override;
virtual void flush() override;
virtual size_t write(uint8_t ch);
using Print::write;
virtual void putEOF() override {};
virtual ~flashStream ();
};
@@ -58,15 +59,6 @@ public:
void setSize(unsigned int _size);
int open(short fileNum, char mode='\0') ;
virtual int open(String _filename, char mode='\0') override;
/*
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
{
pos = 0;
startPos = _startPos;
streamSize = _size;
return 1;
};
*/
virtual unsigned int seek(unsigned int _pos = 0);
virtual int available() override;
virtual int read() ;

View File

@@ -143,6 +143,7 @@ void Item::Parse() {
case CH_RGBW:
case CH_RGB:
case CH_DIMMER:
case CH_RGBWW:
driver = new out_dmx (this);
// debugSerial<<F("DMX driver created")<<endl;
break;

View File

@@ -56,6 +56,7 @@ e-mail anklimov@gmail.com
#define CH_MBUS 14
#define CH_UARTBRIDGE 15
#define CH_ELEVATOR 16
#define CH_RGBWW 17
#define CH_MULTIVENT 18
//#define CHANNEL_TYPES 13

View File

@@ -179,6 +179,11 @@ bool itemCmd::setS(uint8_t s)
//! Internally 1 - cold, 101 - warm light
bool itemCmd::setColorTemp(int t)
{
if (!t)
{
param.colorTemp=0;
return true;
}
int par=map(t,153,500,0,100);
switch (cmd.itemArgType)
{
@@ -199,7 +204,7 @@ bool itemCmd::setColorTemp(int t)
return true;
}
//! Setup color tempetature parameter from HSV or HSV255 types. return 0..100 value in success.
//! Return color tempetature parameter from HSV or HSV255 types. return 153..500 value in success.
//! -1 - if no value stored
int itemCmd::getColorTemp()
{

View File

@@ -1305,7 +1305,6 @@ int loadConfigFromEEPROM()
sysConfStream.open(FN_CONFIG_JSON,'r');
#endif
//JSONStream.seek();
if (sysConfStream.peek() == '{') {
aJsonStream as = aJsonStream(&sysConfStream);
cleanConf();
@@ -1364,8 +1363,8 @@ if (arg_cnt>1)
aJsonStream jsonEEPROMStream = aJsonStream(&sysConfStream);
infoSerial<<F("Saving config to EEPROM..");
aJson.print(root, &jsonEEPROMStream);
sysConfStream.putEOF();
sysConfStream.flush();
//sysConfStream.putEOF();
//sysConfStream.flush();
sysConfStream.close();
infoSerial<<F("Saved to EEPROM")<<endl;
#endif
@@ -1488,7 +1487,7 @@ int cmdFunctionSetMac(int arg_cnt, char **args) {
}
int cmdFunctionGet(int arg_cnt, char **args) {
int result;
if (arg_cnt>1)
{
if (!strcasecmp_P(args[1],ON_P)) {sysConf.setLoadHTTPConfig(true); return 200;};
@@ -1518,6 +1517,7 @@ bool loadConfigFromHttp(int arg_cnt, char **args)
int responseStatusCode = 0;
char URI[64];
char configServer[32]="";
String etag=sysConf.getETAG();
if (arg_cnt > 1) {
strncpy(configServer, args[1], sizeof(configServer) - 1);
sysConf.setServer(configServer);
@@ -1605,6 +1605,7 @@ bool loadConfigFromHttp(int arg_cnt, char **args)
//debugSerial<<"making GET request");get
debugSerial<<F("Before request: Free:")<<freeRam()<<endl;
htclient.beginRequest();
htclient.sendHeader("If-None-Match:",etag);
responseStatusCode = htclient.get(URI);
htclient.endRequest();
@@ -1612,12 +1613,15 @@ bool loadConfigFromHttp(int arg_cnt, char **args)
{
// read the status code and body of the response
responseStatusCode = htclient.responseStatusCode();
while (htclient.headerAvailable())
if (htclient.readHeaderName() == "ETAG") sysConf.setETAG(htclient.readHeaderValue());
response = htclient.responseBody();
htclient.stop();
wdt_res();
infoSerial<<F("HTTP Status code: ")<<responseStatusCode<<endl;
//delay(1000);
if (responseStatusCode == 200) {
debugSerial<<F("Free:")<<freeRam()<<endl;
debugSerial<<F("Response Len:")<<response.length()<<endl;
@@ -1642,6 +1646,11 @@ bool loadConfigFromHttp(int arg_cnt, char **args)
errorSerial<<F("Config retrieving failed\n");
return false;
}
}
else if (responseStatusCode == 304)
{
errorSerial<<F("Config not changed\n");
return false;
}
else
{

View File

@@ -47,6 +47,8 @@ int out_dmx::getChanType()
return CH_RGB;
case 4:
return CH_RGBW;
case 5:
return CH_RGBWW;
default:
return item->itemType;
}
@@ -81,11 +83,15 @@ if (cType==CH_DIMMER) //Single channel
return 1;
}
int colorTemp = cmd.getColorTemp();
switch (cType)
{
case CH_RGB:
storageType=ST_RGB;
break;
case CH_RGBWW:
cmd.setColorTemp(0); //Supress cold conversoin
case CH_RGBW:
storageType=ST_RGBW;
break;
@@ -99,6 +105,16 @@ st.assignFrom(cmd);
debugSerial<<F("Assigned:");st.debugOut();
switch (cType)
{
case CH_RGBWW:
if (colorTemp == -1) colorTemp = 255/2;
else colorTemp=map(colorTemp,153,500,0,255);
DmxWrite(getChannelAddr(3), map(st.param.w,0,255,0,colorTemp));
DmxWrite(getChannelAddr(4), map(st.param.w,0,255,0,255-colorTemp));
DmxWrite(iaddr, st.param.r);
DmxWrite(getChannelAddr(1), st.param.g);
DmxWrite(getChannelAddr(2), st.param.b);
break;
case CH_RGBW:
DmxWrite(getChannelAddr(3), st.param.w);
case CH_RGB:

View File

@@ -199,11 +199,15 @@
//All options available
#ifdef CONTROLLINO
#define modbusSerial Serial3
#ifndef AC_Serial
#define AC_Serial Serial2
#endif
#else
#define modbusSerial Serial2
#ifndef AC_Serial
#define AC_Serial Serial3
#endif
#endif
#define dmxin DMXSerial
#define dmxout DmxSimple
#endif