Skip to content

Commit 918c9a5

Browse files
authored
Merge pull request #36 from hnez/startup-errors
tacd: show message on screen if hardware setup failed
2 parents f2e8277 + 6bce630 commit 918c9a5

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

src/main.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

1818
use anyhow::Result;
19+
use async_std::future::pending;
1920
use futures::{select, FutureExt};
21+
use log::{error, info};
2022

2123
mod adc;
2224
mod backlight;
@@ -50,17 +52,11 @@ use regulators::Regulators;
5052
use setup_mode::SetupMode;
5153
use system::System;
5254
use temperatures::Temperatures;
53-
use ui::{setup_display, Ui, UiResources};
55+
use ui::{message, setup_display, Display, Ui, UiResources};
5456
use usb_hub::UsbHub;
5557
use watchdog::Watchdog;
5658

57-
#[async_std::main]
58-
async fn main() -> Result<()> {
59-
env_logger::init();
60-
61-
// Show a splash screen very early on
62-
let display = setup_display();
63-
59+
async fn init() -> Result<(Ui, HttpServer, Option<Watchdog>)> {
6460
// The BrokerBuilder collects topics that should be exported via the
6561
// MQTT/REST APIs.
6662
// The topics are also used to pass around data inside the tacd.
@@ -122,9 +118,6 @@ async fn main() -> Result<()> {
122118
// in the web interface.
123119
journal::serve(&mut http_server.server);
124120

125-
// Expose the display as a .png on the web server
126-
ui::serve_display(&mut http_server.server, display.screenshooter());
127-
128121
// Set up the user interface for the hardware display on the TAC.
129122
// The different screens receive updates via the topics provided in
130123
// the UiResources struct.
@@ -153,7 +146,19 @@ async fn main() -> Result<()> {
153146
// and expose the topics via HTTP and MQTT-over-websocket.
154147
bb.build(&mut http_server.server);
155148

156-
log::info!("Setup complete. Handling requests");
149+
Ok((ui, http_server, watchdog))
150+
}
151+
152+
async fn run(
153+
ui: Ui,
154+
mut http_server: HttpServer,
155+
watchdog: Option<Watchdog>,
156+
display: Display,
157+
) -> Result<()> {
158+
// Expose the display as a .png on the web server
159+
ui::serve_display(&mut http_server.server, display.screenshooter());
160+
161+
info!("Setup complete. Handling requests");
157162

158163
// Run until the user interface, http server or (if selected) the watchdog
159164
// exits (with an error).
@@ -172,3 +177,32 @@ async fn main() -> Result<()> {
172177

173178
Ok(())
174179
}
180+
181+
#[async_std::main]
182+
async fn main() -> Result<()> {
183+
env_logger::init();
184+
185+
// Show a splash screen very early on
186+
let display = setup_display();
187+
188+
match init().await {
189+
Ok((ui, http_server, watchdog)) => run(ui, http_server, watchdog, display).await,
190+
Err(e) => {
191+
// Display a detailed error message on stderr (and thus in the journal) ...
192+
error!("Failed to initialize tacd: {e}");
193+
194+
// ... and a generic message on the LCD, as it can not fit a lot of detail.
195+
display.clear();
196+
display.with_lock(|target| {
197+
message(
198+
target,
199+
"tacd failed to start!\n\nCheck log for info.\nWaiting for watchdog\nto restart tacd.",
200+
);
201+
});
202+
203+
// Wait forever (or more likely until the systemd watchdog timer hits)
204+
// to give the user a chance to actually see the error message.
205+
pending().await
206+
}
207+
}
208+
}

src/ui.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod widgets;
3535
use alerts::{AlertList, Alerter};
3636
use buttons::{handle_buttons, Button, ButtonEvent, Direction, PressDuration, Source};
3737
pub use display::{Display, ScreenShooter};
38+
pub use screens::message;
3839
use screens::{splash, ActivatableScreen, AlertScreen, NormalScreen, Screen};
3940

4041
pub struct UiResources {

src/ui/screens.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,24 @@ const fn row_anchor(row_num: u8) -> Point {
166166
Point::new(8, 52 + (row_num as i32) * 20)
167167
}
168168

169-
pub(super) fn splash(target: &mut DisplayExclusive) -> Rectangle {
169+
pub fn message(target: &mut DisplayExclusive, text: &str) -> Rectangle {
170170
let ui_text_style: MonoTextStyle<BinaryColor> =
171171
MonoTextStyle::new(&UI_TEXT_FONT, BinaryColor::On);
172172

173-
let text = Text::with_alignment(
174-
"Welcome",
175-
Point::new(120, 120),
176-
ui_text_style,
177-
Alignment::Center,
178-
);
173+
let mut text = Text::with_alignment(text, Point::zero(), ui_text_style, Alignment::Center);
174+
175+
let offset = Point::new(120, 120) - text.bounding_box().center();
176+
text.translate_mut(offset);
179177

180178
text.draw(target).unwrap();
181179

182180
text.bounding_box()
183181
}
184182

183+
pub fn splash(target: &mut DisplayExclusive) -> Rectangle {
184+
message(target, "Welcome")
185+
}
186+
185187
pub(super) fn init(
186188
res: &UiResources,
187189
alerts: &Arc<Topic<AlertList>>,

0 commit comments

Comments
 (0)