Add the possibility to the screen to handle a touch gesture.

A default action is taken if the current screen doesn't handle it.
This commit is contained in:
JF 2020-03-15 21:01:24 +01:00
parent 2c55ab20b4
commit 8ed6ffaaf8
7 changed files with 61 additions and 25 deletions

View File

@ -242,6 +242,7 @@ set(INCLUDE_FILES
Logging/NrfLogger.h Logging/NrfLogger.h
BlinkApp/BlinkApp.h BlinkApp/BlinkApp.h
DisplayApp/DisplayApp.h DisplayApp/DisplayApp.h
DisplayApp/TouchEvents.h
DisplayApp/Screens/Screen.h DisplayApp/Screens/Screen.h
DisplayApp/Screens/Clock.h DisplayApp/Screens/Clock.h
DisplayApp/Screens/Message.h DisplayApp/Screens/Message.h
@ -288,4 +289,4 @@ link_directories(
../ ../
) )
nRF5x_addExecutable(pinetime-app "${SOURCE_FILES}") nRF5x_addExecutable(pinetime-app "${SOURCE_FILES}" ${INCLUDE_FILES})

View File

@ -110,12 +110,13 @@ void DisplayApp::Refresh() {
case Messages::TouchEvent: { case Messages::TouchEvent: {
if (state != States::Running) break; if (state != States::Running) break;
auto gesture = OnTouchEvent(); auto gesture = OnTouchEvent();
if(!currentScreen->OnTouchEvent(gesture)) {
switch (gesture) { switch (gesture) {
case DisplayApp::TouchEvents::SwipeUp: case TouchEvents::SwipeUp:
currentScreen->OnButtonPushed(); currentScreen->OnButtonPushed();
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up); lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
break; break;
case DisplayApp::TouchEvents::SwipeDown: case TouchEvents::SwipeDown:
currentScreen->OnButtonPushed(); currentScreen->OnButtonPushed();
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down); lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down);
break; break;
@ -123,6 +124,7 @@ void DisplayApp::Refresh() {
break; break;
} }
} }
}
break; break;
case Messages::ButtonPushed: case Messages::ButtonPushed:
if(onClockApp) if(onClockApp)
@ -189,32 +191,31 @@ void DisplayApp::PushMessage(DisplayApp::Messages msg) {
} }
} }
DisplayApp::TouchEvents DisplayApp::OnTouchEvent() { TouchEvents DisplayApp::OnTouchEvent() {
auto info = touchPanel.GetTouchInfo(); auto info = touchPanel.GetTouchInfo();
if(info.isTouch) { if(info.isTouch) {
switch(info.gesture) { switch(info.gesture) {
case Pinetime::Drivers::Cst816S::Gestures::SingleTap: case Pinetime::Drivers::Cst816S::Gestures::SingleTap:
// TODO set x,y to LittleVgl
lvgl.SetNewTapEvent(info.x, info.y); lvgl.SetNewTapEvent(info.x, info.y);
return DisplayApp::TouchEvents::Tap; return TouchEvents::Tap;
case Pinetime::Drivers::Cst816S::Gestures::LongPress: case Pinetime::Drivers::Cst816S::Gestures::LongPress:
return DisplayApp::TouchEvents::LongTap; return TouchEvents::LongTap;
case Pinetime::Drivers::Cst816S::Gestures::DoubleTap: case Pinetime::Drivers::Cst816S::Gestures::DoubleTap:
return DisplayApp::TouchEvents::DoubleTap; return TouchEvents::DoubleTap;
case Pinetime::Drivers::Cst816S::Gestures::SlideRight: case Pinetime::Drivers::Cst816S::Gestures::SlideRight:
return DisplayApp::TouchEvents::SwipeRight; return TouchEvents::SwipeRight;
case Pinetime::Drivers::Cst816S::Gestures::SlideLeft: case Pinetime::Drivers::Cst816S::Gestures::SlideLeft:
return DisplayApp::TouchEvents::SwipeLeft; return TouchEvents::SwipeLeft;
case Pinetime::Drivers::Cst816S::Gestures::SlideDown: case Pinetime::Drivers::Cst816S::Gestures::SlideDown:
return DisplayApp::TouchEvents::SwipeDown; return TouchEvents::SwipeDown;
case Pinetime::Drivers::Cst816S::Gestures::SlideUp: case Pinetime::Drivers::Cst816S::Gestures::SlideUp:
return DisplayApp::TouchEvents::SwipeUp; return TouchEvents::SwipeUp;
case Pinetime::Drivers::Cst816S::Gestures::None: case Pinetime::Drivers::Cst816S::Gestures::None:
default: default:
return DisplayApp::TouchEvents::None; return TouchEvents::None;
} }
} }
return DisplayApp::TouchEvents::None; return TouchEvents::None;
} }
void DisplayApp::StartApp(DisplayApp::Apps app) { void DisplayApp::StartApp(DisplayApp::Apps app) {

View File

@ -15,6 +15,7 @@
#include "LittleVgl.h" #include "LittleVgl.h"
#include <date/date.h> #include <date/date.h>
#include <DisplayApp/Screens/Clock.h> #include <DisplayApp/Screens/Clock.h>
#include "TouchEvents.h"
namespace Pinetime { namespace Pinetime {
@ -26,8 +27,7 @@ namespace Pinetime {
public: public:
enum class States {Idle, Running}; enum class States {Idle, Running};
enum class Messages : uint8_t {GoToSleep, GoToRunning, UpdateDateTime, UpdateBleConnection, UpdateBatteryLevel, TouchEvent, SwitchScreen,ButtonPushed} ; enum class Messages : uint8_t {GoToSleep, GoToRunning, UpdateDateTime, UpdateBleConnection, UpdateBatteryLevel, TouchEvent, SwitchScreen,ButtonPushed} ;
enum class TouchEvents { None, Tap, SwipeLeft, SwipeRight, SwipeUp, SwipeDown, LongTap, DoubleTap
};
DisplayApp(Pinetime::Drivers::St7789& lcd, DisplayApp(Pinetime::Drivers::St7789& lcd,
Pinetime::Components::LittleVgl& lvgl, Pinetime::Components::LittleVgl& lvgl,
Pinetime::Drivers::Cst816S&, Pinetime::Drivers::Cst816S&,

View File

@ -70,3 +70,23 @@ uint8_t Brightness::LevelToInt(Pinetime::Controllers::BrightnessController::Leve
default : return 0; default : return 0;
} }
} }
bool Brightness::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
switch(event) {
case TouchEvents::SwipeLeft:
brightness.Lower();
SetValue();
return true;
case TouchEvents::SwipeRight:
brightness.Higher();
SetValue();
return true;
default:
return false;
}
}
void Brightness::SetValue() {
lv_slider_set_value(slider, LevelToInt(brightness.Level()), LV_ANIM_OFF);
lv_label_set_text(slider_label, LevelToString(brightness.Level()));
}

View File

@ -13,6 +13,7 @@ namespace Pinetime {
~Brightness() override; ~Brightness() override;
bool Refresh() override; bool Refresh() override;
bool OnButtonPushed() override; bool OnButtonPushed() override;
bool OnTouchEvent(TouchEvents event) override;
void OnValueChanged(); void OnValueChanged();
private: private:
@ -25,6 +26,7 @@ namespace Pinetime {
const char* LevelToString(Controllers::BrightnessController::Levels level); const char* LevelToString(Controllers::BrightnessController::Levels level);
uint8_t LevelToInt(Controllers::BrightnessController::Levels level); uint8_t LevelToInt(Controllers::BrightnessController::Levels level);
void SetValue(uint8_t value); void SetValue(uint8_t value);
void SetValue();
}; };
} }
} }

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "../TouchEvents.h"
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
@ -15,6 +16,9 @@ namespace Pinetime {
// Return false if the button hasn't been handled by the app, true if it has been handled // Return false if the button hasn't been handled by the app, true if it has been handled
virtual bool OnButtonPushed() { return false; } virtual bool OnButtonPushed() { return false; }
// Return false if the event hasn't been handled by the app, true if it has been handled
virtual bool OnTouchEvent(TouchEvents event) { return false; }
protected: protected:
DisplayApp* app; DisplayApp* app;
}; };

View File

@ -0,0 +1,8 @@
#pragma once
namespace Pinetime {
namespace Applications {
enum class TouchEvents { None, Tap, SwipeLeft, SwipeRight, SwipeUp, SwipeDown, LongTap, DoubleTap};
}
}