const microzig = @import("microzig");
const rp2xxx = microzig.hal;

const build_options = @import("build_options");

const motor = @import("motor.zig");

pub const BoardConfig = struct {
    flash_size_bytes: usize,
    flash_target_offset: usize,
    md: motor.MotorDriver,
    pin_conf: rp2xxx.pins.GlobalConfiguration,
    uart: ?rp2xxx.uart.UART,
};

pub fn boardConfig(comptime board: build_options.@"build.Board") BoardConfig {
    const b = switch (board) {
        .pico => blk: {
            const FLASH_SIZE_BYTES = 8 * 1024 * 1024;

            const pin_conf = rp2xxx.pins.GlobalConfiguration{
                .GPIO4 = .{ .name = "uart_tx", .function = .UART1_TX },
                .GPIO15 = .{ .name = "ddc", .direction = .in },
                .GPIO16 = .{ .name = "mot1", .direction = .out },
                .GPIO17 = .{ .name = "mot2", .direction = .out },
                .GPIO18 = .{ .name = "speed", .function = .PWM1_A },
                .GPIO26 = .{ .name = "m1emf", .function = .ADC0 },
                .GPIO27 = .{ .name = "m2emf", .function = .ADC1 },
            };

            const pins = pin_conf.pins();

            break :blk BoardConfig{
                .pin_conf = pin_conf,
                .flash_size_bytes = FLASH_SIZE_BYTES,
                .flash_target_offset = FLASH_SIZE_BYTES - rp2xxx.flash.SECTOR_SIZE,
                .md = motor.MotorDriver{ .l293 = .{
                    .fwd = pins.mot1,
                    .bwd = pins.mot2,
                    .speed = pins.speed,
                } },
                .uart = if (build_options.uart) rp2xxx.uart.instance.UART1 else null,
            };
        },
        .gabk => {},
    };

    return b;
}