Skip to content

Conversation

Gnyblast
Copy link
Contributor

No description provided.

@Gnyblast Gnyblast changed the title rp2xxxx: ssd1306 oled display example rp2xxx: ssd1306 oled display example Aug 14, 2025
Copy link
Contributor

@mattnite mattnite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important that examples are compiled so that CI catches breaks. It's okay to add a dependency to the example as it is separate from the actual package. I also see that it is your package, and that is fine, but it needs to have a compatible LICENSE, and the dependency must link to a commit with that license. If you fail to maintain that package in the future and it blocks our CI, this example can be removed.

@tact1m4n3
Copy link
Collaborator

You should also add the example to build.zig as well adding the module import. These are the changes you need to make:

diff --git a/examples/raspberrypi/rp2xxx/build.zig b/examples/raspberrypi/rp2xxx/build.zig
index 1add360d..8a4382ed 100644
--- a/examples/raspberrypi/rp2xxx/build.zig
+++ b/examples/raspberrypi/rp2xxx/build.zig
@@ -9,6 +9,8 @@ pub fn build(b: *std.Build) void {
     const optimize = b.standardOptimizeOption(.{});
     const maybe_example = b.option([]const u8, "example", "only build matching examples");
 
+    const font8x8_dep = b.dependency("font8x8", .{});
+
     const mz_dep = b.dependency("microzig", .{});
     const mb = MicroBuild.init(b, mz_dep) orelse return;
 
@@ -68,6 +70,9 @@ pub fn build(b: *std.Build) void {
         .{ .name = "dma", .file = "src/dma.zig" },
         .{ .name = "cyw43", .file = "src/cyw43.zig" },
         .{ .name = "mlx90640", .file = "src/mlx90640.zig" },
+        .{ .name = "ssd1306-oled", .file = "src/ssd1306_oled.zig", .imports = &.{
+            .{ .name = "font8x8", .module = font8x8_dep.module("font8x8") },
+        } },
     };
 
     var available_examples = std.ArrayList(Example).init(b.allocator);
@@ -77,12 +82,14 @@ pub fn build(b: *std.Build) void {
             .target = mb.ports.rp2xxx.boards.raspberrypi.pico,
             .name = b.fmt("pico_{s}", .{example.name}),
             .file = example.file,
+            .imports = example.imports,
         }) catch @panic("out of memory");
 
         available_examples.append(.{
             .target = mb.ports.rp2xxx.boards.raspberrypi.pico2_arm,
             .name = b.fmt("pico2_arm_{s}", .{example.name}),
             .file = example.file,
+            .imports = example.imports,
         }) catch @panic("out of memory");
 
         if (example.works_with_riscv) {
@@ -90,6 +97,7 @@ pub fn build(b: *std.Build) void {
                 .target = mb.ports.rp2xxx.boards.raspberrypi.pico2_riscv,
                 .name = b.fmt("pico2_riscv_{s}", .{example.name}),
                 .file = example.file,
+                .imports = example.imports,
             }) catch @panic("out of memory");
         }
     }
@@ -110,6 +118,7 @@ pub fn build(b: *std.Build) void {
             .target = example.target,
             .optimize = optimize,
             .root_source_file = b.path(example.file),
+            .imports = example.imports,
         });
 
         // `install_firmware()` is the MicroZig pendant to `Build.installArtifact()`
@@ -127,10 +136,12 @@ const Example = struct {
     target: *const microzig.Target,
     name: []const u8,
     file: []const u8,
+    imports: []const std.Build.Module.Import = &.{},
 };
 
 const ChipAgnosticExample = struct {
     name: []const u8,
     file: []const u8,
     works_with_riscv: bool = true,
+    imports: []const std.Build.Module.Import = &.{},
 };

To test that your example is building you should run zig build -Dexample=ssd1306-oled in the rp2xxx examples directory and check the zig-out directory.

const i2c0 = i2c.instance.num(0);
const lcd_address = rp2xxx.i2c.Address.new(0x3C);
const empty_row: []const u8 = " ";
const four_rows = empty_row ++ empty_row ++ empty_row ++ empty_row;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty_row ** 4 is probably nicer


const i2c0 = i2c.instance.num(0);
const lcd_address = rp2xxx.i2c.Address.new(0x3C);
const empty_row: []const u8 = " ";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to also do " " ** xxx. Makes it easy to see how wide a row is.

const four_rows = empty_row ++ empty_row ++ empty_row ++ empty_row;

pub fn main() void {
var backinf_buffer: [200 * 1024]u8 = undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers? Would be nice to define these as a const for someone with a different display to more easily understand and change these values

const font8x8 = @import("font8x8");

// Compile-time pin configuration
const pin_config = rp2xxx.pins.GlobalConfiguration{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is fine, it's probably overkill. You can probably just copy how the pins are configured for other i2c examples e.g. i2c_bus_scan

lcd.write_gdram(text) catch continue;

counter += 1;
time.sleep_ms(1000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You sleep here but also at the top of the loop. Is this intentional?


var counter: u8 = 0;
while (true) : (time.sleep_ms(1000)) {
var allocator = fba.allocator();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably want to move this and the defer free out of the loop.

Comment on lines +75 to +81
@memcpy(buf[0..64], four_rows);
for (0..to_be_added) |i| {
@memcpy(buf[(64 + i)..(64 + i + 1)], " ");
}
@memcpy(buf[(64 + to_be_added)..(64 + to_be_added + str.len)], str);
for ((64 + str.len + to_be_added)..buf.len) |i| {
@memcpy(buf[i..(i + 1)], " ");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does 64 mean here? Avoid magic numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants