aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spaghetti-monster/fsm-events.c103
-rw-r--r--spaghetti-monster/fsm-events.h77
-rw-r--r--spaghetti-monster/fsm-pcint.c2
3 files changed, 95 insertions, 87 deletions
diff --git a/spaghetti-monster/fsm-events.c b/spaghetti-monster/fsm-events.c
index 93c38c9..b4cb671 100644
--- a/spaghetti-monster/fsm-events.c
+++ b/spaghetti-monster/fsm-events.c
@@ -23,6 +23,57 @@
#include <util/delay_basic.h>
+void append_emission(Event event, uint16_t arg) {
+ uint8_t i;
+ // find last entry
+ for(i=0;
+ (i<EMISSION_QUEUE_LEN) && (emissions[i].event != EV_none);
+ i++) { }
+ // add new entry
+ if (i < EMISSION_QUEUE_LEN) {
+ emissions[i].event = event;
+ emissions[i].arg = arg;
+ } else {
+ // TODO: if queue full, what should we do?
+ }
+}
+
+void delete_first_emission() {
+ uint8_t i;
+ for(i=0; i<EMISSION_QUEUE_LEN-1; i++) {
+ emissions[i].event = emissions[i+1].event;
+ emissions[i].arg = emissions[i+1].arg;
+ }
+ emissions[i].event = EV_none;
+ emissions[i].arg = 0;
+}
+
+void process_emissions() {
+ while (emissions[0].event != EV_none) {
+ emit_now(emissions[0].event, emissions[0].arg);
+ delete_first_emission();
+ }
+}
+
+// Call stacked callbacks for the given event until one handles it.
+uint8_t emit_now(Event event, uint16_t arg) {
+ for(int8_t i=state_stack_len-1; i>=0; i--) {
+ uint8_t err = state_stack[i](event, arg);
+ if (! err) return 0;
+ }
+ return 1; // event not handled
+}
+
+void emit(Event event, uint16_t arg) {
+ // add this event to the queue for later,
+ // so we won't use too much time during an interrupt
+ append_emission(event, arg);
+}
+
+void emit_current_event(uint16_t arg) {
+ emit(current_event, arg);
+}
+
void empty_event_sequence() {
current_event = EV_none;
ticks_since_last_event = 0;
@@ -63,42 +114,9 @@ uint8_t push_event(uint8_t ev_type) { // only for use by PCINT_inner()
}
return 0; // unexpected event type
-
}
-void append_emission(Event event, uint16_t arg) {
- uint8_t i;
- // find last entry
- for(i=0;
- (i<EMISSION_QUEUE_LEN) && (emissions[i].event != EV_none);
- i++) { }
- // add new entry
- if (i < EMISSION_QUEUE_LEN) {
- emissions[i].event = event;
- emissions[i].arg = arg;
- } else {
- // TODO: if queue full, what should we do?
- }
-}
-
-void delete_first_emission() {
- uint8_t i;
- for(i=0; i<EMISSION_QUEUE_LEN-1; i++) {
- emissions[i].event = emissions[i+1].event;
- emissions[i].arg = emissions[i+1].arg;
- }
- emissions[i].event = EV_none;
- emissions[i].arg = 0;
-}
-
-void process_emissions() {
- while (emissions[0].event != EV_none) {
- emit_now(emissions[0].event, emissions[0].arg);
- delete_first_emission();
- }
-}
-
// explicitly interrupt these "nice" delays
volatile uint8_t nice_delay_interrupt = 0;
inline void interrupt_nice_delays() { nice_delay_interrupt = 1; }
@@ -194,23 +212,4 @@ uint8_t nice_delay_s() {
}
*/
-// Call stacked callbacks for the given event until one handles it.
-uint8_t emit_now(Event event, uint16_t arg) {
- for(int8_t i=state_stack_len-1; i>=0; i--) {
- uint8_t err = state_stack[i](event, arg);
- if (! err) return 0;
- }
- return 1; // event not handled
-}
-
-void emit(Event event, uint16_t arg) {
- // add this event to the queue for later,
- // so we won't use too much time during an interrupt
- append_emission(event, arg);
-}
-
-void emit_current_event(uint16_t arg) {
- emit(current_event, arg);
-}
-
#endif
diff --git a/spaghetti-monster/fsm-events.h b/spaghetti-monster/fsm-events.h
index f83d306..79a0aff 100644
--- a/spaghetti-monster/fsm-events.h
+++ b/spaghetti-monster/fsm-events.h
@@ -22,6 +22,23 @@
#include <avr/pgmspace.h>
+
+// timeout durations in ticks (each tick 1/62th s)
+#ifndef HOLD_TIMEOUT
+#define HOLD_TIMEOUT 24
+#endif
+#ifndef RELEASE_TIMEOUT
+#define RELEASE_TIMEOUT 18
+#endif
+
+// return codes for Event handlers
+// Indicates whether this handler consumed (handled) the Event, or
+// if the Event should be sent to the next handler in the stack.
+#define EVENT_HANDLED 0
+#define EVENT_NOT_HANDLED 1
+#define MISCHIEF_MANAGED EVENT_HANDLED
+#define MISCHIEF_NOT_MANAGED EVENT_NOT_HANDLED
+
// typedefs
typedef uint8_t Event;
typedef struct Emission {
@@ -29,22 +46,35 @@ typedef struct Emission {
uint16_t arg;
} Emission;
-#define EVENT_HANDLED 0
-#define EVENT_NOT_HANDLED 1
-#define MISCHIEF_MANAGED EVENT_HANDLED
-#define MISCHIEF_NOT_MANAGED EVENT_NOT_HANDLED
-
Event current_event;
// at 0.016 ms per tick, 255 ticks = 4.08 s
static volatile uint16_t ticks_since_last_event = 0;
-// timeout durations in ticks (each tick 1/62th s)
-#ifndef HOLD_TIMEOUT
-#define HOLD_TIMEOUT 24
-#endif
-#ifndef RELEASE_TIMEOUT
-#define RELEASE_TIMEOUT 18
-#endif
+// maximum number of events which can be waiting at one time
+// (would probably be okay to reduce this to 4, but it's higher to be safe)
+#define EMISSION_QUEUE_LEN 16
+// was "volatile" before, changed to regular var since IRQ rewrites seem
+// to have removed the need for it to be volatile
+// no comment about "volatile emissions"
+Emission emissions[EMISSION_QUEUE_LEN];
+
+void append_emission(Event event, uint16_t arg);
+void delete_first_emission();
+void process_emissions();
+uint8_t emit_now(Event event, uint16_t arg);
+void emit(Event event, uint16_t arg);
+void emit_current_event(uint16_t arg);
+void empty_event_sequence();
+uint8_t push_event(uint8_t ev_type); // only for use by PCINT_inner()
+
+
+// TODO: Maybe move these to their own file...
+// ... this probably isn't the right place for delays.
+inline void interrupt_nice_delays();
+uint8_t nice_delay_ms(uint16_t ms);
+//uint8_t nice_delay_s();
+void delay_4ms(uint8_t ms);
+
/* Event structure
* Bit 7: 1 for a button input event, 0 for all others.
@@ -201,27 +231,4 @@ static volatile uint16_t ticks_since_last_event = 0;
#define EV_click15_hold_release (B_CLICK|B_HOLD|B_RELEASE|B_TIMEOUT|15)
-void empty_event_sequence();
-uint8_t push_event(uint8_t ev_type); // only for use by PCINT_inner()
-
-
-#define EMISSION_QUEUE_LEN 16
-// was "volatile" before, changed to regular var since IRQ rewrites seem
-// to have removed the need for it to be volatile
-// no comment about "volatile emissions"
-Emission emissions[EMISSION_QUEUE_LEN];
-
-void append_emission(Event event, uint16_t arg);
-void delete_first_emission();
-void process_emissions();
-//#define emit_now emit
-uint8_t emit_now(Event event, uint16_t arg);
-void emit(Event event, uint16_t arg);
-void emit_current_event(uint16_t arg);
-
-uint8_t nice_delay_ms(uint16_t ms);
-//uint8_t nice_delay_s();
-inline void interrupt_nice_delays();
-void delay_4ms(uint8_t ms);
-
#endif
diff --git a/spaghetti-monster/fsm-pcint.c b/spaghetti-monster/fsm-pcint.c
index 021a076..24cc82c 100644
--- a/spaghetti-monster/fsm-pcint.c
+++ b/spaghetti-monster/fsm-pcint.c
@@ -106,4 +106,6 @@ void PCINT_inner(uint8_t pressed) {
}
ticks_since_last_event = 0;
}
+
+
#endif