From 139e1679be026f99ccfae8e459f0432723eed397 Mon Sep 17 00:00:00 2001 From: Pinghao Wu Date: Tue, 10 Sep 2024 00:05:41 +0800 Subject: [PATCH] count squats with joycon --- .gitignore | 1 + Makefile | 4 +-- cmd/squats/main+joycon.ha | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cmd/squats/main+joycon.ha diff --git a/.gitignore b/.gitignore index c158694..5ae8e9a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /buttonc /evdev/codes.ha /event-codes +/squats diff --git a/Makefile b/Makefile index ff36afe..86ab629 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SENSORS ?= joycon SENSORS_LIBS = HEADERS_PREFIX ?= /usr/include -BINARIES = event-codes evdev-dump-events uinput-pointer sensors-dump pointerd pointerc motion-control buttonc +BINARIES = event-codes evdev-dump-events uinput-pointer sensors-dump pointerd pointerc motion-control buttonc squats ifeq ($(SENSORS), joycon) SENSORS_LIBS = -levdev @@ -23,7 +23,7 @@ buttonc pointerc: hare build -o $@ tools/$@/ pointerd: evdev/codes.ha hare build -levdev -o $@ cmd/$@/ -motion-control: +motion-control squats: hare build $(SENSORS_LIBS) -T +$(SENSORS) -o $@ cmd/$@/ clean: diff --git a/cmd/squats/main+joycon.ha b/cmd/squats/main+joycon.ha new file mode 100644 index 0000000..a3311c3 --- /dev/null +++ b/cmd/squats/main+joycon.ha @@ -0,0 +1,66 @@ +use errors; +use fmt; +use sensors::joycon; +use time; +use unix::signal; +use unix::tty; +use os; + +let exit: bool = false; + +fn sigint(sig: signal::sig, info: *signal::siginfo, ucontext: *opaque) void = { + exit = true; +}; + +type status = enum u8 { + UP, + DOWN, + IDLE, +}; + +def THRESHOLD: f64 = 10.0; + +export fn main() void = { + const joycon = joycon::new(void)!; + defer joycon::destroy(joycon); + + signal::handle(signal::sig::INT, &sigint, signal::flag::RESTART); + + const screen = tty::isatty(os::stdout_file); + + let count = 0u64; + let last = status::UP; + let lastts = time::now(time::clock::MONOTONIC); + + for (let i = 0; !exit; i += 1) { + const sample = joycon::next_sample(joycon)!; + const gyro = sample.gyroscope; + + if (i % 50 == 0) { + if (screen) { + fmt::print("\x1b[1;1H\x1b[0J")!; + }; + + fmt::printfln("Gyroscope:\t{}\t{}\t{}\tdps\n", + gyro.0, gyro.1, gyro.2)!; + const s = if (gyro.1 > THRESHOLD) status::UP + else if (gyro.1 < -THRESHOLD) status::DOWN + else status::IDLE; + const want = if (last == status::UP) status::DOWN + else status::UP; + const ts = time::now(time::clock::MONOTONIC); + if (s == want && time::diff(lastts, ts) > time::SECOND) { + last = s; + lastts = ts; + count += 1; + }; + + fmt::println(switch (s) { + case status::UP => yield 'U'; + case status::DOWN => yield 'D'; + case status::IDLE => yield 'X'; + }, count / 2)!; + + }; + }; +}; -- 2.45.2