|
| 1 | +// This file is part of tacd, the LXA TAC system daemon |
| 2 | +// Copyright (C) 2023 Pengutronix e.K. |
| 3 | +// |
| 4 | +// This program is free software; you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation; either version 2 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License along |
| 15 | +// with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + |
| 18 | +use async_std::prelude::*; |
| 19 | +use async_std::sync::Arc; |
| 20 | +use async_std::task::spawn; |
| 21 | +use async_trait::async_trait; |
| 22 | +use embedded_graphics::{ |
| 23 | + mono_font::MonoTextStyle, pixelcolor::BinaryColor, prelude::*, text::Text, |
| 24 | +}; |
| 25 | + |
| 26 | +use super::widgets::*; |
| 27 | +use super::{ |
| 28 | + row_anchor, ActivatableScreen, ActiveScreen, AlertList, AlertScreen, Alerter, Display, |
| 29 | + InputEvent, Screen, Ui, |
| 30 | +}; |
| 31 | +use crate::broker::Topic; |
| 32 | +use crate::measurement::Measurement; |
| 33 | +use crate::temperatures::Warning; |
| 34 | + |
| 35 | +const SCREEN_TYPE: AlertScreen = AlertScreen::OverTemperature; |
| 36 | + |
| 37 | +pub struct OverTemperatureScreen; |
| 38 | + |
| 39 | +struct Active { |
| 40 | + widgets: WidgetContainer, |
| 41 | +} |
| 42 | + |
| 43 | +impl OverTemperatureScreen { |
| 44 | + pub fn new(alerts: &Arc<Topic<AlertList>>, warning: &Arc<Topic<Warning>>) -> Self { |
| 45 | + let (mut warning_events, _) = warning.clone().subscribe_unbounded(); |
| 46 | + let alerts = alerts.clone(); |
| 47 | + |
| 48 | + spawn(async move { |
| 49 | + while let Some(warning) = warning_events.next().await { |
| 50 | + match warning { |
| 51 | + Warning::Okay => alerts.deassert(SCREEN_TYPE), |
| 52 | + Warning::SocHigh | Warning::SocCritical => alerts.assert(SCREEN_TYPE), |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + Self |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl ActivatableScreen for OverTemperatureScreen { |
| 62 | + fn my_type(&self) -> Screen { |
| 63 | + Screen::Alert(SCREEN_TYPE) |
| 64 | + } |
| 65 | + |
| 66 | + fn activate(&mut self, ui: &Ui, display: Display) -> Box<dyn ActiveScreen> { |
| 67 | + let ui_text_style: MonoTextStyle<BinaryColor> = |
| 68 | + MonoTextStyle::new(&UI_TEXT_FONT, BinaryColor::On); |
| 69 | + |
| 70 | + display.with_lock(|target| { |
| 71 | + Text::new("Temperature alert!", row_anchor(0), ui_text_style) |
| 72 | + .draw(target) |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + Text::new( |
| 76 | + "TAC is overheating.\nProvide more airflow\nand check loads.", |
| 77 | + row_anchor(2), |
| 78 | + ui_text_style, |
| 79 | + ) |
| 80 | + .draw(target) |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + Text::new("SoC Temperature:", row_anchor(6), ui_text_style) |
| 84 | + .draw(target) |
| 85 | + .unwrap(); |
| 86 | + }); |
| 87 | + |
| 88 | + let mut widgets = WidgetContainer::new(display); |
| 89 | + |
| 90 | + widgets.push(|display| { |
| 91 | + DynamicWidget::text_center( |
| 92 | + ui.res.temperatures.soc_temperature.clone(), |
| 93 | + display, |
| 94 | + Point::new(120, 210), |
| 95 | + Box::new(|meas: &Measurement| format!("{:-4.0} C", meas.value)), |
| 96 | + ) |
| 97 | + }); |
| 98 | + |
| 99 | + Box::new(Active { widgets }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[async_trait] |
| 104 | +impl ActiveScreen for Active { |
| 105 | + fn my_type(&self) -> Screen { |
| 106 | + Screen::Alert(SCREEN_TYPE) |
| 107 | + } |
| 108 | + |
| 109 | + async fn deactivate(mut self: Box<Self>) -> Display { |
| 110 | + self.widgets.destroy().await |
| 111 | + } |
| 112 | + |
| 113 | + fn input(&mut self, _ev: InputEvent) {} |
| 114 | +} |
0 commit comments