From 38cfc8567a87b959795144fb7ce6ada2c1e8e0d1 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 3 Apr 2025 10:49:16 -0400 Subject: [PATCH] send button data to fifo --- buttons.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- buttons.h | 22 ++++++++++++++++++++ main.c | 3 +++ 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/buttons.c b/buttons.c index 22eb0b4..0a14ab5 100644 --- a/buttons.c +++ b/buttons.c @@ -182,6 +182,7 @@ void ButtonISR(void) { ButtonReadJoystick(); // Convert joystick state to button presses. The result is in gButtons. uint32_t presses = ~old_buttons & gButtons; // detect button presses (transitions from not pressed to pressed) presses |= ButtonAutoRepeat(); // autorepeat presses if a button is held long enough +#ifdef DISPLAY_TIME static bool tic = false; static bool running = true; if (presses & 1) { // EK-TM4C1294XL button 1 pressed @@ -190,11 +191,66 @@ void ButtonISR(void) { if (presses & 2) { // EK-TM4C1294XL button 2 pressed gTime = 0; } - if (presses & 4) { // boosterpack button 1 pressed - drawRequested = 1; - } if (running) { if (tic) gTime++; // increment time every other ISR call tic = !tic; } +#endif // DISPLAY_TIME + Button buttons[9]; + uint8_t length, i; + length = buttons_from_mask(buttons, presses); + for (i=0; i>= 1; + } + + return len; +} + +volatile DataType button_fifo[FIFO_SIZE]; // FIFO storage array +volatile int fifo_head = 0; // index of the first item in the FIFO +volatile int fifo_tail = 0; // index one step past the last item + +// put data into the FIFO, skip if full +// returns 1 on success, 0 if FIFO was full +int fifo_put(DataType data) +{ + int new_tail = fifo_tail + 1; + if (new_tail >= FIFO_SIZE) new_tail = 0; // wrap around + if (fifo_head != new_tail) { // if the FIFO is not full + button_fifo[fifo_tail] = data; // store data into the FIFO + fifo_tail = new_tail; // advance FIFO tail index + return 1; // success + } + return 0; // full +} + +// get data from the FIFO +// returns 1 on success, 0 if FIFO was empty +int fifo_get(DataType *data) +{ + if (fifo_head != fifo_tail) { // if the FIFO is not empty + *data = button_fifo[fifo_head]; // read data from the FIFO + + if (fifo_head >= FIFO_SIZE-1) { + fifo_head = 0; // reset if past end + } else { + fifo_head++; // increment if within buffer + } + + return 1; // success + } + return 0; // empty } diff --git a/buttons.h b/buttons.h index 4a4e799..0c84998 100644 --- a/buttons.h +++ b/buttons.h @@ -52,4 +52,26 @@ void ButtonReadJoystick(void); // autorepeat button presses if a button is held long enough uint32_t ButtonAutoRepeat(void); +typedef enum { + SW1 = 0, + SW2, + S1, + S2, + Select, + Right, + Left, + Up, + Down, +} Button; + +// convert mask into array of buttons +// +// returns length +uint8_t buttons_from_mask(Button *array, uint32_t buttons); + +#define FIFO_SIZE 11 // FIFO capacity is 1 item fewer +typedef Button DataType; // FIFO data type +int fifo_put(DataType data); +int fifo_get(DataType *data); + #endif /* BUTTONS_H_ */ diff --git a/main.c b/main.c index 634b4cb..4c11f3b 100644 --- a/main.c +++ b/main.c @@ -71,8 +71,11 @@ int main(void) { GrContextInit(&sContext, &g_sCrystalfontz128x128); // Initialize the grlib graphics context GrContextFontSet(&sContext, &g_sFontFixed6x8); // select font +#ifdef DISPLAY_TIME uint32_t time; // local copy of gTime char str[50]; // string buffer +#endif // DISPLAY_TIME + // full-screen rectangle tRectangle rectFullScreen = {0, 0, GrContextDpyWidthGet(&sContext)-1, GrContextDpyHeightGet(&sContext)-1};