#adafruit #ht16k33 #embedded-hal-driver #no-std

no-std adafruit-7segment

ht16k33 crate 之上的小封装,用于驱动 Adafruit 7 段 LED 数码背光板

1 个不稳定版本

0.1.0 2020年8月7日

#1390硬件支持

MIT/Apache

38KB
393

Adafruit 7段数码背光板 Hal

Version info Documentation Build Status

ht16k33 crate 的基础上添加了额外的功能,用于通过 embedded-hal 的特质驱动 Adafruit 7段 LED 数码背光板。

基于 adafruit-alphanum4 crate 开发,并针对 7段背光板进行了修改。

特性

  • 向 4 个段之一发送 u8。限制为 0x00 到 0x0F。
  • 向 4 个段之一发送 AsciiChar。限制为 ASCII 十六进制字符和减号。
  • 设置或清除与 4 个段之一的点。
  • 设置或清除冒号。
  • f32 格式化为 1 到 4 段

用法

嵌入式平台

STM32F4-Discovery 板上的示例

有关其他平台的示例,请参阅 ht16k33 crate

Cargo.toml 依赖示例

[dependencies]
htk16k33 = { version = "0.4.0", default-features = false }
adafruit-7segment = { version = "0.1.0", default-features = false  }
embedded-hal = "0.2.3"
cortex-m = "0.6.2"
cortex-m-rt = "0.6.12"
panic-halt = "0.2.0"

[dependencies.stm32f4xx-hal]
version = "0.8"
features = ["rt", "stm32f407"]

测试代码

#![no_main]
#![no_std]

use panic_halt as _;

use cortex_m;
use cortex_m_rt::entry;
use stm32f4xx_hal as hal;

use crate::hal::{i2c::I2c, prelude::*, stm32};
use ht16k33::{HT16K33, Dimming, Display};
use adafruit_7segment::{SevenSegment, Index};
pub use ascii::{ToAsciiChar, AsciiChar};

#[entry]
fn main() -> ! {
 if let (Some(dp), Some(cp)) = (
   stm32::Peripherals::take(),
   cortex_m::peripheral::Peripherals::take(),
 ) {
   // Set up the system clock. We want to run at 48MHz for this one.
   let rcc = dp.RCC.constrain();
   let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();

   const DISP_I2C_ADDR: u8 = 112;

   // Set up I2C - SCL is PB8 and SDA is PB7; they are set to Alternate Function 4
   // as per the STM32F407 datasheet.
   let gpiob = dp.GPIOB.split();
   let scl = gpiob.pb8.into_alternate_af4().set_open_drain();
   let sda = gpiob.pb7.into_alternate_af4().set_open_drain();
   let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 400.khz(), clocks);

   let mut ht16k33 = HT16K33::new(i2c, DISP_I2C_ADDR);
   ht16k33.initialize().expect("Failed to initialize ht16k33");
   ht16k33.set_display(Display::ON).expect("Could not turn on the display!");
   ht16k33.set_dimming(Dimming::BRIGHTNESS_MIN).expect("Could not set dimming!");

   // Sending individual digits
   ht16k33.update_buffer_with_digit(Index::One, 1);
   ht16k33.update_buffer_with_digit(Index::Two, 2);
   ht16k33.update_buffer_with_digit(Index::Three, 3);
   ht16k33.update_buffer_with_digit(Index::Four, 4);

   // Sending ascii
   ht16k33.update_buffer_with_char(Index::One, AsciiChar::new('A'));
   ht16k33.update_buffer_with_char(Index::Two, AsciiChar::new('B'));

   // Setting the decimal point
   ht16k33.update_buffer_with_dot(Index::Two, true);

   // Formatting a float using the whole display
   ht16k33.update_buffer_with_float(Index::One, -3.14, 2, 10).unwrap();

   // Putting a character in front of a float
   ht16k33.update_buffer_with_char(Index::One, AsciiChar::new('b'));
   // Display will read "b-3.1"
   ht16k33.update_buffer_with_float(Index::Two, -3.14, 2, 10).unwrap();

   // This will panic because there aren't enough digits to display this number
   ht16k33.update_buffer_with_float(Index::One, 12345., 0, 10).expect("Oops");

   // Note: none of the above methods actually commit the buffer to the display,
   // call write_display_buffer to actually send it to the display
   ht16k33.write_display_buffer().unwrap()
  }
loop {}
}

所有平台,使用 I2C 模拟

use ht16k33::i2c_mock::I2cMock;
use ht16k33::{HT16K33, Dimming, Display};
use adafruit_7segment::{SevenSegment, Index};

// The I2C device address.
const DISP_I2C_ADDR: u8 = 112;

// Create a mock I2C device.
let mut i2c = I2cMock::new();

let mut ht16k33 = HT16K33::new(i2c, DISP_I2C_ADDR);
ht16k33.initialize().expect("Failed to initialize ht16k33");
ht16k33.set_display(Display::ON).expect("Could not turn on the display!");
ht16k33.set_dimming(Dimming::BRIGHTNESS_MIN).expect("Could not set dimming!");

// Sending individual digits
ht16k33.update_buffer_with_digit(Index::One, 1);
ht16k33.update_buffer_with_digit(Index::Two, 2);
ht16k33.update_buffer_with_digit(Index::Three, 3);
ht16k33.update_buffer_with_digit(Index::Four, 4);

// Note: none of the above methods actually commit the buffer to the display,
// call write_display_buffer to actually send it to the display
ht16k33.write_display_buffer().unwrap()

性能警告

由于 ht16k33 crate 的 api,显示缓冲区无法直接访问,因此每个构成字符的 LED 都会按顺序更新。背光板上硬件的配置允许通过设置缓冲区中的单个 16 位值来更新字符。每次更新时都对 16 位的每个位进行迭代显然不是最优的,但对于我的当前使用来说已经足够快。如果 ht16k33 crate 更新为允许对缓冲区进行可变访问,则可以改进这一点。

发布历史

这是一份变更日志,描述了每个版本的最重要的更改。

版本 0.1.0 — 2020年8月7日

  • 第一个版本

许可证

根据您的要求,许可在 Apache 许可证,版本 2.0MIT 许可证 下。

贡献

除非您明确声明,否则根据Apache-2.0许可证定义,您有意提交的任何贡献,用于工作内容,应以上述方式双授权,不得附加任何额外条款或条件。

依赖项

~375KB