Microbug / MicroBitDAL_SB2_TEST

Fork of MicroBitDALImageRewrite by Joe Finney

Revision:
1:3e0360107f98
Child:
4:f998ee705a20
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MicroBitButton.cpp	Thu Apr 16 13:50:24 2015 +0000
@@ -0,0 +1,58 @@
+#include "inc/MicroBit.h"
+#include "inc/MicroBitButton.h"
+#include "inc/MicroBitMessageBus.h"
+/**
+  * Constructor. 
+  * Create a button representation with the given ID.
+  * @param id the ID of the new LED object.
+  * @param name the physical pin on the processor that this butotn is connected to.
+  */
+MicroBitButton::MicroBitButton(int id, PinName name) : pin(name), irq(name)
+{
+    this->id = id;
+    this->name = name;
+    irq.rise(this, &MicroBitButton::rising);
+    irq.fall(this, &MicroBitButton::falling);
+}
+
+/**
+  * Interrupt on change handler for this button.
+  */
+void MicroBitButton::rising()
+{
+    // TODO: FIX THIS. This many of the mbed libraries aren't re-entrant...
+    MicroBitEvent *evt = new MicroBitEvent();
+    evt->source = id;
+    evt->context = NULL;
+    evt->timestamp = 0;
+    evt->value = MICROBIT_BUTTON_EVT_UP;
+    
+    MicroBit::MessageBus.send(evt);        
+}
+
+/**
+  * Interrupt on change handler for this button.
+  */
+void MicroBitButton::falling()
+{
+    // TODO: FIX THIS. This many of the mbed libraries aren't re-entrant...
+    MicroBitEvent *evt = new MicroBitEvent();
+    evt->source = id;
+    evt->context = NULL;
+    evt->timestamp = 0;
+    evt->value = MICROBIT_BUTTON_EVT_DOWN;
+    
+    MicroBit::MessageBus.send(evt);        
+}
+
+
+/**
+  * Tests if this Button is currently pressed.
+  * @return 1 if this button is pressed, 0 otherwise.
+  */
+int MicroBitButton::isPressed()
+{
+    return !pin;
+}
+
+