Code cleanup for keyboard task

This commit is contained in:
Thomas Hooge 2025-08-25 20:47:42 +02:00
parent c4406fd009
commit d7251eeb8a
4 changed files with 118 additions and 98 deletions

View File

@ -81,7 +81,7 @@ void hardwareInit(GwApi *api)
Wire.begin(); Wire.begin();
// Init PCF8574 digital outputs // Init PCF8574 digital outputs
Wire.setClock(I2C_SPEED); // Set I2C clock on 10 kHz Wire.setClock(I2C_SPEED); // Set I2C clock as defined
if(pcf8574_Out.begin()){ // Initialize PCF8574 if(pcf8574_Out.begin()){ // Initialize PCF8574
pcf8574_Out.write8(255); // Clear all outputs pcf8574_Out.write8(255); // Clear all outputs
} }

View File

@ -1,8 +1,8 @@
#ifndef _OBP60FUNCTIONS_H // SPDX-License-Identifier: GPL-2.0-or-later
#define _OBP60FUNCTIONS_H #if defined BOARD_OBP60S3 || defined BOARD_OBP40S3
#include <Arduino.h> #include <Arduino.h>
#include "OBP60Hardware.h" #include "OBP60Hardware.h"
#include "OBPKeyboardTask.h"
// Global vars // Global vars
@ -58,9 +58,9 @@ void initKeys(CommonData &commonData) {
commonData.keydata[5].h = height; commonData.keydata[5].h = height;
} }
#ifdef HARDWARE_V21 #ifdef HARDWARE_V21
// Keypad functions for original OBP60 hardware // Keypad functions for original OBP60 hardware
int readKeypad(GwLog* logger, uint thSensitivity, bool use_syspage) { int readKeypad(GwLog* logger, uint thSensitivity, bool use_syspage) {
// Touch sensor values // Touch sensor values
// 35000 - Not touched // 35000 - Not touched
@ -233,35 +233,35 @@ void initKeys(CommonData &commonData) {
keycodeold2 = keycode2; keycodeold2 = keycode2;
return keystatus; return keystatus;
} }
#endif #endif
#ifdef BOARD_OBP40S3 #ifdef BOARD_OBP40S3
int readSensorpads(){ int readSensorpads(){
// Read key code // Read key code
if(digitalRead(UP) == LOW){ if (digitalRead(UP) == LOW) {
keycode = 10; // Left swipe keycode = 10; // Left swipe
} }
else if(digitalRead(DOWN) == LOW){ else if (digitalRead(DOWN) == LOW) {
keycode = 9; // Right swipe keycode = 9; // Right swipe
} }
else if(digitalRead(CONF) == LOW){ else if (digitalRead(CONF) == LOW) {
keycode = 3; // Key 3 keycode = 3; // Key 3
} }
else if(digitalRead(MENUE) == LOW){ else if (digitalRead(MENUE) == LOW) {
keycode = 1; // Key 1 keycode = 1; // Key 1
} }
else if(digitalRead(EXIT) == LOW){ else if (digitalRead(EXIT) == LOW) {
keycode = 2; // Key 2 keycode = 2; // Key 2
} }
else{ else {
keycode = 0; // No key activ keycode = 0; // No key activ
} }
return keycode; return keycode;
} }
// Keypad functions for OBP60 clone (thSensitivity is inactiv) // Keypad functions for OBP60 clone (thSensitivity is inactiv)
int readKeypad(GwLog* logger, uint thSensitivity, bool use_syspage) { int readKeypad(GwLog* logger, uint thSensitivity, bool use_syspage) {
pinMode(UP, INPUT); pinMode(UP, INPUT);
pinMode(DOWN, INPUT); pinMode(DOWN, INPUT);
pinMode(CONF, INPUT); pinMode(CONF, INPUT);
@ -286,18 +286,51 @@ void initKeys(CommonData &commonData) {
} }
// Copy keycode // Copy keycode
keycodeold = keycode; keycodeold = keycode;
// 100% Task-CPU RLY?
while(readSensorpads() > 0){} // Wait for pad release while(readSensorpads() > 0){} // Wait for pad release
delay(keydelay); delay(keydelay);
} }
} }
else{ else {
keycode = 0; keycode = 0;
keycodeold = 0; keycodeold = 0;
keystatus = 0; keystatus = 0;
} }
return keystatus; return keystatus;
}
#endif
void keyboardTask(void *param) {
// params needed:
// queue
// logger
// sensitivity
// use_syspage for deep sleep activation
KbTaskData *data = (KbTaskData *)param;
int keycode = 0;
data->logger->logDebug(GwLog::LOG, "Start keyboard task");
while (true) {
keycode = readKeypad(data->logger, data->sensitivity, data->use_syspage);
//send a key event
if (keycode != 0) {
xQueueSend(data->queue, &keycode, 0);
data->logger->logDebug(GwLog::LOG,"kbtask: send keycode: %d", keycode);
} }
#endif delay(20); // 50Hz update rate (20ms)
}
vTaskDelete(NULL);
}
void createKeyboardTask(KbTaskData *param) {
TaskHandle_t xHandle = NULL;
if (xTaskCreate(keyboardTask, "keyboard", configMINIMAL_STACK_SIZE + 1024, param, configMAX_PRIORITIES-1, &xHandle) != pdPASS) {
param->logger->logDebug(GwLog::ERROR, "Failed to create keyboard task!");
};
}
#endif #endif

View File

@ -0,0 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "GwLog.h"
#include "Pagedata.h"
typedef struct {
QueueHandle_t queue;
GwLog* logger = nullptr;
uint sensitivity = 100;
#ifdef BOARD_OBP40S3
bool use_syspage = true;
#endif
} KbTaskData;
void initKeys(CommonData &commonData);
void createKeyboardTask(KbTaskData *param);

View File

@ -13,10 +13,12 @@
#include <NMEA0183Messages.h> #include <NMEA0183Messages.h>
#include <GxEPD2_BW.h> // GxEPD2 lib for b/w E-Ink displays #include <GxEPD2_BW.h> // GxEPD2 lib for b/w E-Ink displays
#include "OBP60Extensions.h" // Functions lib for extension board #include "OBP60Extensions.h" // Functions lib for extension board
#include "OBP60Keypad.h" // Functions for keypad #include "OBPKeyboardTask.h" // Functions lib for keyboard handling
#include "BoatDataCalibration.h" // Functions lib for data instance calibration #include "BoatDataCalibration.h" // Functions lib for data instance calibration
#include "OBPRingBuffer.h" // Functions lib with ring buffer for history storage of some boat data #include "OBPRingBuffer.h" // Functions lib with ring buffer for history storage of some boat data
#include "OBPDataOperations.h" // Functions lib for data operations such as true wind calculation #include "OBPDataOperations.h" // Functions lib for data operations such as true wind calculation
#include "OBP60QRWiFi.h" // Functions lib for WiFi QR code
#include "OBPSensorTask.h" // Functions lib for sensor data
#ifdef BOARD_OBP40S3 #ifdef BOARD_OBP40S3
#include "driver/rtc_io.h" // Needs for weakup from deep sleep #include "driver/rtc_io.h" // Needs for weakup from deep sleep
@ -26,8 +28,6 @@
// Pictures // Pictures
#include "images/OBP_400x300.xbm" // OBP Logo #include "images/OBP_400x300.xbm" // OBP Logo
#include "images/unknown.xbm" // unknown page indicator #include "images/unknown.xbm" // unknown page indicator
#include "OBP60QRWiFi.h" // Functions lib for WiFi QR code
#include "OBPSensorTask.h" // Functions lib for sensor data
// Global vars // Global vars
bool initComplete = false; // Initialization complete bool initComplete = false; // Initialization complete
@ -119,35 +119,6 @@ void OBP60Init(GwApi *api){
} }
typedef struct {
int page0 = 0;
QueueHandle_t queue;
GwLog* logger = nullptr;
// GwApi* api = NULL;
uint sensitivity = 100;
bool use_syspage = true;
} MyData;
// Keyboard Task
void keyboardTask(void *param){
MyData *data=(MyData *)param;
int keycode = 0;
data->logger->logDebug(GwLog::LOG,"Start keyboard task");
// Loop for keyboard task
while (true){
keycode = readKeypad(data->logger, data->sensitivity, data->use_syspage);
//send a key event
if(keycode != 0){
xQueueSend(data->queue, &keycode, 0);
data->logger->logDebug(GwLog::LOG,"Send keycode: %d", keycode);
}
delay(20); // 50Hz update rate (20ms)
}
vTaskDelete(NULL);
}
class BoatValueList{ class BoatValueList{
public: public:
static const int MAXVALUES=100; static const int MAXVALUES=100;
@ -718,18 +689,18 @@ void OBP60Task(GwApi *api){
doImageRequest(api, &pageNumber, pages, request); doImageRequest(api, &pageNumber, pages, request);
}); });
//now we have prepared the page data // now we have prepared the page data
//we start a separate task that will fetch our keys... // we start a separate task that will fetch our keys...
MyData allParameters; KbTaskData kbparams;
allParameters.logger=api->getLogger(); kbparams.logger = api->getLogger();
allParameters.page0=3; kbparams.queue = xQueueCreate(10, sizeof(int));
allParameters.queue=xQueueCreate(10,sizeof(int)); kbparams.sensitivity = api->getConfig()->getInt(GwConfigDefinitions::tSensitivity);
allParameters.sensitivity= api->getConfig()->getInt(GwConfigDefinitions::tSensitivity);
#ifdef BOARD_OBP40S3 #ifdef BOARD_OBP40S3
allParameters.use_syspage = syspage_enabled; kbparams.use_syspage = syspage_enabled;
#endif #endif
xTaskCreate(keyboardTask,"keyboard",2000,&allParameters,configMAX_PRIORITIES-1,NULL); createKeyboardTask(&kbparams);
SharedData *shared=new SharedData(api); // we start a separate task to collect sensor data
SharedData *shared = new SharedData(api);
createSensorTask(shared); createSensorTask(shared);
// Task Loop // Task Loop
@ -836,7 +807,7 @@ void OBP60Task(GwApi *api){
// Check the keyboard message // Check the keyboard message
int keyboardMessage=0; int keyboardMessage=0;
while (xQueueReceive(allParameters.queue,&keyboardMessage,0)){ while (xQueueReceive(kbparams.queue,&keyboardMessage, 0)) {
LOG_DEBUG(GwLog::LOG,"new key from keyboard %d",keyboardMessage); LOG_DEBUG(GwLog::LOG,"new key from keyboard %d",keyboardMessage);
keypressed = true; keypressed = true;