Skip to content

Commit

Permalink
Refactor gamecube_send synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
u1f992 committed Oct 22, 2024
1 parent 13fbb0b commit 534e080
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/jiangtun-core/src/jiangtun.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static jiangtun_bool_t process_input(jiangtun_t *j,
jiangtun_command_push(j->commands[i], c);
if (jiangtun_command_accepted(j->commands[i])) {
sprintf(j->buffer, "command #%lu accepted", (unsigned long)i);
serial_log(j, JIANGTUN_LOG_LEVEL_INFO, j->buffer);
serial_log(j, JIANGTUN_LOG_LEVEL_DEBUG, j->buffer);

if (!jiangtun_command_run(j->commands[i], &(j->reports[i]))) {
serial_log(j, JIANGTUN_LOG_LEVEL_WARN,
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ framework = arduino
board_build.core = earlephilhower
lib_deps =
adafruit/Adafruit NeoPixel@^1.12.3
https://github.com/u1f992/Bluewhale.git#inject-logger
https://github.com/u1f992/Bluewhale.git#fe88ecda920f0fd4e6cf9497f9168fde630a6758
debug_tool = cmsis-dap

[env:rp2040-release]
Expand All @@ -25,7 +25,7 @@ framework = arduino
board_build.core = earlephilhower
lib_deps =
adafruit/Adafruit NeoPixel@^1.12.3
https://github.com/u1f992/Bluewhale.git#inject-logger
https://github.com/u1f992/Bluewhale.git#fe88ecda920f0fd4e6cf9497f9168fde630a6758
debug_tool = cmsis-dap
build_flags =
-DNDEBUG
30 changes: 29 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static Gamecube_Data_t gamecube_data = defaultGamecubeData;
static bool gamecube_data_reset = false;
static mutex_t gamecube_data_mtx;
static bool current_reset_state = true; // to ensure initial releasing
static bool write_at_least_once = false;
static mutex_t write_at_least_once_mtx;

static Adafruit_NeoPixel pixels(1, PIN_XIAO_NEOPIXEL, NEO_GRB + NEO_KHZ800);
static jiangtun_board_t board;
Expand Down Expand Up @@ -64,6 +66,16 @@ static jiangtun_bool_t gamecube_send(jiangtun_board_t *board,
assert(board != NULL);
assert(report != NULL);

bool is_initialized = mutex_is_initialized(&write_at_least_once_mtx);
if (is_initialized) {
bool _write_at_least_once;
do {
mutex_enter_blocking(&write_at_least_once_mtx);
_write_at_least_once = write_at_least_once;
mutex_exit(&write_at_least_once_mtx);
} while (!_write_at_least_once);
}

if (changed) {
mutex_enter_blocking(&gamecube_data_mtx);
gamecube_data.report.a = report->a ? 1 : 0;
Expand All @@ -87,6 +99,15 @@ static jiangtun_bool_t gamecube_send(jiangtun_board_t *board,

gamecube_data_reset = report->reset ? true : false;
mutex_exit(&gamecube_data_mtx);

if (!is_initialized) {
write_at_least_once = false;
mutex_init(&write_at_least_once_mtx);
} else {
mutex_enter_blocking(&write_at_least_once_mtx);
write_at_least_once = false;
mutex_exit(&write_at_least_once_mtx);
}
}

return JIANGTUN_TRUE;
Expand Down Expand Up @@ -147,7 +168,8 @@ void loop() { jiangtun_loop(&j); }

void setup1() {
servo.attach(PIN_SERVO, 500, 2400);
while (!mutex_is_initialized(&gamecube_data_mtx))
/* Do not start `loop1` until the first `gamecube_send` (press start) */
while (!mutex_is_initialized(&write_at_least_once_mtx))
;
}

Expand All @@ -156,8 +178,10 @@ void loop1() {
bool ret = gamecube.write(gamecube_data);
bool reset = gamecube_data_reset;
mutex_exit(&gamecube_data_mtx);

if (!ret) {
Serial.println("[core2]\tfailed to send report");
return;
}

if (!(current_reset_state) && reset) {
Expand All @@ -169,4 +193,8 @@ void loop1() {
pinMode(PIN_RESET, INPUT);
}
current_reset_state = reset;

mutex_enter_blocking(&write_at_least_once_mtx);
write_at_least_once = true;
mutex_exit(&write_at_least_once_mtx);
}

0 comments on commit 534e080

Please sign in to comment.