From 73ba3b275091262690c4426278f28d4adf586415 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Sun, 26 Jan 2020 16:20:37 +0100 Subject: [PATCH] Added initialization sequence Added EEPROM persistence --- src/include/colors.h | 23 +++++++++++ src/lib/persist.cpp | 39 ++++++++++++++++++ src/lib/persist.h | 50 +++++++++++++++++++++++ src/main.cpp | 94 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 197 insertions(+), 9 deletions(-) create mode 100644 src/include/colors.h create mode 100644 src/lib/persist.cpp create mode 100644 src/lib/persist.h diff --git a/src/include/colors.h b/src/include/colors.h new file mode 100644 index 0000000..c920710 --- /dev/null +++ b/src/include/colors.h @@ -0,0 +1,23 @@ +#ifndef __colors +#define __colors + +#define BLACK 0x0000 +#define BLUE 0x001F +#define RED 0xF800 +#define GREEN 0x07E0 +#define CYAN 0x07FF +#define MAGENTA 0xF81F +#define YELLOW 0xFFE0 +#define WHITE 0xFFFF + + +#define COLOR_INITSEQ_BACKGROUND BLACK +#define COLOR_INITSEQ_TITLE YELLOW +#define COLOR_INITSEQ_ITEMS WHITE +#define COLOR_INITSEQ_SUCCESS GREEN +#define COLOR_INITSEQ_ERROR RED + + +#define COLOR_MENU_BACKGROUND BLACK + +#endif \ No newline at end of file diff --git a/src/lib/persist.cpp b/src/lib/persist.cpp new file mode 100644 index 0000000..da25eb0 --- /dev/null +++ b/src/lib/persist.cpp @@ -0,0 +1,39 @@ +#include "./persist.h" +#include + + +void Persist::init() +{ + PersistHeader header; + EEPROM.get(0, header); + + if (header.identifier == PersistIdentifier) + { + if (header.version >= 1) + this->readHeights(); + + this->initialized = true; + } +} + + +void Persist::readHeights() +{ + EEPROM.get(PersistHeightsAddress, this->heights); +} + + +void Persist::writeHeights() +{ + EEPROM.put(PersistHeightsAddress, this->heights); +} + + +void Persist::setHeightOffset(uint8_t newValue) +{ + if (newValue == this->heights.offset) + return; + + this->heights.offset = newValue; + this->writeHeights(); +} diff --git a/src/lib/persist.h b/src/lib/persist.h new file mode 100644 index 0000000..de67d47 --- /dev/null +++ b/src/lib/persist.h @@ -0,0 +1,50 @@ +#ifndef __persist +#define __persist +#include +#include + + +#define PersistIdentifier 0x42 +#define PersistVersion1 1 + + +struct PersistHeader +{ + uint8_t identifier; + uint8_t version; +}; + + +#define PersistHeightsAddress sizeof(PersistHeader) +struct PersistHeights +{ + uint8_t offset; + uint16_t setting[2]; +}; + + + +class Persist +{ + public: + Persist() + { + memset(&this->heights, 0, sizeof(PersistHeights)); + } + + void init(); + + // How many centimeters up the reflective surface is from the ground + uint8_t getHeightOffset() { return this->heights.offset; } + void setHeightOffset(uint8_t newValue); + + + private: + void readHeights(); + void writeHeights(); + + bool initialized = false; + PersistHeights heights; +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a6e0698..84a2233 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,48 +5,124 @@ #include "./include/display.h" #include "./include/heightsensor.h" +#include "./include/colors.h" +#include "./lib/persist.h" #include "./lib/menu.h" #include "./lib/vl53l0x.h" +// Forward declarations +void initSequenceStart(); +void initSequenceSuccess(uint8_t which); +void initSequenceError(uint8_t which, const char* message); + +#define INITSEQ_EEPROM 0 +#define INITSEQ_HEIGHTSENSORINIT 1 +#define INITSEQ_HEIGHTSENSORBUDGET 2 +#define INITSEQ_HEIGHTSENSORTEST 3 +#define INITSEQ_MAX INITSEQ_HEIGHTSENSORINIT + + + auto display = Adafruit_ST7789(DISPLAY_PORT_CS, DISPLAY_PORT_DC, DISPLAY_PORT_RST); auto heightSensor = VL53L0X(); +auto persist = Persist(); auto menu = Menu(&display); + void setup() { display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT, SPI_MODE3); display.setRotation(DISPLAY_ROTATION); - display.fillScreen(ST77XX_BLACK); - menu.init(); + initSequenceStart(); - // TODO draw "initializing" text + // Load settings from EEPROM + persist.init(); + initSequenceSuccess(INITSEQ_EEPROM); + + + + // Initialize VL53L0X sensor Wire.begin(); VL53L0XResult result; if (!heightSensor.init(HEIGHTSENSOR_I2C_ADDRESS, &result)) { - // TODO draw "height sensor error" text - + initSequenceError(INITSEQ_HEIGHTSENSORINIT, "TODO: error message"); while(1); } + initSequenceSuccess(INITSEQ_HEIGHTSENSORINIT); + VL53L0X_Error error; if (!heightSensor.setMeasurementTimingBudget(33000, &error)) { - // TODO draw "height sensor budget error" text - + initSequenceError(INITSEQ_HEIGHTSENSORBUDGET, "TODO: error message"); while(1); } + initSequenceSuccess(INITSEQ_HEIGHTSENSORBUDGET); - display.fillScreen(ST77XX_BLACK); + + // TODO height sensor test - are we getting stable values + initSequenceSuccess(INITSEQ_HEIGHTSENSORTEST); + + + delay(1000); + + + // Show the main menu + display.fillScreen(COLOR_MENU_BACKGROUND); + menu.init(); } + void loop() { // TODO -} \ No newline at end of file +} + + + +/* + + Helper functions for the status display during the initialization sequence + +*/ +// Default font is 5x7, x2 for the current text size +#define initSequenceTextY(which) ((1 + which) * 14) + +void initSequenceStart() +{ + display.fillScreen(COLOR_INITSEQ_BACKGROUND); + display.setTextSize(2); + + display.setCursor(0, 0); + display.setTextColor(COLOR_INITSEQ_TITLE); + display.println("Initializing..."); + + display.setTextColor(COLOR_INITSEQ_ITEMS); + display.println(" reading EEPROM"); + display.println(" height sensor init"); + display.println(" height sensor budget"); + display.println(" height sensor test"); +} + + +void initSequenceSuccess(uint8_t which) +{ + + display.drawChar(0, initSequenceTextY(which), 'v', COLOR_INITSEQ_SUCCESS, GREEN, 2); +} + + +void initSequenceError(uint8_t which, const char* message) +{ + display.drawChar(0, initSequenceTextY(which), 'x', COLOR_INITSEQ_ERROR, COLOR_INITSEQ_ERROR, 2); + + display.setCursor(0, initSequenceTextY(INITSEQ_MAX + 2)); + display.print(message); +}