From d12bae58216080e8ca59d06390e0389a6ae0e23d Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Mon, 4 Jul 2022 16:20:38 +0800 Subject: [PATCH] motion-control: code dedup --- cmd/motion-control/main+gy801.ha | 28 ---------------------------- cmd/motion-control/main+joycon.ha | 28 ---------------------------- cmd/motion-control/state.ha | 30 ++++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 56 deletions(-) create mode 100644 cmd/motion-control/state.ha diff --git a/cmd/motion-control/main+gy801.ha b/cmd/motion-control/main+gy801.ha index 09e190d..0dfca16 100644 --- a/cmd/motion-control/main+gy801.ha +++ b/cmd/motion-control/main+gy801.ha @@ -17,34 +17,6 @@ use strconv; use time; use unix::signal; -type state = enum u8 { - NORMAL, - BLOCK, - RESET, - EXIT, -}; - -let global_state: state = state::NORMAL; - -fn sigint(sig: int, info: *signal::siginfo, ucontext: *void) void = { - global_state = state::EXIT; -}; - -fn sigusr1(sig: int, info: *signal::siginfo, ucontext: *void) void = { - switch (global_state) { - case state::NORMAL => - fmt::println("Block")!; - global_state = state::BLOCK; - case state::BLOCK => - fmt::println("Reset")!; - global_state = state::RESET; - case state::RESET => - fmt::println("Reset is already pending")!; - case state::EXIT => - fmt::println("Exit pending")!; - }; -}; - export fn main() int = { const help: []getopt::help = [ "control pointerd with motion", diff --git a/cmd/motion-control/main+joycon.ha b/cmd/motion-control/main+joycon.ha index 9e6f9f0..bdf9287 100644 --- a/cmd/motion-control/main+joycon.ha +++ b/cmd/motion-control/main+joycon.ha @@ -16,34 +16,6 @@ use strconv; use time; use unix::signal; -type state = enum u8 { - NORMAL, - BLOCK, - RESET, - EXIT, -}; - -let global_state: state = state::NORMAL; - -fn sigint(sig: int, info: *signal::siginfo, ucontext: *void) void = { - global_state = state::EXIT; -}; - -fn sigusr1(sig: int, info: *signal::siginfo, ucontext: *void) void = { - switch (global_state) { - case state::NORMAL => - fmt::println("Block")!; - global_state = state::BLOCK; - case state::BLOCK => - fmt::println("Reset")!; - global_state = state::RESET; - case state::RESET => - fmt::println("Reset is already pending")!; - case state::EXIT => - fmt::println("Exit pending")!; - }; -}; - export fn main() int = { const help: []getopt::help = [ "control pointerd with motion", diff --git a/cmd/motion-control/state.ha b/cmd/motion-control/state.ha new file mode 100644 index 0000000..840f081 --- /dev/null +++ b/cmd/motion-control/state.ha @@ -0,0 +1,30 @@ +use fmt; +use unix::signal; + +type state = enum u8 { + NORMAL, + BLOCK, + RESET, + EXIT, +}; + +let global_state: state = state::NORMAL; + +fn sigint(sig: int, info: *signal::siginfo, ucontext: *void) void = { + global_state = state::EXIT; +}; + +fn sigusr1(sig: int, info: *signal::siginfo, ucontext: *void) void = { + switch (global_state) { + case state::NORMAL => + fmt::println("Block")!; + global_state = state::BLOCK; + case state::BLOCK => + fmt::println("Reset")!; + global_state = state::RESET; + case state::RESET => + fmt::println("Reset is already pending")!; + case state::EXIT => + fmt::println("Exit pending")!; + }; +}; -- 2.43.0