From c0a764ad00af949ceaa9c998941a50d31fddc503 Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Tue, 17 May 2022 20:51:34 +0800 Subject: [PATCH] gy-801-dump: continuous measuring --- tools/gy-801-dump/main.ha | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tools/gy-801-dump/main.ha b/tools/gy-801-dump/main.ha index d1e2de1..ea5cd16 100644 --- a/tools/gy-801-dump/main.ha +++ b/tools/gy-801-dump/main.ha @@ -1,10 +1,20 @@ use fmt; use fs; +use io; use os; use rt; +use linux::timerfd; use sensors::ADXL345; use sensors::L3G4200D; use strconv; +use time; +use unix::signal; + +let exit: bool = false; + +fn sigint(sig: int, info: *signal::siginfo, ucontext: *void) void = { + exit = true; +}; export fn main() void = { if (len(os::args) != 2) { @@ -40,9 +50,17 @@ export fn main() void = { ADXL345::init(ADXL345)!; L3G4200D::init(L3G4200D)!; - const acc_readings = ADXL345::read(ADXL345)!; - const gyro_readings = L3G4200D::read(L3G4200D)!; + const fd = timerfd::new(time::clock::MONOTONIC, 0)!; + defer io::close(fd)!; + timerfd::set(fd, (500 * time::MILLISECOND): timerfd::interval, 0)!; + signal::handle(signal::SIGINT, &sigint); - fmt::printfln("Accelerometer: {} {} {} g", acc_readings.0, acc_readings.1, acc_readings.2)!; - fmt::printfln("Gyroscope: {} {} {} dps", gyro_readings.0, gyro_readings.1, gyro_readings.2)!; + for (!exit) { + timerfd::read(fd)!; + const acc_readings = ADXL345::read(ADXL345)!; + const gyro_readings = L3G4200D::read(L3G4200D)!; + + fmt::printfln("Accelerometer: {} {} {} g", acc_readings.0, acc_readings.1, acc_readings.2)!; + fmt::printfln("Gyroscope: {} {} {} dps", gyro_readings.0, gyro_readings.1, gyro_readings.2)!; + }; }; -- 2.45.2