JohnDoneth / hd44780-driver

Implementation of the embedded-hal traits for the HD44780.
MIT License
37 stars 40 forks source link

Custom Characters #8

Open h4llow3En opened 5 years ago

h4llow3En commented 5 years ago

In my now abandoned project about the HD44780 I tried my luck with the following function to create custom characters. Maybe this is a bit of help to you.

static CGRAM_ADDRESS: u8 = 0x40;
...
    /// Creates a new custom character from a bitmap on the given `address`
    pub fn create_char(&self, address: u8, bitmap: [u8; 8]) -> Result<u8, &'static str> {
        // send new custom character to cgram address
        match address {
            0...7 => {
                // Just send the COMMAND command to the given address
                self.command(CGRAM_ADDRESS | address << 3);
                for row in &bitmap {
                    self.send_byte(bitmap[*row as usize], DATA);
                }
                Ok(address)
            },
            _ => Err("address must be between 0 and 7"),
        }

    }
felipetavares commented 4 months ago

Just ported your function for the current version of the project (+ the esp-hal 1.0 support in another PR) @h4llow3En

pub fn create_char<D: DelayNs>(&mut self, address: u8, bitmap: [u8; 8], delay: &mut D) -> Result<u8> {
    // send new custom character to cgram address                                                     
    match address {                                                                                   
        0..=7 => {                                                                                    
            // Just send the COMMAND command to the given address                                     
            self.write_command(0x40 | address << 3, delay);                                           
            for row in &bitmap {                                                                      
                self.write_byte(*row, delay);                                                         
            }                                                                                         
            Ok(address)                                                                               
        }                                                                                             
        _ => Err(Error),                                                                              
    }                                                                                                 
}