-
Notifications
You must be signed in to change notification settings - Fork 147
rp2xxx: ssd1306 oled display example #646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4bfc6df
1d78d14
c180aed
c3cc2a7
4c68a9a
e591500
ad934a4
76cbd46
7173e92
db192a5
e278d65
6559259
e780dea
dbbc148
18c0839
a4cac98
501b50e
987f307
2eb54d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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{ | ||
.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 = " "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be nicer to also do |
||
const four_rows = empty_row ++ empty_row ++ empty_row ++ empty_row; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
pub fn main() void { | ||
var backinf_buffer: [200 * 1024]u8 = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does 64 mean here? Avoid magic numbers |
||
} | ||
return buf; | ||
} |
There was a problem hiding this comment.
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