From df61b1f24dfd28751250440e0cbed376f7ed2bd8 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Thu, 30 Jan 2020 16:01:26 +0100 Subject: [PATCH] Implemented screen timeout and button detection --- doc/errors.md | 47 ----------------------------------------- platformio.ini | 4 +--- src/include/config.h | 13 ++++++++++++ src/lib/screen.cpp | 24 ++++++++++++++++++++- src/lib/screen.h | 13 +++++++++--- src/lib/screen/home.cpp | 17 +++++++++++++-- src/lib/screen/home.h | 3 ++- src/main.cpp | 41 ++++++++++++++++++++--------------- 8 files changed, 88 insertions(+), 74 deletions(-) delete mode 100644 doc/errors.md diff --git a/doc/errors.md b/doc/errors.md deleted file mode 100644 index 16ebd12..0000000 --- a/doc/errors.md +++ /dev/null @@ -1,47 +0,0 @@ - -# Error codes reference -During startup a number of error codes may be presented. This document serves as a reference for tracing the problem. - -### height sensor -Errors are logged as "code @ position". Code is the status code as returned by the VL53L0X API, and position determines which API call during initialization failed. Test - - -| Code | Constant | Description | -|--|--|--| -| -1 | VL53L0X_ERROR_CALIBRATION_WARNING | Warning invalid calibration data may be in used VL53L0X_InitData(), VL53L0X_GetOffsetCalibrationData, VL53L0X_SetOffsetCalibrationData | -| -2 | VL53L0X_ERROR_MIN_CLIPPED | Warning parameter passed was clipped to min before to be applied | -| -3 | VL53L0X_ERROR_UNDEFINED | Unqualified error | -| -4 | VL53L0X_ERROR_INVALID_PARAMS | Parameter passed is invalid or out of range | -| -5 | VL53L0X_ERROR_NOT_SUPPORTED | Function is not supported in current mode or configuration | -| -6 | VL53L0X_ERROR_RANGE_ERROR | Device report a ranging error interrupt status | -| -7 | VL53L0X_ERROR_TIME_OUT | Aborted due to time out | -| -8 | VL53L0X_ERROR_MODE_NOT_SUPPORTED | Asked mode is not supported by the device | -| -9 | VL53L0X_ERROR_BUFFER_TOO_SMALL | ... | -| -10 | VL53L0X_ERROR_GPIO_NOT_EXISTING | User tried to setup a non-existing GPIO pin | -| -11 | VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED | unsupported GPIO functionality | -| -12 | VL53L0X_ERROR_INTERRUPT_NOT_CLEARED | Error during interrupt clear | -| -20 | VL53L0X_ERROR_CONTROL_INTERFACE | error reported from IO functions | -| -30 | VL53L0X_ERROR_INVALID_COMMAND | The command is not allowed in the current device state (power down) | -| -40 | VL53L0X_ERROR_DIVISION_BY_ZERO | In the function a division by zero occurs | -| -50 | VL53L0X_ERROR_REF_SPAD_INIT | Error during reference SPAD initialization | -| -99 | VL53L0X_ERROR_NOT_IMPLEMENTED | - -
- -| Position | Description | -|--|--| -| 1 | VL53L0X_DataInit | -| 2 | VL53L0X_SetDeviceAddress | -| 3 | VL53L0X_GetDeviceInfo | -| 4 | VL53L0X_StaticInit | -| 5 | VL53L0X_PerformRefSpadManagement | -| 6 | VL53L0X_PerformRefCalibration | -| 7 | VL53L0X_SetDeviceMode | -| 8 | VL53L0X_SetLimitCheckEnable(VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE) | -| 9 | VL53L0X_SetLimitCheckEnable(VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE) | -| 10 | VL53L0X_SetLimitCheckEnable(VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD) | -| 11 | VL53L0X_SetLimitCheckValue(VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD) | - - -### timing budget -Errors are logged as an VL53L0X API code. See the table above for reference. \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 77c38fa..8949348 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,13 +17,11 @@ lib_deps = Bounce2 Adafruit GFX Library Adafruit ST7735 and ST7789 Library - Adafruit_VL53L0X [env:uno] platform = atmelavr -board = atmega328p -board_build.f_cpu = 16000000L +board = uno framework = arduino upload_port = COM3 diff --git a/src/include/config.h b/src/include/config.h index a43848a..e8a8c90 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -27,6 +27,16 @@ class Config { public: + /* + + Buttons + + */ + static const uint8_t ButtonPortUp = 3; + static const uint8_t ButtonPortMenu = 5; + static const uint8_t ButtonPortDown = 6; + + /* Display @@ -47,6 +57,9 @@ class Config static const uint8_t DisplayPortBL = 7; + static const uint16_t DisplayIdleTime = 10000; + + /* Height sensor Settings for a VL53L0X time-of-flight sensor. diff --git a/src/lib/screen.cpp b/src/lib/screen.cpp index a15d1aa..d5896f9 100644 --- a/src/lib/screen.cpp +++ b/src/lib/screen.cpp @@ -1,7 +1,29 @@ #include "./screen.h" +#include "include/config.h" Adafruit_GFX* BaseScreen::getDisplay() { return this->screenManager->getDisplay(); -} \ No newline at end of file +} + + +void ScreenManager::init() +{ + pinMode(Config::DisplayPortBL, OUTPUT); + digitalWrite(Config::DisplayPortBL, HIGH); +} + + +void ScreenManager::displayOff() +{ + digitalWrite(Config::DisplayPortBL, LOW); + this->display->sendCommand(ST77XX_SLPIN); +} + + +void ScreenManager::displayOn() +{ + this->display->sendCommand(ST77XX_SLPOUT); + digitalWrite(Config::DisplayPortBL, HIGH); +} diff --git a/src/lib/screen.h b/src/lib/screen.h index 59bedf5..001c698 100644 --- a/src/lib/screen.h +++ b/src/lib/screen.h @@ -1,7 +1,7 @@ #ifndef __screen #define __screen -#include +#include #include "include/config.h" @@ -43,11 +43,14 @@ class BaseScreen class ScreenManager { public: - ScreenManager(Adafruit_GFX* display) + ScreenManager(Adafruit_ST7789* display) { this->display = display; } + void init(); + + inline void button(Button button) { this->getCurrentScreen()->onButton(button); } inline void tick() { this->getCurrentScreen()->onTick(); } inline BaseScreen* getCurrentScreen() { return this->currentScreen; } @@ -66,8 +69,12 @@ class ScreenManager this->currentScreen->onShow(); } + + void displayOff(); + void displayOn(); + private: - Adafruit_GFX* display; + Adafruit_ST7789* display; BaseScreen* currentScreen = nullptr; }; diff --git a/src/lib/screen/home.cpp b/src/lib/screen/home.cpp index 5b3c374..335800a 100644 --- a/src/lib/screen/home.cpp +++ b/src/lib/screen/home.cpp @@ -38,6 +38,16 @@ void HomeScreen::onShow() void HomeScreen::onButton(Button button) { + if (this->idle) + { + this->getScreenManager()->displayOn(); + this->idle = false; + this->showTime = State.CurrentTime; + + // TODO should preset buttons activate immediately? + return; + } + switch (button) { case Button::Up: @@ -59,8 +69,11 @@ void HomeScreen::onButton(Button button) void HomeScreen::onTick() { - // TODO turn off screen after timeout - //if (State.CurrentTime ) + if (!this->idle && State.CurrentTime - this->showTime >= Config::DisplayIdleTime) + { + this->getScreenManager()->displayOff(); + this->idle = true; + } } diff --git a/src/lib/screen/home.h b/src/lib/screen/home.h index 1cf3fd8..559153e 100644 --- a/src/lib/screen/home.h +++ b/src/lib/screen/home.h @@ -19,7 +19,8 @@ class HomeScreen : public BaseScreen void onTick(); private: - uint32_t showTime; + uint32_t showTime = 0; + bool idle = false; void drawPreset1(); void drawPreset2(); diff --git a/src/main.cpp b/src/main.cpp index 277d12c..33c49af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "./include/config.h" #include "./lib/debug.h" @@ -42,6 +43,8 @@ auto heightSensor = VL53L0X(); auto screenManager = ScreenManager(&display); +Bounce buttons[3]; + /* @@ -50,15 +53,17 @@ auto screenManager = ScreenManager(&display); */ void setup() { - State.CurrentTime = millis(); DebugInit(); - pinMode(Config::DisplayPortBL, OUTPUT); - digitalWrite(Config::DisplayPortBL, HIGH); + buttons[0].attach(Config::ButtonPortUp, INPUT_PULLUP); + buttons[1].attach(Config::ButtonPortMenu, INPUT_PULLUP); + buttons[2].attach(Config::ButtonPortDown, INPUT_PULLUP); display.init(Config::DisplayWidth, Config::DisplayHeight, SPI_MODE3); display.setRotation(Config::DisplayRotation); + screenManager.init(); + initSequenceStart(); // Load settings from EEPROM @@ -73,6 +78,8 @@ void setup() initSequenceEnd(); + State.CurrentTime = millis(); + if (initialized) { State.CurrentHeight = currentHeight; @@ -190,6 +197,20 @@ void loop() updateHeight(); } + buttons[0].update(); + buttons[1].update(); + buttons[2].update(); + + + if (buttons[0].rose()) + screenManager.button(Button::Up); + + if (buttons[1].rose()) + screenManager.button(Button::Menu); + + if (buttons[2].rose()) + screenManager.button(Button::Down); + screenManager.tick(); } @@ -225,20 +246,6 @@ bool heightSensorGetRange(uint16_t* measurement) } -/* - - For display sleep: - - - delay(1000); - display.sendCommand(ST77XX_SLPIN); - // toggle backlight pin - - - For motor sleep toggle slp pin - */ - - /*