Skip to content

Commit

Permalink
Implement defining custom characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinTimBarndt committed Sep 17, 2024
1 parent 3304d93 commit ec34e2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/character.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use core::iter::once;

use embedded_hal::delay::DelayNs;

use crate::{bus::DataBus, charset::CharsetWithFallback, error::Result, memory_map::DisplayMemoryMap, HD44780};

pub struct CharacterDefinition {
pub pattern: [u8; 10],
pub cursor: u8,

// /// Since lines 12 to 16 are not used for display,
// /// they can be used for general data RAM.
// pub data: Option<[u8; 5]>,
}

impl<B, M, C> HD44780<B, M, C>
where
B: DataBus,
M: DisplayMemoryMap,
C: CharsetWithFallback,
{
pub fn define_custom_character<D: DelayNs>(
&mut self,
code: u8,
def: &CharacterDefinition,
delay: &mut D,
) -> Result<(), B::Error> {
self.write_command(0b01000000 | (code & 0b11) << 4, delay)?;
delay.delay_us(100);

let lines = def.pattern.iter().cloned().chain(once(def.cursor));
for line in lines {
self.write_byte(line, delay)?;
}

// Change back to DDRAM
self.set_cursor_pos(0, delay)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use entry_mode::{CursorMode, EntryMode};

pub mod setup;

pub mod character;
pub mod charset;

pub mod memory_map;
Expand Down

0 comments on commit ec34e2b

Please sign in to comment.