From 7434f0b4da0ef538b6f57468c964150553d902ad Mon Sep 17 00:00:00 2001 From: MatthewColvin Date: Wed, 4 Oct 2023 11:42:45 -0500 Subject: [PATCH] add initial Key press implementation for hardware next is to actually spin up something to queue and handle key events --- Platformio/HAL/Targets/ESP32/HardwareRevX.cpp | 30 +--- Platformio/HAL/Targets/ESP32/HardwareRevX.hpp | 34 +---- .../HAL/Targets/ESP32/display/display.cpp | 136 +++++++++--------- Platformio/HAL/Targets/ESP32/keys/keys.cpp | 25 ++++ Platformio/HAL/Targets/ESP32/keys/keys.hpp | 37 +++++ Platformio/HAL/Targets/ESP32/omoteconfig.h | 4 +- Platformio/platformio.ini | 1 + 7 files changed, 139 insertions(+), 128 deletions(-) create mode 100644 Platformio/HAL/Targets/ESP32/keys/keys.cpp create mode 100644 Platformio/HAL/Targets/ESP32/keys/keys.hpp diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index a10ee97..e517360 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -74,6 +74,7 @@ void HardwareRevX::init() { mDisplay = Display::getInstance(); mBattery = std::make_shared(ADC_BAT, CRG_STAT); mWifiHandler = wifiHandler::getInstance(); + mKeys = std::make_shared(); restorePreferences(); mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) { @@ -112,6 +113,8 @@ std::shared_ptr HardwareRevX::battery() { return mBattery; } std::shared_ptr HardwareRevX::display() { return mDisplay; } +std::shared_ptr HardwareRevX::keys() { return mKeys; } + void HardwareRevX::activityDetection() { static int accXold; static int accYold; @@ -323,31 +326,4 @@ void HardwareRevX::loopHandler() { } IMUTaskTimer = millis(); } - - // Keypad Handling - customKeypad.getKey(); // Populate key list - for (int i = 0; i < LIST_MAX; - i++) { // Handle multiple keys (Not really necessary in this case) - if (customKeypad.key[i].kstate == PRESSED || - customKeypad.key[i].kstate == HOLD) { - standbyTimer = - sleepTimeout; // Reset the sleep timer when a button is pressed - int keyCode = customKeypad.key[i].kcode; - Serial.println(customKeypad.key[i].kchar); - // Send IR codes depending on the current device (tabview page) - if (currentDevice == 1) { - IrSender.sendRC5(IrSender.encodeRC5X( - 0x00, keyMapTechnisat[keyCode / ROWS][keyCode % ROWS])); - } else if (currentDevice == 2) { - IrSender.sendSony((keyCode / ROWS) * (keyCode % ROWS), 15); - } - } - } - // IR Test - // tft.drawString("IR Command: ", 10, 90, 1); - // decode_results results; - // if (IrReceiver.decode(&results)) { - // IrReceiver.resume(); // Enable receiving of the next value - //} //tft.drawString(String(results.command) + " ", 80, 90, 1); - // } \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index a78fb9f..892a711 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -5,19 +5,21 @@ #include "battery.hpp" #include "lvgl.h" #include "wifihandler.hpp" +#include #include #include #include #include -#include // modified for inverted logic #include #include #include #include +#include "omoteconfig.h" + #include "BatteryInterface.h" #include "display.hpp" -#include "omoteconfig.h" +#include "keys.hpp" #include "wifiHandlerInterface.h" class HardwareRevX : public HardwareAbstract { @@ -34,6 +36,7 @@ public: virtual std::shared_ptr battery() override; virtual std::shared_ptr display() override; virtual std::shared_ptr wifi() override; + virtual std::shared_ptr keys() override; virtual char getCurrentDevice() override; virtual void setCurrentDevice(char currentDevice) override; @@ -69,6 +72,7 @@ private: std::shared_ptr mBattery; std::shared_ptr mDisplay; std::shared_ptr mWifiHandler; + std::shared_ptr mKeys; // IMU Motion Detection LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19. int standbyTimer = SLEEP_TIMEOUT; @@ -85,31 +89,5 @@ private: IRsend IrSender = IRsend(IR_LED, true); IRrecv IrReceiver = IRrecv(IR_RX); - // Keypad declarations - static const byte ROWS = 5; // four rows - static const byte COLS = 5; // four columns - // define the symbols on the buttons of the keypads - char hexaKeys[ROWS][COLS] = { - {'s', '^', '-', 'm', 'r'}, // source, channel+, Volume-, mute, record - {'i', 'r', '+', 'k', 'd'}, // info, right, Volume+, OK, down - {'4', 'v', '1', '3', '2'}, // blue, channel-, red, yellow, green - {'>', 'o', 'b', 'u', 'l'}, // forward, off, back, up, left - {'?', 'p', 'c', '<', '='} // ?, play, config, rewind, stop - }; - byte rowPins[ROWS] = {SW_A, SW_B, SW_C, SW_D, - SW_E}; // connect to the row pinouts of the keypad - byte colPins[COLS] = {SW_1, SW_2, SW_3, SW_4, - SW_5}; // connect to the column pinouts of the keypad - Keypad customKeypad = - Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); - - byte keyMapTechnisat[ROWS][COLS] = {{0x69, 0x20, 0x11, 0x0D, 0x56}, - {0x4F, 0x37, 0x10, 0x57, 0x51}, - {0x6E, 0x21, 0x6B, 0x6D, 0x6C}, - {0x34, 0x0C, 0x22, 0x50, 0x55}, - {'?', 0x35, 0x2F, 0x32, 0x36}}; - byte virtualKeyMapTechnisat[10] = {0x1, 0x2, 0x3, 0x4, 0x5, - 0x6, 0x7, 0x8, 0x9, 0x0}; - static std::shared_ptr mInstance; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/display/display.cpp b/Platformio/HAL/Targets/ESP32/display/display.cpp index a8941cc..10932d1 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.cpp +++ b/Platformio/HAL/Targets/ESP32/display/display.cpp @@ -1,43 +1,39 @@ #include "display.hpp" -#include "omoteconfig.h" #include "Wire.h" #include "driver/ledc.h" +#include "omoteconfig.h" -std::shared_ptr Display::getInstance() -{ - if (DisplayAbstract::mInstance == nullptr) - { - DisplayAbstract::mInstance = std::shared_ptr(new Display(LCD_BL, LCD_EN)); - } - return std::static_pointer_cast(mInstance); +std::shared_ptr Display::getInstance() { + if (DisplayAbstract::mInstance == nullptr) { + DisplayAbstract::mInstance = + std::shared_ptr(new Display(LCD_BL, LCD_EN)); + } + return std::static_pointer_cast(mInstance); } -Display::Display(int backlight_pin, int enable_pin): DisplayAbstract(), - mBacklightPin(backlight_pin), - mEnablePin(enable_pin), - tft(TFT_eSPI()), - touch(Adafruit_FT6206()) -{ - pinMode(mEnablePin, OUTPUT); - digitalWrite(mEnablePin, HIGH); - pinMode(mBacklightPin, OUTPUT); - digitalWrite(mBacklightPin, HIGH); +Display::Display(int backlight_pin, int enable_pin) + : DisplayAbstract(), mBacklightPin(backlight_pin), mEnablePin(enable_pin), + tft(TFT_eSPI()), touch(Adafruit_FT6206()) { + pinMode(mEnablePin, OUTPUT); + digitalWrite(mEnablePin, HIGH); + pinMode(mBacklightPin, OUTPUT); + digitalWrite(mBacklightPin, HIGH); - setupBacklight(); // This eliminates the flash of the backlight + setupBacklight(); // This eliminates the flash of the backlight - // Slowly charge the VSW voltage to prevent a brownout - // Workaround for hardware rev 1! - for(int i = 0; i < 100; i++){ - digitalWrite(this->mEnablePin, HIGH); // LCD Logic off - delayMicroseconds(1); - digitalWrite(this->mEnablePin, LOW); // LCD Logic on - } + // Slowly charge the VSW voltage to prevent a brownout + // Workaround for hardware rev 1! + for (int i = 0; i < 100; i++) { + digitalWrite(this->mEnablePin, HIGH); // LCD Logic off + delayMicroseconds(1); + digitalWrite(this->mEnablePin, LOW); // LCD Logic on + } - setupTFT(); - setupTouchScreen(); - mFadeTaskMutex = xSemaphoreCreateBinary(); - xSemaphoreGive(mFadeTaskMutex); + setupTFT(); + setupTouchScreen(); + mFadeTaskMutex = xSemaphoreCreateBinary(); + xSemaphoreGive(mFadeTaskMutex); } void Display::setupBacklight() { @@ -62,7 +58,7 @@ void Display::setupBacklight() { ledc_timer_config(&ledc_timer); } -void Display::onTouch(Notification::HandlerTy aTouchHandler){ +void Display::onTouch(Notification::HandlerTy aTouchHandler) { mTouchEvent.onNotify(std::move(aTouchHandler)); } @@ -75,25 +71,22 @@ void Display::setupTFT() { tft.setSwapBytes(true); } -void Display::setupTouchScreen(){ - // Configure i2c pins and set frequency to 400kHz - Wire.begin(SDA, SCL, 400000); - touch.begin(128); // Initialize touchscreen and set sensitivity threshold +void Display::setupTouchScreen() { + // Configure i2c pins and set frequency to 400kHz + Wire.begin(TFT_SDA, TFT_SCL, 400000); + touch.begin(128); // Initialize touchscreen and set sensitivity threshold } -void Display::setBrightness(uint8_t brightness) -{ +void Display::setBrightness(uint8_t brightness) { mAwakeBrightness = brightness; Serial.print("Set Brightness:"); Serial.println(mAwakeBrightness); startFade(); } -uint8_t Display::getBrightness(){ - return mAwakeBrightness; -} +uint8_t Display::getBrightness() { return mAwakeBrightness; } -void Display::setCurrentBrightness(uint8_t brightness){ +void Display::setCurrentBrightness(uint8_t brightness) { mBrightness = brightness; auto duty = static_cast(mBrightness); ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, duty); @@ -101,18 +94,17 @@ void Display::setCurrentBrightness(uint8_t brightness){ // Serial.println(mBrightness); } -void Display::turnOff() -{ - digitalWrite(this->mBacklightPin, HIGH); - digitalWrite(this->mEnablePin, HIGH); - pinMode(this->mBacklightPin, INPUT); - pinMode(this->mEnablePin, INPUT); - gpio_hold_en((gpio_num_t) mBacklightPin); - gpio_hold_en((gpio_num_t) mEnablePin); +void Display::turnOff() { + digitalWrite(this->mBacklightPin, HIGH); + digitalWrite(this->mEnablePin, HIGH); + pinMode(this->mBacklightPin, INPUT); + pinMode(this->mEnablePin, INPUT); + gpio_hold_en((gpio_num_t)mBacklightPin); + gpio_hold_en((gpio_num_t)mEnablePin); } -void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data){ - // int16_t touchX, touchY; +void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) { + // int16_t touchX, touchY; touchPoint = touch.getPoint(); int16_t touchX = touchPoint.x; int16_t touchY = touchPoint.y; @@ -140,48 +132,50 @@ void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data){ } } -void Display::fadeImpl(void* ){ +void Display::fadeImpl(void *) { bool fadeDone = false; - while(!fadeDone){ + while (!fadeDone) { fadeDone = getInstance()->fade(); - vTaskDelay(3 / portTICK_PERIOD_MS); // 3 miliseconds between steps - // 0 - 255 will take about .75 seconds to fade up. + vTaskDelay(3 / portTICK_PERIOD_MS); // 3 miliseconds between steps + // 0 - 255 will take about .75 seconds to fade up. } - - xSemaphoreTake(getInstance()->mFadeTaskMutex,portMAX_DELAY); + + xSemaphoreTake(getInstance()->mFadeTaskMutex, portMAX_DELAY); getInstance()->mDisplayFadeTask = nullptr; xSemaphoreGive(getInstance()->mFadeTaskMutex); vTaskDelete(nullptr); // Delete Fade Task } -bool Display::fade(){ - //Early return no fade needed. - if (mBrightness == mAwakeBrightness || - isAsleep && mBrightness == 0){return true;} - - bool fadeDown = isAsleep || mBrightness > mAwakeBrightness; - if (fadeDown){ +bool Display::fade() { + // Early return no fade needed. + if (mBrightness == mAwakeBrightness || isAsleep && mBrightness == 0) { + return true; + } + + bool fadeDown = isAsleep || mBrightness > mAwakeBrightness; + if (fadeDown) { setCurrentBrightness(mBrightness - 1); auto setPoint = isAsleep ? 0 : mAwakeBrightness; return mBrightness == setPoint; - }else{ + } else { setCurrentBrightness(mBrightness + 1); return mBrightness == mAwakeBrightness; } } -void Display::startFade(){ - xSemaphoreTake(mFadeTaskMutex,portMAX_DELAY); +void Display::startFade() { + xSemaphoreTake(mFadeTaskMutex, portMAX_DELAY); // Only Create Task if it is needed - if(mDisplayFadeTask == nullptr){ - xTaskCreate(&Display::fadeImpl, "Display Fade Task", - 1024, nullptr, 5, &mDisplayFadeTask); + if (mDisplayFadeTask == nullptr) { + xTaskCreate(&Display::fadeImpl, "Display Fade Task", 1024, nullptr, 5, + &mDisplayFadeTask); } xSemaphoreGive(mFadeTaskMutex); } -void Display::flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) { +void Display::flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, + lv_color_t *color_p) { uint32_t w = (area->x2 - area->x1 + 1); uint32_t h = (area->y2 - area->y1 + 1); diff --git a/Platformio/HAL/Targets/ESP32/keys/keys.cpp b/Platformio/HAL/Targets/ESP32/keys/keys.cpp new file mode 100644 index 0000000..f08b212 --- /dev/null +++ b/Platformio/HAL/Targets/ESP32/keys/keys.cpp @@ -0,0 +1,25 @@ +#include "keys.hpp" + +Keys::Keys() {} +void Keys::HandleKeyPresses(){ + +}; +void Keys::QueueKeyEvent(KeyEvent aJustOccuredKeyEvent){ + +}; + +void Keys::GrabKeys() { + customKeypad.getKey(); // Populate key list + for (int i = 0; i < LIST_MAX; + i++) { // Handle multiple keys (Not really necessary in this case) + if (customKeypad.key[i].kstate == PRESSED || + customKeypad.key[i].kstate == HOLD) { + // May need to think about resetting sleep timer in key handler.... + // standbyTimer = + // sleepTimeout; // Reset the sleep timer when a button is pressed + int keyCode = customKeypad.key[i].kcode; + // Queue Keys here!! + Serial.println(customKeypad.key[i].kchar); + } + } +} \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/keys/keys.hpp b/Platformio/HAL/Targets/ESP32/keys/keys.hpp new file mode 100644 index 0000000..9fa58c4 --- /dev/null +++ b/Platformio/HAL/Targets/ESP32/keys/keys.hpp @@ -0,0 +1,37 @@ +#pragma once +#include "KeyPressAbstract.hpp" +#include "omoteconfig.h" +#include // modified for inverted logic + +class Keys : public KeyPressAbstract { +public: + Keys(); + void HandleKeyPresses() override; + void QueueKeyEvent(KeyEvent aJustOccuredKeyEvent) override; + +protected: + void GrabKeys(); + +private: + QueueHandle_t mKeyPressQueueHandle; + TaskHandle_t mKeyGrabbingTask; + TaskHandle_t mKeyHandlingTask; + + // Keypad declarations + static const byte ROWS = 5; // four rows + static const byte COLS = 5; // four columns + // define the symbols on the buttons of the keypads + char hexaKeys[ROWS][COLS] = { + {'s', '^', '-', 'm', 'r'}, // source, channel+, Volume-, mute, record + {'i', 'r', '+', 'k', 'd'}, // info, right, Volume+, OK, down + {'4', 'v', '1', '3', '2'}, // blue, channel-, red, yellow, green + {'>', 'o', 'b', 'u', 'l'}, // forward, off, back, up, left + {'?', 'p', 'c', '<', '='} // ?, play, config, rewind, stop + }; + byte rowPins[ROWS] = {SW_A, SW_B, SW_C, SW_D, + SW_E}; // connect to the row pinouts of the keypad + byte colPins[COLS] = {SW_1, SW_2, SW_3, SW_4, + SW_5}; // connect to the column pinouts of the keypad + Keypad customKeypad = + Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/omoteconfig.h b/Platformio/HAL/Targets/ESP32/omoteconfig.h index 462abe7..88e077c 100644 --- a/Platformio/HAL/Targets/ESP32/omoteconfig.h +++ b/Platformio/HAL/Targets/ESP32/omoteconfig.h @@ -45,8 +45,8 @@ #define IR_VCC 25 // IR receiver power #define IR_LED 33 // IR LED output -#define SCL 22 -#define SDA 19 +#define TFT_SCL 22 +#define TFT_SDA 19 #define ACC_INT 20 #define CRG_STAT 21 // battery charger feedback \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 634aac3..64f0eb0 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -111,6 +111,7 @@ build_flags = -I HAL/Targets/ESP32/battery -I HAL/Targets/ESP32/display -I HAL/Targets/ESP32/wifiHandler + -I HAL/Targets/ESP32/keys build_unflags = -std=gnu++11