I am trying to code a driver for an LCM1602C LCD display in Rust for the Arduino Uno using arduino_hal
because I have not found a library for it. What I have thus far is:
use arduino_hal::Peripherals;
use arduino_hal::port::Pin;
use arduino_hal::port::mode::Output;
struct Lcd {
rs: Pin<Output, PD1>,
en: Pin<Output, PD2>,
d4: Pin<Output, PD4>,
d5: Pin<Output, PD5>,
d6: Pin<Output, PD6>,
d7: Pin<Output, PD7>,
}
impl Lcd {
pub fn new() -> Self {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let rs = pins.d1.into_output();
let en = pins.d2.into_output();
let d4 = pins.d4.into_output();
let d5 = pins.d5.into_output();
let d6 = pins.d6.into_output();
let d7 = pins.d7.into_output();
Lcd {
rs: rs,
en: en,
d4: d4,
d5: d5,
d6: d6,
d7: d7,
}
}
pub fn init(&mut self) {
// Initialization sequence
for _ in 0..3 {
self.write_nibble(0x03, false);
arduino_hal::delay_ms(5);
}
self.write_nibble(0x02, false);
arduino_hal::delay_ms(5);
// Function set
self.write_command(0x28);
arduino_hal::delay_ms(5);
// Display on/off control
self.write_command(0x0C);
arduino_hal::delay_ms(5);
// Clear display
self.write_command(0x01);
arduino_hal::delay_ms(5);
// Entry mode set
self.write_command(0x06);
arduino_hal::delay_ms(5);
}
pub fn write_command(&mut self, command: u8) {
self.write_byte(command, false);
}
pub fn write_string(&mut self, s: &str) {
for c in s.chars() {
self.write_byte(c as u8, true);
arduino_hal::delay_ms(1);
}
}
pub fn write_byte(&mut self, byte: u8, rs: bool) {
self.rs.set_high();
if rs {
self.rs.set_high();
} else {
self.rs.set_low();
}
self.write_nibble(byte >> 4, rs);
self.write_nibble(byte & 0x0F, rs);
}
fn write_nibble(&mut self, nibble: u8, rs: bool) {
if (nibble & 0x01) != 0 {
self.d4.set_high();
} else {
self.d4.set_low();
}
if (nibble & 0x02) != 0 {
self.d5.set_high();
} else {
self.d5.set_low();
}
if (nibble & 0x04) != 0 {
self.d6.set_high();
} else {
self.d6.set_low();
}
if (nibble & 0x08) != 0 {
self.d7.set_high();
} else {
self.d7.set_low();
}
self.enable();
}
fn enable(&mut self) {
self.en.set_high();
arduino_hal::delay_us(1);
self.en.set_low();
arduino_hal::delay_us(1);
}
}
The problem I have is that this is Not how one should set up the pins in the Lcd
struct, even though it looks like the documentation (I believe). The error I get looks like this:
error[E0412]: cannot find type `PD1` in this scope
--> src/lcd.rs:7:21
|
6 | struct Lcd {
| - help: you might be missing a type parameter: `<PD1>`
7 | rs: Pin<Output, PD1>,
| ^^^ not found in this scope
I have also tried to not have the Pin
set but only have Pin<Output>
on every pin, but then I get
error[E0308]: mismatched types
--> src/lcd.rs:28:17
|
28 | rs: rs,
| ^^ expected struct `Dynamic`, found struct `PD1`
|
= note: expected struct `avr_hal_generic::port::Pin<_, Dynamic>`
found struct `avr_hal_generic::port::Pin<_, PD1>`
which seems to contradict the last error message.
Do anyone know what is happening?
Edit:
To help you reproduce the error you can follow the setup at avr-hal and generate a project using cargo generate --git https://github.com/Rahix/avr-hal-template.git
. I am on an Arch Linux box.