Events a la mockup
authorWintermate <wintermute@hannover.ccc.de>
Tue, 8 Oct 2013 00:48:35 +0000 (02:48 +0200)
committerWintermate <wintermute@hannover.ccc.de>
Tue, 8 Oct 2013 00:48:35 +0000 (02:48 +0200)
Makefile
badge/main.c
badge/ui/event.c [new file with mode: 0644]
badge/ui/event.h [new file with mode: 0644]

index ef3b99f..474d2be 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -33,8 +33,8 @@ OPTDEFINES = -D __NEWLIB__
 SRCS = \\r
   badge/main.c \\r
   badge/ui/display.c \\r
+  badge/ui/event.c \\r
   badge/ui/sprite.c\r
-\r
 SRCS += \\r
   dataflash/at45db041d.c \\r
   dataflash/iobase.c \\r
index 81be76b..ab3f7c3 100644 (file)
 
 #include "ui/display.h"
 #include "ui/sprite.h"
+#include "ui/event.h"
 
 #include "r0ketports.h"
 #include "drivers/fatfs/ff.h"
 
-enum {
-  BADGE_BTN_UP    =  1,
-  BADGE_BTN_LEFT  =  2,
-  BADGE_BTN_DOWN  =  4,
-  BADGE_BTN_RIGHT =  8,
-  BADGE_BTN_A     = 16,
-  BADGE_BTN_B     = 32,
-  BADGE_BTN_MID   = 64
-};
-
-uint8_t getInputRaw(void) {
-    uint8_t result = BTN_NONE;
-
-    if (gpioGetValue(RB_BTN3)==0) {
-        result |= BADGE_BTN_UP;
-    }
-
-    if (gpioGetValue(RB_BTN2)==0) {
-        result |= BADGE_BTN_DOWN;
-    }
-
-    if (gpioGetValue(RB_BTN4)==0) {
-        result |= BADGE_BTN_MID;
-    }
-
-    if (gpioGetValue(RB_BTN0)==0) {
-        result |= BADGE_BTN_LEFT;
-    }
-
-    if (gpioGetValue(RB_BTN1)==0) {
-        result |= BADGE_BTN_RIGHT;
-    }
-
-    if(gpioGetValue(RB_HB3) == 0) {
-      result |= BADGE_BTN_A;
-    }
-
-    if(gpioGetValue(RB_HB4) == 0 || gpioGetValue(RB_HB5) == 0) {
-      result |= BADGE_BTN_B;
-    }
-
-    return result;
-}
-
 void backlightInit(void) {
   /* Enable the clock for CT16B1 */
   SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
@@ -126,8 +83,6 @@ void backlightInit(void) {
   gpioSetValue(RB_PWR_LCDBL, 0);
 }
 
-
-
 void rbInit() {
     // TODO FIXME special port disable ? LEDs BTNs ?
 
@@ -217,134 +172,72 @@ int main(void)
   cpuInit();
   systickInit(CFG_SYSTICK_DELAY_IN_MS);
 
-  //pmuInit();
-  //adcInit();
+//  pmuInit();
+//  adcInit();
   rbInit();
 
   badge_display_init();
 
   gpioSetDir(0, 11, gpioDirection_Output);
-
-//  usbMSCInit();
-
-  RB_HB3_IO&= ~IOCON_PIO0_2_FUNC_MASK;
-  RB_HB3_IO|=  IOCON_PIO0_2_FUNC_GPIO;
-  gpioSetDir(RB_HB3, gpioDirection_Input);
-  gpioSetPullup(&RB_HB3_IO, gpioPullupMode_PullUp);
-
-  RB_HB4_IO&= ~IOCON_PIO1_4_FUNC_MASK;
-  RB_HB4_IO|=  IOCON_PIO1_4_FUNC_GPIO;
-  gpioSetDir(RB_HB4, gpioDirection_Input);
-  gpioSetPullup(&RB_HB4_IO, gpioPullupMode_PullUp);
-
-
-  badge_framebuffer fb = {
-    {
-      {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
+  gpioSetValue(0, 11, 0);
+
+  {
+    badge_framebuffer fb;
+    int res = 0;
+    FATFS fatvol;
+
+    if(FR_OK == f_mount(0, &fatvol)) {
+      FIL fil;
+      if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
+        UINT readbytes;
+
+        if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
+          gpioSetDir(RB_LED3, gpioDirection_Output);
+          gpioSetValue(RB_LED3, 1);
+        }
+
+        f_close(&fil);
+      } else {
+        fb.data[0][0] = res;
+        gpioSetDir(RB_LED1, gpioDirection_Output);
+        gpioSetValue(RB_LED1, 1);
       }
-    }
-  };
-
-  //  badge_framebuffer_flush(&fb);
-
-  int res = 0;
-  FATFS fatvol;
-
-  if(FR_OK == f_mount(0, &fatvol)) {
-    FIL fil;
-    if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
-      UINT readbytes;
-
-      if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
-        gpioSetDir(RB_LED3, gpioDirection_Output);
-        gpioSetValue(RB_LED3, 1);
-      }
-
-      f_close(&fil);
     } else {
-      fb.data[0][0] = res;
-      gpioSetDir(RB_LED1, gpioDirection_Output);
-      gpioSetValue(RB_LED1, 1);
+      gpioSetDir(RB_LED2, gpioDirection_Output);
+      gpioSetValue(RB_LED2, 1);
     }
-  } else {
-    gpioSetDir(RB_LED2, gpioDirection_Output);
-    gpioSetValue(RB_LED2, 1);
-  }
-
-  badge_framebuffer_flush(&fb);
 
-  for(uint8_t i = 0; ; ++i) {
-    gpioSetValue(0, 11, i & 1);
-
-    uint8_t buttons = getInputRaw();
-    memset(&fb, 0, sizeof(fb));
+    badge_framebuffer_flush(&fb);
+  }
 
-    badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
+  badge_event_start();
 
-    if(buttons & BADGE_BTN_UP)    { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
-    if(buttons & BADGE_BTN_DOWN)  { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
-    if(buttons & BADGE_BTN_LEFT)  { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_MID)   { badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_A)     { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
-    if(buttons & BADGE_BTN_B)     { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
+  uint8_t buttons = 0;
 
-    badge_framebuffer_flush(&fb);
+  for(uint8_t i = 0; ; ++i) {
+    badge_event_t event = badge_event_wait();
 
-    systickDelay(500);
+    switch(badge_event_type(event)) {
+    case BADGE_EVENT_USER_INPUT: {
+      buttons = badge_event_current_input_state();
+      break;
+    }
+    case BADGE_EVENT_GAME_TICK: {
+      badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
+      badge_framebuffer fb = { { { 0 } } };
+
+      if(buttons & BADGE_EVENT_KEY_UP)    { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_DOWN)  { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_LEFT)  { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_CENTER){ badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
+
+      badge_framebuffer_flush(&fb);
+      break;
+    }
+    }
   }
 
   return 0;
diff --git a/badge/ui/event.c b/badge/ui/event.c
new file mode 100644 (file)
index 0000000..5511bca
--- /dev/null
@@ -0,0 +1,67 @@
+#include "event.h"
+
+#include <core/gpio/gpio.h>
+#include <core/timer32/timer32.h>
+#include <r0ketports.h>
+
+// We depend on input being popped often, so no queue.
+static badge_event_t volatile event_buffer;
+static uint8_t       volatile event_flag;
+
+enum {
+  BADGE_EVENT_FLAG_INPUT = 1,
+  BADGE_EVENT_FLAG_TIMER = 2
+};
+
+static uint8_t badge_input_raw(void) {
+    uint8_t result = BTN_NONE;
+
+    if (gpioGetValue(RB_BTN3) == 0) { result |= BADGE_EVENT_KEY_UP    ; }
+    if (gpioGetValue(RB_BTN2) == 0) { result |= BADGE_EVENT_KEY_DOWN  ; }
+    if (gpioGetValue(RB_BTN4) == 0) { result |= BADGE_EVENT_KEY_CENTER; }
+    if (gpioGetValue(RB_BTN0) == 0) { result |= BADGE_EVENT_KEY_LEFT  ; }
+    if (gpioGetValue(RB_BTN1) == 0) { result |= BADGE_EVENT_KEY_RIGHT ; }
+    // TODO: Knöpfe.
+
+    return result;
+}
+
+uint8_t badge_event_current_input_state(void) {
+  return badge_event_new_input_state(event_buffer);
+}
+
+void badge_event_irq(void) {
+  uint8_t old_state = badge_event_current_input_state();
+  uint8_t new_state = badge_input_raw();
+
+  if(new_state != old_state) {
+    event_buffer = badge_event_new(BADGE_EVENT_USER_INPUT,
+                                   old_state,
+                                   new_state);
+    event_flag |= BADGE_EVENT_FLAG_INPUT;
+  }
+
+  event_flag |= BADGE_EVENT_FLAG_TIMER;
+}
+
+badge_event_t badge_event_wait(void) {
+  while(!event_flag)
+    ;
+
+  // User input takes precedence.
+  if(event_flag & BADGE_EVENT_FLAG_INPUT) {
+    badge_event_t event = event_buffer;
+    event_flag &= ~BADGE_EVENT_FLAG_INPUT;
+    return event;
+  }
+
+  uint8_t state = badge_event_current_input_state();
+  event_flag &= ~BADGE_EVENT_FLAG_TIMER;
+  return badge_event_new(BADGE_EVENT_GAME_TICK, state, state);
+}
+
+void badge_event_start(void) {
+  timer32Init(0, TIMER32_CCLK_10MS);
+  timer32SetIntHandler(badge_event_irq);
+  timer32Enable(0);
+}
diff --git a/badge/ui/event.h b/badge/ui/event.h
new file mode 100644 (file)
index 0000000..5871207
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef INCLUDED_BADGE2013_MOCKUP_EVENT_H
+#define INCLUDED_BADGE2013_MOCKUP_EVENT_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+  enum {
+    BADGE_EVENT_USER_INPUT,
+    BADGE_EVENT_GAME_TICK,
+  };
+
+  enum {
+    BADGE_EVENT_KEY_UP     =  1,
+    BADGE_EVENT_KEY_LEFT   =  2,
+    BADGE_EVENT_KEY_DOWN   =  4,
+    BADGE_EVENT_KEY_RIGHT  =  8,
+    BADGE_EVENT_KEY_CENTER = 16,
+    BADGE_EVENT_KEY_BTN_A  = 32,
+    BADGE_EVENT_KEY_BTN_B  = 64
+  };
+
+  typedef uint16_t badge_event_t;
+
+  static inline uint8_t badge_event_type           (badge_event_t event) { return (uint8_t) (event      & 0x03); }
+  static inline uint8_t badge_event_old_input_state(badge_event_t event) { return (uint8_t) (event >> 2 & 0x7f); }
+  static inline uint8_t badge_event_new_input_state(badge_event_t event) { return (uint8_t) (event >> 9       ); }
+
+  uint8_t badge_event_current_input_state(void);
+
+  static inline badge_event_t badge_event_new(uint8_t type, uint8_t old_input_state, uint8_t new_input_state) {
+    return (badge_event_t) ((type & 0x3) | ((badge_event_t) (old_input_state & 0x7f) << 2) | ((badge_event_t) (new_input_state & 0x7f) << 9));
+  }
+
+  badge_event_t badge_event_wait(void);
+  void          badge_event_start(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
This page took 0.034598 seconds and 4 git commands to generate.