7
mirror of https://github.com/tenderlove/initial-v.git synced 2025-04-22 04:13:22 +00:00

adding client code

This commit is contained in:
Aaron Patterson 2023-02-13 16:35:38 -08:00
parent f9eb3098bf
commit ca53a042fd
No known key found for this signature in database
GPG Key ID: 953170BCB4FFAFC6
2 changed files with 139 additions and 0 deletions

36
client/build.zig Normal file
View File

@ -0,0 +1,36 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("initial-v", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkLibC();
exe.linkSystemLibrary("hidapi");
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}

103
client/src/main.zig Normal file
View File

@ -0,0 +1,103 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const process = std.process;
const warn = std.debug.print;
const c = @cImport({
@cInclude("hidapi.h");
});
const stdlib = @cImport({
@cInclude("stdlib.h");
});
const Command = enum {
none,
backlight,
reset,
drive,
neutral,
reverse,
park,
vim,
general,
};
pub fn wcstombs(allocator: *const Allocator, str: [*c]c_int) ?[]const u8 {
var buf: [4096]u8 = undefined;
var len = stdlib.wcstombs(&buf, str, 4096);
if (len > 0) {
const result = allocator.alloc(u8, len) catch return null;
std.mem.copy(u8, result, buf[0..len]);
return result;
} else {
return null;
}
}
const keywords = std.ComptimeStringMap(Command, .{
.{ "drive", .drive },
.{ "neutral", .neutral },
.{ "reverse", .reverse },
.{ "park", .park },
.{ "vim", .vim },
.{ "general", .general },
});
pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len != 2) {
std.log.err("Got wrong number of args {d}", .{args.len});
std.process.exit(1);
}
const command: Command = keywords.get(args.ptr[1]) orelse
@panic("Unknown command");
std.log.info("Got command {any}", .{command});
const bytes = [_]u8{ 0x0, @enumToInt(command) };
const devs = c.hid_enumerate(0, 0);
var list_head = devs;
while (list_head) |item| {
if (item.*.product_id == 0x11a1 and
item.*.vendor_id == 0x2e5 and
item.*.usage == 0x61)
{
break;
}
list_head = list_head.*.next;
}
if (list_head != null) {
var name = wcstombs(&allocator, list_head.*.product_string).?;
std.log.info("{s}", .{name});
var handle = c.hid_open(list_head.*.vendor_id, list_head.*.product_id, null);
var counter: u16 = 0;
while (handle == null) {
handle = c.hid_open(list_head.*.vendor_id, list_head.*.product_id, null);
counter += 1;
if (counter > 1000) break;
}
if (handle) |good| {
_ = c.hid_write(good, &bytes, 2);
}
} else {
std.log.info("couldn't find device", .{});
}
}
test "basic test" {
try std.testing.expectEqual(10, 3 + 7);
}