Skip to content

Commit 28c8364

Browse files
committed
Support drawing greyscale
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent f278916 commit 28c8364

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/control.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub enum _CommandVals {
1010
_Animate = 0x04,
1111
_Panic = 0x05,
1212
_Draw = 0x06,
13+
_StageGreyCol = 0x07,
14+
_DrawGreyColBuffer = 0x08,
1315
}
1416

1517
pub enum PatternVals {
@@ -24,14 +26,24 @@ pub enum PatternVals {
2426
}
2527

2628
pub enum Command {
29+
/// Set brightness scaling
2730
Brightness(u8),
31+
/// Display pre-programmed pattern
2832
Pattern(PatternVals),
33+
/// Reset into bootloader
2934
BootloaderReset,
35+
/// Light up a percentage of the screen
3036
Percentage(u8),
37+
/// Go to sleepe or wake up
3138
Sleep(bool),
39+
/// Start/stop animation (vertical scrolling)
3240
Animate(bool),
41+
/// Panic. Just to test what happens
3342
Panic,
34-
Draw([u8; 39]),
43+
/// Draw black/white on the grid
44+
Draw([u8; DRAW_BYTES]),
45+
StageGreyCol(u8, [u8; HEIGHT]),
46+
DrawGreyColBuffer,
3547
_Unknown,
3648
}
3749

@@ -68,14 +80,24 @@ pub fn parse_command(count: usize, buf: &[u8]) -> Option<Command> {
6880
0x04 => Some(Command::Animate(arg == 1)),
6981
0x05 => Some(Command::Panic),
7082
0x06 => {
71-
if count >= 3 + 39 {
72-
let mut bytes = [0; 39];
73-
bytes.clone_from_slice(&buf[3..3 + 39]);
83+
if count >= 3 + DRAW_BYTES {
84+
let mut bytes = [0; DRAW_BYTES];
85+
bytes.clone_from_slice(&buf[3..3 + DRAW_BYTES]);
7486
Some(Command::Draw(bytes))
7587
} else {
7688
None
7789
}
7890
}
91+
0x07 => {
92+
if count >= 3 + 1 + HEIGHT {
93+
let mut bytes = [0; HEIGHT];
94+
bytes.clone_from_slice(&buf[4..4 + HEIGHT]);
95+
Some(Command::StageGreyCol(buf[3], bytes))
96+
} else {
97+
None
98+
}
99+
}
100+
0x08 => Some(Command::DrawGreyColBuffer),
79101
_ => None, //Some(Command::Unknown),
80102
}
81103
} else {
@@ -125,6 +147,15 @@ pub fn handle_command(command: &Command, state: &mut State, matrix: &mut Foo) {
125147
Command::Animate(a) => state.animate = *a,
126148
Command::Panic => panic!("Ahhh"),
127149
Command::Draw(vals) => state.grid = draw(vals),
150+
Command::StageGreyCol(col, vals) => {
151+
draw_grey_col(&mut state.col_buffer, *col, vals);
152+
}
153+
Command::DrawGreyColBuffer => {
154+
// Copy the staging buffer to the real grid and display it
155+
state.grid = state.col_buffer.clone();
156+
// Zero the old staging buffer, just for good measure.
157+
state.col_buffer = percentage(0);
158+
}
128159
_ => {}
129160
}
130161
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ enum SleepState {
147147

148148
pub struct State {
149149
grid: Grid,
150+
col_buffer: Grid,
150151
animate: bool,
151152
brightness: u8,
152153
sleeping: SleepState,
@@ -225,6 +226,7 @@ fn main() -> ! {
225226

226227
let mut state = State {
227228
grid: percentage(100),
229+
col_buffer: Grid::default(),
228230
animate: false,
229231
brightness: 120,
230232
sleeping: SleepState::Awake,

src/patterns.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use crate::{lotus::LotusLedMatrix, Grid};
1010
pub const WIDTH: usize = 9;
1111
pub const HEIGHT: usize = 34;
1212

13+
/// Bytes needed to represent all LEDs with a single bit
14+
/// math.ceil(WIDTH * HEIGHT / 8)
15+
pub const DRAW_BYTES: usize = 39;
16+
1317
pub type Foo = LotusLedMatrix<
1418
bsp::hal::I2C<
1519
I2C1,
@@ -20,7 +24,7 @@ pub type Foo = LotusLedMatrix<
2024
>,
2125
>;
2226

23-
pub fn draw(bytes: &[u8; 39]) -> Grid {
27+
pub fn draw(bytes: &[u8; DRAW_BYTES]) -> Grid {
2428
let mut grid = Grid::default();
2529

2630
for y in 0..HEIGHT {
@@ -39,6 +43,13 @@ pub fn draw(bytes: &[u8; 39]) -> Grid {
3943

4044
grid
4145
}
46+
47+
pub fn draw_grey_col(grid: &mut Grid, col: u8, levels: &[u8; HEIGHT]) {
48+
for y in 0..HEIGHT {
49+
grid.0[8 - col as usize][y as usize] = levels[y];
50+
}
51+
}
52+
4253
pub fn display_sleep() -> Grid {
4354
Grid([
4455
[

0 commit comments

Comments
 (0)