mirror of
https://github.com/cjdenio/train-map.git
synced 2026-12-01 05:26:19 +00:00
marginal improvements
This commit is contained in:
2 files changed
+25
-13
No files matched your search
@@ -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);
|
||||
|
||||
+18
-11
@@ -46,14 +46,20 @@ 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);
|
||||
const App = struct {
|
||||
router: ArduinoRouter,
|
||||
client: std.http.Client,
|
||||
current_frame: u1 = 0,
|
||||
led_map: *const std.StaticStringMap(Led),
|
||||
|
||||
fn run_loop(self: *App, allocator: std.mem.Allocator, io: std.Io) !void {
|
||||
const feed = try get_vehicles(&self.client, allocator);
|
||||
defer feed.deinit();
|
||||
|
||||
var regs: [0xB4]u8 = @splat(0x00);
|
||||
|
||||
for (feed.value.vehicles) |vehicle| {
|
||||
const thing = led_map.get(vehicle.nearest_station.id);
|
||||
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 });
|
||||
@@ -66,11 +72,11 @@ fn run_loop(client: *std.http.Client, allocator: std.mem.Allocator, led_map: std
|
||||
}
|
||||
}
|
||||
|
||||
try router.notify(allocator, "clearFrame", &.{msgpack.Payload.intToPayload(~current_frame)});
|
||||
try self.router.notify(allocator, "clearFrame", &.{msgpack.Payload.intToPayload(~self.current_frame)});
|
||||
|
||||
for (regs, 0..) |value, reg| {
|
||||
try router.notify(allocator, "setRegister", &.{
|
||||
msgpack.Payload.uintToPayload(~current_frame), // page
|
||||
try self.router.notify(allocator, "setRegister", &.{
|
||||
msgpack.Payload.uintToPayload(~self.current_frame), // page
|
||||
msgpack.Payload.uintToPayload(reg), // register
|
||||
msgpack.Payload.uintToPayload(value), // value
|
||||
});
|
||||
@@ -79,8 +85,11 @@ fn run_loop(client: *std.http.Client, allocator: std.mem.Allocator, led_map: std
|
||||
std.debug.print("{x} IS {x}\n", .{ reg, value });
|
||||
}
|
||||
|
||||
try router.notify(allocator, "displayFrame", &.{msgpack.Payload.intToPayload(~current_frame)});
|
||||
try self.router.notify(allocator, "displayFrame", &.{msgpack.Payload.intToPayload(~self.current_frame)});
|
||||
|
||||
self.current_frame = ~self.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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user