Added initialization sequence

Added EEPROM persistence
This commit is contained in:
Mark van Renswoude 2020-01-26 16:20:37 +01:00
parent be0b4a4872
commit 73ba3b2750
4 changed files with 197 additions and 9 deletions

23
src/include/colors.h Normal file
View File

@ -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

39
src/lib/persist.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "./persist.h"
#include <EEPROM.h>
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();
}

50
src/lib/persist.h Normal file
View File

@ -0,0 +1,50 @@
#ifndef __persist
#define __persist
#include <stdint.h>
#include <string.h>
#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

View File

@ -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
}
}
/*
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);
}