renderbug-cpp/src/Config.cpp

117 lines
3.0 KiB
C++
Raw Normal View History

2021-03-29 08:10:55 +00:00
#include "./Config.h"
#include "./Static.h"
#include <ArduinoLog.h>
#include <EEPROM.h>
2021-03-29 08:10:55 +00:00
constexpr uint16_t HardwareConfig::MAX_LED_NUM;
HardwareConfig
HardwareConfig::load() {
HardwareConfig ret;
#ifndef BOARD_TEENSY
2021-04-10 18:10:25 +00:00
EEPROM.begin(sizeof(ret));
#endif
2021-03-29 08:10:55 +00:00
EEPROM.get(0, ret);
#ifndef BOARD_TEENSY
2021-04-10 18:10:25 +00:00
EEPROM.end();
#endif
2021-04-10 18:10:25 +00:00
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();
#ifndef BOARD_TEENSY
2021-04-10 18:10:25 +00:00
EEPROM.begin(sizeof(dataCopy));
#endif
2021-03-29 08:10:55 +00:00
EEPROM.put(0, dataCopy);
#ifndef BOARD_TEENSY
2021-04-10 18:10:25 +00:00
EEPROM.commit();
EEPROM.end();
#endif
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);
}
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;
default:
break;
2021-03-29 08:10:55 +00:00
}
}
STATIC_ALLOC(ConfigService);
STATIC_TASK(ConfigService);