|
| 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 | +use serde::{Deserialize, Serialize}; |
| 26 | + |
| 27 | +use super::widgets::*; |
| 28 | +use super::{ |
| 29 | + row_anchor, ActivatableScreen, ActiveScreen, AlertList, AlertScreen, Alerter, Display, |
| 30 | + InputEvent, Screen, Ui, |
| 31 | +}; |
| 32 | +use crate::broker::Topic; |
| 33 | +use crate::dut_power::{OutputRequest, OutputState}; |
| 34 | + |
| 35 | +const SCREEN_TYPE: AlertScreen = AlertScreen::PowerFail; |
| 36 | + |
| 37 | +pub struct PowerFailScreen; |
| 38 | + |
| 39 | +#[derive(Serialize, Deserialize, Clone)] |
| 40 | +enum Highlight { |
| 41 | + TurnOn, |
| 42 | + KeepOff, |
| 43 | +} |
| 44 | + |
| 45 | +impl Highlight { |
| 46 | + fn next(&self) -> Self { |
| 47 | + match self { |
| 48 | + Self::TurnOn => Self::KeepOff, |
| 49 | + Self::KeepOff => Self::TurnOn, |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +struct Active { |
| 55 | + widgets: WidgetContainer, |
| 56 | + highlight: Arc<Topic<Highlight>>, |
| 57 | + request: Arc<Topic<OutputRequest>>, |
| 58 | +} |
| 59 | + |
| 60 | +impl PowerFailScreen { |
| 61 | + pub fn new(alerts: &Arc<Topic<AlertList>>, out_state: &Arc<Topic<OutputState>>) -> Self { |
| 62 | + let (mut out_state_events, _) = out_state.clone().subscribe_unbounded(); |
| 63 | + |
| 64 | + let alerts = alerts.clone(); |
| 65 | + |
| 66 | + spawn(async move { |
| 67 | + while let Some(state) = out_state_events.next().await { |
| 68 | + match state { |
| 69 | + OutputState::On | OutputState::Off | OutputState::OffFloating => { |
| 70 | + alerts.deassert(SCREEN_TYPE) |
| 71 | + } |
| 72 | + OutputState::InvertedPolarity |
| 73 | + | OutputState::OverCurrent |
| 74 | + | OutputState::OverVoltage |
| 75 | + | OutputState::RealtimeViolation => alerts.assert(SCREEN_TYPE), |
| 76 | + OutputState::Changing => {} |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + Self |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl ActivatableScreen for PowerFailScreen { |
| 86 | + fn my_type(&self) -> Screen { |
| 87 | + Screen::Alert(SCREEN_TYPE) |
| 88 | + } |
| 89 | + |
| 90 | + fn activate(&mut self, ui: &Ui, display: Display) -> Box<dyn ActiveScreen> { |
| 91 | + let ui_text_style: MonoTextStyle<BinaryColor> = |
| 92 | + MonoTextStyle::new(&UI_TEXT_FONT, BinaryColor::On); |
| 93 | + |
| 94 | + display.with_lock(|target| { |
| 95 | + Text::new( |
| 96 | + "DUT Power error", |
| 97 | + row_anchor(0) - (row_anchor(1) - row_anchor(0)), |
| 98 | + ui_text_style, |
| 99 | + ) |
| 100 | + .draw(target) |
| 101 | + .unwrap(); |
| 102 | + }); |
| 103 | + |
| 104 | + let mut widgets = WidgetContainer::new(display); |
| 105 | + |
| 106 | + widgets.push(|display| { |
| 107 | + DynamicWidget::text( |
| 108 | + ui.res.dut_pwr.state.clone(), |
| 109 | + display, |
| 110 | + row_anchor(2), |
| 111 | + Box::new(|state: &OutputState| { |
| 112 | + let msg = match state { |
| 113 | + OutputState::On | OutputState::Off | OutputState::OffFloating => { |
| 114 | + "The error was resolved" |
| 115 | + } |
| 116 | + OutputState::InvertedPolarity => { |
| 117 | + "Output disabled due to\ninverted polarity." |
| 118 | + } |
| 119 | + OutputState::OverCurrent => "DUT powered off due to\nan overcurrent event.", |
| 120 | + OutputState::OverVoltage => "DUT powered off due to\nan overvoltage event.", |
| 121 | + OutputState::RealtimeViolation => { |
| 122 | + "Output disabled due to\na realtime violation." |
| 123 | + } |
| 124 | + OutputState::Changing => "", |
| 125 | + }; |
| 126 | + |
| 127 | + msg.to_string() |
| 128 | + }), |
| 129 | + ) |
| 130 | + }); |
| 131 | + |
| 132 | + let highlight = Topic::anonymous(Some(Highlight::KeepOff)); |
| 133 | + |
| 134 | + widgets.push(|display| { |
| 135 | + DynamicWidget::text( |
| 136 | + highlight.clone(), |
| 137 | + display, |
| 138 | + row_anchor(6), |
| 139 | + Box::new(|highlight: &Highlight| { |
| 140 | + let msg = match highlight { |
| 141 | + Highlight::TurnOn => "> Turn output back on\n Keep output off", |
| 142 | + Highlight::KeepOff => " Turn output back on\n> Keep output off", |
| 143 | + }; |
| 144 | + |
| 145 | + msg.to_string() |
| 146 | + }), |
| 147 | + ) |
| 148 | + }); |
| 149 | + |
| 150 | + let request = ui.res.dut_pwr.request.clone(); |
| 151 | + |
| 152 | + Box::new(Active { |
| 153 | + widgets, |
| 154 | + highlight, |
| 155 | + request, |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +#[async_trait] |
| 161 | +impl ActiveScreen for Active { |
| 162 | + fn my_type(&self) -> Screen { |
| 163 | + Screen::Alert(SCREEN_TYPE) |
| 164 | + } |
| 165 | + |
| 166 | + async fn deactivate(mut self: Box<Self>) -> Display { |
| 167 | + self.widgets.destroy().await |
| 168 | + } |
| 169 | + |
| 170 | + fn input(&mut self, ev: InputEvent) { |
| 171 | + match ev { |
| 172 | + InputEvent::NextScreen => {} |
| 173 | + InputEvent::ToggleAction(_) => { |
| 174 | + self.highlight |
| 175 | + .modify(|highlight| highlight.map(|s| s.next())); |
| 176 | + } |
| 177 | + InputEvent::PerformAction(_) => match self.highlight.try_get() { |
| 178 | + Some(Highlight::TurnOn) => self.request.set(OutputRequest::On), |
| 179 | + Some(Highlight::KeepOff) => self.request.set(OutputRequest::Off), |
| 180 | + None => {} |
| 181 | + }, |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments