]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - btn.c
Convert to task form
[rgblamp.git] / btn.c
diff --git a/btn.c b/btn.c
new file mode 100644 (file)
index 0000000..7b3c75e
--- /dev/null
+++ b/btn.c
@@ -0,0 +1,50 @@
+/*
+ * File:   buttons.c
+ *
+ * Button control
+ */
+
+#include <htc.h>
+#include "btn.h"
+#include "tmr.h"
+#include "task.h"
+
+/* All the buttons are on PORTB.  RB0 is the push button.
+ * RB4 and RB5 are the rocker switch positions left and right, respectively.
+ */
+#define ALLMASK    0b00110001
+
+/* Initialize the button module */
+void btn_init()
+{
+  nWPUEN = 0;          /* enable weak pull-ups on PortB */
+  TRISB |= ALLMASK;    /* all button pins as inputs */
+  ANSELB &= ~ALLMASK;  /* turn off ADC inputs on button pins */
+  IOCBF &= ~ALLMASK;   /* clear pin interrupt flags */
+  IOCIE = 1;           /* enable interrupt-on-change */
+  GIE = 1;             /* enable global interrupts */
+}
+
+void btn_isr()
+{
+  if (IOCIF) {
+    if (IOCBF0) {
+      _btn_pbdis();
+      tmr_start(TMR_BTN_PB, 1);
+    }
+    if (IOCBF4) {
+      _btn_rsdis();
+      tmr_start(TMR_BTN_RS, 1);
+    }
+    if (IOCBF5) {
+      _btn_rsdis();
+      tmr_start(TMR_BTN_RS, 1);
+    }
+  }
+
+  if (tmr_fired(TMR_BTN_PB))
+    _task_post(TASK_BTN_PB);
+  if (tmr_fired(TMR_BTN_RS))
+    _task_post(TASK_BTN_RS);
+}
+