charlcd/custom_char.rs
1//! Create custom characters by defining their pixels.
2//!
3//! A custom character is considered as a 8x5 pixel array.
4//!
5//! You can declare a custom character by defining an array of type `[u8; 8]`
6//! where:
7//!
8//! - each byte represents a line;
9//! - each bit in the byte represent a pixel.
10//!
11//! Only the 5 lower bits of the byte are used, because the character width is
12//! 5 pixels.
13//!
14//! # Example
15//!
16//! ```
17//! /// ▸
18//! #[cfg_attr(rustfmt, rustfmt_skip)]
19//! pub const RIGHT_TRIANGLE: [u8; 8] = [
20//! 0b00000,
21//! 0b01000,
22//! 0b01100,
23//! 0b01110,
24//! 0b01100,
25//! 0b01000,
26//! 0b00000,
27//! 0b00000,
28//! ];
29//! ```
30//!
31//! The `#[cfg_attr(rustfmt, rustfmt_skip)]` part is required in order to
32//! avoid `rustfmt` put each item after the next one so that we cannot see the
33//! visual pixel representation of the custom character anymore.
34//!
35//! The custom character can then be put into the screen's memory by using the
36//! [`Screen::custom_char`][super::Screen::custom_char] function.
37
38/// X axis mirror of a custom LCD character
39pub const fn mirror_x(src: [u8; 8]) -> [u8; 8] {
40 // const fn do not support for loops currently, hence manual unroll
41 [
42 src[0].reverse_bits() >> 3,
43 src[1].reverse_bits() >> 3,
44 src[2].reverse_bits() >> 3,
45 src[3].reverse_bits() >> 3,
46 src[4].reverse_bits() >> 3,
47 src[5].reverse_bits() >> 3,
48 src[6].reverse_bits() >> 3,
49 src[7].reverse_bits() >> 3,
50 ]
51}
52
53/// Y axis mirror of a custom LCD character
54pub const fn mirror_y(src: [u8; 8]) -> [u8; 8] {
55 // const fn do not support for loops currently, hence manual unroll
56 [
57 src[7], src[6], src[5], src[4], src[3], src[2], src[1], src[0],
58 ]
59}
60
61/// ▸
62#[cfg_attr(rustfmt, rustfmt_skip)]
63pub const RIGHT_TRIANGLE: [u8; 8] = [
64 0b00000,
65 0b01000,
66 0b01100,
67 0b01110,
68 0b01100,
69 0b01000,
70 0b00000,
71 0b00000,
72];
73
74/// ◂
75pub const LEFT_TRIANGLE: [u8; 8] = mirror_x(RIGHT_TRIANGLE);
76
77/// ▴
78#[cfg_attr(rustfmt, rustfmt_skip)]
79pub const UP_TRIANGLE: [u8; 8] = [
80 0b00000,
81 0b00000,
82 0b00100,
83 0b01110,
84 0b11111,
85 0b00000,
86 0b00000,
87 0b00000,
88];
89
90/// ▾
91pub const DOWN_TRIANGLE: [u8; 8] = mirror_y(UP_TRIANGLE);