diff options
-rw-r--r-- | ButtonHandler/ButtonHandler.cpp | 45 | ||||
-rw-r--r-- | ButtonHandler/ButtonHandler.h | 36 | ||||
-rw-r--r-- | main.cpp | 39 |
3 files changed, 117 insertions, 3 deletions
diff --git a/ButtonHandler/ButtonHandler.cpp b/ButtonHandler/ButtonHandler.cpp new file mode 100644 index 0000000..23bcb02 --- /dev/null +++ b/ButtonHandler/ButtonHandler.cpp @@ -0,0 +1,45 @@ +#include "ButtonHandler.h" + +ButtonHandler::ButtonHandler(osThreadId main) + : _main(main), + _sw1(PA_12), + _sw2(PA_11), + _sw1_time(0), + _event(none) +{ + // gpio goes low when push button is pressed + // fall handler will be the press, rise handler will be the release + _sw1.fall(this, &ButtonHandler::sw1_fall); + _sw1.rise(this, &ButtonHandler::sw1_rise); + // need to set mode to PullUp after attaching handlers - won't work otherwise + _sw1.mode(PullUp); + + _sw2.fall(this, &ButtonHandler::sw2_fall); + _sw2.mode(PullUp); +} + +ButtonEvent ButtonHandler::getButtonEvent() { + ButtonEvent event = _event; + _event = none; + return event; +} + +void ButtonHandler::sw1_rise() { + _sw1_timer.stop(); + _sw1_time = _sw1_timer.read_ms(); + + if (_sw1_time > 10) { + _event = (_sw1_time > 500) ? sw1_hold : sw1_press; + osSignalSet(_main, buttonSignal); + } +} + +void ButtonHandler::sw1_fall() { + _sw1_timer.reset(); + _sw1_timer.start(); +} + +void ButtonHandler::sw2_fall() { + _event = sw2_press; + osSignalSet(_main, buttonSignal); +} diff --git a/ButtonHandler/ButtonHandler.h b/ButtonHandler/ButtonHandler.h new file mode 100644 index 0000000..3eb8298 --- /dev/null +++ b/ButtonHandler/ButtonHandler.h @@ -0,0 +1,36 @@ +#ifndef __BUTTONHANDLER_H__ +#define __BUTTONHANDLER_H__ + +#include "mbed.h" +#include "rtos.h" + +#define buttonSignal (uint32_t)0x01 + +typedef enum { + none = 0, + sw1_press, + sw1_hold, + sw2_press +} ButtonEvent; + +class ButtonHandler { + public: + ButtonHandler(osThreadId main); + ~ButtonHandler(); + + ButtonEvent getButtonEvent(); + + private: + void sw1_fall(); + void sw1_rise(); + void sw2_fall(); + + osThreadId _main; + InterruptIn _sw1; + InterruptIn _sw2; + Timer _sw1_timer; + time_t _sw1_time; + ButtonEvent _event; +}; + +#endif @@ -13,9 +13,14 @@ #include "DOGS102.h" #include "NCP5623B.h" #include "LayoutStartup.h" +// button header +#include "ButtonHandler.h" // misc heders #include <string> +// only here for button handling example code in main +#include "font_6x8.h" + // LCD and backlight controllers SPI lcd_spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK); I2C backlight_i2c(I2C_SDA, I2C_SCL); @@ -24,6 +29,9 @@ DigitalOut lcd_cd(XBEE_ON_SLEEP, 1); DOGS102* lcd; NCP5623B* lcd_backlight; +// Button controller +ButtonHandler* buttons; + // Serial debug port Serial debug(USBTX, USBRX); @@ -33,14 +41,39 @@ int main() { lcd = new DOGS102(lcd_spi, lcd_spi_cs, lcd_cd); lcd_backlight = new NCP5623B(backlight_i2c); - logInfo("starting..."); + // display startup screen for 3 seconds LayoutStartup ls(lcd); ls.display(); + osDelay(3000); + + osThreadId main_id = Thread::gettid(); + buttons = new ButtonHandler(main_id); while (true) { - logInfo("in loop"); - osDelay(5000); + char buf[16]; + size_t size; + + osEvent e = Thread::signal_wait(buttonSignal); + if (e.status == osEventSignal) { + ButtonEvent ev = buttons->getButtonEvent(); + switch (ev) { + case sw1_press: + size = snprintf(buf, sizeof(buf), "SW1 press"); + break; + case sw1_hold: + size = snprintf(buf, sizeof(buf), "SW1 hold"); + break; + case sw2_press: + size = snprintf(buf, sizeof(buf), "SW2 press"); + break; + } + + lcd->clearBuffer(); + lcd->startUpdate(); + lcd->writeText(0, 0, font_6x8, buf, size); + lcd->endUpdate(); + } } return 0; |