diff --git a/Cargo.lock b/Cargo.lock index 7ef8aad..5ff2924 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,15 +127,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "crc-any" -version = "2.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774646b687f63643eb0f4bf13dc263cb581c8c9e57973b6ddf78bda3994d88df" -dependencies = [ - "debug-helper", -] - [[package]] name = "critical-section" version = "1.1.1" @@ -148,12 +139,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" -[[package]] -name = "debug-helper" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f578e8e2c440e7297e008bb5486a3a8a194775224bbc23729b0dbdfaeebf162e" - [[package]] name = "defmt" version = "0.3.2" @@ -623,29 +608,6 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" -[[package]] -name = "rp-pico" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aab28f6f4e19cec2d61b64cdd685e69794b81c579fd3b765579c46018fe616d0" -dependencies = [ - "cortex-m", - "cortex-m-rt", - "fugit", - "rp2040-boot2", - "rp2040-hal", - "usb-device", -] - -[[package]] -name = "rp2040-boot2" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c773ec49b836077aa144b58dc7654a243e1eecdb6cf0d25361ae7c7600fabd8" -dependencies = [ - "crc-any", -] - [[package]] name = "rp2040-hal" version = "0.8.0" @@ -713,7 +675,6 @@ dependencies = [ "panic-probe", "pio", "pio-proc", - "rp-pico", "rp2040-hal", ] diff --git a/Cargo.toml b/Cargo.toml index 2b03f6b..b6e5ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ defmt-rtt = "0.4.0" panic-probe = { version = "0.3.0", features = ["print-defmt"] } # We're using a Pico by default on this template -rp-pico = "0.7.0" rp2040-hal = { version="0.8", features=["rt", "critical-section-impl"] } pio-proc = "0.2" diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..5219252 --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +(cargo build --target thumbv6m-none-eabi --release && elf2uf2-rs target/thumbv6m-none-eabi/release/rustgcc) || echo "Build failed." \ No newline at end of file diff --git a/src/gccstate.rs b/src/gccstate.rs index fa3797e..a32a8ee 100644 --- a/src/gccstate.rs +++ b/src/gccstate.rs @@ -1,5 +1,11 @@ use embedded_hal::digital::v2::InputPin; -use rp2040_hal::gpio::{bank0::*, Pin, PinId, PullUp, PullUpInput}; +use rp2040_hal::gpio::{bank0::*, Pin, PullUpInput}; + +const MOD1_POS_VAL: u8 = 186; // 127 + 69 (max walk is 69) +const MOD1_NEG_VAL: u8 = 68; // 127 - 69 + +const MOD2_POS_VAL: u8 = 159; +const MOD2_NEG_VAL: u8 = 95; macro_rules! determine_stick_state { ($previous_state:ident, $current_state:ident, $button:ident) => { @@ -16,20 +22,54 @@ macro_rules! determine_stick_state { }; } +macro_rules! get_current_positive_val { + ($pin_state:ident, $pin_axis:ident, $modifier_state:ident) => { + match $pin_state.$pin_axis { + true => match $modifier_state { + ModifierState::MOD1 => MOD1_POS_VAL, + ModifierState::MOD2 => MOD2_POS_VAL, + ModifierState::NONE => 255, + }, + false => 127, + } + }; +} + +macro_rules! get_current_negative_val { + ($pin_state:ident, $pin_axis:ident, $modifier_state:ident) => { + match $pin_state.$pin_axis { + true => match $modifier_state { + ModifierState::MOD1 => MOD1_NEG_VAL, + ModifierState::MOD2 => MOD2_NEG_VAL, + ModifierState::NONE => 0, + }, + false => 127, + } + }; +} + +macro_rules! get_current_axis_val { + ($pin_state:ident, $pos_axis:ident, $neg_axis:ident, $modifier_state:ident) => { + match $pin_state.$neg_axis { + true => get_current_negative_val!($pin_state, $neg_axis, $modifier_state), + false => get_current_positive_val!($pin_state, $pos_axis, $modifier_state), + } + }; +} + macro_rules! determine_positive_stick_axis { ($stick_state:ident, $modifier_state:ident, $gccstate:ident, $pin_state:ident, $axis:ident, $neg_axis:ident) => { match $stick_state { StickButtonState::PRESSED => { $gccstate.$axis = match $modifier_state { - ModifierState::MOD1 => 159, // 127 + 32 - ModifierState::MOD2 => 191, // 127 + 64 - ModifierState::NONE => 255, // 127 + 128 + ModifierState::MOD1 => MOD1_POS_VAL, + ModifierState::MOD2 => MOD2_POS_VAL, + ModifierState::NONE => 255, } } - StickButtonState::RELEASED => match $pin_state.$neg_axis { - true => {} - false => $gccstate.$axis = 127, - }, + StickButtonState::RELEASED => { + $gccstate.$axis = get_current_negative_val!($pin_state, $neg_axis, $modifier_state) + } StickButtonState::UNCHANGED => {} } }; @@ -40,15 +80,14 @@ macro_rules! determine_negative_stick_axis { match $stick_state { StickButtonState::PRESSED => { $gccstate.$axis = match $modifier_state { - ModifierState::MOD1 => 95, // 127 - 32 - ModifierState::MOD2 => 63, // 127 - 64 - ModifierState::NONE => 0, // 127 - 128 + ModifierState::MOD1 => MOD1_NEG_VAL, + ModifierState::MOD2 => MOD2_NEG_VAL, + ModifierState::NONE => 0, } } - StickButtonState::RELEASED => match $pin_state.$pos_axis { - true => {} - false => $gccstate.$axis = 127, - }, + StickButtonState::RELEASED => { + $gccstate.$axis = get_current_positive_val!($pin_state, $pos_axis, $modifier_state) + } StickButtonState::UNCHANGED => {} } }; @@ -120,8 +159,8 @@ impl From for [u8; 8] { response[3] = value.lsticky; response[4] = value.rstickx; response[5] = value.rsticky; - response[6] = value.l as u8 * 127; - response[7] = value.r as u8 * 127; + response[6] = value.l as u8 * 255; + response[7] = value.r as u8 * 255; response } @@ -298,10 +337,10 @@ pub fn pinstate_to_gccstate( gccstate.a = current_pin_state.a; gccstate.b = current_pin_state.b; gccstate.x = current_pin_state.x; - gccstate.y = current_pin_state.y; - gccstate.l = current_pin_state.l; - gccstate.r = current_pin_state.r || current_pin_state.ddown; - gccstate.z = current_pin_state.zl || current_pin_state.zr || current_pin_state.dleft; + gccstate.y = current_pin_state.y || current_pin_state.zr; + gccstate.l = current_pin_state.l || current_pin_state.zr; + gccstate.r = current_pin_state.r || current_pin_state.ddown || current_pin_state.zl; + gccstate.z = current_pin_state.zl; let stick_to_dpad = current_pin_state.q || current_pin_state.e; @@ -315,18 +354,52 @@ pub fn pinstate_to_gccstate( gccstate.dright = current_pin_state.right; return gccstate; + } else { + gccstate.dup = false; + gccstate.ddown = false; + gccstate.dleft = false; + gccstate.dright = false; } // MOD1 = soft modifier, makes stick go half length // MOD2 = hard modifier, makes stick go quarter length - let modifier_state = match current_pin_state.shift || current_pin_state.dup { + let modifier_state = match current_pin_state.shift { true => ModifierState::MOD1, - false => match current_pin_state.ddown { + false => match current_pin_state.dup { true => ModifierState::MOD2, false => ModifierState::NONE, }, }; + // turnaround up tilt angles + match determine_stick_state!(previous_pin_state, current_pin_state, dleft) { + StickButtonState::PRESSED | StickButtonState::UNCHANGED => { + if current_pin_state.dleft { + gccstate.rsticky = 255; + gccstate.rstickx = 53; + } + } + StickButtonState::RELEASED => { + gccstate.rsticky = get_current_axis_val!(current_pin_state, cup, cdown, modifier_state); + gccstate.rstickx = + get_current_axis_val!(current_pin_state, cleft, cright, modifier_state); + } + } + + match determine_stick_state!(previous_pin_state, current_pin_state, dright) { + StickButtonState::PRESSED | StickButtonState::UNCHANGED => { + if current_pin_state.dright { + gccstate.rsticky = 255; + gccstate.rstickx = 201; + } + } + StickButtonState::RELEASED => { + gccstate.rsticky = get_current_axis_val!(current_pin_state, cup, cdown, modifier_state); + gccstate.rstickx = + get_current_axis_val!(current_pin_state, cleft, cright, modifier_state); + } + } + // determine down / up presses for stick buttons let lstick_up_state = determine_stick_state!(previous_pin_state, current_pin_state, up); let lstick_down_state = determine_stick_state!(previous_pin_state, current_pin_state, down); @@ -406,27 +479,33 @@ pub fn pinstate_to_gccstate( cright ); - // clamp lstickx if lsticky is 0 or 255 for uptilt and downtilt angles + // clamp lstickx if lsticky is 0 or 255, so that if we hit A, + // we get an up or down attack instead of left or right + // comment this if you want ftilt/-smash angles instead if gccstate.lsticky <= 1 || gccstate.lsticky >= 254 { - gccstate.lstickx = gccstate.lstickx.max(53).min(201); + gccstate.lstickx = gccstate.lstickx.max(18).min(236); } else { gccstate.lstickx = match gccstate.lstickx { - 201 => 255, - 53 => 0, + 236 => 255, + 18 => 0, _ => gccstate.lstickx, } } - // do the same for cstick - if gccstate.rsticky <= 1 || gccstate.rsticky >= 254 { - gccstate.rstickx = gccstate.rstickx.max(53).min(201); - } else { - gccstate.rstickx = match gccstate.rstickx { - 201 => 255, - 53 => 0, - _ => gccstate.rstickx, - } - } + // C-stick logic, however this is kinda useless since the game won't + // let you do turnaround up/down tilt like this + // + // so for C-stick it's usually better to just keep the angles the defaults, + // in order to easily perform angled f-tilt's / smashes + // if gccstate.rsticky <= 1 || gccstate.rsticky >= 254 { + // gccstate.rstickx = gccstate.rstickx.max(18).min(236); + // } else { + // gccstate.rstickx = match gccstate.rstickx { + // 201 => 255, + // 53 => 0, + // _ => gccstate.rstickx, + // } + // } gccstate } diff --git a/src/joybus.rs b/src/joybus.rs index fa64188..dcc7a0d 100644 --- a/src/joybus.rs +++ b/src/joybus.rs @@ -1,19 +1,16 @@ use cortex_m::delay::Delay; -use embedded_hal::digital::v2::{InputPin, OutputPin}; -use rp2040_hal::gpio::{bank0::BankPinId, PinId}; -use rp_pico::{ - hal::{ - pio::{ - InstalledProgram, Rx, StateMachine, StateMachineIndex, Stopped, Tx, UninitStateMachine, - PIO, - }, - prelude::_rphal_pio_PIOExt, - }, + +use rp2040_hal::{ + gpio::{bank0::BankPinId, PinId}, pac::{PIO0, RESETS}, + pio::{ + InstalledProgram, PIOBuilder, Rx, StateMachine, StateMachineIndex, Stopped, Tx, + UninitStateMachine, PIO, + }, + prelude::_rphal_pio_PIOExt, }; use crate::{ - gccstate::GccState, input::CURRENT_STATE, pio::{to_pio_response, PioResponse}, }; @@ -59,10 +56,10 @@ where PE: _rphal_pio_PIOExt, SM: StateMachineIndex, { - let (sm, rx, tx) = rp_pico::hal::pio::PIOBuilder::from_program(installed) + let (sm, rx, tx) = PIOBuilder::from_program(installed) .set_pins(pin_id, 1) - .in_shift_direction(rp_pico::hal::pio::ShiftDirection::Left) - .out_shift_direction(rp_pico::hal::pio::ShiftDirection::Right) + .in_shift_direction(rp2040_hal::pio::ShiftDirection::Left) + .out_shift_direction(rp2040_hal::pio::ShiftDirection::Right) .pull_threshold(32) .push_threshold(8) .clock_divisor_fixed_point(5, 0) diff --git a/src/main.rs b/src/main.rs index 26c4739..9984f64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,7 +81,7 @@ fn main() -> ! { .unwrap(); datatransfer( - pins.gpio28.into_pull_up_input(), + pins.gpio24.into_pull_up_input(), pac.PIO0, &mut pac.RESETS, delay,