ht32-panel-daemon 0.8.0

Daemon with web UI for HT32 panel control
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Digits face inspired by Casio digital watches.
//!
//! Features a retro LCD aesthetic with large time display and
//! segmented areas for system metrics.

use super::{
    complication_names, complication_options, complications, date_formats, draw_mini_analog_clock,
    time_formats, Complication, EnabledComplications, Face, Theme,
};
use crate::rendering::Canvas;
use crate::sensors::data::SystemData;

/// Dim a color by mixing it toward the background.
fn dim_color(color: u32, background: u32, factor: f32) -> u32 {
    let r1 = ((color >> 16) & 0xFF) as f32;
    let g1 = ((color >> 8) & 0xFF) as f32;
    let b1 = (color & 0xFF) as f32;
    let r2 = ((background >> 16) & 0xFF) as f32;
    let g2 = ((background >> 8) & 0xFF) as f32;
    let b2 = (background & 0xFF) as f32;

    let r = (r1 * factor + r2 * (1.0 - factor)) as u32;
    let g = (g1 * factor + g2 * (1.0 - factor)) as u32;
    let b = (b1 * factor + b2 * (1.0 - factor)) as u32;

    (r << 16) | (g << 8) | b
}

/// Derive colors from theme for the digits face.
struct FaceColors {
    /// LCD segment "on" color
    segment_on: u32,
    /// LCD segment "off" color (ghost segments)
    segment_off: u32,
    /// Label text color
    label: u32,
    /// Divider line color
    divider: u32,
}

impl FaceColors {
    fn from_theme(theme: &Theme) -> Self {
        Self {
            segment_on: theme.primary,
            segment_off: dim_color(theme.primary, theme.background, 0.2),
            label: dim_color(theme.text, theme.background, 0.7), // Higher for better contrast
            divider: dim_color(theme.primary, theme.background, 0.35),
        }
    }
}

/// Font sizes.
const FONT_TIME: f32 = 32.0;
const FONT_LARGE: f32 = 20.0;
const FONT_MEDIUM: f32 = 14.0;
const FONT_SMALL: f32 = 11.0;

/// A Casio-inspired digital watch face.
pub struct DigitsFace;

impl DigitsFace {
    /// Creates a new digits face.
    pub fn new() -> Self {
        Self
    }

    /// Draws a horizontal divider line.
    fn draw_divider(canvas: &mut Canvas, y: i32, width: u32, margin: i32, color: u32) {
        canvas.fill_rect(margin, y, width - (margin * 2) as u32, 1, color);
    }

    /// Draws a labeled value in the segmented LCD style.
    fn draw_segment_value(
        canvas: &mut Canvas,
        x: i32,
        y: i32,
        label: &str,
        value: &str,
        label_color: u32,
        value_color: u32,
    ) {
        canvas.draw_text(x, y, label, FONT_SMALL, label_color);
        canvas.draw_text(x, y + 10, value, FONT_LARGE, value_color);
    }

    /// Draws a labeled value with medium fonts for landscape CPU/RAM row.
    fn draw_segment_value_medium(
        canvas: &mut Canvas,
        x: i32,
        y: i32,
        label: &str,
        value: &str,
        label_color: u32,
        value_color: u32,
    ) {
        canvas.draw_text(x, y, label, FONT_SMALL, label_color);
        canvas.draw_text(x, y + 12, value, 26.0, value_color); // Between FONT_LARGE (20) and FONT_TIME (32)
    }
}

impl Default for DigitsFace {
    fn default() -> Self {
        Self::new()
    }
}

impl Face for DigitsFace {
    fn name(&self) -> &str {
        "digits"
    }

    fn available_complications(&self) -> Vec<Complication> {
        vec![
            complications::time(true),
            complications::date(true, date_formats::ISO),
            complications::ip_address(true),
            complications::network(true),
            complications::disk_io(true),
            complications::cpu_temp(true),
        ]
    }

    fn render(
        &self,
        canvas: &mut Canvas,
        data: &SystemData,
        theme: &Theme,
        comp: &EnabledComplications,
    ) {
        let colors = FaceColors::from_theme(theme);
        let (width, _height) = canvas.dimensions();
        let portrait = width < 200;
        let margin = 6;
        let mut y = margin;

        let is_on = |id: &str| comp.is_enabled(self.name(), id, true);

        // Get time format option
        let time_format = comp
            .get_option(
                self.name(),
                complication_names::TIME,
                complication_options::TIME_FORMAT,
            )
            .map(|s| s.as_str())
            .unwrap_or(time_formats::DIGITAL_24H);

        // Get date format option
        let date_format = comp
            .get_option(
                self.name(),
                complication_names::DATE,
                complication_options::DATE_FORMAT,
            )
            .map(|s| s.as_str())
            .unwrap_or(date_formats::ISO);

        if portrait {
            // Portrait layout
            let col_width = (width as i32 - margin * 3) / 2;

            // Hostname at top (always shown)
            let host_width = canvas.text_width(&data.hostname, FONT_MEDIUM);
            let host_x = (width as i32 - host_width) / 2;
            canvas.draw_text(host_x, y, &data.hostname, FONT_MEDIUM, colors.label);
            y += canvas.line_height(FONT_MEDIUM) + 2;

            // Complication: Time
            if is_on(complication_names::TIME) {
                if time_format == time_formats::ANALOGUE {
                    // Draw small analog clock
                    let clock_radius = 18_u32;
                    let clock_cx = width as i32 / 2;
                    let clock_cy = y + clock_radius as i32 + 2;
                    draw_mini_analog_clock(
                        canvas,
                        clock_cx,
                        clock_cy,
                        clock_radius,
                        data.hour,
                        data.minute,
                        colors.segment_on,
                        colors.segment_on,
                    );
                    y += (clock_radius * 2) as i32 + 6;
                } else {
                    let time_str = data.format_time(time_format);
                    let time_width = canvas.text_width(&time_str, FONT_TIME);
                    let time_x = (width as i32 - time_width) / 2;
                    canvas.draw_text(time_x, y, &time_str, FONT_TIME, colors.segment_on);
                    y += canvas.line_height(FONT_TIME) + 2;
                }
            }

            // Complication: Date (centered, if not hidden)
            if is_on(complication_names::DATE) {
                if let Some(date_str) = data.format_date(date_format) {
                    let date_width = canvas.text_width(&date_str, FONT_MEDIUM);
                    let date_x = (width as i32 - date_width) / 2;
                    canvas.draw_text(date_x, y, &date_str, FONT_MEDIUM, colors.label);
                    y += canvas.line_height(FONT_MEDIUM) + 4;
                }
            }

            // CPU on its own line with bigger number
            Self::draw_divider(canvas, y, width, margin, colors.divider);
            y += 6;
            canvas.draw_text(margin, y, "CPU", FONT_SMALL, colors.label);
            let cpu_val = format!("{:.0}%", data.cpu_percent);
            let cpu_val_w = canvas.text_width(&cpu_val, FONT_TIME);
            canvas.draw_text(
                width as i32 - margin - cpu_val_w,
                y - 4,
                &cpu_val,
                FONT_TIME,
                colors.segment_on,
            );
            y += canvas.line_height(FONT_TIME);

            // RAM on its own line with bigger number
            Self::draw_divider(canvas, y, width, margin, colors.divider);
            y += 6;
            canvas.draw_text(margin, y, "RAM", FONT_SMALL, colors.label);
            let ram_val = format!("{:.0}%", data.ram_percent);
            let ram_val_w = canvas.text_width(&ram_val, FONT_TIME);
            canvas.draw_text(
                width as i32 - margin - ram_val_w,
                y - 4,
                &ram_val,
                FONT_TIME,
                colors.segment_on,
            );
            y += canvas.line_height(FONT_TIME);

            // Complication: Disk I/O
            if is_on(complication_names::DISK_IO) {
                Self::draw_divider(canvas, y, width, margin, colors.divider);
                y += 6;
                let disk_r = SystemData::format_rate_compact(data.disk_read_rate);
                let disk_w = SystemData::format_rate_compact(data.disk_write_rate);
                Self::draw_segment_value(
                    canvas,
                    margin,
                    y,
                    "DSK R",
                    &disk_r,
                    colors.label,
                    colors.segment_on,
                );
                Self::draw_segment_value(
                    canvas,
                    margin + col_width + margin,
                    y,
                    "DSK W",
                    &disk_w,
                    colors.label,
                    colors.segment_on,
                );
                y += 32;
            }

            // Complication: Network
            if is_on(complication_names::NETWORK) {
                Self::draw_divider(canvas, y, width, margin, colors.divider);
                y += 6;
                let net_rx = SystemData::format_rate_compact(data.net_rx_rate);
                let net_tx = SystemData::format_rate_compact(data.net_tx_rate);
                Self::draw_segment_value(
                    canvas,
                    margin,
                    y,
                    "NET \u{2193}",
                    &net_rx,
                    colors.label,
                    colors.segment_on,
                );
                Self::draw_segment_value(
                    canvas,
                    margin + col_width + margin,
                    y,
                    "NET \u{2191}",
                    &net_tx,
                    colors.label,
                    colors.segment_on,
                );
                y += 32;
            }

            // Uptime and IP at bottom (where hostname used to be)
            Self::draw_divider(canvas, y, width, margin, colors.divider);
            y += 6;

            // Uptime (always shown)
            canvas.draw_text(
                margin,
                y,
                &format!("UP {}", data.uptime),
                FONT_SMALL,
                colors.label,
            );
            y += canvas.line_height(FONT_SMALL) + 6;

            // Complication: IP address
            if is_on(complication_names::IP_ADDRESS) {
                if let Some(ref ip) = data.display_ip {
                    // IP label
                    canvas.draw_text(margin, y, "IP:", FONT_SMALL, colors.label);
                    y += canvas.line_height(FONT_SMALL);
                    // IP address on next line, smaller font to fit
                    canvas.draw_text(margin, y, ip, FONT_SMALL, colors.label);
                }
            }
        } else {
            // Landscape layout - larger metrics to fill space
            let col_width = (width as i32 - margin * 5) / 4;

            // Row 1: Hostname on left, Time on right
            canvas.draw_text(margin, y, &data.hostname, FONT_MEDIUM, colors.label);
            if is_on(complication_names::TIME) {
                if time_format == time_formats::ANALOGUE {
                    // Draw small analog clock on the right
                    let clock_radius = 12_u32;
                    let clock_cx = width as i32 - margin - clock_radius as i32;
                    let clock_cy = y + clock_radius as i32;
                    draw_mini_analog_clock(
                        canvas,
                        clock_cx,
                        clock_cy,
                        clock_radius,
                        data.hour,
                        data.minute,
                        colors.segment_on,
                        colors.segment_on,
                    );
                } else {
                    let time_str = data.format_time(time_format);
                    let time_width = canvas.text_width(&time_str, FONT_LARGE);
                    canvas.draw_text(
                        width as i32 - margin - time_width,
                        y,
                        &time_str,
                        FONT_LARGE,
                        colors.segment_on,
                    );
                }
            }
            y += canvas.line_height(FONT_LARGE);

            // Row 2: Uptime on left, Date on right (below time)
            let uptime_text = format!("Up: {}", data.uptime);
            canvas.draw_text(margin, y, &uptime_text, FONT_SMALL, colors.label);
            if is_on(complication_names::DATE) {
                if let Some(date_str) = data.format_date(date_format) {
                    let date_width = canvas.text_width(&date_str, FONT_SMALL);
                    canvas.draw_text(
                        width as i32 - margin - date_width,
                        y,
                        &date_str,
                        FONT_SMALL,
                        colors.label,
                    );
                }
            }
            y += canvas.line_height(FONT_SMALL) + 2;

            // Row 3: IP address on left with label
            if is_on(complication_names::IP_ADDRESS) {
                if let Some(ref ip) = data.display_ip {
                    let ip_text = format!("IP: {}", ip);
                    canvas.draw_text(margin, y, &ip_text, FONT_SMALL, colors.label);
                }
            }
            y += canvas.line_height(FONT_SMALL) + 4;

            Self::draw_divider(canvas, y, width, margin, colors.divider);
            y += 6;

            // Row 1: CPU (base), RAM (base), Temp (complication) - medium text
            Self::draw_segment_value_medium(
                canvas,
                margin,
                y,
                "CPU",
                &format!("{:.0}%", data.cpu_percent),
                colors.label,
                colors.segment_on,
            );
            Self::draw_segment_value_medium(
                canvas,
                margin + col_width + margin,
                y,
                "RAM",
                &format!("{:.0}%", data.ram_percent),
                colors.label,
                colors.segment_on,
            );
            // Complication: CPU temperature
            if is_on(complication_names::CPU_TEMP) {
                if let Some(temp) = data.cpu_temp {
                    Self::draw_segment_value_medium(
                        canvas,
                        margin + (col_width + margin) * 2,
                        y,
                        "TEMP",
                        &format!("{:.0}°", temp),
                        colors.label,
                        colors.segment_on,
                    );
                }
            }
            y += 42;

            Self::draw_divider(canvas, y, width, margin, colors.divider);
            y += 6;

            // Row 2: Disk R, Disk W (complication), Net Down, Net Up (complication) - smaller text
            if is_on(complication_names::DISK_IO) {
                let disk_r = SystemData::format_rate_compact(data.disk_read_rate);
                let disk_w = SystemData::format_rate_compact(data.disk_write_rate);
                Self::draw_segment_value(
                    canvas,
                    margin,
                    y,
                    "DSK R",
                    &disk_r,
                    colors.label,
                    colors.segment_on,
                );
                Self::draw_segment_value(
                    canvas,
                    margin + col_width + margin,
                    y,
                    "DSK W",
                    &disk_w,
                    colors.label,
                    colors.segment_on,
                );
            }
            if is_on(complication_names::NETWORK) {
                let net_rx = SystemData::format_rate_compact(data.net_rx_rate);
                let net_tx = SystemData::format_rate_compact(data.net_tx_rate);
                Self::draw_segment_value(
                    canvas,
                    margin + (col_width + margin) * 2,
                    y,
                    "NET \u{2193}",
                    &net_rx,
                    colors.label,
                    colors.segment_on,
                );
                Self::draw_segment_value(
                    canvas,
                    margin + (col_width + margin) * 3,
                    y,
                    "NET \u{2191}",
                    &net_tx,
                    colors.label,
                    colors.segment_on,
                );
            }
        }
        let _ = y;
    }
}