liquid crystal is a modular library for alphanumeric lcd displays compatible with the hd44780 controller, made in Rust with Embedded_hal API
features: - does not borrow the Delay function - easily extensible - user-friend
First you must choose a display communication interface, this library provides two built-in interfaces, parallel and I2C (you can create your own interfaces, see here ), then just pass the interface to the display
rust
let mut lcd_interface = Parallel::new(D4, D5, D6, D7, rs, en);
let mut lcd = LiquidCristal::new(&mut lcd_interface);
(this may change in the future, see here )
first you must configure the display.
for this you must call the "init" function and then "fastconfig", the "fastconfig" function receives a configuration struct as an argument, this library comes with one by default, but you can create your own. see here (you can configure directly with the low level "send" function, not recommended if you don't know how to configure the HD44780)
rust
lcd.init(&mut delay);
lcd.fast_config(&mut delay, DEFAULT_CONFIG);
you can send text and commands by the "write" function, this function receives a reference from a delay function and an Enum "SendType" which can be Text or Command
to send a text, pass a &str to the "Text" variant
to send a command, pass a command from the command list to the "Command" varient
rust
lcd.write(&mut delay,Command(Clear))
.write(&mut delay,Text("hello World!"));
you can send custom characters to variant "CustomChar", but first you need to create your custom character by function "custom_char", this function receives delay like all others, a reference to an array of u8 with size 8, and the slot that he will occupy
HD44780 allows you to create 8 custom characters (slot 0 - 7), you can create and modify these slots at any time, but only 8 different characters can be written at the same time on the display. (creating these characters returns the display to its initial position, then use "set_cursor" after creating these characters)
to send use the CustomChar variant with the character slot:
```rust let lightning: [u8; 8] = [0x03, 0x06, 0x0C, 0x1F, 0x1F, 0x03, 0x06, 0x0C];
lcd.custom_char(&mut delay, &lightning, 0);
lcd.write(&mut delay, CustomChar(0));
```
```rust
use panichalt as _; use cortexmrt::entry; use stm32f1xxhal::{pac, prelude::}; use liquid_crystal::{Commands, SendType , LiquidCristal}; use Commands::; use SendType::*; use liquidcrystal::Parallel; use liquidcrystal::DEFAULT_CONFIG;
fn main() -> ! { let cp = cortex_m::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut gpioc = dp.GPIOC.split();
let mut gpioa = dp.GPIOA.split();
let en = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let rs = gpioc.pc14.into_push_pull_output(&mut gpioc.crh);
let d4 = gpioc.pc15.into_push_pull_output(&mut gpioc.crh);
let d5 = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
let d6 = gpioa.pa1.into_push_pull_output(&mut gpioa.crl);
let d7 = gpioa.pa2.into_push_pull_output(&mut gpioa.crl);
let mut delay = cp.SYST.delay(&clocks);
let mut lcd_interface = Parallel::new(d4, d5, d6, d7, rs, en);
let mut lcd = LiquidCristal::new(&mut lcd_interface);
lcd.init(&mut delay);
lcd.fast_config(&mut delay, DEFAULT_CONFIG);
lcd.write(&mut delay,Text("hello World!"))
.write(&mut delay,Command(MoveLine2))
.write(&mut delay,Text("made in Rust!!!"));
loop {}
} ```
to create your own interface, you must implement the "Interface" Trait which contains the "send" function
"send" receives a u8 value which the bits represent:
| BIT7 | BIT6 |BIT5| BIT4| BIT3| BIT2| BIT1| BIT0|
| :------ | :------ | :------| :------| :------| :------| :------| :------|
| DATA7
| DATA6
| DATA5
| DATA4
| Reserved
| ENABLE
| READ_WRITE
| REGISTER_SELECT
|
(still no function to read, so keep the READ_WRITE pin in pull down)
(Reserved
corresponds to the display backlight in the I2C module)
where 0 and 1 represent the state of the pin 1: HIGH 0: LOW
connect the bits to their respective ports, and congratulations you have created your own interface
(working on the documentation for the commands)
"FastConfig" is a struct that contains the display configuration
rust
struct FastConfig{
pub entry_mode: (ShiftConfig, ShiftState),
pub display: (Display, Cursor, Blink),
pub display_config: (Bits,DisplayLines,CharSize),
}
each attribute corresponds to a type of configuration (defined in the HD44780 manual)
choose a variant from each configuration enum to create a display configuration
(working on documentation for the Enums)
I use lcd display for a long time, and I always had to rewrite the Drive when I need to use some IO expander, because the current APIs don't provide a simple way to port the communication.
this API is currently a personal test using embedded_hal, current syntax may change based on users feedback.