send button data to fifo

This commit is contained in:
Andy Killorin 2025-04-03 10:49:16 -04:00
parent f5b7492de6
commit 38cfc8567a
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
3 changed files with 84 additions and 3 deletions

View file

@ -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<length; i++) {
fifo_put(buttons[i]);
}
}
uint8_t buttons_from_mask(Button *array, uint32_t buttons) {
uint8_t i, len;
len = 0;
for(i=0; i<9; i++) {
if (buttons & 1) {
array[len++] = (Button) i;
}
buttons >>= 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
}

View file

@ -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_ */

3
main.c
View file

@ -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};