From fe64176e7b8a0a7a9df733701d08762b60c2511f Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Thu, 15 Jul 2021 14:11:27 +0300 Subject: New touch handler, with issues --- src/main.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index ffbba5e7..f427db40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,7 @@ #include "drivers/TwiMaster.h" #include "drivers/Cst816s.h" #include "systemtask/SystemTask.h" +#include "touchhandler/TouchHandler.h" #if NRF_LOG_ENABLED #include "logging/NrfLogger.h" @@ -118,6 +119,7 @@ Pinetime::Drivers::WatchdogView watchdogView(watchdog); Pinetime::Controllers::NotificationManager notificationManager; Pinetime::Controllers::MotionController motionController; Pinetime::Controllers::TimerController timerController; +Pinetime::Controllers::TouchHandler touchHandler(touchPanel, lvgl); Pinetime::Controllers::FS fs {spiNorFlash}; Pinetime::Controllers::Settings settingsController {fs}; @@ -136,7 +138,8 @@ Pinetime::Applications::DisplayApp displayApp(lcd, settingsController, motorController, motionController, - timerController); + timerController, + touchHandler); Pinetime::System::SystemTask systemTask(spi, lcd, @@ -162,7 +165,7 @@ Pinetime::System::SystemTask systemTask(spi, void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { - systemTask.OnTouchEvent(); + touchHandler.WakeUp(); return; } @@ -309,6 +312,10 @@ int main(void) { lvgl.Init(); systemTask.Start(); + + touchHandler.Register(&systemTask); + touchHandler.Start(); + nimble_port_init(); vTaskStartScheduler(); -- cgit v1.2.3 From 93ccbf38e81b30165e49e897c01cac2eb54cc331 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Fri, 16 Jul 2021 01:49:20 +0300 Subject: Fix touch wakeup and code cleanup --- src/displayapp/LittleVgl.cpp | 21 --------------------- src/drivers/TwiMaster.cpp | 12 ++++++++---- src/drivers/TwiMaster.h | 3 ++- src/main.cpp | 4 +++- src/systemtask/SystemTask.cpp | 38 +++++++++++++++++++------------------- src/systemtask/SystemTask.h | 13 +++++++++---- src/touchhandler/TouchHandler.cpp | 5 ++--- 7 files changed, 43 insertions(+), 53 deletions(-) (limited to 'src/main.cpp') diff --git a/src/displayapp/LittleVgl.cpp b/src/displayapp/LittleVgl.cpp index b5669713..2bd5e57b 100644 --- a/src/displayapp/LittleVgl.cpp +++ b/src/displayapp/LittleVgl.cpp @@ -181,27 +181,6 @@ bool LittleVgl::GetTouchPadInfo(lv_indev_data_t* ptr) { ptr->state = LV_INDEV_STATE_REL; } return false; - /* - auto info = touchPanel.GetTouchInfo(); - - if((previousClick.x != info.x || previousClick.y != info.y) && - (info.gesture == Drivers::Cst816S::Gestures::SingleTap)) { - // TODO For an unknown reason, the first touch is taken twice into account. - // 'firstTouch' is a quite'n'dirty workaound until I find a better solution - if(firstTouch) ptr->state = LV_INDEV_STATE_REL; - else ptr->state = LV_INDEV_STATE_PR; - firstTouch = false; - previousClick.x = info.x; - previousClick.y = info.y; - } - else { - ptr->state = LV_INDEV_STATE_REL; - } - - ptr->point.x = info.x; - ptr->point.y = info.y; - return false; - */ } void LittleVgl::InitTheme() { diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index fc9edf81..456c3e64 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -12,9 +12,10 @@ TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {m } void TwiMaster::Init() { + sleeping = false; if(mutex == nullptr) mutex = xSemaphoreCreateBinary(); - + NRF_GPIO->PIN_CNF[params.pinScl] = ((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | ((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | @@ -176,11 +177,14 @@ void TwiMaster::Sleep() { nrf_gpio_cfg_default(6); nrf_gpio_cfg_default(7); NRF_LOG_INFO("[TWIMASTER] Sleep"); + sleeping = true; } void TwiMaster::Wakeup() { - Init(); - NRF_LOG_INFO("[TWIMASTER] Wakeup"); + if (sleeping) { + Init(); + NRF_LOG_INFO("[TWIMASTER] Wakeup"); + } } /* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX. @@ -206,4 +210,4 @@ void TwiMaster::FixHwFreezed() { // Re-enable I²C twiBaseAddress->ENABLE = twi_state; -} \ No newline at end of file +} diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index 6175b99b..5748ec65 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -39,6 +39,7 @@ namespace Pinetime { uint8_t internalBuffer[maxDataSize + registerSize]; uint32_t txStartedCycleCount = 0; static constexpr uint32_t HwFreezedDelay {161000}; + bool sleeping; }; } -} \ No newline at end of file +} diff --git a/src/main.cpp b/src/main.cpp index f427db40..b8d4b023 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -161,10 +161,12 @@ Pinetime::System::SystemTask systemTask(spi, heartRateController, displayApp, heartRateApp, - fs); + fs, + touchHandler); void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { + twiMaster.Wakeup(); touchHandler.WakeUp(); return; } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 7277fc93..93d19863 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -60,7 +60,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, Pinetime::Applications::HeartRateTask& heartRateApp, - Pinetime::Controllers::FS& fs) + Pinetime::Controllers::FS& fs, + Pinetime::Controllers::TouchHandler& touchHandler) : spi {spi}, lcd {lcd}, spiNorFlash {spiNorFlash}, @@ -72,18 +73,18 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, dateTimeController {dateTimeController}, timerController {timerController}, watchdog {watchdog}, - notificationManager{notificationManager}, + notificationManager {notificationManager}, motorController {motorController}, heartRateSensor {heartRateSensor}, motionSensor {motionSensor}, settingsController {settingsController}, - heartRateController{heartRateController}, - motionController{motionController}, - displayApp{displayApp}, + heartRateController {heartRateController}, + motionController {motionController}, + displayApp {displayApp}, heartRateApp(heartRateApp), - fs{fs}, + fs {fs}, + touchHandler {touchHandler}, nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) { - } void SystemTask::Start() { @@ -105,11 +106,11 @@ void SystemTask::Work() { APP_GPIOTE_INIT(2); app_timer_init(); - + spi.Init(); spiNorFlash.Init(); spiNorFlash.Wakeup(); - + fs.Init(); nimbleController.Init(); @@ -228,14 +229,14 @@ void SystemTask::Work() { isWakingUp = false; break; case Messages::TouchWakeUp: { - twiMaster.Wakeup(); - auto touchInfo = touchPanel.GetTouchInfo(); - twiMaster.Sleep(); - if (touchInfo.isValid and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and - settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or - (touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and - settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)))) { + auto gesture = touchHandler.GestureGet(); + if ((gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap && + settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) || + (gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap && + settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap))) { GoToRunning(); + } else { + twiMaster.Sleep(); } } break; case Messages::GoToSleep: @@ -311,7 +312,7 @@ void SystemTask::Work() { break; case Messages::OnChargingEvent: motorController.SetDuration(15); - // Battery level is updated on every message - there's no need to do anything + // Battery level is updated on every message - there's no need to do anything break; default: @@ -410,14 +411,13 @@ void SystemTask::PushMessage(System::Messages msg) { isGoingToSleep = true; } - if(in_isr()) { + if (in_isr()) { BaseType_t xHigherPriorityTaskWoken; xHigherPriorityTaskWoken = pdFALSE; xQueueSendFromISR(systemTasksMsgQueue, &msg, &xHigherPriorityTaskWoken); if (xHigherPriorityTaskWoken) { /* Actual macro used here is port specific. */ portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - } } else { xQueueSend(systemTasksMsgQueue, &msg, portMAX_DELAY); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index f8cf6370..a7ee73ad 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -17,6 +17,7 @@ #include "components/motor/MotorController.h" #include "components/timer/TimerController.h" #include "components/fs/FS.h" +#include "touchhandler/TouchHandler.h" #ifdef PINETIME_IS_RECOVERY #include "displayapp/DisplayAppRecovery.h" @@ -24,7 +25,7 @@ #else #include "components/settings/Settings.h" #include "displayapp/DisplayApp.h" - #include "displayapp/LittleVgl.h" + #include "displayapp/LittleVgl.h" #endif #include "drivers/Watchdog.h" @@ -39,6 +40,9 @@ namespace Pinetime { class TwiMaster; class Hrs3300; } + namespace Controllers { + class TouchHandler; + } namespace System { class SystemTask { public: @@ -62,7 +66,8 @@ namespace Pinetime { Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, Pinetime::Applications::HeartRateTask& heartRateApp, - Pinetime::Controllers::FS& fs); + Pinetime::Controllers::FS& fs, + Pinetime::Controllers::TouchHandler& touchHandler); void Start(); void PushMessage(Messages msg); @@ -91,7 +96,6 @@ namespace Pinetime { Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; - Pinetime::Controllers::Ble& bleController; Pinetime::Controllers::DateTime& dateTimeController; Pinetime::Controllers::TimerController& timerController; @@ -106,13 +110,14 @@ namespace Pinetime { Pinetime::Drivers::Bma421& motionSensor; Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::HeartRateController& heartRateController; - + Controllers::BrightnessController brightnessController; Pinetime::Controllers::MotionController& motionController; Pinetime::Applications::DisplayApp& displayApp; Pinetime::Applications::HeartRateTask& heartRateApp; Pinetime::Controllers::FS& fs; + Pinetime::Controllers::TouchHandler& touchHandler; Pinetime::Controllers::NimbleController nimbleController; static constexpr uint8_t pinSpiSck = 2; diff --git a/src/touchhandler/TouchHandler.cpp b/src/touchhandler/TouchHandler.cpp index c6f8629f..86dc29f6 100644 --- a/src/touchhandler/TouchHandler.cpp +++ b/src/touchhandler/TouchHandler.cpp @@ -2,9 +2,7 @@ using namespace Pinetime::Controllers; -TouchHandler::TouchHandler(Drivers::Cst816S& touchPanel, Components::LittleVgl& lvgl) - : touchPanel {touchPanel}, - lvgl {lvgl} { +TouchHandler::TouchHandler(Drivers::Cst816S& touchPanel, Components::LittleVgl& lvgl) : touchPanel {touchPanel}, lvgl {lvgl} { } void TouchHandler::CancelTap() { @@ -35,6 +33,7 @@ void TouchHandler::Work() { vTaskSuspend(taskHandle); info = touchPanel.GetTouchInfo(); if (systemTask->IsSleeping()) { + gesture = info.gesture; systemTask->PushMessage(System::Messages::TouchWakeUp); } else { x = info.x; -- cgit v1.2.3 From 3e42297bd86fa20daaf7f59f6bc0a69de6cf9e53 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Sun, 18 Jul 2021 12:32:46 +0300 Subject: Update --- src/drivers/Cst816s.h | 2 +- src/main.cpp | 3 --- src/systemtask/SystemTask.cpp | 3 +++ src/touchhandler/TouchHandler.cpp | 44 ++++++++++++++++++++------------------- 4 files changed, 27 insertions(+), 25 deletions(-) (limited to 'src/main.cpp') diff --git a/src/drivers/Cst816s.h b/src/drivers/Cst816s.h index 26bdf4e0..d4c17bb8 100644 --- a/src/drivers/Cst816s.h +++ b/src/drivers/Cst816s.h @@ -21,7 +21,7 @@ namespace Pinetime { uint16_t y = 0; Gestures gesture = Gestures::None; bool touching = false; - bool isValid = false; + bool isValid = true; }; Cst816S(TwiMaster& twiMaster, uint8_t twiAddress); diff --git a/src/main.cpp b/src/main.cpp index b8d4b023..62e4446f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -315,9 +315,6 @@ int main(void) { systemTask.Start(); - touchHandler.Register(&systemTask); - touchHandler.Start(); - nimble_port_init(); vTaskStartScheduler(); diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 93d19863..252d3cc3 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -144,6 +144,9 @@ void SystemTask::Work() { heartRateSensor.Disable(); heartRateApp.Start(); + touchHandler.Register(this); + touchHandler.Start(); + nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); nrf_gpio_pin_set(15); diff --git a/src/touchhandler/TouchHandler.cpp b/src/touchhandler/TouchHandler.cpp index 0d0b8273..187aa696 100644 --- a/src/touchhandler/TouchHandler.cpp +++ b/src/touchhandler/TouchHandler.cpp @@ -30,37 +30,39 @@ void TouchHandler::Process(void* instance) { } void TouchHandler::Work() { - Pinetime::Drivers::Cst816S::Gestures prevGesture = Pinetime::Drivers::Cst816S::Gestures::None; + bool slideReleased = true; while (true) { vTaskSuspend(taskHandle); info = touchPanel.GetTouchInfo(); - if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) { - if (prevGesture != info.gesture) { - if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown || info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideLeft || - info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideUp || info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideRight) { - prevGesture = info.gesture; + if (info.isValid) { + if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) { + if (slideReleased) { + if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideLeft || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideUp || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideRight) { + slideReleased = false; + } + gesture = info.gesture; } - gesture = info.gesture; } - } - if (systemTask->IsSleeping()) { - systemTask->PushMessage(System::Messages::TouchWakeUp); - } else { - if (info.touching) { - if (!isCancelled) { - lvgl.SetNewTouchPoint(info.x, info.y, true); - } - } else { - if (isCancelled) { - lvgl.SetNewTouchPoint(-1, -1, false); - isCancelled = false; + if (!systemTask->IsSleeping()) { + if (info.touching) { + if (!isCancelled) { + lvgl.SetNewTouchPoint(info.x, info.y, true); + } } else { - lvgl.SetNewTouchPoint(info.x, info.y, false); + if (isCancelled) { + lvgl.SetNewTouchPoint(-1, -1, false); + isCancelled = false; + } else { + lvgl.SetNewTouchPoint(info.x, info.y, false); + } + slideReleased = true; } - prevGesture = Pinetime::Drivers::Cst816S::Gestures::None; } systemTask->OnTouchEvent(); } -- cgit v1.2.3 From d35a54c060700e537d3e39723b7ee9b1d71b36d8 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 9 Aug 2021 21:49:35 +0300 Subject: Try to fix bootloop --- src/main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index ffbba5e7..a18c3942 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include // nimble #define min // workaround: nimble's min/max macros conflict with libstdc++ @@ -300,6 +301,14 @@ int main(void) { nrf_drv_clock_init(); + // Unblock i2c? + nrf_gpio_cfg_output(pinTwiScl); + for (uint8_t i = 0; i < 16; i++) { + nrf_gpio_pin_toggle(pinTwiScl); + nrf_delay_us(5); + } + nrf_gpio_cfg_default(pinTwiScl); + debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback); debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback); -- cgit v1.2.3 From ff81a72533e479388d49334d0ec3e0635b6e7f40 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 9 Aug 2021 22:07:06 +0300 Subject: Better pin configuration --- src/main.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index a18c3942..39d8906c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -302,7 +302,12 @@ int main(void) { nrf_drv_clock_init(); // Unblock i2c? - nrf_gpio_cfg_output(pinTwiScl); + nrf_gpio_cfg(pinTwiScl, + NRF_GPIO_PIN_DIR_OUTPUT, + NRF_GPIO_PIN_INPUT_DISCONNECT, + NRF_GPIO_PIN_NOPULL, + NRF_GPIO_PIN_S0D1, + NRF_GPIO_PIN_NOSENSE); for (uint8_t i = 0; i < 16; i++) { nrf_gpio_pin_toggle(pinTwiScl); nrf_delay_us(5); -- cgit v1.2.3 From 5eb56d9a06652cb10358ecba051bbfe8ec2957a7 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 9 Aug 2021 22:11:35 +0300 Subject: Set pin before loop --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 39d8906c..d301be67 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -308,6 +308,7 @@ int main(void) { NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0D1, NRF_GPIO_PIN_NOSENSE); + nrf_gpio_pin_set(pinTwiScl); for (uint8_t i = 0; i < 16; i++) { nrf_gpio_pin_toggle(pinTwiScl); nrf_delay_us(5); -- cgit v1.2.3 From 7e92577c14895f57f5adda27ad54adbbc4b7ffe9 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Tue, 10 Aug 2021 21:02:51 +0300 Subject: Revert "Merge branch 'unblock_i2c' into develop" This reverts commit 275a84b3238874d213271f4287e6c1c5bfcb4353, reversing changes made to 9fb37550886f09f6510e99a5b452262c53c3987c. --- src/main.cpp | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 9a5b0cfe..62e4446f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include // nimble #define min // workaround: nimble's min/max macros conflict with libstdc++ @@ -306,20 +305,6 @@ int main(void) { nrf_drv_clock_init(); - // Unblock i2c? - nrf_gpio_cfg(pinTwiScl, - NRF_GPIO_PIN_DIR_OUTPUT, - NRF_GPIO_PIN_INPUT_DISCONNECT, - NRF_GPIO_PIN_NOPULL, - NRF_GPIO_PIN_S0D1, - NRF_GPIO_PIN_NOSENSE); - nrf_gpio_pin_set(pinTwiScl); - for (uint8_t i = 0; i < 16; i++) { - nrf_gpio_pin_toggle(pinTwiScl); - nrf_delay_us(5); - } - nrf_gpio_cfg_default(pinTwiScl); - debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback); debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback); -- cgit v1.2.3 From 8a694adb0979339664da0af6d51c480d26c5527b Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Tue, 10 Aug 2021 22:03:34 +0300 Subject: Rework TouchHandler into not a task --- src/drivers/TwiMaster.cpp | 12 ++---- src/drivers/TwiMaster.h | 3 +- src/main.cpp | 3 +- src/systemtask/SystemTask.cpp | 8 ++-- src/touchhandler/TouchHandler.cpp | 80 ++++++++++++++++----------------------- src/touchhandler/TouchHandler.h | 9 ++--- 6 files changed, 47 insertions(+), 68 deletions(-) (limited to 'src/main.cpp') diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index 456c3e64..fc9edf81 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -12,10 +12,9 @@ TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {m } void TwiMaster::Init() { - sleeping = false; if(mutex == nullptr) mutex = xSemaphoreCreateBinary(); - + NRF_GPIO->PIN_CNF[params.pinScl] = ((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | ((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | @@ -177,14 +176,11 @@ void TwiMaster::Sleep() { nrf_gpio_cfg_default(6); nrf_gpio_cfg_default(7); NRF_LOG_INFO("[TWIMASTER] Sleep"); - sleeping = true; } void TwiMaster::Wakeup() { - if (sleeping) { - Init(); - NRF_LOG_INFO("[TWIMASTER] Wakeup"); - } + Init(); + NRF_LOG_INFO("[TWIMASTER] Wakeup"); } /* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX. @@ -210,4 +206,4 @@ void TwiMaster::FixHwFreezed() { // Re-enable I²C twiBaseAddress->ENABLE = twi_state; -} +} \ No newline at end of file diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index 5748ec65..6175b99b 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -39,7 +39,6 @@ namespace Pinetime { uint8_t internalBuffer[maxDataSize + registerSize]; uint32_t txStartedCycleCount = 0; static constexpr uint32_t HwFreezedDelay {161000}; - bool sleeping; }; } -} +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 62e4446f..dffec28a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -166,8 +166,7 @@ Pinetime::System::SystemTask systemTask(spi, void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == pinTouchIrq) { - twiMaster.Wakeup(); - touchHandler.WakeUp(); + systemTask.OnTouchEvent(); return; } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 7ba7cf69..5f363130 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -150,9 +150,6 @@ void SystemTask::Work() { heartRateSensor.Disable(); heartRateApp.Start(); - touchHandler.Register(this); - touchHandler.Start(); - nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); nrf_gpio_pin_set(15); @@ -244,6 +241,8 @@ void SystemTask::Work() { isDimmed = false; break; case Messages::TouchWakeUp: { + twiMaster.Wakeup(); + touchHandler.GetNewTouchInfo(); auto gesture = touchHandler.GestureGet(); if ((gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap && settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) || @@ -299,6 +298,9 @@ void SystemTask::Work() { xTimerStart(dimTimer, 0); break; case Messages::OnTouchEvent: + if (touchHandler.GetNewTouchInfo()) { + touchHandler.UpdateLvglTouchPoint(); + } ReloadIdleTimer(); break; case Messages::OnButtonEvent: diff --git a/src/touchhandler/TouchHandler.cpp b/src/touchhandler/TouchHandler.cpp index 187aa696..c1a3c0d0 100644 --- a/src/touchhandler/TouchHandler.cpp +++ b/src/touchhandler/TouchHandler.cpp @@ -18,61 +18,47 @@ Pinetime::Drivers::Cst816S::Gestures TouchHandler::GestureGet() { return returnGesture; } -void TouchHandler::Start() { - if (pdPASS != xTaskCreate(TouchHandler::Process, "Touch", 100, this, 0, &taskHandle)) { - APP_ERROR_HANDLER(NRF_ERROR_NO_MEM); - } -} - -void TouchHandler::Process(void* instance) { - auto* app = static_cast(instance); - app->Work(); -} - -void TouchHandler::Work() { - bool slideReleased = true; - while (true) { - vTaskSuspend(taskHandle); +bool TouchHandler::GetNewTouchInfo() { + info = touchPanel.GetTouchInfo(); - info = touchPanel.GetTouchInfo(); - - if (info.isValid) { - if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) { - if (slideReleased) { - if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown || - info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideLeft || - info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideUp || - info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideRight) { - slideReleased = false; - } - gesture = info.gesture; - } - } + if (!info.isValid) { + return false; + } - if (!systemTask->IsSleeping()) { + if (info.gesture != Pinetime::Drivers::Cst816S::Gestures::None) { + if (slideReleased) { + if (info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideDown || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideLeft || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideUp || + info.gesture == Pinetime::Drivers::Cst816S::Gestures::SlideRight) { if (info.touching) { - if (!isCancelled) { - lvgl.SetNewTouchPoint(info.x, info.y, true); - } - } else { - if (isCancelled) { - lvgl.SetNewTouchPoint(-1, -1, false); - isCancelled = false; - } else { - lvgl.SetNewTouchPoint(info.x, info.y, false); - } - slideReleased = true; + gesture = info.gesture; + slideReleased = false; } + } else { + gesture = info.gesture; } - systemTask->OnTouchEvent(); } } -} -void TouchHandler::Register(Pinetime::System::SystemTask* systemTask) { - this->systemTask = systemTask; + if (!info.touching) { + slideReleased = true; + } + + return true; } -void TouchHandler::WakeUp() { - vTaskResume(taskHandle); +void TouchHandler::UpdateLvglTouchPoint() { + if (info.touching) { + if (!isCancelled) { + lvgl.SetNewTouchPoint(info.x, info.y, true); + } + } else { + if (isCancelled) { + lvgl.SetNewTouchPoint(-1, -1, false); + isCancelled = false; + } else { + lvgl.SetNewTouchPoint(info.x, info.y, false); + } + } } diff --git a/src/touchhandler/TouchHandler.h b/src/touchhandler/TouchHandler.h index 6b8189f5..38cb36cb 100644 --- a/src/touchhandler/TouchHandler.h +++ b/src/touchhandler/TouchHandler.h @@ -19,9 +19,9 @@ namespace Pinetime { public: explicit TouchHandler(Drivers::Cst816S&, Components::LittleVgl&); void CancelTap(); + bool GetNewTouchInfo(); + void UpdateLvglTouchPoint(); void Register(Pinetime::System::SystemTask* systemTask); - void Start(); - void WakeUp(); bool IsTouching() const { return info.touching; @@ -34,16 +34,13 @@ namespace Pinetime { } Drivers::Cst816S::Gestures GestureGet(); private: - static void Process(void* instance); - void Work(); Pinetime::Drivers::Cst816S::TouchInfos info; - Pinetime::System::SystemTask* systemTask = nullptr; - TaskHandle_t taskHandle; Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Drivers::Cst816S::Gestures gesture; bool isCancelled = false; + bool slideReleased = true; }; } } -- cgit v1.2.3 From 81a36dc31ed22237e3cc06c8f4ba2a5cbcf07f8e Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 16 Aug 2021 18:26:10 +0300 Subject: Simplify parameters and cleanup --- src/drivers/TwiMaster.cpp | 43 ++++++++++--------------------------------- src/drivers/TwiMaster.h | 19 +++++++------------ src/main.cpp | 5 ++--- 3 files changed, 19 insertions(+), 48 deletions(-) (limited to 'src/main.cpp') diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index 429a6eb7..f17d7168 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -8,19 +8,20 @@ using namespace Pinetime::Drivers; // TODO use shortcut to automatically send STOP when receive LastTX, for example // TODO use DMA/IRQ -TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {module}, params {params} { +TwiMaster::TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl) + : module {module}, frequency {frequency}, pinSda {pinSda}, pinScl {pinScl} { mutex = xSemaphoreCreateBinary(); } void TwiMaster::ConfigurePins() const { - NRF_GPIO->PIN_CNF[params.pinScl] = + NRF_GPIO->PIN_CNF[pinScl] = (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - NRF_GPIO->PIN_CNF[params.pinSda] = + NRF_GPIO->PIN_CNF[pinSda] = (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | @@ -31,28 +32,12 @@ void TwiMaster::ConfigurePins() const { void TwiMaster::Init() { ConfigurePins(); - switch (module) { - case Modules::TWIM1: - twiBaseAddress = NRF_TWIM1; - break; - default: - return; - } + twiBaseAddress = module; - switch (static_cast(params.frequency)) { - case Frequencies::Khz100: - twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100; - break; - case Frequencies::Khz250: - twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250; - break; - case Frequencies::Khz400: - twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400; - break; - } + twiBaseAddress->FREQUENCY = frequency; - twiBaseAddress->PSEL.SCL = params.pinScl; - twiBaseAddress->PSEL.SDA = params.pinSda; + twiBaseAddress->PSEL.SCL = pinScl; + twiBaseAddress->PSEL.SDA = pinSda; twiBaseAddress->EVENTS_LASTRX = 0; twiBaseAddress->EVENTS_STOPPED = 0; twiBaseAddress->EVENTS_LASTTX = 0; @@ -63,12 +48,6 @@ void TwiMaster::Init() { twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos); - /* // IRQ - NVIC_ClearPendingIRQ(_IRQn); - NVIC_SetPriority(_IRQn, 2); - NVIC_EnableIRQ(_IRQn); - */ - xSemaphoreGive(mutex); } @@ -194,12 +173,10 @@ void TwiMaster::Wakeup() { * */ void TwiMaster::FixHwFreezed() { NRF_LOG_INFO("I2C device frozen, reinitializing it!"); - // Disable I²C + uint32_t twi_state = NRF_TWI1->ENABLE; - twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; - ConfigurePins(); + Sleep(); - // Re-enable I²C twiBaseAddress->ENABLE = twi_state; } diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index b8d36d59..30ac6c5f 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -8,16 +8,9 @@ namespace Pinetime { namespace Drivers { class TwiMaster { public: - enum class Modules { TWIM1 }; - enum class Frequencies { Khz100, Khz250, Khz400 }; enum class ErrorCodes { NoError, TransactionFailed }; - struct Parameters { - uint32_t frequency; - uint8_t pinSda; - uint8_t pinScl; - }; - TwiMaster(const Modules module, const Parameters& params); + TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl); void Init(); ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size); @@ -26,16 +19,18 @@ namespace Pinetime { void Sleep(); void Wakeup(); - void ConfigurePins() const; - private: ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop); void FixHwFreezed(); + void ConfigurePins() const; + NRF_TWIM_Type* twiBaseAddress; SemaphoreHandle_t mutex = nullptr; - const Modules module; - const Parameters params; + NRF_TWIM_Type* module; + uint32_t frequency; + uint8_t pinSda; + uint8_t pinScl; static constexpr uint8_t maxDataSize {16}; static constexpr uint8_t registerSize {1}; uint8_t internalBuffer[maxDataSize + registerSize]; diff --git a/src/main.cpp b/src/main.cpp index ffbba5e7..4e94ab19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,9 +81,8 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; // The TWI device should work @ up to 400Khz but there is a HW bug which prevent it from // respecting correct timings. According to erratas heet, this magic value makes it run // at ~390Khz with correct timings. -static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000}; -Pinetime::Drivers::TwiMaster twiMaster {Pinetime::Drivers::TwiMaster::Modules::TWIM1, - Pinetime::Drivers::TwiMaster::Parameters {MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl}}; +//static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000}; +Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, TWI_FREQUENCY_FREQUENCY_K250, pinTwiSda, pinTwiScl}; Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress}; #ifdef PINETIME_IS_RECOVERY static constexpr bool isFactory = true; -- cgit v1.2.3 From 40392d7b9199c87de9784cacb1dee5476e5a5b6f Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Wed, 18 Aug 2021 21:40:27 +0300 Subject: Use highest frequency and move mutex creation to Init --- src/drivers/TwiMaster.cpp | 5 ++++- src/main.cpp | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'src/main.cpp') diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp index f17d7168..76009278 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/TwiMaster.cpp @@ -10,7 +10,6 @@ using namespace Pinetime::Drivers; TwiMaster::TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl) : module {module}, frequency {frequency}, pinSda {pinSda}, pinScl {pinScl} { - mutex = xSemaphoreCreateBinary(); } void TwiMaster::ConfigurePins() const { @@ -30,6 +29,10 @@ void TwiMaster::ConfigurePins() const { } void TwiMaster::Init() { + if (mutex == nullptr) { + mutex = xSemaphoreCreateBinary(); + } + ConfigurePins(); twiBaseAddress = module; diff --git a/src/main.cpp b/src/main.cpp index 4e94ab19..524035fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,8 +81,8 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; // The TWI device should work @ up to 400Khz but there is a HW bug which prevent it from // respecting correct timings. According to erratas heet, this magic value makes it run // at ~390Khz with correct timings. -//static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000}; -Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, TWI_FREQUENCY_FREQUENCY_K250, pinTwiSda, pinTwiScl}; +static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000}; +Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl}; Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress}; #ifdef PINETIME_IS_RECOVERY static constexpr bool isFactory = true; -- cgit v1.2.3