Skip to content

Commit 22cb785

Browse files
committed
refactor: use draw_err helper to reduce map_err boilerplate
1 parent 1ab0126 commit 22cb785

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

src/display.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::config::DisplayConfig as AppDisplayConfig;
22
use crate::display_types::{Display, FONT_5X8, FONT_6X12, PCSENIOR8, PCSENIOR8_STYLE, PROFONT12};
3+
use display_interface::DisplayError;
34
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*, text::Text};
45
use linux_embedded_hal::I2cdev;
56
use log::{debug, info, warn};
@@ -29,25 +30,19 @@ impl PoeDisplay {
2930
}
3031

3132
pub fn set_brightness(&mut self, brightness: Brightness) -> Result<(), Box<dyn std::error::Error>> {
32-
self.display
33-
.set_brightness(brightness)
34-
.map_err(|e| format!("Set brightness error: {:?}", e))?;
33+
self.display.set_brightness(brightness).map_err(draw_err)?;
3534
Ok(())
3635
}
3736

3837
pub fn display_off(&mut self) -> Result<(), Box<dyn std::error::Error>> {
3938
debug!("Turning display OFF.");
40-
self.display
41-
.set_display_on(false)
42-
.map_err(|e| format!("Display off error: {:?}", e))?;
39+
self.display.set_display_on(false).map_err(draw_err)?;
4340
Ok(())
4441
}
4542

4643
pub fn display_on(&mut self) -> Result<(), Box<dyn std::error::Error>> {
4744
debug!("Turning display ON.");
48-
self.display
49-
.set_display_on(true)
50-
.map_err(|e| format!("Display on error: {:?}", e))?;
45+
self.display.set_display_on(true).map_err(draw_err)?;
5146
Ok(())
5247
}
5348

@@ -62,66 +57,58 @@ impl PoeDisplay {
6257
) -> Result<(), Box<dyn std::error::Error>> {
6358
let disp = &mut self.display;
6459

65-
disp.clear(BinaryColor::Off)
66-
.map_err(|e| format!("Display clear error: {:?}", e))?;
60+
disp.clear(BinaryColor::Off).map_err(draw_err)?;
6761

6862
let ip_width = ip_address.len() as i32 * VALUE_CHAR_WIDTH;
6963
let ip_x_position = (DISPLAY_WIDTH as i32 - ip_width) / 2;
7064
let ip_pos = Point::new(ip_x_position, IP_ROW_Y) + offset;
7165
Text::new(ip_address, ip_pos, PCSENIOR8_STYLE)
7266
.draw(disp)
73-
.map_err(|e| format!("Draw IP error: {:?}", e))?;
67+
.map_err(draw_err)?;
7468

7569
let cpu_width = cpu_usage.len() as i32 * VALUE_CHAR_WIDTH;
7670
let cpu_pos = Point::new(LEFT_COL_RIGHT - cpu_width, STATS_ROW1_Y) + offset;
7771
let next = Text::new(cpu_usage, cpu_pos, PCSENIOR8_STYLE)
7872
.draw(disp)
79-
.map_err(|e| format!("Draw CPU error: {:?}", e))?;
80-
let next = Text::new("%", next, FONT_6X12)
81-
.draw(disp)
82-
.map_err(|e| format!("Draw CPU % error: {:?}", e))?;
73+
.map_err(draw_err)?;
74+
let next = Text::new("%", next, FONT_6X12).draw(disp).map_err(draw_err)?;
8375
Text::new("CPU", next + X_MARGIN, FONT_5X8)
8476
.draw(disp)
85-
.map_err(|e| format!("Draw CPU label error: {:?}", e))?;
77+
.map_err(draw_err)?;
8678

8779
let ram_width = ram_usage.len() as i32 * VALUE_CHAR_WIDTH;
8880
let ram_pos = Point::new(LEFT_COL_RIGHT - ram_width, STATS_ROW2_Y) + offset;
8981
let next = Text::new(ram_usage, ram_pos, PCSENIOR8_STYLE)
9082
.draw(disp)
91-
.map_err(|e| format!("Draw RAM error: {:?}", e))?;
92-
let next = Text::new("%", next, FONT_6X12)
93-
.draw(disp)
94-
.map_err(|e| format!("Draw RAM % error: {:?}", e))?;
83+
.map_err(draw_err)?;
84+
let next = Text::new("%", next, FONT_6X12).draw(disp).map_err(draw_err)?;
9585
Text::new("RAM", next + X_MARGIN, FONT_5X8)
9686
.draw(disp)
97-
.map_err(|e| format!("Draw RAM label error: {:?}", e))?;
87+
.map_err(draw_err)?;
9888

9989
let temp_width = temp.len() as i32 * VALUE_CHAR_WIDTH;
10090
let temp_pos = Point::new(RIGHT_COL_RIGHT - temp_width, STATS_ROW1_Y) + offset;
10191
let next = Text::new(temp, temp_pos, PCSENIOR8_STYLE)
10292
.draw(disp)
103-
.map_err(|e| format!("Draw temp error: {:?}", e))?;
93+
.map_err(draw_err)?;
10494
let next = Text::new("°", next + Point::new(0, 3), PROFONT12)
10595
.draw(disp)
106-
.map_err(|e| format!("Draw degree symbol error: {:?}", e))?;
96+
.map_err(draw_err)?;
10797
Text::new("C", next - Point::new(0, 2), PCSENIOR8_STYLE)
10898
.draw(disp)
109-
.map_err(|e| format!("Draw C error: {:?}", e))?;
99+
.map_err(draw_err)?;
110100

111101
let disk_width = disk_usage.len() as i32 * VALUE_CHAR_WIDTH;
112102
let disk_pos = Point::new(RIGHT_COL_RIGHT - disk_width, STATS_ROW2_Y) + offset;
113103
let next = Text::new(disk_usage, disk_pos, PCSENIOR8_STYLE)
114104
.draw(disp)
115-
.map_err(|e| format!("Draw disk error: {:?}", e))?;
116-
let next = Text::new("%", next, FONT_6X12)
117-
.draw(disp)
118-
.map_err(|e| format!("Draw disk % error: {:?}", e))?;
105+
.map_err(draw_err)?;
106+
let next = Text::new("%", next, FONT_6X12).draw(disp).map_err(draw_err)?;
119107
Text::new("DISK", next + X_MARGIN, FONT_5X8)
120108
.draw(disp)
121-
.map_err(|e| format!("Draw DISK label error: {:?}", e))?;
109+
.map_err(draw_err)?;
122110

123-
disp.flush()
124-
.map_err(|e| format!("Display flush error: {:?}", e))?;
111+
disp.flush().map_err(draw_err)?;
125112
Ok(())
126113
}
127114
}
@@ -143,6 +130,10 @@ fn map_brightness_value(value: u8) -> Brightness {
143130
}
144131
}
145132

133+
fn draw_err(e: DisplayError) -> Box<dyn std::error::Error> {
134+
format!("Display draw error: {:?}", e).into()
135+
}
136+
146137
fn initialize_display(
147138
i2c: I2cdev,
148139
display_config: &AppDisplayConfig,

0 commit comments

Comments
 (0)