Skip to content

Commit

Permalink
Fixing settings in the abstraction branch (#39)
Browse files Browse the repository at this point in the history
* Fixing settings

* updating the simulator
  • Loading branch information
obi11235 authored Sep 17, 2023
1 parent 6a78c4c commit 5989736
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Platformio/HAL/HardwareAbstract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ class HardwareAbstract {
virtual std::shared_ptr<BatteryInterface> battery() = 0;
virtual std::shared_ptr<DisplayAbstract> display() = 0;
virtual std::shared_ptr<wifiHandlerInterface> wifi() = 0;

virtual char getCurrentDevice() = 0;
virtual void setCurrentDevice(char currentDevice) = 0;

virtual bool getWakeupByIMUEnabled() = 0;
virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) = 0;

virtual uint16_t getSleepTimeout() = 0;
virtual void setSleepTimeout(uint16_t sleepTimeout) = 0;

protected:

};
37 changes: 34 additions & 3 deletions Platformio/HAL/Targets/ESP32/HardwareRevX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void HardwareRevX::init() {
mWifiHandler = wifiHandler::getInstance();
restorePreferences();

mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint){ standbyTimer = SLEEP_TIMEOUT;});
mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint){ standbyTimer = this->getSleepTimeout();});

setupIMU();
setupIR();
Expand Down Expand Up @@ -134,19 +134,45 @@ void HardwareRevX::activityDetection() {
standbyTimer = 0;
// If the motion exceeds the threshold, the standbyTimer is reset
if (motion > MOTION_THRESHOLD)
standbyTimer = SLEEP_TIMEOUT;
standbyTimer = sleepTimeout;

// Store the current acceleration and time
accXold = accX;
accYold = accY;
accZold = accZ;
}

char HardwareRevX::getCurrentDevice(){
return currentDevice;
}

void HardwareRevX::setCurrentDevice(char currentDevice){
this->currentDevice = currentDevice;
}

bool HardwareRevX::getWakeupByIMUEnabled(){
return wakeupByIMUEnabled;
}

void HardwareRevX::setWakeupByIMUEnabled(bool wakeupByIMUEnabled){
this->wakeupByIMUEnabled = wakeupByIMUEnabled;
}

uint16_t HardwareRevX::getSleepTimeout(){
return sleepTimeout;
}

void HardwareRevX::setSleepTimeout(uint16_t sleepTimeout){
this->sleepTimeout = sleepTimeout;
standbyTimer = sleepTimeout;
}

void HardwareRevX::enterSleep() {
// Save settings to internal flash memory
preferences.putBool("wkpByIMU", wakeupByIMUEnabled);
preferences.putUChar("blBrightness", mDisplay->getBrightness());
preferences.putUChar("currentDevice", currentDevice);
preferences.putUInt("sleepTimeout", sleepTimeout);
if (!preferences.getBool("alreadySetUp"))
preferences.putBool("alreadySetUp", true);
preferences.end();
Expand Down Expand Up @@ -259,6 +285,11 @@ void HardwareRevX::restorePreferences() {
wakeupByIMUEnabled = preferences.getBool("wkpByIMU");
backlight_brightness = preferences.getUChar("blBrightness");
currentDevice = preferences.getUChar("currentDevice");
sleepTimeout = preferences.getUInt("sleepTimeout");
// setting the default to prevent a 0ms sleep timeout
if(sleepTimeout == 0){
sleepTimeout = SLEEP_TIMEOUT;
}
}
mDisplay->setBrightness(backlight_brightness);
}
Expand Down Expand Up @@ -312,7 +343,7 @@ void HardwareRevX::loopHandler() {
if (customKeypad.key[i].kstate == PRESSED ||
customKeypad.key[i].kstate == HOLD) {
standbyTimer =
SLEEP_TIMEOUT; // Reset the sleep timer when a button is pressed
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)
Expand Down
10 changes: 10 additions & 0 deletions Platformio/HAL/Targets/ESP32/HardwareRevX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ class HardwareRevX : public HardwareAbstract {
virtual std::shared_ptr<BatteryInterface> battery() override;
virtual std::shared_ptr<DisplayAbstract> display() override;
virtual std::shared_ptr<wifiHandlerInterface> wifi() override;

virtual char getCurrentDevice() override;
virtual void setCurrentDevice(char currentDevice) override;

virtual bool getWakeupByIMUEnabled() override;
virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override;

virtual uint16_t getSleepTimeout() override;
virtual void setSleepTimeout(uint16_t sleepTimeout) override;

/// @brief To be ran in loop out in main
// TODO move to a freertos task
void loopHandler();
Expand Down Expand Up @@ -64,6 +73,7 @@ class HardwareRevX : public HardwareAbstract {
// IMU Motion Detection
LIS3DH IMU = LIS3DH(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19.
int standbyTimer = SLEEP_TIMEOUT;
int sleepTimeout = SLEEP_TIMEOUT;
int motion = 0;
WakeReason wakeup_reason;

Expand Down
26 changes: 25 additions & 1 deletion Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,28 @@ std::shared_ptr<DisplayAbstract> HardwareSimulator::display(){
}
std::shared_ptr<wifiHandlerInterface> HardwareSimulator::wifi(){
return mWifiHandler;
}
}

char HardwareSimulator::getCurrentDevice(){
return 0;
}

void HardwareSimulator::setCurrentDevice(char currentDevice){

}

bool HardwareSimulator::getWakeupByIMUEnabled(){
return true;
}

void HardwareSimulator::setWakeupByIMUEnabled(bool wakeupByIMUEnabled){

}

uint16_t HardwareSimulator::getSleepTimeout(){
return 20000;
}

void HardwareSimulator::setSleepTimeout(uint16_t sleepTimeout){

}
9 changes: 9 additions & 0 deletions Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class HardwareSimulator : public HardwareAbstract {
virtual std::shared_ptr<DisplayAbstract> display() override;
virtual std::shared_ptr<wifiHandlerInterface> wifi() override;

virtual char getCurrentDevice() override;
virtual void setCurrentDevice(char currentDevice) override;

virtual bool getWakeupByIMUEnabled() override;
virtual void setWakeupByIMUEnabled(bool wakeupByIMUEnabled) override;

virtual uint16_t getSleepTimeout() override;
virtual void setSleepTimeout(uint16_t sleepTimeout) override;

private:
std::thread mTickThread;
std::thread mHardwareStatusTitleUpdate;
Expand Down
15 changes: 12 additions & 3 deletions Platformio/OmoteUI/OmoteUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void OmoteUI::store_scroll_value_event_cb(lv_event_t *e) {
// Update current device when the tabview page is changes
void OmoteUI::tabview_device_event_cb(lv_event_t *e) {
currentDevice = lv_tabview_get_tab_act(lv_event_get_target(e));
this->mHardware->setCurrentDevice(currentDevice);
}

// Slider Event handler
Expand All @@ -41,8 +42,15 @@ void OmoteUI::appleKey_event_cb(lv_event_t *e) {

// Wakeup by IMU Switch Event handler
void OmoteUI::WakeEnableSetting_event_cb(lv_event_t *e) {
wakeupByIMUEnabled =
lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED);
this->mHardware->setWakeupByIMUEnabled(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED));
}

// Wakeup timeout dropdown Event handler
void OmoteUI::wakeTimeoutSetting_event_cb(lv_event_t *e){
lv_obj_t * drop = lv_event_get_target(e);

int sleepTimeout = sleepTimeoutMap[lv_dropdown_get_selected(drop)];
mHardware->setSleepTimeout(sleepTimeout);
}

// Smart Home Toggle Event handler
Expand Down Expand Up @@ -391,7 +399,8 @@ void OmoteUI::layout_UI() {


// Set current page according to the current Device
lv_tabview_set_act(tabview, 0, LV_ANIM_OFF);
currentDevice = this->mHardware->getCurrentDevice();
lv_tabview_set_act(tabview, currentDevice, LV_ANIM_OFF);


// Create a page indicator
Expand Down
5 changes: 4 additions & 1 deletion Platformio/OmoteUI/OmoteUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class OmoteUI {
void store_scroll_value_event_cb(lv_event_t *e);
// Update current device when the tabview page is changes
void tabview_device_event_cb(lv_event_t *e);
// Update wake timeout handler
void wakeTimeoutSetting_event_cb(lv_event_t *e);
// Slider Event handler
void bl_slider_event_cb(lv_event_t *e);
// Apple Key Event handler
Expand Down Expand Up @@ -78,6 +80,8 @@ class OmoteUI {

std::unique_ptr<poller> batteryPoller;

int sleepTimeoutMap[5] = {10000,30000,60000,180000,600000};

void reset_settings_menu();
void attach_keyboard(lv_obj_t* textarea);
std::shared_ptr<std::vector<WifiInfo>> found_wifi_networks;
Expand Down Expand Up @@ -130,7 +134,6 @@ void create_keyboard();
Images imgs = Images();
uint_fast8_t currentDevice = 4;
lv_color_t color_primary = lv_color_hex(0x303030); // gray
bool wakeupByIMUEnabled = true;

inline static const uint_fast8_t virtualKeyMapTechnisat[10] = {
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0};
Expand Down
9 changes: 7 additions & 2 deletions Platformio/OmoteUI/displaySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void OmoteUI::display_settings(lv_obj_t* parent)
lv_obj_align(wakeToggle, LV_ALIGN_TOP_RIGHT, 0, 29);
lv_obj_set_style_bg_color(wakeToggle, lv_color_hex(0x505050), LV_PART_MAIN);
lv_obj_add_event_cb(wakeToggle, [] (lv_event_t* e) {mInstance->WakeEnableSetting_event_cb(e);}, LV_EVENT_VALUE_CHANGED, NULL);
if(wakeupByIMUEnabled) lv_obj_add_state(wakeToggle, LV_STATE_CHECKED); // set default state
if(mHardware->getWakeupByIMUEnabled()) lv_obj_add_state(wakeToggle, LV_STATE_CHECKED); // set default state

menuLabel = lv_label_create(menuBox);
lv_label_set_text(menuLabel, "Timeout");
Expand All @@ -52,5 +52,10 @@ void OmoteUI::display_settings(lv_obj_t* parent)
lv_obj_set_style_bg_color(lv_dropdown_get_list(drop), color_primary, LV_PART_MAIN);
lv_obj_set_style_border_width(lv_dropdown_get_list(drop), 1, LV_PART_MAIN);
lv_obj_set_style_border_color(lv_dropdown_get_list(drop), lv_color_hex(0x505050), LV_PART_MAIN);

int sleepTimeoutMapSize = sizeof(sleepTimeoutMap)/sizeof(sleepTimeoutMap[0]);
int currentTimeout = mHardware->getSleepTimeout();
for(int i = 0; i < sleepTimeoutMapSize; i++){
if(currentTimeout == sleepTimeoutMap[i]) lv_dropdown_set_selected(drop, i);
}
lv_obj_add_event_cb(drop, [] (lv_event_t* e) {mInstance->wakeTimeoutSetting_event_cb(e);}, LV_EVENT_VALUE_CHANGED, NULL);
}

0 comments on commit 5989736

Please sign in to comment.