summaryrefslogtreecommitdiff
path: root/ButtonHandler
diff options
context:
space:
mode:
authorMike Fiore <mfiore@multitech.com>2015-11-12 12:20:30 -0600
committerMike Fiore <mfiore@multitech.com>2015-11-12 12:20:30 -0600
commitf0f9fc2601dc3421aa3f4d9747748c3bee93f703 (patch)
tree3d884e151729be7fec59af504cb4dfadf31276ac /ButtonHandler
parent0bc27ddeae03930deb6106144b49e0614fe002da (diff)
downloadmtdot-box-evb-factory-firmware-f0f9fc2601dc3421aa3f4d9747748c3bee93f703.tar.gz
mtdot-box-evb-factory-firmware-f0f9fc2601dc3421aa3f4d9747748c3bee93f703.tar.bz2
mtdot-box-evb-factory-firmware-f0f9fc2601dc3421aa3f4d9747748c3bee93f703.zip
add class for handling button events - signals main thread when event occurs and main thread can check event - supports SW1 press, SW2 press, and SW1 hold
Diffstat (limited to 'ButtonHandler')
-rw-r--r--ButtonHandler/ButtonHandler.cpp45
-rw-r--r--ButtonHandler/ButtonHandler.h36
2 files changed, 81 insertions, 0 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