Implement print_char#6
Conversation
u8 in this case will be the byte, that can be displayed. The mappings come from this this picture: https://www.meine-schaltung.de/notizbuch/anzeige/sieben_segment_zeichen/img/alphabet_a_z.jpg
|
Well, I kinda intentionally avoided adding any alphabets here coz the device doesn't natively support letters so displaying any is an approximation/imagination. |
It definitely is =) diff --git a/examples/main.rs b/examples/main.rs
index 94e0064..bb01ca8 100644
--- a/examples/main.rs
+++ b/examples/main.rs
@@ -5,8 +5,10 @@
extern crate panic_halt;
extern crate stm32f103xx_hal as hal;
+extern crate embedded_hal as ehal;
use hal::delay::Delay;
+use ehal::digital::v2::{InputPin, OutputPin};
use hal::prelude::*;
use hal::stm32f103xx::Peripherals;
use embedded_hal::blocking::delay::DelayUs;
@@ -14,13 +16,37 @@ use embedded_hal::blocking::delay::DelayUs;
use cortex_m_rt::entry;
extern crate tm1637;
-use tm1637::{ TM1637 };
+use tm1637::{ TM1637, Error };
struct NoDelay {}
impl DelayUs<u16> for NoDelay {
fn delay_us(&mut self, us: u16) {}
}
+trait PrintChars<E> {
+ fn print_chars(&mut self, address: u8, chars: &[char])->Result<(), Error<E>>;
+}
+
+// I wish Rust has some way to extend generic traits without explicit bounds re-definition
+// so all the types may be infered from those are used in TM1637
+impl<'a, CLK, DIO, D, E> PrintChars<E> for TM1637<'a, CLK, DIO, D>
+where
+ CLK: OutputPin<Error = E>,
+ DIO: InputPin<Error = E> + OutputPin<Error = E>,
+ D: DelayUs<u16>,
+{
+ fn print_chars(&mut self, address: u8, chars: &[char])->Result<(), Error<E>> {
+ self.print_raw_iter(address, chars.iter().map(|b| char_to_raw(*b)))
+ }
+}
+
+fn char_to_raw(c: char) -> u8 {
+ match c {
+ 'a' | 'A' => 0x77,
+ _ => 0x00,
+ }
+}
+
#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
@@ -49,6 +75,8 @@ fn main() -> ! {
tm.print_raw(3, &[i]);
tm.set_brightness(i >> 5);
+
+ tm.print_chars(0, &['A', 'a']);
}
}
}it compiles but I have no device to check how it works. |
|
It would be great if Rust allow write just: impl for TM1637 /*please, do it for all TM1637 specialisations*/ {
fn print_chars(&mut self, address: u8, chars: &[char]) /*please, infer the result type for me =)*/ {
self.print_raw_iter(address, chars.iter().map(|b| char_to_raw(*b)))
}
}just to decouple this function implementation from the library implementation details. |
|
But that would require the user to write this trait out with all its generic types. I introduced a |
|
I also think about adding a |
Hello,
this PR addresses issue #4 by implementing a
print_chars()function.This uses your techniques. Please let me know, if i did something wrong.
I have tested this on multiple display-pico combinations with great success.
As the display is sort of limited in its capabilities, some letters look a bit wrong, but i tried my best to follow this picture (with some assumptions involved).
The function increases the footprint of this library, by adding a mapping-helper-function that stores all A-z chars if used.
Cheers!