Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/raspberrypi/rp2xxx/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
.version = "0.0.0",
.dependencies = .{
.microzig = .{ .path = "../../.." },
.font8x8 = .{
.url = "git+https://github.com/Gnyblast/zig-ssd1306-gddram-fonts#84dd5fab70ddc1198eef1cd1305cc82f08f419dc",
.hash = "ssd1306_font_8x8-0.0.0-oGb_cERcAAA6NJzWDOMepgnY6r4blNEHjpkldDaRAui5",
},
},
.paths = .{
"README.md",
Expand Down
84 changes: 84 additions & 0 deletions examples/raspberrypi/rp2xxx/src/ssd1306_oled.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const i2c = rp2xxx.i2c;
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

.GPIO8 = .{
.name = "SDA",
.function = .I2C0_SDA,
.schmitt_trigger = .enabled,
.slew_rate = .slow,
.pull = .up,
.direction = .out,
},
.GPIO9 = .{
.name = "SCL",
.function = .I2C0_SCL,
.schmitt_trigger = .enabled,
.slew_rate = .slow,
.pull = .up,
.direction = .out,
},
};

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;
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


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

var fba = std.heap.FixedBufferAllocator.init(&backinf_buffer);

pin_config.apply();
rp2xxx.i2c.I2C.apply(i2c0, .{ .baud_rate = 400_000, .clock_config = rp2xxx.clock_config });

const I2C_DEVICE = rp2xxx.drivers.I2C_Device.init(i2c0, lcd_address, null);
const lcd = microzig.drivers.display.ssd1306.init(.i2c, I2C_DEVICE, null) catch unreachable;

const print_val = four_rows ++ " WELCOME!";
var buff: [print_val.len * 8]u8 = undefined;

lcd.clear_screen(false) catch unreachable;
lcd.write_gdram(font8x8.Fonts.draw(&buff, print_val)) catch unreachable;

time.sleep_ms(2000);

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.

var temp_buf: [7]u8 = undefined;
const str = std.fmt.bufPrint(&temp_buf, "Try #{}", .{ counter }) catch unreachable;
var counter_buf: [80]u8 = undefined;
const text_centered = center(&counter_buf, str);

const text = font8x8.Fonts.drawAlloc(allocator, text_centered) catch continue;
defer allocator.free(text);

lcd.clear_screen(false) catch continue;
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?

if (counter > 59)
break;
}
}

fn center(buf: []u8, str: []u8) []u8 {
const to_be_added = @divTrunc(16 - str.len, 2);
@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)], " ");
Comment on lines +75 to +81
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

}
return buf;
}
Loading