add initial Key press implementation for hardware next is to actually spin up something to queue and handle key events

This commit is contained in:
MatthewColvin 2023-10-04 11:42:45 -05:00
parent a19e84ee80
commit 7434f0b4da
7 changed files with 139 additions and 128 deletions

View file

@ -74,6 +74,7 @@ void HardwareRevX::init() {
mDisplay = Display::getInstance(); mDisplay = Display::getInstance();
mBattery = std::make_shared<Battery>(ADC_BAT, CRG_STAT); mBattery = std::make_shared<Battery>(ADC_BAT, CRG_STAT);
mWifiHandler = wifiHandler::getInstance(); mWifiHandler = wifiHandler::getInstance();
mKeys = std::make_shared<Keys>();
restorePreferences(); restorePreferences();
mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) { mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) {
@ -112,6 +113,8 @@ std::shared_ptr<BatteryInterface> HardwareRevX::battery() { return mBattery; }
std::shared_ptr<DisplayAbstract> HardwareRevX::display() { return mDisplay; } std::shared_ptr<DisplayAbstract> HardwareRevX::display() { return mDisplay; }
std::shared_ptr<KeyPressAbstract> HardwareRevX::keys() { return mKeys; }
void HardwareRevX::activityDetection() { void HardwareRevX::activityDetection() {
static int accXold; static int accXold;
static int accYold; static int accYold;
@ -323,31 +326,4 @@ void HardwareRevX::loopHandler() {
} }
IMUTaskTimer = millis(); 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);
//
} }

View file

@ -5,19 +5,21 @@
#include "battery.hpp" #include "battery.hpp"
#include "lvgl.h" #include "lvgl.h"
#include "wifihandler.hpp" #include "wifihandler.hpp"
#include <Arduino.h>
#include <IRrecv.h> #include <IRrecv.h>
#include <IRremoteESP8266.h> #include <IRremoteESP8266.h>
#include <IRsend.h> #include <IRsend.h>
#include <IRutils.h> #include <IRutils.h>
#include <Keypad.h> // modified for inverted logic
#include <Preferences.h> #include <Preferences.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include "omoteconfig.h"
#include "BatteryInterface.h" #include "BatteryInterface.h"
#include "display.hpp" #include "display.hpp"
#include "omoteconfig.h" #include "keys.hpp"
#include "wifiHandlerInterface.h" #include "wifiHandlerInterface.h"
class HardwareRevX : public HardwareAbstract { class HardwareRevX : public HardwareAbstract {
@ -34,6 +36,7 @@ public:
virtual std::shared_ptr<BatteryInterface> battery() override; virtual std::shared_ptr<BatteryInterface> battery() override;
virtual std::shared_ptr<DisplayAbstract> display() override; virtual std::shared_ptr<DisplayAbstract> display() override;
virtual std::shared_ptr<wifiHandlerInterface> wifi() override; virtual std::shared_ptr<wifiHandlerInterface> wifi() override;
virtual std::shared_ptr<KeyPressAbstract> keys() override;
virtual char getCurrentDevice() override; virtual char getCurrentDevice() override;
virtual void setCurrentDevice(char currentDevice) override; virtual void setCurrentDevice(char currentDevice) override;
@ -69,6 +72,7 @@ private:
std::shared_ptr<Battery> mBattery; std::shared_ptr<Battery> mBattery;
std::shared_ptr<Display> mDisplay; std::shared_ptr<Display> mDisplay;
std::shared_ptr<wifiHandler> mWifiHandler; std::shared_ptr<wifiHandler> mWifiHandler;
std::shared_ptr<Keys> mKeys;
// IMU Motion Detection // IMU Motion Detection
LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19. LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19.
int standbyTimer = SLEEP_TIMEOUT; int standbyTimer = SLEEP_TIMEOUT;
@ -85,31 +89,5 @@ private:
IRsend IrSender = IRsend(IR_LED, true); IRsend IrSender = IRsend(IR_LED, true);
IRrecv IrReceiver = IRrecv(IR_RX); 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<HardwareRevX> mInstance; static std::shared_ptr<HardwareRevX> mInstance;
}; };

View file

@ -1,43 +1,39 @@
#include "display.hpp" #include "display.hpp"
#include "omoteconfig.h"
#include "Wire.h" #include "Wire.h"
#include "driver/ledc.h" #include "driver/ledc.h"
#include "omoteconfig.h"
std::shared_ptr<Display> Display::getInstance() std::shared_ptr<Display> Display::getInstance() {
{ if (DisplayAbstract::mInstance == nullptr) {
if (DisplayAbstract::mInstance == nullptr) DisplayAbstract::mInstance =
{ std::shared_ptr<Display>(new Display(LCD_BL, LCD_EN));
DisplayAbstract::mInstance = std::shared_ptr<Display>(new Display(LCD_BL, LCD_EN)); }
} return std::static_pointer_cast<Display>(mInstance);
return std::static_pointer_cast<Display>(mInstance);
} }
Display::Display(int backlight_pin, int enable_pin): DisplayAbstract(), Display::Display(int backlight_pin, int enable_pin)
mBacklightPin(backlight_pin), : DisplayAbstract(), mBacklightPin(backlight_pin), mEnablePin(enable_pin),
mEnablePin(enable_pin), tft(TFT_eSPI()), touch(Adafruit_FT6206()) {
tft(TFT_eSPI()), pinMode(mEnablePin, OUTPUT);
touch(Adafruit_FT6206()) digitalWrite(mEnablePin, HIGH);
{ pinMode(mBacklightPin, OUTPUT);
pinMode(mEnablePin, OUTPUT); digitalWrite(mBacklightPin, HIGH);
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 // Slowly charge the VSW voltage to prevent a brownout
// Workaround for hardware rev 1! // Workaround for hardware rev 1!
for(int i = 0; i < 100; i++){ for (int i = 0; i < 100; i++) {
digitalWrite(this->mEnablePin, HIGH); // LCD Logic off digitalWrite(this->mEnablePin, HIGH); // LCD Logic off
delayMicroseconds(1); delayMicroseconds(1);
digitalWrite(this->mEnablePin, LOW); // LCD Logic on digitalWrite(this->mEnablePin, LOW); // LCD Logic on
} }
setupTFT(); setupTFT();
setupTouchScreen(); setupTouchScreen();
mFadeTaskMutex = xSemaphoreCreateBinary(); mFadeTaskMutex = xSemaphoreCreateBinary();
xSemaphoreGive(mFadeTaskMutex); xSemaphoreGive(mFadeTaskMutex);
} }
void Display::setupBacklight() { void Display::setupBacklight() {
@ -62,7 +58,7 @@ void Display::setupBacklight() {
ledc_timer_config(&ledc_timer); ledc_timer_config(&ledc_timer);
} }
void Display::onTouch(Notification<TS_Point>::HandlerTy aTouchHandler){ void Display::onTouch(Notification<TS_Point>::HandlerTy aTouchHandler) {
mTouchEvent.onNotify(std::move(aTouchHandler)); mTouchEvent.onNotify(std::move(aTouchHandler));
} }
@ -75,25 +71,22 @@ void Display::setupTFT() {
tft.setSwapBytes(true); tft.setSwapBytes(true);
} }
void Display::setupTouchScreen(){ void Display::setupTouchScreen() {
// Configure i2c pins and set frequency to 400kHz // Configure i2c pins and set frequency to 400kHz
Wire.begin(SDA, SCL, 400000); Wire.begin(TFT_SDA, TFT_SCL, 400000);
touch.begin(128); // Initialize touchscreen and set sensitivity threshold touch.begin(128); // Initialize touchscreen and set sensitivity threshold
} }
void Display::setBrightness(uint8_t brightness) void Display::setBrightness(uint8_t brightness) {
{
mAwakeBrightness = brightness; mAwakeBrightness = brightness;
Serial.print("Set Brightness:"); Serial.print("Set Brightness:");
Serial.println(mAwakeBrightness); Serial.println(mAwakeBrightness);
startFade(); startFade();
} }
uint8_t Display::getBrightness(){ uint8_t Display::getBrightness() { return mAwakeBrightness; }
return mAwakeBrightness;
}
void Display::setCurrentBrightness(uint8_t brightness){ void Display::setCurrentBrightness(uint8_t brightness) {
mBrightness = brightness; mBrightness = brightness;
auto duty = static_cast<int>(mBrightness); auto duty = static_cast<int>(mBrightness);
ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, duty); ledcWrite(LCD_BACKLIGHT_LEDC_CHANNEL, duty);
@ -101,18 +94,17 @@ void Display::setCurrentBrightness(uint8_t brightness){
// Serial.println(mBrightness); // Serial.println(mBrightness);
} }
void Display::turnOff() void Display::turnOff() {
{ digitalWrite(this->mBacklightPin, HIGH);
digitalWrite(this->mBacklightPin, HIGH); digitalWrite(this->mEnablePin, HIGH);
digitalWrite(this->mEnablePin, HIGH); pinMode(this->mBacklightPin, INPUT);
pinMode(this->mBacklightPin, INPUT); pinMode(this->mEnablePin, INPUT);
pinMode(this->mEnablePin, INPUT); gpio_hold_en((gpio_num_t)mBacklightPin);
gpio_hold_en((gpio_num_t) mBacklightPin); gpio_hold_en((gpio_num_t)mEnablePin);
gpio_hold_en((gpio_num_t) mEnablePin);
} }
void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data){ void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
// int16_t touchX, touchY; // int16_t touchX, touchY;
touchPoint = touch.getPoint(); touchPoint = touch.getPoint();
int16_t touchX = touchPoint.x; int16_t touchX = touchPoint.x;
int16_t touchY = touchPoint.y; 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; bool fadeDone = false;
while(!fadeDone){ while (!fadeDone) {
fadeDone = getInstance()->fade(); fadeDone = getInstance()->fade();
vTaskDelay(3 / portTICK_PERIOD_MS); // 3 miliseconds between steps vTaskDelay(3 / portTICK_PERIOD_MS); // 3 miliseconds between steps
// 0 - 255 will take about .75 seconds to fade up. // 0 - 255 will take about .75 seconds to fade up.
} }
xSemaphoreTake(getInstance()->mFadeTaskMutex,portMAX_DELAY); xSemaphoreTake(getInstance()->mFadeTaskMutex, portMAX_DELAY);
getInstance()->mDisplayFadeTask = nullptr; getInstance()->mDisplayFadeTask = nullptr;
xSemaphoreGive(getInstance()->mFadeTaskMutex); xSemaphoreGive(getInstance()->mFadeTaskMutex);
vTaskDelete(nullptr); // Delete Fade Task vTaskDelete(nullptr); // Delete Fade Task
} }
bool Display::fade(){ bool Display::fade() {
//Early return no fade needed. // Early return no fade needed.
if (mBrightness == mAwakeBrightness || if (mBrightness == mAwakeBrightness || isAsleep && mBrightness == 0) {
isAsleep && mBrightness == 0){return true;} return true;
}
bool fadeDown = isAsleep || mBrightness > mAwakeBrightness;
if (fadeDown){ bool fadeDown = isAsleep || mBrightness > mAwakeBrightness;
if (fadeDown) {
setCurrentBrightness(mBrightness - 1); setCurrentBrightness(mBrightness - 1);
auto setPoint = isAsleep ? 0 : mAwakeBrightness; auto setPoint = isAsleep ? 0 : mAwakeBrightness;
return mBrightness == setPoint; return mBrightness == setPoint;
}else{ } else {
setCurrentBrightness(mBrightness + 1); setCurrentBrightness(mBrightness + 1);
return mBrightness == mAwakeBrightness; return mBrightness == mAwakeBrightness;
} }
} }
void Display::startFade(){ void Display::startFade() {
xSemaphoreTake(mFadeTaskMutex,portMAX_DELAY); xSemaphoreTake(mFadeTaskMutex, portMAX_DELAY);
// Only Create Task if it is needed // Only Create Task if it is needed
if(mDisplayFadeTask == nullptr){ if (mDisplayFadeTask == nullptr) {
xTaskCreate(&Display::fadeImpl, "Display Fade Task", xTaskCreate(&Display::fadeImpl, "Display Fade Task", 1024, nullptr, 5,
1024, nullptr, 5, &mDisplayFadeTask); &mDisplayFadeTask);
} }
xSemaphoreGive(mFadeTaskMutex); 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 w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1); uint32_t h = (area->y2 - area->y1 + 1);

View file

@ -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);
}
}
}

View file

@ -0,0 +1,37 @@
#pragma once
#include "KeyPressAbstract.hpp"
#include "omoteconfig.h"
#include <Keypad.h> // 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);
};

View file

@ -45,8 +45,8 @@
#define IR_VCC 25 // IR receiver power #define IR_VCC 25 // IR receiver power
#define IR_LED 33 // IR LED output #define IR_LED 33 // IR LED output
#define SCL 22 #define TFT_SCL 22
#define SDA 19 #define TFT_SDA 19
#define ACC_INT 20 #define ACC_INT 20
#define CRG_STAT 21 // battery charger feedback #define CRG_STAT 21 // battery charger feedback

View file

@ -111,6 +111,7 @@ build_flags =
-I HAL/Targets/ESP32/battery -I HAL/Targets/ESP32/battery
-I HAL/Targets/ESP32/display -I HAL/Targets/ESP32/display
-I HAL/Targets/ESP32/wifiHandler -I HAL/Targets/ESP32/wifiHandler
-I HAL/Targets/ESP32/keys
build_unflags = build_unflags =
-std=gnu++11 -std=gnu++11