Skip to content

Commit

Permalink
Maybe fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
u1f992 committed Nov 21, 2023
1 parent 64b5c06 commit ef4f50a
Showing 1 changed file with 135 additions and 43 deletions.
178 changes: 135 additions & 43 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

static const int SERIAL_INACTIVE_TIMEOUT = 100;
static int inactive_count = 0;
static mutex_t serial1_mutex;

static CGamecubeConsole console(5);
static Servo servo;
Expand All @@ -32,6 +33,81 @@ static NxamfBytesBuffer *buffer;

static NxamfButtonState reset_state = NXAMF_BUTTON_STATE_RELEASED;

static const char *_button_to_string(NxamfButtonState state)
{
switch (state)
{
case NXAMF_BUTTON_STATE_RELEASED:
return "RELEASED";
case NXAMF_BUTTON_STATE_PRESSED:
return "PRESSED";
default:
return "[Unknown]";
}
}

static const char *_hat_to_string(NxamfHatState state)
{
switch (state)
{
case NXAMF_HAT_STATE_UP:
return "UP";
case NXAMF_HAT_STATE_UPRIGHT:
return "UPRIGHT";
case NXAMF_HAT_STATE_RIGHT:
return "RIGHT";
case NXAMF_HAT_STATE_DOWNRIGHT:
return "DOWNRIGHT";
case NXAMF_HAT_STATE_DOWN:
return "DOWN";
case NXAMF_HAT_STATE_DOWNLEFT:
return "DOWNLEFT";
case NXAMF_HAT_STATE_LEFT:
return "LEFT";
case NXAMF_HAT_STATE_UPLEFT:
return "UPLEFT";
case NXAMF_HAT_STATE_NEUTRAL:
return "NEUTRAL";
default:
return "[Unknown]";
}
}

static char _format_state_buffer[1024];
static char *format_state(const NxamfGamepadState *state)
{
snprintf(_format_state_buffer, 1024,
"Gamepad State:\n"
" Y: %s\t"
"B: %s\t"
"A: %s\t"
"X: %s\n"
" L: %s\t"
"R: %s\t"
"ZL: %s\t"
"ZR: %s\n"
" Minus: %s\t"
"Plus: %s\t"
"L Click: %s\t"
"R Click: %s\n"
" Home: %s\t"
"Capture: %s\n"
" Hat: %s\n"
" Left Stick: (x=%u, y=%u)\n"
" Right Stick: (x=%u, y=%u)\n",
_button_to_string(state->y), _button_to_string(state->b),
_button_to_string(state->a), _button_to_string(state->x),
_button_to_string(state->l), _button_to_string(state->r),
_button_to_string(state->zl), _button_to_string(state->zr),
_button_to_string(state->minus), _button_to_string(state->plus),
_button_to_string(state->l_click), _button_to_string(state->r_click),
_button_to_string(state->home), _button_to_string(state->capture),
_hat_to_string(state->hat),
state->l_stick.x, state->l_stick.y, state->r_stick.x, state->r_stick.y);

return _format_state_buffer;
}

static int64_t led_off(alarm_id_t id, void *user_data)
{
digitalWrite(LED_BUILTIN, LOW);
Expand All @@ -40,7 +116,7 @@ static int64_t led_off(alarm_id_t id, void *user_data)

static void async_led_on_for_100ms()
{
digitalWriteFast(LED_BUILTIN, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
alarm_id_t alarm_id = add_alarm_in_ms(100, led_off, NULL, false);
}

Expand All @@ -54,7 +130,7 @@ static void reflect_state(NxamfGamepadState *state)
async_led_on_for_100ms();

mutex_enter_blocking(&d_mutex);

d.report.y = (uint8_t)state->y;
d.report.b = (uint8_t)state->b;
d.report.a = (uint8_t)state->a;
Expand Down Expand Up @@ -154,28 +230,69 @@ void setup()
{
Serial.begin(9600);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#ifndef NDEBUG
mutex_init(&serial1_mutex);
mutex_enter_blocking(&serial1_mutex);
Serial1.setTX(0);
Serial1.setRX(1);
Serial1.begin();
mutex_exit(&serial1_mutex);
#define Serial1_println(msg) \
mutex_enter_blocking(&serial1_mutex); \
Serial1.println(msg); \
mutex_exit(&serial1_mutex);
#else
#define Serial1_println(msg) ((void)0)
#endif

// Setup for SG90
servo.attach(PIN_SERVO, 500, 2400);

mutex_init(&d_mutex);
mutex_enter_blocking(&d_mutex);
d.report.a = 0;
d.report.b = 0;
d.report.x = 0;
d.report.y = 0;
d.report.start = 0;
d.report.dleft = 0;
d.report.dright = 0;
d.report.ddown = 0;
d.report.dup = 0;
d.report.z = 0;
d.report.r = 0;
d.report.l = 0;
d.report.xAxis = 128;
d.report.yAxis = 128;
d.report.cxAxis = 128;
d.report.cyAxis = 128;
d.report.left = 0;
d.report.right = 0;

// Omajinai to recognize the controller
d.report.start = 1;
console.write(d);
d.report.start = 0;
console.write(d);
mutex_exit(&d_mutex);

nxmc2 = nxmc2_protocol_new();
pokecon = pokecon_protocol_new();
orca = orca_protocol_new();
protocols[0] = (NxamfBytesProtocolInterface *)nxmc2;
protocols[1] = (NxamfBytesProtocolInterface *)pokecon;
protocols[2] = (NxamfBytesProtocolInterface *)orca;
mux = nxamf_protocol_multiplexer_new(protocols, 3);
if (nxmc2 == NULL || pokecon == NULL || orca == NULL || mux == NULL)
{
abort();
}
buffer = nxamf_bytes_buffer_new((NxamfBytesProtocolInterface *)mux);
if (buffer == NULL)
{
abort();
}
assert(
nxmc2 != NULL &&
pokecon != NULL &&
orca != NULL &&
mux != NULL &&
buffer != NULL);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}

void loop()
Expand All @@ -193,47 +310,22 @@ void loop()
inactive_count = 0;

uint8_t packet = Serial.read();
Serial1_println(packet);
NxamfGamepadState *state = nxamf_bytes_buffer_append(buffer, packet);
if (state == NULL)
{
return;
}

Serial1_println(format_state(state));
reflect_state(state);

nxamf_gamepad_state_delete(state);
state = NULL;
}

void setup1()
{
mutex_init(&d_mutex);

mutex_enter_blocking(&d_mutex);

d.report.a = 0;
d.report.b = 0;
d.report.x = 0;
d.report.y = 0;
d.report.start = 0;
d.report.dleft = 0;
d.report.dright = 0;
d.report.ddown = 0;
d.report.dup = 0;
d.report.z = 0;
d.report.r = 0;
d.report.l = 0;
d.report.xAxis = 128;
d.report.yAxis = 128;
d.report.cxAxis = 128;
d.report.cyAxis = 128;
d.report.left = 0;
d.report.right = 0;

// Omajinai to recognize the controller
d.report.start = 1;
console.write(d);
d.report.start = 0;
console.write(d);

mutex_exit(&d_mutex);
}

void loop1()
Expand All @@ -244,6 +336,6 @@ void loop1()

if (!ret)
{
Serial.println("GC is not powered on or not connected.");
Serial1_println("GC is not powered on or not connected.");
}
}

0 comments on commit ef4f50a

Please sign in to comment.