From b7cd1d4dd88f12cb259c5604d2541d2e976d0697 Mon Sep 17 00:00:00 2001 From: Matthias Portzel Date: Fri, 19 Apr 2024 23:37:38 -0400 Subject: [PATCH] Some high-level cleanup --- src/algorithm.zig | 18 +++++++++--------- src/main.zig | 17 +++-------------- src/rp2040-bot.zig | 22 ++++++++++++++++++++++ src/simulated-bot.zig | 8 ++++++-- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/algorithm.zig b/src/algorithm.zig index e7d181e..17b2e91 100644 --- a/src/algorithm.zig +++ b/src/algorithm.zig @@ -6,18 +6,20 @@ const Point = map.Point; const Cardinal = map.Cardinal; const Mouse = @import("mouse.zig").Mouse; -pub const is_robot = @import("builtin").os.tag == .freestanding; - // Doesn't have any state // It's a hardware abstraction layer -pub const robot = if (is_robot) @import("rp2040-bot.zig") else @import("simulated-bot.zig"); +pub const is_irl = @import("builtin").os.tag == .freestanding; +pub const robot = if (is_irl) @import("rp2040-bot.zig") else @import("simulated-bot.zig"); // It's a little weird to do this as a global, but it just returns // an instance of Mouse with initial values var mouse = @import("mouse.zig").initialize(); pub fn setup () void { + // Just pass through to robot.setup to do any robot-specific setup (hardware, allocations, and starting alt-core) robot.setup(); + + // Any maze-related setup that can't be done at comptime can be done here } //// Node @@ -76,9 +78,7 @@ const Node = struct { } } - if (!is_robot) { - robot.writeCellScore(self.p, self.score); - } + robot.writeCellScore(self.p, self.score); } }; @@ -151,9 +151,9 @@ pub fn setWall(p: Point, direction: Cardinal, value: bool) void { assert(value); } - // If we're running in sim, display that we saw a wall - if (!is_robot and value) { - robot.showWall(p, direction); + // Display that we saw a wall (e.g. on the simulator display) + if (value) { + robot.recordWall(p, direction); } // We don't keep track of the edges, we just assume they're there diff --git a/src/main.zig b/src/main.zig index dc0656a..cda8a2f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -19,10 +19,6 @@ pub fn alt_core () noreturn { } const is_robot = algo.is_robot; -// // TODO: Refactor again. algo.is_robot??? -// if (algo.is_robot) { -// alt_core -// } const time = @import("microzig").hal.time; @@ -35,21 +31,14 @@ fn core1 () noreturn { pub fn main() !noreturn { // Power On test - // TODO: rename is_robot to is_physical or something - if (!is_robot) { - // TODO: spawn a thread to run alt-core operations - }else { - // multicore.launch_core1(core1); - } - algo.setup(); // // Flood fill to the goal - // algo.floodFill(map.goals[0..]); + algo.floodFill(map.goals[0..]); // // Flood fill back to the start - // const starts = [_]map.Point{ map.start }; - // algo.floodFill(starts[0..]); + const starts = [_]map.Point{ map.start }; + algo.floodFill(starts[0..]); robot.stall(); } diff --git a/src/rp2040-bot.zig b/src/rp2040-bot.zig index 8e30d42..dd23d5d 100644 --- a/src/rp2040-bot.zig +++ b/src/rp2040-bot.zig @@ -3,6 +3,8 @@ // The code in this file will be dirty, it will deal with setting up encoders and all sorts of BS const map = @import("map.zig"); +const Point = map.Point; +const Cardinal = map.Cardinal; // Hardware-specific declarations const microzig = @import("microzig"); @@ -259,6 +261,8 @@ pub fn setup () void { // pins.onboard_led.toggle(); // pins.onboard_led.toggle(); // TODO + + // start_alt_core() } pub fn moveForward() void { @@ -291,6 +295,13 @@ pub fn readBack() bool { return true; } +// Multicore isn't functional upstream +// const multicore = rp2040.multicore; +// fn alt_core_main_function noreturn {} +pub fn start_alt_core() !void { + // multicore.launch_core1(alt_core_main_function); +} + pub fn stall() noreturn { pins.motor_right_1.put(1); @@ -335,3 +346,14 @@ pub fn stall() noreturn { // pins.led_6.put(0); } } + +// --- Functions not implemented for the IRL bot --- +pub fn writeCellScore(p: Point, score: usize) void { + _ = p; + _ = score; + // stdout.print("setText {d} {d} {d}\n", .{p.x, p.y, score}) catch unreachable; +} +pub fn recordWall(p: Point, direction: Cardinal) void { + _ = p; + _ = direction; +} diff --git a/src/simulated-bot.zig b/src/simulated-bot.zig index b77712e..92e3c58 100644 --- a/src/simulated-bot.zig +++ b/src/simulated-bot.zig @@ -13,7 +13,11 @@ const stdout = std.io.getStdOut().writer(); // Since this only runs on my computer, we can do whatever we want here -pub fn setup () void {} +pub fn setup () void { + // TODO: Create alt thread and start multi-core + // // Blocked on upstream multi-core support + // robot.start_alt_core(); +} // TODO: create a utility function that takes an allocator and reads from std in and returns a &[]u8 @@ -84,7 +88,7 @@ pub fn readBack() bool { std.debug.panic(); } -pub fn showWall(p: Point, direction: Cardinal) void { +pub fn recordWall(p: Point, direction: Cardinal) void { const dir :u8 = switch (direction) { .north => 'n', .south => 's',