gamebridge: Twitch Control #2

Merged
cadey merged 29 commits from gamebridge into master 2020-05-11 22:40:05 +00:00
3 changed files with 62 additions and 34 deletions
Showing only changes of commit d05b467578 - Show all commits

View File

@ -32,18 +32,18 @@ impl Lerper {
let scalar = self.scalar; let scalar = self.scalar;
self.scalar = match scalar { self.scalar = match scalar {
_ if scalar == self.goal => self.goal, _ if scalar == self.goal => self.goal,
_ if scalar == self.max => { _ if scalar >= self.max => {
self.extended_tick = now; self.extended_tick = now;
scalar - 1 scalar - 1
}, }
_ if scalar == self.min => { _ if scalar <= self.min => {
self.extended_tick = now; self.extended_tick = now;
scalar - 1 scalar + 1
}, }
_ => { _ => {
let t = (now - self.extended_tick) as f64 / self.lerp_time; let t = (now - self.extended_tick) as f64 / self.lerp_time;
lerp(self.scalar, 0, t) lerp(self.scalar, 0, t)
}, }
}; };
if self.scalar >= self.max { if self.scalar >= self.max {
@ -54,7 +54,6 @@ impl Lerper {
return self.min; return self.min;
} }
log::info!("before: {}, after: {}", scalar, self.scalar);
self.scalar self.scalar
} }
@ -87,13 +86,14 @@ mod test {
use super::Lerper; use super::Lerper;
let mut lerper = Lerper::init(15.0, 127, -128, 0); let mut lerper = Lerper::init(15.0, 127, -128, 0);
for case in [(127, 3, 126), (100, 8, 66)].iter() { for case in [(127, 3, 126), (100, 8, 66), (-124, 8, -82)].iter() {
let scalar = case.0; let scalar = case.0;
let now = case.1; let now = case.1;
let want = case.2; let want = case.2;
lerper.update(scalar); lerper.update(scalar);
assert_eq!(lerper.apply(now), want); let result = lerper.apply(now);
assert_eq!(result, want);
} }
} }
} }

View File

@ -42,7 +42,7 @@ fn main() -> Result<()> {
let mut vblank = File::open("vblank")?; let mut vblank = File::open("vblank")?;
let mut input = OpenOptions::new().write(true).open("input")?; let mut input = OpenOptions::new().write(true).open("input")?;
const STICK_LERP_TIME: f64 = 330.0; // 330 frames to lerp stick positions down to 0 const STICK_LERP_TIME: f64 = 270.0; // 270 frames to lerp stick positions down to 0
const BUTTON_LERP_TIME: f64 = 20.0; // 20 frames to lerp button inputs down to 0 const BUTTON_LERP_TIME: f64 = 20.0; // 20 frames to lerp button inputs down to 0
let st = { let st = {
@ -53,7 +53,7 @@ fn main() -> Result<()> {
sticky: Lerper::init(STICK_LERP_TIME, 127, -128, 0), sticky: Lerper::init(STICK_LERP_TIME, 127, -128, 0),
a_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0), a_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0),
b_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0), b_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0),
z_button: Lerper::init(BUTTON_LERP_TIME / 4.0, 64, -1, 0), // z button is special z_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0),
r_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0), r_button: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0),
start: Lerper::init(BUTTON_LERP_TIME / 4.0, 64, -1, 0), // z button is special start: Lerper::init(BUTTON_LERP_TIME / 4.0, 64, -1, 0), // z button is special
c_left: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0), c_left: Lerper::init(BUTTON_LERP_TIME, 64, -1, 0),
@ -93,8 +93,12 @@ fn main() -> Result<()> {
//data.stickx.update(data.controller[2] as i64); //data.stickx.update(data.controller[2] as i64);
//data.sticky.update(data.controller[3] as i64); //data.sticky.update(data.controller[3] as i64);
debug!("x before: {}", data.stickx.scalar);
let mut stickx_scalar = data.stickx.apply(frame) as i8; let mut stickx_scalar = data.stickx.apply(frame) as i8;
debug!("x after: {}", data.stickx.scalar);
debug!("y before: {}", data.sticky.scalar);
let mut sticky_scalar = data.sticky.apply(frame) as i8; let mut sticky_scalar = data.sticky.apply(frame) as i8;
debug!("y after: {}", data.sticky.scalar);
let dist = stick_distance(stickx_scalar, sticky_scalar); let dist = stick_distance(stickx_scalar, sticky_scalar);
if dist <= 10 { if dist <= 10 {
@ -106,30 +110,53 @@ fn main() -> Result<()> {
let mut hi = HiButtons::NONE; let mut hi = HiButtons::NONE;
let mut lo = LoButtons::NONE; let mut lo = LoButtons::NONE;
const BUTTON_PUSH_THRESHOLD: i64 = 16; const BUTTON_PUSH_THRESHOLD: i64 = 2;
// high buttons // high buttons
data.a_button.apply(frame); data.a_button.apply(frame);
if data.a_button.pressed(BUTTON_PUSH_THRESHOLD) { hi = hi | HiButtons::A_BUTTON; } if data.a_button.pressed(BUTTON_PUSH_THRESHOLD) {
hi = hi | HiButtons::A_BUTTON;
}
data.b_button.apply(frame); data.b_button.apply(frame);
if data.b_button.pressed(BUTTON_PUSH_THRESHOLD) { hi = hi | HiButtons::B_BUTTON; } if data.b_button.pressed(BUTTON_PUSH_THRESHOLD) {
hi = hi | HiButtons::B_BUTTON;
}
data.z_button.apply(frame); data.z_button.apply(frame);
if data.z_button.pressed(BUTTON_PUSH_THRESHOLD) { hi = hi | HiButtons::Z_BUTTON; } if data.z_button.pressed(BUTTON_PUSH_THRESHOLD) {
hi = hi | HiButtons::Z_BUTTON;
}
data.start.apply(frame); data.start.apply(frame);
if data.start.pressed(BUTTON_PUSH_THRESHOLD) { hi = hi | HiButtons::START; } if data.start.pressed(BUTTON_PUSH_THRESHOLD) {
info!("start: {}", data.start.scalar); hi = hi | HiButtons::START;
}
data.r_button.apply(frame); data.r_button.apply(frame);
if data.r_button.pressed(BUTTON_PUSH_THRESHOLD) { lo = lo | LoButtons::R_BUTTON; } if data.r_button.pressed(BUTTON_PUSH_THRESHOLD) {
lo = lo | LoButtons::R_BUTTON;
}
data.c_up.apply(frame); data.c_up.apply(frame);
if data.c_up.pressed(BUTTON_PUSH_THRESHOLD) { lo = lo | LoButtons::C_UP; } if data.c_up.pressed(BUTTON_PUSH_THRESHOLD) {
lo = lo | LoButtons::C_UP;
}
data.c_down.apply(frame); data.c_down.apply(frame);
if data.c_down.pressed(BUTTON_PUSH_THRESHOLD) { lo = lo | LoButtons::C_DOWN; } if data.c_down.pressed(BUTTON_PUSH_THRESHOLD) {
lo = lo | LoButtons::C_DOWN;
}
data.c_left.apply(frame); data.c_left.apply(frame);
if data.c_left.pressed(BUTTON_PUSH_THRESHOLD) { lo = lo | LoButtons::C_LEFT; } if data.c_left.pressed(BUTTON_PUSH_THRESHOLD) {
lo = lo | LoButtons::C_LEFT;
}
data.c_right.apply(frame); data.c_right.apply(frame);
if data.c_right.pressed(BUTTON_PUSH_THRESHOLD) { lo = lo | LoButtons::C_RIGHT; } if data.c_right.pressed(BUTTON_PUSH_THRESHOLD) {
lo = lo | LoButtons::C_RIGHT;
}
println!(
"[ rust] {:02x}{:02x} {:02x}{:02x}",
hi.bits(),
lo.bits(),
stickx_scalar as u8,
sticky_scalar as u8
);
controller[0] = hi.bits() as u8; controller[0] = hi.bits() as u8;
controller[1] = lo.bits() as u8; controller[1] = lo.bits() as u8;
controller[2] = stickx_scalar as u8; controller[2] = stickx_scalar as u8;

View File

@ -80,18 +80,19 @@ async fn run_loop(
let chatline = msg.data.to_string(); let chatline = msg.data.to_string();
let chatline = chatline.to_ascii_lowercase(); let chatline = chatline.to_ascii_lowercase();
let mut data = st.write().unwrap(); let mut data = st.write().unwrap();
const BUTTON_ADD_AMT: i64 = 64;
match chatline.as_str() { match chatline.as_str() {
"a" => data.a_button.add(1024), "a" => data.a_button.add(BUTTON_ADD_AMT),
"b" => data.b_button.add(1024), "b" => data.b_button.add(BUTTON_ADD_AMT),
"z" => data.z_button.add(1024), "z" => data.z_button.add(BUTTON_ADD_AMT),
"r" => data.r_button.add(1024), "r" => data.r_button.add(BUTTON_ADD_AMT),
"cup" => data.c_up.add(1024), "cup" => data.c_up.add(BUTTON_ADD_AMT),
"cdown" => data.c_down.add(1024), "cdown" => data.c_down.add(BUTTON_ADD_AMT),
"cleft" => data.c_left.add(1024), "cleft" => data.c_left.add(BUTTON_ADD_AMT),
"cright" => data.c_right.add(1024), "cright" => data.c_right.add(BUTTON_ADD_AMT),
"start" => data.start.add(1024), "start" => data.start.add(BUTTON_ADD_AMT),
"up" => data.stickx.add(127), "up" => data.sticky.add(127),
"down" => data.sticky.add(-128), "down" => data.sticky.add(-128),
"left" => data.stickx.add(-128), "left" => data.stickx.add(-128),
"right" => data.stickx.add(127), "right" => data.stickx.add(127),