-
Notifications
You must be signed in to change notification settings - Fork 0
Reduce thread yield #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f028b23
77a7517
bf3d8ea
2e00758
ce9546c
0e1dfd8
d2d9f98
bc6574e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #define sda_pin 21 | ||
| #define scl_pin 20 | ||
| #define frequency 400000 | ||
|
|
||
| void setup_i2c(); | ||
| void start_game(); | ||
| void cycle_choice(); | ||
| void confirm_choice(); | ||
|
|
||
| enum GameState { | ||
| STATE_MENU, | ||
| STATE_PLAYING, | ||
| STATE_OVER_LOST, | ||
| STATE_OVER_WIN, | ||
| }; | ||
|
|
||
| enum ActionType { | ||
| Action_Select, | ||
| Action_Confirm, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #include "main.h" | ||
| #include <display.h> | ||
| #include <esp_log.h> | ||
| #include <driver/i2c.h> | ||
|
|
@@ -8,19 +9,50 @@ | |
| #include <sound.h> | ||
| #include <esp_random.h> | ||
|
|
||
| #define sda_pin 21 | ||
| #define scl_pin 20 | ||
| #define frequency 400000 | ||
|
|
||
| void setup_i2c(); | ||
| void start_game(); | ||
| void cycle_choice(); | ||
| void confirm_choice(); | ||
|
|
||
| rps_choice playerChoice = ROCK; // Default choice is rock, player can cycle through choices | ||
| enum GameState currentState = STATE_MENU; | ||
|
|
||
| // Game flow: main menu -> game screen -> result screen -> back to game screen (if not win) | ||
|
|
||
| QueueHandle_t actionQueue; | ||
|
|
||
| void queueHandle() { | ||
| while(1) { | ||
| enum ActionType actionOpt; | ||
| if(xQueueReceive(actionQueue, &actionOpt, portMAX_DELAY) == pdTRUE) { | ||
| if(actionOpt == Action_Confirm) { | ||
| switch(currentState) { | ||
| case STATE_MENU: | ||
| start_game(); | ||
| break; | ||
| case STATE_PLAYING: | ||
| confirm_choice(); | ||
| break; | ||
| case STATE_OVER_LOST: | ||
| start_game(); | ||
| break; | ||
| case STATE_OVER_WIN: | ||
| break; | ||
| } | ||
| } | ||
| if(actionOpt == Action_Select && currentState == STATE_PLAYING) | ||
|
Comment on lines
+28
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Rapidly pressing the confirm button queues multiple actions. The sequential processing of these actions causes the result screen to be immediately overwritten by the new game screen. Suggested FixTo prevent this, consider clearing the action queue after a significant state transition. For example, after processing an action that moves the game from Prompt for AI Agent |
||
| cycle_choice(); | ||
| } | ||
| else | ||
| vTaskDelay(pdMS_TO_TICKS(5)); | ||
| } | ||
| } | ||
|
|
||
| void selectHandle() { | ||
| enum ActionType curAction = Action_Select; | ||
| xQueueSend(actionQueue, &curAction, 0); | ||
| } | ||
|
|
||
| void confirmHandle() { | ||
| enum ActionType curAction = Action_Confirm; | ||
| xQueueSend(actionQueue, &curAction, 0); | ||
| } | ||
|
|
||
| void app_main() { | ||
| // Initialize all components | ||
| ESP_LOGI("main", "Starting application"); | ||
|
|
@@ -29,13 +61,17 @@ void app_main() { | |
| display_init(); | ||
| btnctrl_init(); | ||
| init_sound(); | ||
|
|
||
| // Queue Managers | ||
| actionQueue = xQueueCreate(8, sizeof(enum ActionType)); | ||
| xTaskCreate(queueHandle, "main_control_handle", 4096, NULL, 5, NULL); | ||
|
|
||
| set_led(1); // Turn on LED to indicate device is ready | ||
|
|
||
| // Initiate main menu | ||
| init_main_menu(); | ||
| play_main_menu_sound(); | ||
| btnctrl_register_event(NULL, start_game); | ||
| play_sound(SOUND_MAIN_MENU); | ||
| btnctrl_register_event(selectHandle, confirmHandle); | ||
| } | ||
|
|
||
| void setup_i2c() { | ||
|
|
@@ -56,42 +92,38 @@ void setup_i2c() { | |
|
|
||
| // Initialize game screen and register button events for cycling and confirming choice | ||
| void start_game() { | ||
| btnctrl_unregister_event(); // Clear main menu event | ||
|
|
||
| init_game_screen(playerChoice); | ||
| play_select_sound(); | ||
|
|
||
| btnctrl_register_event(cycle_choice, confirm_choice); | ||
| play_sound(SOUND_SELECT); | ||
| currentState = STATE_PLAYING; | ||
| } | ||
|
|
||
| // Update game screen with current player choice when cycle button is pressed | ||
| void cycle_choice() { | ||
| playerChoice = (playerChoice + 1) % 3; // Cycle through ROCK, PAPER, SCISSORS | ||
| update_game_screen(playerChoice); | ||
| play_select_sound(); | ||
| play_sound(SOUND_SELECT); | ||
| } | ||
|
|
||
|
|
||
| // Determine and display the game outcome | ||
| void confirm_choice() { | ||
| btnctrl_unregister_event(); // Clear start_game events | ||
|
|
||
| rps_choice cpuChoice = (rps_choice)(esp_random() % 3); // Generate random CPU choice | ||
| rps_outcome outcome = determine_rps_outcome(playerChoice, cpuChoice); // Determine game outcome | ||
| init_result_screen(outcome, playerChoice, cpuChoice); // Update screen with outcome and choices | ||
|
|
||
| // Play sound based on outcome and register event to start new game if not a win | ||
| switch (outcome) { | ||
| case WIN: | ||
| play_win_sound(); | ||
| play_sound(SOUND_WIN); | ||
| currentState = STATE_OVER_WIN; | ||
| break; | ||
| case LOSE: | ||
| play_lost_sound(); | ||
| btnctrl_register_event(NULL, start_game); | ||
| play_sound(SOUND_LOST); | ||
| currentState = STATE_OVER_LOST; | ||
| break; | ||
| case DRAW: | ||
| play_draw_sound(); | ||
| btnctrl_register_event(NULL, start_game); | ||
| play_sound(SOUND_DRAW); | ||
| currentState = STATE_OVER_LOST; | ||
| break; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.