Skip to content

Commit

Permalink
Fix Lightbar init and writing of config params
Browse files Browse the repository at this point in the history
  • Loading branch information
thankthemaker committed Nov 5, 2023
1 parent 03e1ab8 commit d523770
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/AppConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ boolean AppConfiguration::readPreferences() {
config.idleLightTimeout = doc["idleLightTimeout"] | 60000;
config.mallGrab = doc["mallGrab"].as<boolean>()| false;
config.mtuSize = doc["mtuSize"] | 512;
config.oddevenActive = doc["oddevenActive"] | true;
config.oddevenActive = doc["oddevenActive"] | false;
config.lightsSwitch = true;
config.saveConfig = false;
config.sendConfig = false;
config.sendConfigFinished = true;

if(doc.overflowed()) {
return false;
Expand Down Expand Up @@ -113,6 +114,8 @@ boolean AppConfiguration::savePreferences() {
doc["idleLightTimeout"] = config.idleLightTimeout;
doc["mallGrab"] = config.mallGrab;
doc["mtuSize"] = config.mtuSize;
doc["oddevenActive"] = config.oddevenActive;

String json = "";
serializeJson(doc, json);
ESP_LOGI(TAG, "savePreferences: %s", json.c_str());
Expand Down
26 changes: 13 additions & 13 deletions src/AppConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
struct Config {
BEGIN_VISITABLES(Config);
VISITABLE(String, deviceName);
VISITABLE(boolean, otaUpdateActive);
VISITABLE(boolean, isNotificationEnabled);
VISITABLE(boolean, isBatteryNotificationEnabled);
VISITABLE(boolean, isCurrentNotificationEnabled);
VISITABLE(boolean, isErpmNotificationEnabled);
VISITABLE(bool, otaUpdateActive);
VISITABLE(bool, isNotificationEnabled);
VISITABLE(bool, isBatteryNotificationEnabled);
VISITABLE(bool, isCurrentNotificationEnabled);
VISITABLE(bool, isErpmNotificationEnabled);
VISITABLE(double, minBatteryVoltage);
VISITABLE(double, lowBatteryVoltage);
VISITABLE(double, maxBatteryVoltage);
Expand All @@ -40,24 +40,24 @@ struct Config {
VISITABLE(int, lightColorSecondaryBlue);
VISITABLE(int, lightbarTurnOffErpm);
VISITABLE(int, lightbarMaxBrightness);
VISITABLE(boolean, brakeLightEnabled);
VISITABLE(bool, brakeLightEnabled);
VISITABLE(int, numberPixelLight);
VISITABLE(int, numberPixelBatMon);
VISITABLE(int, vescId);
VISITABLE(boolean, saveConfig);
VISITABLE(boolean, sendConfig);
VISITABLE(bool, saveConfig);
VISITABLE(bool, sendConfig);
VISITABLE(String , ledType);
VISITABLE(String , lightBarLedType);
VISITABLE(String , ledFrequency);
VISITABLE(String , lightBarLedFrequency);
VISITABLE(boolean , isLightBarReversed);
VISITABLE(boolean , isLightBarLedTypeDifferent);
VISITABLE(bool , isLightBarReversed);
VISITABLE(bool , isLightBarLedTypeDifferent);
VISITABLE(int , idleLightTimeout);
VISITABLE(bool , mallGrab);
VISITABLE(int , mtuSize);
VISITABLE(boolean , oddevenActive);
VISITABLE(boolean, lightsSwitch);
VISITABLE(boolean, sendConfigFinished);
VISITABLE(bool , oddevenActive);
VISITABLE(bool, lightsSwitch);
VISITABLE(bool, sendConfigFinished);
END_VISITABLES;
};

Expand Down
8 changes: 4 additions & 4 deletions src/BleServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {

//Serial.println(String(key.c_str()) + String("=") + String(value.c_str()));

snprintf(buf, bufSize, "Updated param \"%s\" to %s", key.c_str(), value.c_str());
if (key == "config") {
AppConfiguration::getInstance()->config.sendConfig = true;
} else if (key == "save") {
Expand All @@ -273,7 +274,6 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
AppConfiguration::getInstance()->config.startSoundIndex = parseInt(value);
Buzzer::stopSound();
Buzzer::playSound(RTTTL_MELODIES(strtol(value.c_str(), nullptr, 10)));
snprintf(buf, bufSize, "Updated param \"StartSoundIndex\" to %s", value.c_str());
} else if (key == "startLightIndex") {
AppConfiguration::getInstance()->config.startLightIndex = parseInt(value);
} else if (key == "batteryWarningSoundIndex") {
Expand Down Expand Up @@ -327,11 +327,11 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
} else if (key == "lightBarLedFrequency") {
AppConfiguration::getInstance()->config.lightBarLedFrequency = value.c_str();
} else if (key == "isLightBarReversed") {
AppConfiguration::getInstance()->config.isLightBarReversed = value.c_str();
AppConfiguration::getInstance()->config.isLightBarReversed = parseInt(value);
} else if (key == "isLightBarLedTypeDifferent") {
AppConfiguration::getInstance()->config.isLightBarLedTypeDifferent = value.c_str();
AppConfiguration::getInstance()->config.isLightBarLedTypeDifferent = parseInt(value);
} else if (key == "mallGrab") {
AppConfiguration::getInstance()->config.mallGrab = value.c_str();
AppConfiguration::getInstance()->config.mallGrab = parseInt(value);
} else if (key == "mtuSize") {
uint16_t mtu = strtol(value.c_str(), nullptr, 10);
AppConfiguration::getInstance()->config.mtuSize = mtu;
Expand Down
26 changes: 12 additions & 14 deletions src/LightBarController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ boolean pixelCountOdd = false;
AdcState lastAdcState = AdcState::ADC_NONE;
unsigned long lastAdcStateChange = 0;

LightBarController::LightBarController() {
LightBarController::LightBarController() {}

void LightBarController::init() {
pixel_count = AppConfiguration::getInstance()->config.numberPixelBatMon;
min_voltage = (int) AppConfiguration::getInstance()->config.minBatteryVoltage * 100;
max_voltage = (int) AppConfiguration::getInstance()->config.maxBatteryVoltage * 100;
pixelCountOdd = pixel_count % 2 == 1;
uint8_t ledType;
if(AppConfiguration::getInstance()->config.isLightBarLedTypeDifferent)
{
if(AppConfiguration::getInstance()->config.isLightBarLedTypeDifferent) {
ledType = LedControllerFactory::getInstance()->determineLedType(true);
}
else
{
} else {
ledType = LedControllerFactory::getInstance()->determineLedType();
}
voltage_range = max_voltage - min_voltage;
Expand All @@ -32,15 +31,15 @@ LightBarController::LightBarController() {
lightPixels.begin(); // This initializes the NeoPixel library.
for (int j = 0; j < pixel_count; j++) {
int actualIndex = j;
if(AppConfiguration::getInstance()->config.isLightBarReversed)
{
if(AppConfiguration::getInstance()->config.isLightBarReversed) {
actualIndex = pixel_count - 1 - j;
}
lightPixels.setPixelColor(actualIndex, 51, 153, 255);
}
lightPixels.show();
}


// updates the light bar, depending on the LED count
void LightBarController::updateLightBar(double voltage, uint16_t switchstate, double adc1, double adc2, double erpm) {
AdcState adcState = this->mapSwitchState(switchstate, adc1 > adc2);
Expand Down Expand Up @@ -68,9 +67,9 @@ void LightBarController::updateLightBar(double voltage, uint16_t switchstate, do
if (adcState != lastAdcState) {
for (int i = 0; i < pixel_count; i++) {
int actualIndex = i;
#ifdef REVERSE_LED_STRIP
if(AppConfiguration::getInstance()->config.isLightBarReversed) {
actualIndex = pixel_count - 1 - i;
#endif
}
lightPixels.setPixelColor(actualIndex, 0, 0, 0);
switch (adcState) {
case ADC_NONE:
Expand All @@ -96,15 +95,14 @@ void LightBarController::updateLightBar(double voltage, uint16_t switchstate, do
// update every pixel individually
for (int i = 0; i < pixel_count; i++) {
int actualIndex = i;
#ifdef REVERSE_LED_STRIP
if(AppConfiguration::getInstance()->config.isLightBarReversed) {
actualIndex = pixel_count - 1 - i;
#endif
}
if (i == whole) {
// the last pixel, the battery voltage somewhere in the range of this pixel
// the lower the remaining value the more the pixel goes from green to red
int val = calcVal(remainder);
lightPixels.setPixelColor(i, AppConfiguration::getInstance()->config.lightbarMaxBrightness - val, val,
0);
lightPixels.setPixelColor(i, AppConfiguration::getInstance()->config.lightbarMaxBrightness - val, val, 0);
}
if (i > whole) {
// these pixels must be turned off, we already reached a lower battery voltage
Expand Down
1 change: 1 addition & 0 deletions src/LightBarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum AdcState {
class LightBarController {
public:
LightBarController();
void init();
void updateLightBar(double voltage, uint16_t switchstate, double adc1, double adc2, double erpm);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define SOFTWARE_VERSION_MAJOR 2
#define SOFTWARE_VERSION_MINOR 5
#define SOFTWARE_VERSION_PATCH 1
#define SOFTWARE_VERSION_PATCH 3

#ifndef ESP32S3
#define HARDWARE_VERSION_MAJOR 3
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void setup() {
#endif
// initialize the LED (either COB or Neopixel)
ledController->init();
lightbar->init();

Buzzer::startSequence();
ledController->startSequence();
Expand Down

0 comments on commit d523770

Please sign in to comment.