diff --git a/.gitignore b/.gitignore index dca1103..dfc7be3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ zig-out/ .zig-cache/ +zig-pkg/ diff --git a/build.zig b/build.zig index e79071f..7e1e512 100644 --- a/build.zig +++ b/build.zig @@ -83,6 +83,13 @@ pub fn build(b: *std.Build) void { }), }); + const msgpack_dep = b.dependency("zig_msgpack", .{ + .target = target, + .optimize = optimize, + }); + + exe.root_module.addImport("msgpack", msgpack_dep.module("msgpack")); + // This declares intent for the executable to be installed into the // install prefix when running `zig build` (i.e. when executing the default // step). By default the install prefix is `zig-out/` but can be overridden diff --git a/build.zig.zon b/build.zig.zon index f4150d8..b3829c1 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -32,42 +32,11 @@ // Once all dependencies are fetched, `zig build` no longer requires // internet connectivity. .dependencies = .{ - // See `zig fetch --save ` for a command-line interface for adding dependencies. - //.example = .{ - // // When updating this field to a new URL, be sure to delete the corresponding - // // `hash`, otherwise you are communicating that you expect to find the old hash at - // // the new URL. If the contents of a URL change this will result in a hash mismatch - // // which will prevent zig from using it. - // .url = "https://example.com/foo.tar.gz", - // - // // This is computed from the file contents of the directory of files that is - // // obtained after fetching `url` and applying the inclusion rules given by - // // `paths`. - // // - // // This field is the source of truth; packages do not come from a `url`; they - // // come from a `hash`. `url` is just one of many possible mirrors for how to - // // obtain a package matching this `hash`. - // .hash = "...", - // - // // When this is provided, the package is found in a directory relative to the - // // build root. In this case the package's hash is irrelevant and therefore not - // // computed. This field and `url` are mutually exclusive. - // .path = "foo", - // - // // When this is set to `true`, a package is declared to be lazily - // // fetched. This makes the dependency only get fetched if it is - // // actually used. - // .lazy = false, - //}, + .zig_msgpack = .{ + .url = "https://github.com/zigcc/zig-msgpack/archive/5884caa.tar.gz", + .hash = "zig_msgpack-0.0.19-evvueNR-BQBqKazGShn1eDXelQwAQz60su5SfPNB9lQk", + }, }, - // Specifies the set of files and directories that are included in this package. - // Only files and directories listed here are included in the `hash` that - // is computed for this package. Only files listed here will remain on disk - // when using the zig package manager. As a rule of thumb, one should list - // files required for compilation plus any license(s). - // Paths are relative to the build root. Use the empty string (`""`) to refer to - // the build root itself. - // A directory listed here means that all files within, recursively, are included. .paths = .{ "build.zig", "build.zig.zon", diff --git a/sketch/sketch.ino b/sketch/sketch.ino new file mode 100644 index 0000000..ea1831d --- /dev/null +++ b/sketch/sketch.ino @@ -0,0 +1,50 @@ +#include +#include + +void writeRegister(uint8_t reg, uint8_t val) { + Wire.beginTransmission(0x74); + uint8_t data[] = {reg, val}; + Wire.write(data, sizeof data); + Wire.endTransmission(); +} + +void clearFrame(uint8_t frame) { + writeRegister(0xFD, frame); + + for (uint8_t reg = 0x00; reg <= 0x11; reg++) { + writeRegister(reg, 0x00); + } +} + +void displayFrame(uint8_t frame) { + writeRegister(0xFD, 0x0B); + writeRegister(0x01, frame); +} + +void setRegister(uint8_t page, uint8_t reg, uint8_t val) { + writeRegister(0xFD, page); + writeRegister(reg, val); +} + +void setup() { + Bridge.begin(); + Wire.begin(); + + // power off + writeRegister(0xFD, 0x0B); + writeRegister(0x0A, 0); + + clearFrame(0); + clearFrame(1); + + // power on + writeRegister(0xFD, 0x0B); + writeRegister(0x0A, 1); + + Bridge.provide("displayFrame", displayFrame); + Bridge.provide("clearFrame", clearFrame); + Bridge.provide("setRegister", setRegister); +} + +void loop() {} + diff --git a/src/arduino_router.zig b/src/arduino_router.zig new file mode 100644 index 0000000..2055b64 --- /dev/null +++ b/src/arduino_router.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const net = std.Io.net; +const msgpack = @import("msgpack"); + +pub const ArduinoRouter = struct { + stream: net.Stream, + io: std.Io, + + pub fn connect(io: std.Io) !@This() { + const address = try net.UnixAddress.init("/var/run/arduino-router.sock"); + const stream = try address.connect(io); + + return .{ .stream = stream, .io = io }; + } + + pub fn notify(self: @This(), allocator: std.mem.Allocator, method: []const u8, args: []const msgpack.Payload) !void { + var request = try msgpack.Payload.arrPayload(3, allocator); + + try request.setArrElement(0, msgpack.Payload.intToPayload(2)); + try request.setArrElement(1, try msgpack.Payload.strToPayload(method, allocator)); + + var argsPayload = try msgpack.Payload.arrPayload(args.len, allocator); + + for (args, 0..) |arg, i| { + try argsPayload.setArrElement(i, arg); + } + + try request.setArrElement(2, argsPayload); + + var buffer: [1024]u8 = undefined; + var writer = self.stream.writer(self.io, &buffer); + + const encoder: msgpack.Encoder = .{ .writer = &writer.interface }; + + try encoder.write(request); + + try writer.interface.flush(); + } + + pub fn deinit(self: @This()) void { + self.stream.close(self.io); + } +}; diff --git a/src/main.zig b/src/main.zig index f7ec421..bf261d4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,8 +1,16 @@ const std = @import("std"); const Io = std.Io; +const msgpack = @import("msgpack"); + +const ArduinoRouter = @import("./arduino_router.zig").ArduinoRouter; + +const Station = struct { + id: []u8, +}; const Vehicle = struct { id: []u8, + nearest_station: Station, }; const Feed = struct { @@ -35,10 +43,106 @@ pub const std_options: std.Options = .{ .http_disable_tls = true, }; +const Led = struct { + row: u3, + column: u3, +}; + pub fn main(init: std.process.Init) !void { + var led_map: std.hash_map.StringHashMap(Led) = .init(init.arena.allocator()); + + try led_map.put("18377", .{ .column = 0, .row = 6 }); // Library Station + try led_map.put("18378", .{ .column = 0, .row = 6 }); // Library Station + try led_map.put("18379", .{ .column = 0, .row = 5 }); // Trolley Station + try led_map.put("18380", .{ .column = 0, .row = 5 }); // Trolley Station + try led_map.put("18383", .{ .column = 0, .row = 3 }); // Stadium Station + try led_map.put("18384", .{ .column = 0, .row = 3 }); // Stadium Station + try led_map.put("18397", .{ .column = 4, .row = 0 }); // Midvale Fort Union Station + try led_map.put("18398", .{ .column = 4, .row = 0 }); // Midvale Fort Union Station + try led_map.put("18399", .{ .column = 4, .row = 1 }); // Midvale Center Station + try led_map.put("18400", .{ .column = 4, .row = 1 }); // Midvale Center Station + try led_map.put("18401", .{ .column = 4, .row = 2 }); // Historic Sandy Station + try led_map.put("18402", .{ .column = 4, .row = 2 }); // Historic Sandy Station + try led_map.put("18403", .{ .column = 4, .row = 4 }); // Sandy Civic Center Station + try led_map.put("18404", .{ .column = 4, .row = 4 }); // Sandy Civic Center Station + try led_map.put("19166", .{ .column = 0, .row = 2 }); // University South Campus Station + try led_map.put("19167", .{ .column = 0, .row = 2 }); // University South Campus Station + try led_map.put("19168", .{ .column = 0, .row = 1 }); // Fort Douglas Station + try led_map.put("19169", .{ .column = 0, .row = 1 }); // Fort Douglas Station + try led_map.put("19170", .{ .column = 0, .row = 0 }); // University Medical Center + try led_map.put("19171", .{ .column = 0, .row = 0 }); // University Medical Center + try led_map.put("21225", .{ .column = 4, .row = 3 }); // Sandy Expo Station + try led_map.put("21226", .{ .column = 4, .row = 3 }); // Sandy Expo Station + try led_map.put("21761", .{ .column = 2, .row = 2 }); // Salt Lake Central Station + try led_map.put("21762", .{ .column = 2, .row = 0 }); // Planetarium Station + try led_map.put("21763", .{ .column = 2, .row = 1 }); // Old Greektown Station + try led_map.put("21799", .{ .column = 2, .row = 2 }); // Salt Lake Central Station + try led_map.put("21800", .{ .column = 2, .row = 0 }); // Planetarium Station + try led_map.put("21801", .{ .column = 2, .row = 1 }); // Old Greektown Station + try led_map.put("22620", .{ .column = 6, .row = 0 }); // Daybreak Parkway Station + try led_map.put("22621", .{ .column = 5, .row = 7 }); // South Jordan Parkway Station + try led_map.put("22622", .{ .column = 5, .row = 7 }); // South Jordan Parkway Station + try led_map.put("22623", .{ .column = 5, .row = 6 }); // 5600 W. Old Bingham Hwy Station + try led_map.put("22624", .{ .column = 5, .row = 6 }); // 5600 W. Old Bingham Hwy Station + try led_map.put("22625", .{ .column = 5, .row = 5 }); // 4800 W. Old Bingham Hwy Station + try led_map.put("22626", .{ .column = 5, .row = 5 }); // 4800 W. Old Bingham Hwy Station + try led_map.put("22627", .{ .column = 5, .row = 4 }); // Jordan Valley Station + try led_map.put("22628", .{ .column = 5, .row = 4 }); // Jordan Valley Station + try led_map.put("22629", .{ .column = 5, .row = 3 }); // 2700 W. Sugar Factory Rd Station + try led_map.put("22630", .{ .column = 5, .row = 3 }); // 2700 W. Sugar Factory Rd Station + try led_map.put("22631", .{ .column = 5, .row = 2 }); // West Jordan City Center Station + try led_map.put("22632", .{ .column = 5, .row = 2 }); // West Jordan City Center Station + try led_map.put("22633", .{ .column = 5, .row = 1 }); // Historic Gardner Station + try led_map.put("22634", .{ .column = 5, .row = 1 }); // Historic Gardner Station + try led_map.put("22635", .{ .column = 5, .row = 0 }); // Bingham Junction Station + try led_map.put("22636", .{ .column = 5, .row = 0 }); // Bingham Junction Station + try led_map.put("22639", .{ .column = 6, .row = 0 }); // Daybreak Parkway Station + try led_map.put("22645", .{ .column = 2, .row = 3 }); // RIVER TRAIL STATION + try led_map.put("22646", .{ .column = 2, .row = 3 }); // River Trail Station + try led_map.put("22647", .{ .column = 2, .row = 4 }); // Redwood Junction Station + try led_map.put("22648", .{ .column = 2, .row = 4 }); // Redwood Junction Station + try led_map.put("22649", .{ .column = 2, .row = 5 }); // Decker Lake Station + try led_map.put("22650", .{ .column = 2, .row = 5 }); // Decker Lake Station + try led_map.put("22651", .{ .column = 2, .row = 6 }); // West Valley Central Station + try led_map.put("22652", .{ .column = 2, .row = 6 }); // West Valley Central Station + try led_map.put("23039", .{ .column = 1, .row = 0 }); // North Temple Bridge/Guadalupe + try led_map.put("23040", .{ .column = 1, .row = 0 }); // North Temple Bridge/Guadalupe + try led_map.put("23041", .{ .column = 1, .row = 1 }); // Jackson/Euclid Station + try led_map.put("23042", .{ .column = 1, .row = 1 }); // Jackson/Euclid Station + try led_map.put("23043", .{ .column = 1, .row = 2 }); // Fairpark Station + try led_map.put("23044", .{ .column = 1, .row = 2 }); // Fairpark Station + try led_map.put("23045", .{ .column = 1, .row = 3 }); // Power Station + try led_map.put("23046", .{ .column = 1, .row = 3 }); // Power Station + try led_map.put("23047", .{ .column = 1, .row = 4 }); // 1940 W. North Temple Station + try led_map.put("23048", .{ .column = 1, .row = 4 }); // 1940 W. North Temple Station + try led_map.put("23049", .{ .column = 1, .row = 5 }); // Airport Station + try led_map.put("23050", .{ .column = 1, .row = 5 }); // Airport Station + try led_map.put("23318", .{ .column = 4, .row = 5 }); // Crescent View Station + try led_map.put("23319", .{ .column = 4, .row = 5 }); // Crescent View Station + try led_map.put("23320", .{ .column = 4, .row = 6 }); // Kimballs Lane Station + try led_map.put("23321", .{ .column = 4, .row = 6 }); // Kimballs Lane Station + try led_map.put("23322", .{ .column = 4, .row = 7 }); // Draper Town Center Station + try led_map.put("23323", .{ .column = 4, .row = 7 }); // Draper Town Center Station + try led_map.put("23565", .{ .column = 6, .row = 2 }); // Central Pointe Station + try led_map.put("23566", .{ .column = 3, .row = 5 }); // South Salt Lake City Station + try led_map.put("23567", .{ .column = 3, .row = 4 }); // 300 East Station + try led_map.put("23568", .{ .column = 3, .row = 3 }); // 500 East Station + try led_map.put("23569", .{ .column = 3, .row = 2 }); // 700 East Station + try led_map.put("23570", .{ .column = 3, .row = 1 }); // Sugarmont Station + try led_map.put("23571", .{ .column = 3, .row = 0 }); // Fairmont Station + try led_map.put("25479", .{ .column = 6, .row = 1 }); // South Jordan Downtown Station + try led_map.put("25480", .{ .column = 6, .row = 1 }); // South Jordan Downtown Station + + const router = try ArduinoRouter.connect(init.io); + defer router.deinit(); var client: std.http.Client = .{ .allocator = init.arena.allocator(), .io = init.io }; defer client.deinit(); + var currentFrame: u1 = 0; + + try router.notify(init.arena.allocator(), "clearFrame", &.{msgpack.Payload.intToPayload(0)}); + try router.notify(init.arena.allocator(), "displayFrame", &.{msgpack.Payload.intToPayload(0)}); + while (true) { var arena = std.heap.ArenaAllocator.init(init.gpa); const allocator = arena.allocator(); @@ -47,10 +151,34 @@ pub fn main(init: std.process.Init) !void { const feed = try get_vehicles(&client, allocator); defer feed.deinit(); + var regs: [9]u8 = @splat(0); + for (feed.value.vehicles) |vehicle| { - std.debug.print("new vehicle: {s}\n", .{vehicle.id}); + const thing = 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.row, value.column }); + regs[value.column] |= (@as(u8, 1) << (value.row)); + } } - try init.io.sleep(.fromSeconds(1), .real); + try router.notify(arena.allocator(), "clearFrame", &.{msgpack.Payload.intToPayload(~currentFrame)}); + + for (regs, 0..) |value, reg| { + try router.notify(arena.allocator(), "setRegister", &.{ + msgpack.Payload.uintToPayload(~currentFrame), // page + msgpack.Payload.uintToPayload(reg * 2), // register + msgpack.Payload.uintToPayload(value), // value + }); + try init.io.sleep(.fromMilliseconds(100), .real); + + std.debug.print("{x} IS {x}\n", .{ reg * 2, value }); + } + + try router.notify(arena.allocator(), "displayFrame", &.{msgpack.Payload.intToPayload(~currentFrame)}); + + currentFrame = ~currentFrame; + + try init.io.sleep(.fromSeconds(5), .real); } }