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
@ -286,6 +286,7 @@ 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);
} }
@ -300,4 +301,36 @@ void initKeys(CommonData &commonData) {
} }
#endif #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);
}
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;
@ -720,15 +691,15 @@ void OBP60Task(GwApi *api){
// 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);
// we start a separate task to collect sensor data
SharedData *shared = new SharedData(api); SharedData *shared = new SharedData(api);
createSensorTask(shared); createSensorTask(shared);
@ -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;