zig-gpio/cmd/detect.zig

26 lines
800 B
Zig
Raw Normal View History

2023-12-14 01:19:46 +00:00
const std = @import("std");
const gpio = @import("gpio");
pub fn main() !void {
var iter_dir = try std.fs.openIterableDirAbsolute("/dev", .{});
defer iter_dir.close();
const stdout = std.io.getStdOut().writer();
var iter = iter_dir.iterate();
while (try iter.next()) |entry| {
if (!hasPrefix(entry.name, "gpiochip")) continue;
const fl = try iter_dir.dir.openFile(entry.name, .{});
var chip = try gpio.getChipByFd(fl.handle);
defer chip.close(); // This will close the fd
try stdout.print("{s} [{s}] ({d} lines)\n", .{ chip.nameSlice(), chip.labelSlice(), chip.lines });
}
}
fn hasPrefix(s: []const u8, prefix: []const u8) bool {
if (s.len < prefix.len) return false;
return (std.mem.eql(u8, s[0..prefix.len], prefix));
}