const std = @import("std");
const pico = @import("microzig").hal;
const pin_conf = pico.pins.GlobalConfiguration{
.GPIO18 = .{ .name = "data", .direction = .out },
.GPIO20 = .{ .name = "latch", .direction = .out },
.GPIO21 = .{ .name = "clock", .direction = .out },
};
const pins = pin_conf.pins();
pub fn main() void {
pin_conf.apply();
var x: u8 = undefined;
while (true) {
// Q7 -> Q0
x = 0b00000001;
for (0..8) |_| {
pins.latch.put(0);
shiftOut(pins.data, pins.clock, .lsbFirst, x);
pins.latch.put(1);
// move the led
x <<= 1;
pico.time.sleep_ms(100);
}
// Q0 -> Q7
x = 0b00000001;
for (0..8) |_| {
pins.latch.put(0);
shiftOut(pins.data, pins.clock, .msbFirst, x);
pins.latch.put(1);
// move the led
x <<= 1;
pico.time.sleep_ms(100);
}
}
}
const BitOrder = enum {
lsbFirst,
msbFirst,
};
fn shiftOut(dataPin: pico.gpio.Pin, clockPin: pico.gpio.Pin, bitorder: BitOrder, val: u8) void {
for (0..8) |i| {
switch (bitorder) {
.lsbFirst => dataPin.put(@as(u1, @truncate(val >> @as(u3, @intCast(i))))),
.msbFirst => dataPin.put(@as(u1, @truncate(val >> @as(u3, @intCast(7 - i))))),
}
// mark data to be ready
clockPin.put(1);
clockPin.put(0);
}
}