summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVasil Zlatanov <v@skozl.com>2017-05-03 23:18:54 +0100
committerVasil Zlatanov <v@skozl.com>2017-05-03 23:18:54 +0100
commit2f5dadf484daf6a37a515c9fbfc11b9045b5f82c (patch)
tree8554e1ec285f0236e6b321ee4e25eabbb8a125de
parent88c994ec08f6c5be8627587bf350c6fd719b2dd5 (diff)
downloade2-switch-2f5dadf484daf6a37a515c9fbfc11b9045b5f82c.tar.gz
e2-switch-2f5dadf484daf6a37a515c9fbfc11b9045b5f82c.tar.bz2
e2-switch-2f5dadf484daf6a37a515c9fbfc11b9045b5f82c.zip
Add interrupts for all inputs
-rw-r--r--src/main.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..cf2214f
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,70 @@
+#include "mbed.h"
+#include "spi_init.h"
+#include "counter.h"
+#include "Adafruit_SSD1306.h"
+
+#define SW_PINS p22, p23, p24, p25
+#define SW_PERIOD 20000 // 20ms
+
+void tout(void);
+
+// Onboard LED
+DigitalOut alive(LED1);
+
+PinName switch_pin[] = { SW_PINS };
+
+Counter *switch_position[4];
+Ticker timer;
+
+volatile uint16_t switch_count[4] = { 0, 0, 0, 0 };
+volatile uint16_t update = 0;
+
+// Initialise display
+SPInit gSpi(D_MOSI_PIN, NC, D_CLK_PIN);
+Adafruit_SSD1306_Spi oled(gSpi, D_DC_PIN, D_RST_PIN, D_CS_PIN, 64, 128);
+
+int main(void)
+{
+ // Enable ISR for the switch rising edge
+ for (int i = 0; i < 3; ++i) {
+ switch_position[i] = new Counter(switch_pin[i]);
+ }
+
+ //Attach switch sampling timer ISR to the timer instance with the required period
+ timer.attach_us(&tout, SW_PERIOD);
+
+ oled.setRotation(2);
+ oled.printf("%ux%u OLED Display\r\n", oled.width(), oled.height());
+
+ for (;;) {
+ if (update) {
+ update = 0;
+
+ oled.setTextCursor(0, 0);
+
+ //Write the latest switch osciallor count
+ for (int i = 0; i < 3; ++i) {
+ oled.printf("\n%05u ", switch_count[i]);
+ }
+
+ //Copy the display buffer to the display
+ oled.display();
+
+ alive = !alive;
+ }
+
+ }
+}
+
+
+//Interrupt Service Routine for the switch sampling timer
+void tout(void)
+{
+ // Query how many times switch triggered
+ for (int i = 0; i < 3; ++i) {
+ switch_count[i] = switch_position[i]->read();
+ switch_position[i]->write(0);
+ }
+ // Update display
+ update = 1;
+}