Initial working commit

main
Joey Hines 2022-11-25 21:41:35 -07:00
commit a9e76f28c9
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
5 changed files with 153 additions and 0 deletions

6
.gitmodules vendored 100644
View File

@ -0,0 +1,6 @@
[submodule "rp2040"]
path = rp2040
url = git@github.com:ZigEmbeddedGroup/rp2040.git
[submodule "uf2"]
path = uf2
url = git@github.com:ZigEmbeddedGroup/uf2.git

23
build.zig 100644
View File

@ -0,0 +1,23 @@
const std = @import("std");
const Builder = std.build.Builder;
const Step = std.build.Step;
const LibExeObjStep = std.build.LibExeObjStep;
const builtin = @import("builtin");
const uf2 = @import("uf2/src/main.zig");
const rp2040 = @import("rp2040/build.zig");
pub fn build(b: *Builder) !void {
const mode = b.standardReleaseOptions();
var exe = rp2040.addPiPicoExecutable(b, "merry_chrysler", "src/main.zig", .{});
exe.setBuildMode(mode);
const uf2_step = uf2.Uf2Step.create(exe.inner, .{
.family_id = .RP2040,
});
uf2_step.install();
exe.install();
}

1
rp2040 160000

@ -0,0 +1 @@
Subproject commit 144d557357314f25dd479e28ef1e16f157f0bb50

122
src/main.zig 100644
View File

@ -0,0 +1,122 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const gpio = rp2040.gpio;
const clocks = rp2040.clocks;
const time = rp2040.time;
const regs = microzig.chip.registers;
const multicore = rp2040.multicore;
const pwm = rp2040.pwm;
const pin_config = rp2040.pins.GlobalConfiguration{
.GPIO0 = .{ .name = "channel0", .function = .PWM0_A },
.GPIO1 = .{ .name = "channel1", .function = .PWM0_B },
.GPIO2 = .{ .name = "channel2", .function = .PWM1_A },
.GPIO3 = .{ .name = "channel3", .function = .PWM1_B },
.GPIO25 = .{ .name = "channelInt", .function = .PWM4_B },
};
const Channels = enum { Channel0, Channel1, Channel2, Channel3, ChannelInt };
const Animations = enum { Candle, Pulse };
const ChannelConfig = struct {
channel: Channels,
animation: Animations,
step: u16,
last_value: u16,
rand_src: std.rand.DefaultPrng,
pub fn init(channel: Channels, animation: Animations) ChannelConfig {
return ChannelConfig{ .channel = channel, .animation = animation, .step = 0, .rand_src = std.rand.DefaultPrng.init(0), .last_value = 0 };
}
fn nextLevelPulse(self: *ChannelConfig) u16 {
if (self.step < 127) {
return self.step;
} else {
return 255 - self.step;
}
}
fn nextLevelCandle(self: *ChannelConfig) u16 {
const Levels = enum(u16) { VeryDim = 255 / 8, Dim = 255 / 4, Half = 255 / 2, Bright = 255 * 3 / 4, FullBright = 255 };
if ((self.step % 10) == 0) {
return @enumToInt(self.rand_src.random().enumValue(Levels));
} else {
return self.last_value;
}
}
pub fn nextLevel(self: *ChannelConfig) u16 {
self.step = ((self.step + 1) % 255);
var value = switch (self.animation) {
Animations.Pulse => self.nextLevelPulse(),
Animations.Candle => self.nextLevelCandle(),
};
self.last_value = value;
return value;
}
};
pub fn cfgChannel(pin: anytype) !void {
pin.slice().setWrap(256);
pin.setLevel(0);
pin.slice().enable();
}
pub fn main() !void {
const pins = pin_config.apply();
try cfgChannel(pins.channel0);
try cfgChannel(pins.channel1);
try cfgChannel(pins.channel2);
try cfgChannel(pins.channel3);
try cfgChannel(pins.channelInt);
var cfg = [_]ChannelConfig{
ChannelConfig.init(Channels.ChannelInt, Animations.Candle),
ChannelConfig.init(Channels.Channel0, Animations.Candle),
ChannelConfig.init(Channels.Channel1, Animations.Pulse),
};
while (true) {
for (cfg) |*channel| {
var level = channel.nextLevel();
switch (channel.channel) {
Channels.ChannelInt => {
pins.channelInt.setLevel(level);
},
Channels.Channel0 => {
pins.channel0.setLevel(level);
},
Channels.Channel1 => {
pins.channel1.setLevel(level);
},
Channels.Channel2 => {
pins.channel2.setLevel(level);
},
Channels.Channel3 => {
pins.channel3.setLevel(level);
},
}
}
time.sleepMs(10);
}
}
test "Test Candle Animations" {
var channel = ChannelConfig.init(Channels.ChannelInt, Animations.Candle);
const val1 = channel.nextLevel();
channel.step = 9;
const val2 = channel.nextLevel();
try std.testing.expect(val1 != val2);
}

1
uf2 160000

@ -0,0 +1 @@
Subproject commit e1a0a5629802933534656af1c286d7fc4076ab2b