Move source code to src directory
This commit is contained in:
25
src/cmd/detect.zig
Normal file
25
src/cmd/detect.zig
Normal file
@@ -0,0 +1,25 @@
|
||||
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));
|
||||
}
|
||||
54
src/cmd/info.zig
Normal file
54
src/cmd/info.zig
Normal file
@@ -0,0 +1,54 @@
|
||||
const std = @import("std");
|
||||
const gpio = @import("gpio");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
var dir = try std.fs.openDirAbsolute("/dev", .{});
|
||||
defer dir.close();
|
||||
|
||||
var args = std.process.args();
|
||||
_ = args.skip(); // Skip the program name
|
||||
|
||||
// Iterate over each argument
|
||||
while (args.next()) |arg| {
|
||||
const fl = try dir.openFileZ(arg, .{});
|
||||
var chip = try gpio.getChipByFd(fl.handle);
|
||||
defer chip.close(); // This will close the fd
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s} - {d} lines:\n", .{ chip.nameSlice(), chip.lines });
|
||||
|
||||
var offset: u32 = 0;
|
||||
while (offset < chip.lines) : (offset += 1) {
|
||||
const lineInfo = try chip.getLineInfo(offset);
|
||||
|
||||
// Create an arraylist to store all the flag strings
|
||||
var flags = std.ArrayList([]const u8).init(alloc);
|
||||
defer flags.deinit();
|
||||
|
||||
// Appand any relevant flag strings to the array list
|
||||
if (lineInfo.flags.input) try flags.append("input");
|
||||
if (lineInfo.flags.output) try flags.append("output");
|
||||
if (lineInfo.flags.used) try flags.append("used");
|
||||
if (lineInfo.flags.active_low) try flags.append("active_low");
|
||||
if (lineInfo.flags.edge_rising) try flags.append("edge_rising");
|
||||
if (lineInfo.flags.edge_falling) try flags.append("edge_falling");
|
||||
if (lineInfo.flags.open_drain) try flags.append("open_drain");
|
||||
if (lineInfo.flags.open_source) try flags.append("open_source");
|
||||
if (lineInfo.flags.bias_pull_up) try flags.append("bias_pull_up");
|
||||
if (lineInfo.flags.bias_pull_down) try flags.append("bias_pull_down");
|
||||
|
||||
// Join the array list into a string
|
||||
const flagStr = try std.mem.join(alloc, ", ", flags.items);
|
||||
defer alloc.free(flagStr);
|
||||
|
||||
const name = if (lineInfo.name[0] != 0) lineInfo.nameSlice() else "<unnamed>";
|
||||
const consumer = if (lineInfo.flags.used) lineInfo.consumerSlice() else "<unused>";
|
||||
|
||||
try stdout.print(" line {d}: {s} {s} [{s}]\n", .{ offset, name, consumer, flagStr });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user