marginal improvements

This commit is contained in:
cjdenio committed 2026-11-06 23:03:41 -07:00
1 parent 5a2c385b56
commit 8be26913e7
2 files changed
+47 -35

No files matched your search

+7 -2
View File
@@ -13,13 +13,18 @@ pub const ArduinoRouter = struct {
return .{ .stream = stream, .io = io };
}
pub fn notify(self: @This(), allocator: std.mem.Allocator, method: []const u8, args: []const msgpack.Payload) !void {
pub fn notify(self: ArduinoRouter, allocator: std.mem.Allocator, method: []const u8, args: []const msgpack.Payload) !void {
var request = try msgpack.Payload.arrPayload(3, allocator);
defer request.free(allocator);
try request.setArrElement(0, msgpack.Payload.intToPayload(2));
try request.setArrElement(1, try msgpack.Payload.strToPayload(method, allocator));
const methodPayload = try msgpack.Payload.strToPayload(method, allocator);
defer methodPayload.free(allocator);
try request.setArrElement(1, methodPayload);
var argsPayload = try msgpack.Payload.arrPayload(args.len, allocator);
defer argsPayload.free(allocator);
for (args, 0..) |arg, i| {
try argsPayload.setArrElement(i, arg);
+40 -33
View File
@@ -46,41 +46,50 @@ pub const std_options: std.Options = .{
.http_disable_tls = true,
};
fn run_loop(client: *std.http.Client, allocator: std.mem.Allocator, led_map: std.StaticStringMap(Led), router: *ArduinoRouter, current_frame: u1, io: std.Io) !void {
const feed = try get_vehicles(client, allocator);
defer feed.deinit();
const App = struct {
router: ArduinoRouter,
client: std.http.Client,
current_frame: u1 = 0,
led_map: *const std.StaticStringMap(Led),
var regs: [0xB4]u8 = @splat(0x00);
fn run_loop(self: *App, allocator: std.mem.Allocator, io: std.Io) !void {
const feed = try get_vehicles(&self.client, allocator);
defer feed.deinit();
for (feed.value.vehicles) |vehicle| {
const thing = led_map.get(vehicle.nearest_station.id);
var regs: [0xB4]u8 = @splat(0x00);
if (thing) |value| {
std.debug.print("train at {s}, setting reg {} to {}\n", .{ vehicle.nearest_station.id, value.column, value.row });
regs[value.led_reg()] |= (@as(u8, 1) << (value.column));
if (regs[value.pwm_reg()] == 0) {
regs[value.pwm_reg()] = 50;
} else {
regs[value.pwm_reg()] = 127;
for (feed.value.vehicles) |vehicle| {
const thing = self.led_map.get(vehicle.nearest_station.id);
if (thing) |value| {
std.debug.print("train at {s}, setting reg {} to {}\n", .{ vehicle.nearest_station.id, value.column, value.row });
regs[value.led_reg()] |= (@as(u8, 1) << (value.column));
if (regs[value.pwm_reg()] == 0) {
regs[value.pwm_reg()] = 50;
} else {
regs[value.pwm_reg()] = 127;
}
}
}
try self.router.notify(allocator, "clearFrame", &.{msgpack.Payload.intToPayload(~self.current_frame)});
for (regs, 0..) |value, reg| {
try self.router.notify(allocator, "setRegister", &.{
msgpack.Payload.uintToPayload(~self.current_frame), // page
msgpack.Payload.uintToPayload(reg), // register
msgpack.Payload.uintToPayload(value), // value
});
try io.sleep(.fromMilliseconds(5), .real);
std.debug.print("{x} IS {x}\n", .{ reg, value });
}
try self.router.notify(allocator, "displayFrame", &.{msgpack.Payload.intToPayload(~self.current_frame)});
self.current_frame = ~self.current_frame;
}
try router.notify(allocator, "clearFrame", &.{msgpack.Payload.intToPayload(~current_frame)});
for (regs, 0..) |value, reg| {
try router.notify(allocator, "setRegister", &.{
msgpack.Payload.uintToPayload(~current_frame), // page
msgpack.Payload.uintToPayload(reg), // register
msgpack.Payload.uintToPayload(value), // value
});
try io.sleep(.fromMilliseconds(5), .real);
std.debug.print("{x} IS {x}\n", .{ reg, value });
}
try router.notify(allocator, "displayFrame", &.{msgpack.Payload.intToPayload(~current_frame)});
}
};
pub fn main(init: std.process.Init) !void {
const led_map = build_led_map();
@@ -90,7 +99,7 @@ pub fn main(init: std.process.Init) !void {
var client: std.http.Client = .{ .allocator = init.arena.allocator(), .io = init.io };
defer client.deinit();
var current_frame: u1 = 0;
var app: App = .{ .router = router, .client = client, .led_map = &led_map };
try router.notify(init.arena.allocator(), "clearFrame", &.{msgpack.Payload.intToPayload(0)});
try router.notify(init.arena.allocator(), "displayFrame", &.{msgpack.Payload.intToPayload(0)});
@@ -100,9 +109,7 @@ pub fn main(init: std.process.Init) !void {
const allocator = arena.allocator();
defer arena.deinit();
run_loop(&client, allocator, led_map, &router, current_frame, init.io) catch |e| std.debug.print("ERROR OCCURED: {s}\n", .{@errorName(e)});
current_frame = ~current_frame;
app.run_loop(allocator, init.io) catch |e| std.debug.print("ERROR OCCURED: {s}\n", .{@errorName(e)});
try init.io.sleep(.fromSeconds(5), .real);
}