From b8819b8e7304076fdaeb206405978fac07814cdf Mon Sep 17 00:00:00 2001 From: Alessandro Gatti Date: Wed, 28 Aug 2019 21:13:39 +0200 Subject: [PATCH] Refactor OLED on/off mechanism. Made the display on/off mechanism a bit more self-descriptive by replacing bare true/false values with an enum with more appropriate value names. OLED automatic turn-off logic has been cleaned up, along with minor updates to the OLED initialisation sequence. --- workspace/TS100/Core/Inc/OLED.hpp | 18 ++++++++++------ workspace/TS100/Core/Src/GUIThread.cpp | 29 +++++++++++++++----------- workspace/TS100/Core/Src/OLED.cpp | 14 +++++++------ 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/workspace/TS100/Core/Inc/OLED.hpp b/workspace/TS100/Core/Inc/OLED.hpp index f57e3355..009c4d7b 100644 --- a/workspace/TS100/Core/Inc/OLED.hpp +++ b/workspace/TS100/Core/Inc/OLED.hpp @@ -26,8 +26,14 @@ extern "C" { #define OLED_WIDTH 96 #define FRAMEBUFFER_START 17 -class OLED { +class OLED { public: + + enum DisplayState : bool { + OFF = false, + ON = true + }; + static void initialize(); // Startup the I2C coms (brings screen out of reset etc) // Draw the buffer out to the LCD using the DMA Channel @@ -38,11 +44,11 @@ public: //or we need to goto double buffering } - // Turn the screen on or not - static void displayOnOff(bool on) { - displayOnOffState = on; - screenBuffer[1] = on ? 0xAF : 0xAE; + static void setDisplayState(DisplayState state) { + displayState = state; + screenBuffer[1] = (state == ON) ? 0xAF : 0xAE; } + static void setRotation(bool leftHanded); // Set the rotation for the screen // Get the current rotation of the LCD static bool getRotation() { @@ -96,7 +102,7 @@ private: static uint8_t* firstStripPtr; // Pointers to the strips to allow for buffer having extra content static uint8_t* secondStripPtr; //Pointers to the strips static bool inLeftHandedMode; // Whether the screen is in left or not (used for offsets in GRAM) - static bool displayOnOffState; // If the display is on or not + static DisplayState displayState; static uint8_t fontWidth, fontHeight; static int16_t cursor_x, cursor_y; static uint8_t displayOffset; diff --git a/workspace/TS100/Core/Src/GUIThread.cpp b/workspace/TS100/Core/Src/GUIThread.cpp index 419e6d7c..8d93716f 100644 --- a/workspace/TS100/Core/Src/GUIThread.cpp +++ b/workspace/TS100/Core/Src/GUIThread.cpp @@ -26,6 +26,11 @@ extern osThreadId GUITaskHandle; extern osThreadId MOVTaskHandle; extern osThreadId PIDTaskHandle; +// TODO: express time constants in terms of dividends of portTICK_RATE_MS + +#define MOVEMENT_INACTIVITY_TIME 6000 +#define BUTTON_INACTIVITY_TIME 6000 + static uint16_t min(uint16_t a, uint16_t b) { if (a > b) return b; @@ -714,7 +719,7 @@ void startGUITask(void const *argument __unused) { for (;;) { ButtonState buttons = getButtonState(); if (buttons != BUTTON_NONE) { - OLED::displayOnOff(true); // turn lcd on + OLED::setDisplayState(OLED::DisplayState::ON); OLED::setFont(0); } if (tempWarningState == 2) @@ -760,17 +765,17 @@ void startGUITask(void const *argument __unused) { getInputVoltageX10(systemSettings.voltageDiv, 0); uint16_t tipTemp = tipMeasurementToC(getTipRawTemp(0)); - if (tipTemp < 50) { - if (systemSettings.sensitivity) { - if ((xTaskGetTickCount() - lastMovementTime) > 6000 - && (xTaskGetTickCount() - lastButtonTime) > 6000) { - OLED::displayOnOff(false); // turn lcd off when no movement - } else - OLED::displayOnOff(true); // turn lcd on - } else - OLED::displayOnOff(true); // turn lcd on - disabled motion sleep - } else - OLED::displayOnOff(true); // turn lcd on when temp > 50C + // Preemptively turn the display on. Turn it off if and only if + // the tip temperature is below 50 degrees C *and* motion sleep + // detection is enabled *and* there has been no activity (movement or + // button presses) in a while. + OLED::setDisplayState(OLED::DisplayState::ON); + + if ((tipTemp < 50) && systemSettings.sensitivity && + (((xTaskGetTickCount() - lastMovementTime) > MOVEMENT_INACTIVITY_TIME) && + ((xTaskGetTickCount() - lastButtonTime) > BUTTON_INACTIVITY_TIME))) { + OLED::setDisplayState(OLED::DisplayState::OFF); + } // Clear the lcd buffer OLED::clearScreen(); diff --git a/workspace/TS100/Core/Src/OLED.cpp b/workspace/TS100/Core/Src/OLED.cpp index 13d6bfee..6348ded3 100644 --- a/workspace/TS100/Core/Src/OLED.cpp +++ b/workspace/TS100/Core/Src/OLED.cpp @@ -18,7 +18,7 @@ uint8_t* OLED::firstStripPtr; // Pointers to the strips to allow for buffer uint8_t* OLED::secondStripPtr; // Pointers to the strips bool OLED::inLeftHandedMode; // Whether the screen is in left or not (used for // offsets in GRAM) -bool OLED::displayOnOffState; // If the display is on or not +OLED::DisplayState OLED::displayState; uint8_t OLED::fontWidth, OLED::fontHeight; int16_t OLED::cursor_x, OLED::cursor_y; uint8_t OLED::displayOffset; @@ -70,16 +70,18 @@ void OLED::initialize() { secondStripPtr = &screenBuffer[FRAMEBUFFER_START + OLED_WIDTH]; fontHeight = 16; displayOffset = 0; - displayOnOffState = true; memcpy(&screenBuffer[0], &REFRESH_COMMANDS[0], sizeof(REFRESH_COMMANDS)); HAL_Delay(50); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET); HAL_Delay(50); - // Send the setup settings - FRToSI2C::Transmit(DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, - sizeof(OLED_Setup_Array)); - displayOnOff(true); + + // Set the display to be ON once the settings block is sent and send the + // initialisation data to the OLED. + + setDisplayState(DisplayState::ON); + FRToSI2C::Transmit(DEVICEADDR_OLED, &OLED_Setup_Array[0], + sizeof(OLED_Setup_Array)); } /*