From eeff151e111fc6c3515539757005d744c48cc892 Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Mon, 16 May 2022 17:36:15 +0800 Subject: [PATCH] tools: add gy-801-dump --- .gitignore | 1 + Makefile | 4 +++- tools/gy-801-dump/main.ha | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tools/gy-801-dump/main.ha diff --git a/.gitignore b/.gitignore index 9179b41..532c959 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /evdev-dump-events /uinput-pointer +/gy-801-dump diff --git a/Makefile b/Makefile index bc461c2..4c2374d 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ evdev-dump-events uinput-pointer: hare build -levdev -o $@ tools/$@/ -.PHONY: evdev-dump-events uinput-pointer +gy-801-dump: + hare build -li2c -o $@ tools/$@/ +.PHONY: evdev-dump-events uinput-pointer gy-801-dump diff --git a/tools/gy-801-dump/main.ha b/tools/gy-801-dump/main.ha new file mode 100644 index 0000000..ac436ee --- /dev/null +++ b/tools/gy-801-dump/main.ha @@ -0,0 +1,34 @@ +use fmt; +use fs; +use os; +use rt; +use sensors::ADXL345; +use strconv; + +export fn main() void = { + if (len(os::args) != 2) { + fmt::fatalf("Usage: {} bus", os::args[0]); + }; + const bus = match (strconv::stoi(os::args[1])) { + case let bus: int => + yield bus; + case => + fmt::fatal("bus should be a integer"); + }; + + const ADXL345 = match (ADXL345::new(bus)) { + case let ADXL345: ADXL345::ADXL345 => + yield ADXL345; + case let err: fs::error => + fmt::fatalf("Cannot open i2c device: {}", fs::strerror(err)); + case let err: rt::errno => + fmt::fatalf("Cannot ioctl i2c device: {}", rt::strerror(err)); + }; + defer ADXL345::destroy(ADXL345); + + ADXL345::init(ADXL345)!; + + const acc_readings = ADXL345::read(ADXL345)!; + + fmt::printfln("Accelerometer: {} {} {} g", acc_readings.0, acc_readings.1, acc_readings.2)!; +}; -- 2.45.2