Crates.io | i2c-character-display |
lib.rs | i2c-character-display |
version | 0.4.0 |
source | src |
created_at | 2024-09-30 00:32:18.958184 |
updated_at | 2024-11-24 18:06:18.664162 |
description | Driver for HD44780-based character displays connected via a I2C adapter |
homepage | |
repository | https://github.com/michaelkamprath/i2c-character-display |
max_upload_size | |
id | 1391192 |
size | 131,411 |
This Rust embedded-hal
-based library is a simple way to control a character display that has either a HD44780
or AiP31068 contoller with an I2C interface
in an embedded, no_std
environment. A number of I2C interfaces are supported:
Key features include:
core::fmt::Write
implementation for easy use with the write!
macroembedded-hal
traits v1.0 and laterdefmt
and ufmt
logging frameworksAdd this to your Cargo.toml
:
[dependencies]
i2c-character-display = { version = "0.4", features = ["defmt"] }
The features = ["defmt"]
line is optional and enables the defmt
feature, which allows the library's errors to be used with the defmt
logging
framework. Another optional feature is features = ["ufmt"]
, which enables the ufmt
feature, allowing the uwriteln!
and uwrite!
macros to be used.
Then select the appropriate adapter for your display:
use i2c_character_display::{AdafruitLCDBackpack, CharacterDisplayPCF8574T, LcdDisplayType};
use embedded_hal::delay::DelayMs;
use embedded_hal::i2c::I2c;
// board setup
let i2c = ...; // I2C peripheral
let delay = ...; // DelayMs implementation
// It is recommended that the `i2c` object be wrapped in an `embedded_hal_bus::i2c::CriticalSectionDevice` so that it can be shared between
// multiple peripherals.
// Adafruit backpack for a single HD44780 controller
let mut lcd = AdafruitLCDBackpack::new(i2c, LcdDisplayType::Lcd16x2, delay);
// PCF8574T adapter for a single HD44780 controller
let mut lcd = CharacterDisplayPCF8574T::new(i2c, LcdDisplayType::Lcd16x2, delay);
// Character display with dual HD44780 controllers using a single PCF8574T I2C adapter
let mut lcd = CharacterDisplayDualHD44780::new(i2c, LcdDisplayType::Lcd40x4, delay);
// Character display with the AiP31068 controller
let mut lcd = CharacterDisplayAIP31068::new(i2c, LcdDisplayType::Lcd16x2, delay);
When creating the display object, you can choose the display type from the LcdDisplayType
enum. The display type should match the physical
display you are using. This display type configures the number of rows and columns, and the internal row offsets for the display.
Initialize the display:
if let Err(e) = lcd.init() {
panic!("Error initializing LCD: {}", e);
}
Use the display:
// set up the display
lcd.backlight(true)?.clear()?.home()?;
// print a message
lcd.print("Hello, world!")?;
// can also use the `core::fmt::write!` macro
use core::fmt::Write;
write!(lcd, "Hello, world!")?;
The optional ufmt
feature enables the ufmt
crate, which allows the uwriteln!
and uwrite!
macros to be used with the display:
use ufmt::uwriteln;
uwriteln!(lcd, "Hello, world!")?;
The various methods for controlling the LCD are also available. Each returns a Result
that wraps the display object in Ok()
, allowing for easy chaining
of commands. For example:
lcd.backlight(true)?.clear()?.home()?.print("Hello, world!")?;
Some I2C adapters support reading data from the HD44780 controller. For the I2C adapters that support it, the read_device_data
method can be used to read
from either the CGRAM or DDRAM at the current cursor position. The read_address_counter
method can be used to read the address counter from the HD44780 controller.
In both cases, the specific meaning of the data depends on the prior commands sent to the display. See the HD44780 datasheet for more information.
All HD44780 controllers support backlight control. The backlight
method can be used to turn the backlight on or off. The AiP31068 controller does not support
backlight control, and calling the backlight
method with a AiP31068 controller will return an error.
Some character displays, such as the 40x4 display, use two HD44780 controllers to drive the display. This library supports these displays by
treating them as one logical display with multiple HD44780 controllers. The CharacterDisplayDualHD44780
type is used to control these displays.
Use the various methods to control the display as you would with a single HD44780 controller display. The set_cursor
method sets the active HD44780
conmtroller device based on the row number you select.
Licensed under the MIT license.