|
| 1 | +const microzig = @import("microzig"); |
| 2 | +const board = microzig.board; |
| 3 | +const hal = microzig.hal; |
| 4 | +const cpu = microzig.cpu; |
| 5 | + |
| 6 | +// Taken from https://github.com/robinjanssens/WCH-Toolchain |
| 7 | + |
| 8 | +pub fn main() !void { |
| 9 | + // Board brings up clocks and time |
| 10 | + board.init(); |
| 11 | + |
| 12 | + const pins = board.pin_config.apply(); |
| 13 | + const ws2812_pin = pins.ws2812; |
| 14 | + |
| 15 | + var i: u8 = 0; |
| 16 | + while (true) { |
| 17 | + const col: u32 = wheel(i); |
| 18 | + set_led(0, red(col), green(col), blue(col)); |
| 19 | + write(ws2812_pin); |
| 20 | + hal.time.sleep_ms(33); |
| 21 | + // Allow overflow to wrap around |
| 22 | + i +%= 1; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Board has a single LED |
| 27 | +const NUM_LEDS = 1; |
| 28 | + |
| 29 | +fn color(r: u8, g: u8, b: u8) u32 { |
| 30 | + return (@as(u32, r) << 16) | (@as(u32, g) << 8) | b; |
| 31 | +} |
| 32 | + |
| 33 | +fn red(col: u32) u8 { |
| 34 | + return @truncate((col >> 16) & 0xFF); |
| 35 | +} |
| 36 | + |
| 37 | +fn green(col: u32) u8 { |
| 38 | + return @truncate((col >> 8) & 0xFF); |
| 39 | +} |
| 40 | + |
| 41 | +fn blue(col: u32) u8 { |
| 42 | + return @truncate(col & 0xFF); |
| 43 | +} |
| 44 | + |
| 45 | +fn wheel(wheel_pos: u8) u32 { |
| 46 | + var pos = 255 -% wheel_pos; |
| 47 | + if (pos < 85) { |
| 48 | + return color(255 -% pos *% 3, 0, pos *% 3); |
| 49 | + } else if (pos < 170) { |
| 50 | + pos -%= 85; |
| 51 | + return color(0, pos *% 3, 255 -% pos *% 3); |
| 52 | + } else { |
| 53 | + pos -%= 170; |
| 54 | + return color(pos *% 3, 255 -% pos *% 3, 0); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +var rgb_array: [3 * NUM_LEDS]u8 = undefined; // Each color is 3 bytes |
| 59 | + |
| 60 | +inline fn nops(comptime n: usize) void { |
| 61 | + inline for (0..n) |_| asm volatile ("nop"); |
| 62 | +} |
| 63 | + |
| 64 | +// Compact timing-accurate bit send using pin.put + NOP sequences |
| 65 | +fn led_send_bit(pin: anytype, bit: u1) void { |
| 66 | + if (bit != 0) { |
| 67 | + // T1H ≈ 800 ns @ 48 MHz |
| 68 | + pin.put(1); |
| 69 | + nops(34); |
| 70 | + pin.put(0); |
| 71 | + // T1L ≈ 400Ns, taken up by other functions |
| 72 | + return; |
| 73 | + } |
| 74 | + // T0H ≈ 400 ns |
| 75 | + pin.put(1); |
| 76 | + nops(14); |
| 77 | + pin.put(0); |
| 78 | + // T0L ≈ 850 ns, 400 ns is taken up by other functions |
| 79 | + nops(20); |
| 80 | +} |
| 81 | + |
| 82 | +// Send a single color for a single LED |
| 83 | +// WS2812B LEDs want 24 bits per led in the string |
| 84 | +fn led_send_color(pin: anytype, r: u8, g: u8, b: u8) void { |
| 85 | + // Send the green component first (MSB) |
| 86 | + var i: i32 = 7; |
| 87 | + while (i >= 0) : (i -= 1) { |
| 88 | + led_send_bit(pin, @truncate((g >> @intCast(i)) & 1)); |
| 89 | + } |
| 90 | + // Send the red component next |
| 91 | + i = 7; |
| 92 | + while (i >= 0) : (i -= 1) { |
| 93 | + led_send_bit(pin, @truncate((r >> @intCast(i)) & 1)); |
| 94 | + } |
| 95 | + // Send the blue component last (LSB) |
| 96 | + i = 7; |
| 97 | + while (i >= 0) : (i -= 1) { |
| 98 | + led_send_bit(pin, @truncate((b >> @intCast(i)) & 1)); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn write(pin: anytype) void { |
| 103 | + var i: usize = 0; |
| 104 | + while (i < NUM_LEDS) : (i += 1) { |
| 105 | + led_send_color(pin, rgb_array[i * 3], rgb_array[i * 3 + 1], rgb_array[i * 3 + 2]); |
| 106 | + } |
| 107 | + hal.time.sleep_ms(1); |
| 108 | +} |
| 109 | + |
| 110 | +fn set_led(i: usize, r: u8, g: u8, b: u8) void { |
| 111 | + rgb_array[i * 3] = r; |
| 112 | + rgb_array[i * 3 + 1] = g; |
| 113 | + rgb_array[i * 3 + 2] = b; |
| 114 | +} |
0 commit comments