Created
October 7, 2024 15:11
-
-
Save jacobzlogar/6bab58cba1217ff178d980a130082fdb to your computer and use it in GitHub Desktop.
ht16k33 i2c 7-segment led display high-level driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
pub struct HT16K33<I2C> { | |
address: u8, | |
i2c: I2C, | |
} | |
impl<I2C> HT16K33<I2C> | |
where | |
I2C: embedded_hal::i2c::I2c, | |
{ | |
pub fn new(address: u8, i2c: I2C) -> Self { | |
Self { address, i2c } | |
} | |
pub fn init(&mut self) -> Result<(), I2C::Error> { | |
self.power_on()?; | |
self.display_on()?; | |
Ok(()) | |
} | |
pub fn blink(&mut self, blink_rate: BlinkRate) -> Result<(), I2C::Error> { | |
self.write(&[blink_rate as u8])?; | |
Ok(()) | |
} | |
pub fn power_on(&mut self) -> Result<(), I2C::Error> { | |
self.write(&[SystemSetup::PowerOn as u8])?; | |
Ok(()) | |
} | |
pub fn power_off(&mut self) -> Result<(), I2C::Error> { | |
self.write(&[SystemSetup::PowerOff as u8])?; | |
Ok(()) | |
} | |
pub fn display_on(&mut self) -> Result<(), I2C::Error> { | |
self.write(&[Command::DisplayOn as u8])?; | |
Ok(()) | |
} | |
pub fn display_off(&mut self) -> Result<(), I2C::Error> { | |
self.write(&[Command::DisplayOff as u8])?; | |
Ok(()) | |
} | |
pub fn write(&mut self, payload: &[u8]) -> Result<(), I2C::Error> { | |
self.i2c.write(self.address, payload)?; | |
Ok(()) | |
} | |
pub fn read(&mut self, payload: &[u8]) -> Result<[u8; 8], I2C::Error> { | |
esp_println::println!("{:?}", payload); | |
let mut data: [u8; 8] = [0u8; 8]; | |
self.i2c.write_read(self.address, &payload, &mut data)?; | |
Ok(data) | |
} | |
pub fn write_digit(&mut self, row: RowData, digit: &SegmentDigit) -> Result<(), I2C::Error> { | |
self.write(&[row as u8, *digit as u8])?; | |
Ok(()) | |
} | |
} | |
#[repr(u8)] | |
#[derive(Copy, Clone)] | |
pub enum SegmentDigit { | |
Zero = 0x3F, | |
One = 0x06, | |
Two = 0x5B, | |
Three = 0x4F, | |
Four = 0x66, | |
Five = 0x6D, | |
Six = 0x7D, | |
Seven = 0x07, | |
Eight = 0x7F, | |
Nine = 0x6F, | |
} | |
impl From<u8> for SegmentDigit { | |
fn from(digit: u8) -> Self { | |
match digit { | |
0 => Self::Zero, | |
1 => Self::One, | |
2 => Self::Two, | |
3 => Self::Three, | |
4 => Self::Four, | |
5 => Self::Five, | |
6 => Self::Six, | |
7 => Self::Seven, | |
8 => Self::Eight, | |
9 => Self::Nine, | |
_ => unreachable!(), | |
} | |
} | |
} | |
#[repr(u8)] | |
enum RowRegister { | |
IntRowOutputRowOutput = 0xA0, | |
IntRowOutputIntOutputLow = 0xA1, | |
IntRowOutputIntOutputHigh = 0xA2, | |
} | |
#[repr(u8)] | |
enum SystemSetup { | |
PowerOn = 0x21, | |
PowerOff = 0x20, | |
} | |
#[repr(u8)] | |
pub enum BlinkRate { | |
Off = 0x81, | |
Blink2Hz = 0x83, | |
Blink1Hz = 0x85, | |
BlinkHalfHz = 0x87, | |
} | |
#[repr(u8)] | |
enum Command { | |
DisplayOff = 0x80, | |
DisplayOn = 0x81, | |
} | |
#[repr(u8)] | |
pub enum AddressRegister { | |
A0R = 0x0, | |
A0W = 0x1, | |
} | |
#[repr(u8)] | |
pub enum RowData { | |
Row0 = 0x0, | |
Row1 = 0x2, | |
Divider = 0x4, | |
Row3 = 0x6, | |
Row4 = 0x8, | |
} | |
impl From<u8> for RowData { | |
fn from(row: u8) -> Self { | |
match row { | |
0 => Self::Row0, | |
1 => Self::Row1, | |
2 => Self::Row3, | |
3 => Self::Row4, | |
_ => Self::Divider, | |
} | |
} | |
} | |
#[repr(u8)] | |
pub enum ColumnData { | |
A0R = 0xE2, | |
A0W = 0xE3, | |
A1R = 0xE4, | |
A1W = 0xE5, | |
A2R = 0xE8, | |
A2W = 0xE9, | |
} | |
#[repr(u8)] | |
pub enum KeyData { | |
KS0 = 0x40, | |
KS1 = 0x41, | |
KS2 = 0x42, | |
KS3 = 0x43, | |
KS4 = 0x44, | |
KS5 = 0x45, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment