2021-03-29 08:10:55 +00:00
|
|
|
#include "./Config.h"
|
|
|
|
#include "./Static.h"
|
|
|
|
#include <ArduinoLog.h>
|
2021-03-31 18:50:00 +00:00
|
|
|
#include <EEPROM.h>
|
2021-03-29 08:10:55 +00:00
|
|
|
|
|
|
|
constexpr uint16_t HardwareConfig::MAX_LED_NUM;
|
|
|
|
|
|
|
|
HardwareConfig
|
|
|
|
HardwareConfig::load() {
|
|
|
|
HardwareConfig ret;
|
2021-04-10 18:10:25 +00:00
|
|
|
EEPROM.begin(sizeof(ret));
|
2021-03-29 08:10:55 +00:00
|
|
|
EEPROM.get(0, ret);
|
2021-04-10 18:10:25 +00:00
|
|
|
EEPROM.end();
|
|
|
|
Log.notice("Loaded config version %d, CRC %d", ret.version, ret.checksum);
|
2021-03-29 08:10:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HardwareConfig::save() {
|
|
|
|
HardwareConfig dataCopy{*this};
|
|
|
|
dataCopy.checksum = getCRC();
|
2021-04-10 18:10:25 +00:00
|
|
|
EEPROM.begin(sizeof(dataCopy));
|
2021-03-29 08:10:55 +00:00
|
|
|
EEPROM.put(0, dataCopy);
|
2021-04-10 18:10:25 +00:00
|
|
|
EEPROM.commit();
|
|
|
|
EEPROM.end();
|
2021-03-29 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LinearCoordinateMapping
|
|
|
|
HardwareConfig::toCoordMap() const
|
|
|
|
{
|
|
|
|
auto pixelCount = min(HardwareConfig::MAX_LED_NUM, std::max((uint16_t)1, data.pixelCount));
|
|
|
|
auto startPixel = min(pixelCount, std::max((uint16_t)1, data.startPixel));
|
|
|
|
return LinearCoordinateMapping{pixelCount, startPixel};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HardwareConfig::isValid() const
|
|
|
|
{
|
|
|
|
return version == 2 && checksum == getCRC() && data.pixelCount <= MAX_LED_NUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
HardwareConfig::getCRC() const
|
|
|
|
{
|
|
|
|
const unsigned char* message = reinterpret_cast<const unsigned char*>(&data);
|
|
|
|
constexpr uint8_t length = sizeof(data);
|
|
|
|
unsigned char i, j, crc = 0;
|
|
|
|
for(i = 0; i < length; i++) {
|
|
|
|
crc ^= message[i];
|
|
|
|
for(j = 0; j < 8; j++) {
|
|
|
|
if (crc & 1) {
|
|
|
|
crc ^= CRC7_POLY;
|
|
|
|
}
|
|
|
|
crc >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigService::onStart()
|
|
|
|
{
|
|
|
|
Log.notice("Starting configuration service...");
|
|
|
|
m_config = HardwareConfig::load();
|
|
|
|
if (m_config.isValid()) {
|
|
|
|
Log.notice("Configuration found!");
|
|
|
|
} else {
|
|
|
|
Log.notice("No configuration found. Writing defaults...");
|
|
|
|
m_config = HardwareConfig{};
|
|
|
|
m_config.save();
|
|
|
|
}
|
|
|
|
m_coordMap = m_config.toCoordMap();
|
|
|
|
|
|
|
|
Log.notice("Configured to use %d pixels, starting at %d", m_config.data.pixelCount, m_config.data.startPixel);
|
2021-04-10 18:10:25 +00:00
|
|
|
/*Log.notice("Loading task states...");
|
2021-03-29 08:10:55 +00:00
|
|
|
for(int i = 0; i < 32; i++) {
|
|
|
|
auto svc = m_config.data.serviceStates[i];
|
2021-04-10 18:10:25 +00:00
|
|
|
if (strnlen(svc.name, 16) > 0) {
|
2021-03-29 08:10:55 +00:00
|
|
|
Log.notice("* %s: %s", svc.name, svc.isDisabled? "DISABLED" : "ENABLED");
|
|
|
|
}
|
2021-04-10 18:10:25 +00:00
|
|
|
}*/
|
2021-03-29 08:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigService::loop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigService::handleEvent(const InputEvent &evt)
|
|
|
|
{
|
|
|
|
switch(evt.intent) {
|
|
|
|
case InputEvent::SetDisplayLength:
|
|
|
|
//Log.info("Updating pixel count from %d to %d", m_coordMap.pixelCount, evt.asInt());
|
|
|
|
m_config.data.pixelCount = evt.asInt();
|
|
|
|
m_coordMap = m_config.toCoordMap();
|
|
|
|
//Log.info("Count is now %d", m_coordMap.pixelCount);
|
|
|
|
break;
|
|
|
|
case InputEvent::SetDisplayOffset:
|
|
|
|
//Log.info("Updating pixel offset from %d to %d", m_coordMap.startPixel, evt.asInt());
|
|
|
|
m_config.data.startPixel = evt.asInt();
|
|
|
|
m_coordMap = m_config.toCoordMap();
|
|
|
|
//Log.info("Offset is now %d", m_coordMap.startPixel);
|
|
|
|
break;
|
|
|
|
case InputEvent::SaveConfigurationRequest:
|
|
|
|
//Log.info("Saving configuration");
|
|
|
|
m_config.save();
|
|
|
|
break;
|
2021-03-31 18:50:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
2021-03-29 08:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC_ALLOC(ConfigService);
|