167 lines
3.5 KiB
C++
167 lines
3.5 KiB
C++
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_ST7789.h>
|
|
|
|
#include "./include/display.h"
|
|
#include "./include/heightsensor.h"
|
|
#include "./include/colors.h"
|
|
#include "./lib/debug.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_HEIGHTSENSORTEST
|
|
|
|
|
|
|
|
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()
|
|
{
|
|
DebugInit();
|
|
|
|
pinMode(DISPLAY_PORT_BL, OUTPUT);
|
|
digitalWrite(DISPLAY_PORT_BL, HIGH);
|
|
|
|
display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT, SPI_MODE3);
|
|
display.setRotation(DISPLAY_ROTATION);
|
|
|
|
initSequenceStart();
|
|
|
|
|
|
// Load settings from EEPROM
|
|
persist.init();
|
|
initSequenceSuccess(INITSEQ_EEPROM);
|
|
|
|
|
|
|
|
// Initialize VL53L0X sensor
|
|
Wire.begin();
|
|
|
|
VL53L0XResult result;
|
|
if (!heightSensor.init(HEIGHTSENSOR_I2C_ADDRESS, &result))
|
|
{
|
|
char message[16] = "";
|
|
char* messagePtr = &message[0];
|
|
|
|
memset(messagePtr, 0, sizeof(message));
|
|
|
|
itoa(result.error, messagePtr, 10);
|
|
messagePtr += strlen(messagePtr);
|
|
|
|
*messagePtr = ' '; messagePtr++;
|
|
*messagePtr = '@'; messagePtr++;
|
|
*messagePtr = ' '; messagePtr++;
|
|
|
|
itoa(result.position, messagePtr, 10);
|
|
initSequenceError(INITSEQ_HEIGHTSENSORINIT, &message[0]);
|
|
|
|
/*
|
|
delay(1000);
|
|
display.sendCommand(ST77XX_SLPIN);
|
|
// toggle backlight pin
|
|
*/
|
|
|
|
|
|
while(1);
|
|
}
|
|
initSequenceSuccess(INITSEQ_HEIGHTSENSORINIT);
|
|
|
|
|
|
VL53L0X_Error error;
|
|
if (!heightSensor.setMeasurementTimingBudget(33000, &error))
|
|
{
|
|
initSequenceError(INITSEQ_HEIGHTSENSORBUDGET, "TODO: error message");
|
|
while(1);
|
|
}
|
|
initSequenceSuccess(INITSEQ_HEIGHTSENSORBUDGET);
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
#define initSequenceTextSize 2
|
|
|
|
// Default font is 5x7
|
|
#define initSequenceTextY(which) ((1 + which) * ((7 + 3) * initSequenceTextSize))
|
|
|
|
|
|
void initSequenceStart()
|
|
{
|
|
display.fillScreen(COLOR_INITSEQ_BACKGROUND);
|
|
display.setTextSize(initSequenceTextSize);
|
|
|
|
display.setCursor(0, 0);
|
|
display.setTextColor(COLOR_INITSEQ_TITLE);
|
|
display.println("Initializing...");
|
|
|
|
display.setTextColor(COLOR_INITSEQ_ITEMS);
|
|
|
|
display.setCursor(0, initSequenceTextY(0));
|
|
display.print(" reading EEPROM");
|
|
|
|
display.setCursor(0, initSequenceTextY(1));
|
|
display.print(" height sensor");
|
|
|
|
display.setCursor(0, initSequenceTextY(2));
|
|
display.print(" timing budget");
|
|
|
|
display.setCursor(0, initSequenceTextY(3));
|
|
display.print(" sensor test");
|
|
}
|
|
|
|
|
|
void initSequenceSuccess(uint8_t which)
|
|
{
|
|
|
|
display.drawChar(0, initSequenceTextY(which), 'v', COLOR_INITSEQ_SUCCESS, GREEN, initSequenceTextSize);
|
|
}
|
|
|
|
|
|
void initSequenceError(uint8_t which, const char* message)
|
|
{
|
|
display.drawChar(0, initSequenceTextY(which), 'x', COLOR_INITSEQ_ERROR, COLOR_INITSEQ_ERROR, initSequenceTextSize);
|
|
|
|
display.setCursor(0, initSequenceTextY(INITSEQ_MAX + 2));
|
|
display.print(message);
|
|
}
|