2020-01-26 09:28:37 +00:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_ST7789.h>
|
|
|
|
|
|
|
|
#include "./include/display.h"
|
|
|
|
#include "./include/heightsensor.h"
|
2020-01-26 15:20:37 +00:00
|
|
|
#include "./include/colors.h"
|
2020-01-27 18:34:08 +00:00
|
|
|
#include "./lib/debug.h"
|
2020-01-26 15:20:37 +00:00
|
|
|
#include "./lib/persist.h"
|
2020-01-28 15:52:18 +00:00
|
|
|
#include "./lib/screen.h"
|
2020-01-26 09:28:37 +00:00
|
|
|
#include "./lib/vl53l0x.h"
|
2020-01-28 15:52:18 +00:00
|
|
|
#include "./lib/limits.h"
|
|
|
|
#include "./lib/state.h"
|
|
|
|
#include "./lib/screen/home.h"
|
2020-01-26 09:28:37 +00:00
|
|
|
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
// Forward declarations
|
2020-01-28 15:52:18 +00:00
|
|
|
inline void setupHeightSensor();
|
|
|
|
inline uint8_t testHeightSensor();
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
void initSequenceStart();
|
|
|
|
void initSequenceSuccess(uint8_t which);
|
2020-01-28 15:52:18 +00:00
|
|
|
void initSequenceError(uint8_t which);
|
|
|
|
void initSequenceDisplayHeight(uint16_t measurement);
|
|
|
|
void initSequenceEnd();
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
|
|
|
|
#define INITSEQ_EEPROM 0
|
|
|
|
#define INITSEQ_HEIGHTSENSORINIT 1
|
|
|
|
#define INITSEQ_HEIGHTSENSORBUDGET 2
|
|
|
|
#define INITSEQ_HEIGHTSENSORTEST 3
|
2020-01-27 18:34:08 +00:00
|
|
|
#define INITSEQ_MAX INITSEQ_HEIGHTSENSORTEST
|
2020-01-26 15:20:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-26 13:10:25 +00:00
|
|
|
auto display = Adafruit_ST7789(DISPLAY_PORT_CS, DISPLAY_PORT_DC, DISPLAY_PORT_RST);
|
|
|
|
auto heightSensor = VL53L0X();
|
2020-01-26 09:28:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
uint32_t currentTime = 0;
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
auto persist = Persist();
|
2020-01-28 15:52:18 +00:00
|
|
|
auto screenManager = ScreenManager(&display, ¤tTime);
|
2020-01-26 09:28:37 +00:00
|
|
|
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Setup
|
2020-01-26 15:20:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
*/
|
2020-01-26 09:28:37 +00:00
|
|
|
void setup()
|
2020-01-11 11:28:42 +00:00
|
|
|
{
|
2020-01-27 18:34:08 +00:00
|
|
|
DebugInit();
|
|
|
|
|
|
|
|
pinMode(DISPLAY_PORT_BL, OUTPUT);
|
|
|
|
digitalWrite(DISPLAY_PORT_BL, HIGH);
|
|
|
|
|
2020-01-26 09:28:37 +00:00
|
|
|
display.init(DISPLAY_WIDTH, DISPLAY_HEIGHT, SPI_MODE3);
|
|
|
|
display.setRotation(DISPLAY_ROTATION);
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
initSequenceStart();
|
|
|
|
|
|
|
|
// Load settings from EEPROM
|
2020-01-28 15:52:18 +00:00
|
|
|
auto initialized = persist.init();
|
2020-01-26 15:20:37 +00:00
|
|
|
initSequenceSuccess(INITSEQ_EEPROM);
|
2020-01-26 09:28:37 +00:00
|
|
|
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
// Initialize VL53L0X sensor
|
2020-01-28 15:52:18 +00:00
|
|
|
setupHeightSensor();
|
|
|
|
auto currentHeight = testHeightSensor();
|
2020-01-26 09:28:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
initSequenceEnd();
|
2020-01-27 18:34:08 +00:00
|
|
|
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
setCurrentHeight(currentHeight + persist.getHeightOffset());
|
|
|
|
screenManager.show<HomeScreen>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// TODO show height configuration screen
|
|
|
|
screenManager.show<HomeScreen>();
|
|
|
|
}
|
2020-01-27 18:34:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
inline void setupHeightSensor()
|
|
|
|
{
|
|
|
|
Wire.begin();
|
|
|
|
|
|
|
|
VL53L0XResult result;
|
|
|
|
if (!heightSensor.init(HEIGHTSENSOR_I2C_ADDRESS, &result))
|
|
|
|
{
|
|
|
|
initSequenceError(INITSEQ_HEIGHTSENSORINIT);
|
2020-01-27 18:34:08 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
display.print(result.error);
|
|
|
|
display.print(" @ ");
|
|
|
|
display.print(result.position);
|
2020-01-27 18:34:08 +00:00
|
|
|
|
2020-01-26 09:28:37 +00:00
|
|
|
while(1);
|
|
|
|
}
|
2020-01-26 15:20:37 +00:00
|
|
|
initSequenceSuccess(INITSEQ_HEIGHTSENSORINIT);
|
|
|
|
|
2020-01-26 09:28:37 +00:00
|
|
|
|
2020-01-26 13:10:25 +00:00
|
|
|
VL53L0X_Error error;
|
|
|
|
if (!heightSensor.setMeasurementTimingBudget(33000, &error))
|
|
|
|
{
|
2020-01-28 15:52:18 +00:00
|
|
|
initSequenceError(INITSEQ_HEIGHTSENSORBUDGET);
|
|
|
|
|
|
|
|
display.print(result.error);
|
|
|
|
|
2020-01-26 13:10:25 +00:00
|
|
|
while(1);
|
|
|
|
}
|
2020-01-26 15:20:37 +00:00
|
|
|
initSequenceSuccess(INITSEQ_HEIGHTSENSORBUDGET);
|
2020-01-28 15:52:18 +00:00
|
|
|
}
|
2020-01-26 15:20:37 +00:00
|
|
|
|
|
|
|
|
2020-01-26 13:10:25 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
inline uint8_t testHeightSensor()
|
|
|
|
{
|
|
|
|
VL53L0X_Error error;
|
|
|
|
uint16_t reference = 0;
|
|
|
|
uint8_t closeCount = 0;
|
2020-01-26 15:20:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
while (closeCount < 3)
|
|
|
|
{
|
|
|
|
uint16_t measurement;
|
|
|
|
|
|
|
|
if (heightSensor.getSingleRangingMeasurement(&measurement, &error) && measurement <= HeightMeasurementMax)
|
|
|
|
{
|
|
|
|
initSequenceDisplayHeight(measurement);
|
|
|
|
|
|
|
|
if (abs(measurement - reference) <= HeightMeasurementStableDelta)
|
|
|
|
closeCount++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reference = measurement;
|
|
|
|
closeCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initSequenceDisplayHeight(0);
|
|
|
|
|
|
|
|
reference = 0;
|
|
|
|
closeCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
}
|
2020-01-26 15:20:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
initSequenceSuccess(INITSEQ_HEIGHTSENSORTEST);
|
|
|
|
return reference / 10;
|
2020-01-26 09:28:37 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Loop
|
|
|
|
|
|
|
|
*/
|
2020-01-26 09:28:37 +00:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// TODO
|
2020-01-26 15:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
For display sleep:
|
|
|
|
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
display.sendCommand(ST77XX_SLPIN);
|
|
|
|
// toggle backlight pin
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Helper functions for the status display during the initialization sequence
|
|
|
|
|
|
|
|
*/
|
2020-01-27 18:34:08 +00:00
|
|
|
#define initSequenceTextSize 2
|
|
|
|
|
|
|
|
// Default font is 5x7
|
|
|
|
#define initSequenceTextY(which) ((1 + which) * ((7 + 3) * initSequenceTextSize))
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
|
|
|
|
void initSequenceStart()
|
|
|
|
{
|
|
|
|
display.fillScreen(COLOR_INITSEQ_BACKGROUND);
|
2020-01-27 18:34:08 +00:00
|
|
|
display.setTextSize(initSequenceTextSize);
|
2020-01-26 15:20:37 +00:00
|
|
|
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
display.setTextColor(COLOR_INITSEQ_TITLE);
|
|
|
|
display.println("Initializing...");
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
display.setTextColor(COLOR_INITSEQ_ITEMS, COLOR_INITSEQ_BACKGROUND);
|
2020-01-27 18:34:08 +00:00
|
|
|
|
|
|
|
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");
|
2020-01-26 15:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initSequenceSuccess(uint8_t which)
|
|
|
|
{
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
display.drawChar(0, initSequenceTextY(which), 'v', COLOR_INITSEQ_SUCCESS, COLOR_INITSEQ_BACKGROUND, initSequenceTextSize);
|
2020-01-26 15:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
void initSequenceError(uint8_t which)
|
2020-01-26 15:20:37 +00:00
|
|
|
{
|
2020-01-28 15:52:18 +00:00
|
|
|
display.drawChar(0, initSequenceTextY(which), 'x', COLOR_INITSEQ_ERROR, COLOR_INITSEQ_BACKGROUND, initSequenceTextSize);
|
|
|
|
display.setCursor(0, initSequenceTextY(INITSEQ_MAX + 2));
|
|
|
|
}
|
|
|
|
|
2020-01-26 15:20:37 +00:00
|
|
|
|
2020-01-28 15:52:18 +00:00
|
|
|
void initSequenceDisplayHeight(uint16_t measurement)
|
|
|
|
{
|
2020-01-26 15:20:37 +00:00
|
|
|
display.setCursor(0, initSequenceTextY(INITSEQ_MAX + 2));
|
2020-01-28 15:52:18 +00:00
|
|
|
|
|
|
|
if (measurement > 0)
|
|
|
|
{
|
|
|
|
display.print(measurement);
|
|
|
|
display.print("mm ");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
display.print("- ");
|
2020-01-26 15:20:37 +00:00
|
|
|
}
|
2020-01-28 15:52:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
void initSequenceEnd()
|
|
|
|
{
|
|
|
|
display.setTextSize(1);
|
|
|
|
delay(1000);
|
|
|
|
}
|