Skip to content

Commit

Permalink
copy over updates on shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
ModischFabrications committed Oct 21, 2023
1 parent 40ba723 commit bd4ecfa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/shared/persistence/persistenceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void callListeners();

const uint8_t N_MAX_LISTENERS = 7;

const uint16_t delayToSaveMs = (60 * 1000);
const uint32_t delayToSaveMs = 60L * 1000;

// we are unable to determine if a variable was initialized and
// we don't want to define a "null" Configuration as default
Expand Down Expand Up @@ -61,7 +61,7 @@ void callListeners() {
fListener listener = listeners[i];

// check for value
if (listener == nullptr) { println(F("Listener not initialised")); }
if (listener == nullptr) { logError(F("Invalid listener")); }
// unpack function pointer from list and call
(*listener)();
}
Expand Down
9 changes: 7 additions & 2 deletions src/shared/persistence/persistenceStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,27 @@ namespace {
const uint16_t EEPROM_VERSION_ADDR = VERSION;
const uint16_t EEPROM_SETTINGS_ADDR = EEPROM_VERSION_ADDR + sizeof(VERSION);

#if defined(ESP32) || defined(ESP8266)
// ESP needs to know how much storage we actually need, AVR won't
const uint16_t MAX_BYTES_USED = 127;
#endif
} // namespace

// call once at startup to reserve (emulated) storage for usage
void setup() {
// ESP needs to know how much storage we actually need, AVR won't
#if defined(ESP32) || defined(ESP8266)
EEPROM.begin(MAX_BYTES_USED);
#endif
}

// save settings to EEPROM for persistent storage
void saveSettings(const Configuration settings) {
EEPROM.write(EEPROM_VERSION_ADDR, VERSION);
EEPROM.put(EEPROM_SETTINGS_ADDR, settings);

#if defined(ESP32) || defined(ESP8266)
// commit planned changes
EEPROM.commit();
#endif
}

/**
Expand Down
39 changes: 22 additions & 17 deletions src/shared/serialWrapper.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once

#include <Arduino.h>
//#include <SafeString.h>

#ifdef DEBUG
#pragma message("-- DEBUG Build, enabling serial output --")
const bool USE_SERIAL = true;
#else
#pragma message("-- RELEASE Build, disabling serial output --")
const bool USE_SERIAL = false;
#endif

namespace {
const uint16_t T_HEARTBEAT = 10L * 1000;

#ifdef SERIAL_LOGGING
#pragma message("-- SERIAL_LOGGING set, enabling warning & errors logs --")
const uint8_t N_MAX_LOGS = 20;
const uint16_t T_HEARTBEAT = 10 * 1000;

struct RingBuffer {
const __FlashStringHelper* log[N_MAX_LOGS] = {nullptr};
Expand All @@ -21,19 +24,17 @@ struct RingBuffer {

RingBuffer errors;
RingBuffer warnings;
#else
#pragma message("-- SERIAL_LOGGING not set, disabling warning & errors logs --")
#endif
} // namespace

// usually 115200 or 9600
void setupSerial(int baud) {
void setupSerial(uint32_t baud) {
if (!USE_SERIAL) return;

Serial.begin(baud);
Serial.println(); // init monitor

#ifdef SafeString_class_h
#pragma message "Rerouting SafeString Output to Serial"
SafeString::setOutput(Serial);
#endif
}

void heartbeatSerial() {
Expand All @@ -45,9 +46,9 @@ void heartbeatSerial() {
uint32_t time = millis();

if (time - lastMsg > T_HEARTBEAT) {
Serial.print(F("cycle time: ["));
Serial.print(F("avg cycle time: ~"));
Serial.print(time - lastCycle);
Serial.println(F("]"));
Serial.println(F("ms"));
lastMsg = time;
}

Expand Down Expand Up @@ -130,32 +131,36 @@ void printArray(const uint8_t* rgb_array, uint8_t length) {
Serial.println("]");
}

// ------ errors
// ------ warnings & errors

void logWarning(const __FlashStringHelper* string) {
print(F("WARN: "));
println(string);
#ifdef SERIAL_LOGGING
if (warnings.iLog >= N_MAX_LOGS) {
println(F("Warning list is full, wraparound"));
warnings.iLog -= N_MAX_LOGS;
}

warnings.log[warnings.iLog++] = string;

print(F("WARN: "));
println(string);
#endif
}

void logError(const __FlashStringHelper* string) {
print(F("ERROR: "));
println(string);
#ifdef SERIAL_LOGGING
if (errors.iLog >= N_MAX_LOGS) {
println(F("Error list is full, wraparound"));
errors.iLog -= N_MAX_LOGS;
}

errors.log[errors.iLog++] = string;

print(F("ERROR: "));
println(string);
#endif
}

#ifdef SERIAL_LOGGING
const RingBuffer& getWarnLog() { return warnings; }

const RingBuffer& getErrorLog() { return errors; }
#endif

0 comments on commit bd4ecfa

Please sign in to comment.