platform for developing on SQFMI's Watchy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

79 lines
1.6 KiB

#include "watchos.h"
#include "Module_Input.h"
struct InputButtonState
{
byte button;
byte pin;
uint64_t mask;
bool pressed = false;
};
uint64_t wakeup_bits;
Module_Input::Module_Input()
{
for (int i = 0; i < MODULE_INPUT_BUTTONS; i++)
{
m_button[i] = new InputButtonState();
}
m_button[0]->button = WATCHOS_BUTTON_BACK;
m_button[0]->pin = HW_BUTTON_TL;
m_button[0]->mask = HW_BUTTON_TL_MASK;
m_button[1]->button = WATCHOS_BUTTON_OK;
m_button[1]->pin = HW_BUTTON_BL;
m_button[1]->mask = HW_BUTTON_BL_MASK;
m_button[2]->button = WATCHOS_BUTTON_UP;
m_button[2]->pin = HW_BUTTON_TR;
m_button[2]->mask = HW_BUTTON_TR_MASK;
m_button[3]->button = WATCHOS_BUTTON_DOWN;
m_button[3]->pin = HW_BUTTON_BR;
m_button[3]->mask = HW_BUTTON_BR_MASK;
}
void Module_Input::start()
{
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1)
{
wakeup_bits = esp_sleep_get_ext1_wakeup_status();
}
else
{
wakeup_bits = 0;
}
}
void Module_Input::tick()
{
byte pressed = 0;
for (int i = 0; i < MODULE_INPUT_BUTTONS; i++)
{
if (digitalRead(m_button[i]->pin) == 1 || (wakeup_bits & m_button[i]->mask) != 0)
{
if (!m_button[i]->pressed)
{
watchos::debug("Button pressed: %d", i);
pressed |= m_button[i]->button;
m_button[i]->pressed = true;
}
}
else
{
if (m_button[i]->pressed)
watchos::debug("Button released!");
m_button[i]->pressed = false;
}
}
if (pressed != 0)
{
watchos::kernel()->pushEvent(WATCHOS_MODULE_INPUT, WATCHOS_EVENT_TYPE_INPUT_PRESSED, pressed, 0);
}
wakeup_bits = 0;
}