nesso 0.0.7

Rust SDK facade for Arduino Nesso N1 on ESP32-C6.
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Lightweight graphics and UI helpers for Nesso display applications.
//!
//! The helpers operate on any `embedded-graphics` draw target and do not own
//! application state. They are intended for small embedded screens where layout
//! and dirty-region rendering should stay predictable.

use embedded_graphics::{
    Drawable,
    mono_font::{MonoTextStyleBuilder, ascii::FONT_6X10},
    pixelcolor::Rgb565,
    prelude::*,
    primitives::{Circle, Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle},
    text::{Alignment, Text},
};

/// Insets applied around a rectangle.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Insets {
    /// Left inset in pixels.
    pub left: u32,
    /// Top inset in pixels.
    pub top: u32,
    /// Right inset in pixels.
    pub right: u32,
    /// Bottom inset in pixels.
    pub bottom: u32,
}

impl Insets {
    /// Creates symmetric horizontal and vertical insets.
    #[must_use]
    pub const fn symmetric(horizontal: u32, vertical: u32) -> Self {
        Self {
            left: horizontal,
            top: vertical,
            right: horizontal,
            bottom: vertical,
        }
    }

    /// Applies the insets to `area`.
    #[must_use]
    pub fn apply(self, area: Rectangle) -> Rectangle {
        let width = area
            .size
            .width
            .saturating_sub(self.left.saturating_add(self.right));
        let height = area
            .size
            .height
            .saturating_sub(self.top.saturating_add(self.bottom));
        Rectangle::new(
            area.top_left + Point::new(self.left as i32, self.top as i32),
            Size::new(width, height),
        )
    }
}

/// Horizontal text alignment.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TextAlign {
    /// Align text to the left edge.
    Left,
    /// Align text around the horizontal center.
    Center,
    /// Align text to the right edge.
    Right,
}

/// Text drawing style for compact embedded UI labels.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LabelStyle {
    /// Text color.
    pub color: Rgb565,
    /// Optional background color used to clear the label area first.
    pub background: Option<Rgb565>,
    /// Horizontal alignment.
    pub align: TextAlign,
}

/// Multi-line text drawing style.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TextBlockStyle {
    /// Text color.
    pub color: Rgb565,
    /// Optional background color used to clear the text block first.
    pub background: Option<Rgb565>,
    /// Space between text baselines in pixels.
    pub line_height: u32,
}

impl TextBlockStyle {
    /// Creates a text block style with the built-in mono font.
    #[must_use]
    pub const fn new(color: Rgb565) -> Self {
        Self {
            color,
            background: None,
            line_height: 12,
        }
    }

    /// Returns this style with a background clear color.
    #[must_use]
    pub const fn with_background(mut self, background: Rgb565) -> Self {
        self.background = Some(background);
        self
    }
}

impl LabelStyle {
    /// Creates a centered label style with no background clear.
    #[must_use]
    pub const fn centered(color: Rgb565) -> Self {
        Self {
            color,
            background: None,
            align: TextAlign::Center,
        }
    }

    /// Returns this style with a background clear color.
    #[must_use]
    pub const fn with_background(mut self, background: Rgb565) -> Self {
        self.background = Some(background);
        self
    }
}

/// Layout helper for a fixed-size screen.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ScreenLayout {
    bounds: Rectangle,
}

impl ScreenLayout {
    /// Creates a layout helper from a screen size.
    #[must_use]
    pub fn new(size: Size) -> Self {
        Self {
            bounds: Rectangle::new(Point::zero(), size),
        }
    }

    /// Returns the full screen bounds.
    #[must_use]
    pub const fn bounds(&self) -> Rectangle {
        self.bounds
    }

    /// Returns the content bounds after applying insets.
    #[must_use]
    pub fn content(&self, insets: Insets) -> Rectangle {
        insets.apply(self.bounds)
    }

    /// Returns a horizontal row inside the screen.
    #[must_use]
    pub fn row(&self, y: i32, height: u32, insets: Insets) -> Rectangle {
        let content = self.content(insets);
        Rectangle::new(
            Point::new(content.top_left.x, y),
            Size::new(content.size.width, height),
        )
    }
}

/// Trait for stateful screens that render into an embedded-graphics target.
pub trait View<D>
where
    D: DrawTarget<Color = Rgb565>,
{
    /// Draws the view into the provided target.
    fn render(&mut self, target: &mut D) -> Result<(), D::Error>;
}

/// Draws one text label inside `area`.
pub fn draw_label<D>(
    target: &mut D,
    area: Rectangle,
    text: &str,
    style: LabelStyle,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    if let Some(background) = style.background {
        area.into_styled(PrimitiveStyle::with_fill(background))
            .draw(target)?;
    }

    let (x, alignment) = match style.align {
        TextAlign::Left => (area.top_left.x, Alignment::Left),
        TextAlign::Center => (
            area.top_left.x + (area.size.width / 2) as i32,
            Alignment::Center,
        ),
        TextAlign::Right => (area.top_left.x + area.size.width as i32, Alignment::Right),
    };
    let y = area.top_left.y + (area.size.height / 2) as i32 + 4;
    let text_style = MonoTextStyleBuilder::new()
        .font(&FONT_6X10)
        .text_color(style.color)
        .build();

    Text::with_alignment(text, Point::new(x, y), text_style, alignment)
        .draw(target)
        .map(|_| ())
}

/// Draws a horizontal progress bar.
pub fn draw_progress_bar<D>(
    target: &mut D,
    area: Rectangle,
    value: u8,
    foreground: Rgb565,
    background: Rgb565,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    area.into_styled(PrimitiveStyle::with_fill(background))
        .draw(target)?;

    let clamped = value.min(100);
    let filled_width = area.size.width.saturating_mul(u32::from(clamped)) / 100;
    if filled_width == 0 {
        return Ok(());
    }

    Rectangle::new(area.top_left, Size::new(filled_width, area.size.height))
        .into_styled(PrimitiveStyle::with_fill(foreground))
        .draw(target)
}

/// Draws a filled pill shape inside `area`.
///
/// The helper clips naturally through the target. Very narrow areas fall back
/// to a filled rectangle.
pub fn draw_filled_pill<D>(target: &mut D, area: Rectangle, color: Rgb565) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    if area.size.width <= area.size.height {
        return area
            .into_styled(PrimitiveStyle::with_fill(color))
            .draw(target);
    }

    let radius = area.size.height / 2;
    let diameter = radius * 2;
    let style = PrimitiveStyle::with_fill(color);
    Circle::new(area.top_left, diameter)
        .into_styled(style)
        .draw(target)?;
    Circle::new(
        Point::new(
            area.top_left.x + area.size.width as i32 - diameter as i32,
            area.top_left.y,
        ),
        diameter,
    )
    .into_styled(style)
    .draw(target)?;
    Rectangle::new(
        Point::new(area.top_left.x + radius as i32, area.top_left.y),
        Size::new(area.size.width - diameter, area.size.height),
    )
    .into_styled(style)
    .draw(target)
}

/// Draws a filled rounded rectangle with radius derived from the area height.
///
/// This is equivalent to [`draw_filled_pill`] and is intended for compact
/// embedded cards, chips, and progress indicators.
pub fn draw_filled_rounded_rect<D>(
    target: &mut D,
    area: Rectangle,
    color: Rgb565,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    draw_filled_pill(target, area, color)
}

/// Draws an outlined circle.
pub fn draw_outlined_circle<D>(
    target: &mut D,
    top_left: Point,
    diameter: u32,
    color: Rgb565,
    stroke_width: u32,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    Circle::new(top_left, diameter)
        .into_styled(
            PrimitiveStyleBuilder::new()
                .stroke_color(color)
                .stroke_width(stroke_width)
                .build(),
        )
        .draw(target)
}

/// Draws a straight line with a fixed stroke width.
pub fn draw_line<D>(
    target: &mut D,
    start: Point,
    end: Point,
    color: Rgb565,
    stroke_width: u32,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    Line::new(start, end)
        .into_styled(
            PrimitiveStyleBuilder::new()
                .stroke_color(color)
                .stroke_width(stroke_width)
                .build(),
        )
        .draw(target)
}

/// Draws an approximated circular arc in degrees.
pub fn draw_arc<D>(
    target: &mut D,
    center: Point,
    radius: i32,
    start_degrees: i32,
    sweep_degrees: i32,
    color: Rgb565,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    if radius <= 0 || sweep_degrees == 0 {
        return Ok(());
    }
    let sweep = sweep_degrees.clamp(-360, 360);
    let step = if sweep > 0 { 4 } else { -4 };
    let mut angle = start_degrees;
    let end = start_degrees + sweep;
    let mut previous = point_on_circle(center, radius, angle);

    while angle != end {
        let next_angle = if step > 0 {
            (angle + step).min(end)
        } else {
            (angle + step).max(end)
        };
        let next = point_on_circle(center, radius, next_angle);
        draw_line(target, previous, next, color, 1)?;
        previous = next;
        angle = next_angle;
    }
    Ok(())
}

/// Draws a filled circular sector in degrees.
///
/// `start_degrees` is measured clockwise from the positive X axis and
/// `sweep_degrees` is clamped to `[-360, 360]`. The implementation is intended
/// for compact embedded gauges and indicators, not sub-pixel antialiasing.
pub fn draw_filled_sector<D>(
    target: &mut D,
    center: Point,
    radius: i32,
    start_degrees: i32,
    sweep_degrees: i32,
    color: Rgb565,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    if radius <= 0 || sweep_degrees == 0 {
        return Ok(());
    }

    let radius_squared = radius * radius;
    let sweep = sweep_degrees.clamp(-360, 360);
    let start = normalize_degrees(start_degrees);
    let end = normalize_degrees(start_degrees + sweep);

    for y in -radius..=radius {
        for x in -radius..=radius {
            if x * x + y * y > radius_squared {
                continue;
            }

            let angle = point_degrees(x, y);
            if angle_in_sweep(angle, start, end, sweep) {
                Pixel(center + Point::new(x, y), color).draw(target)?;
            }
        }
    }

    Ok(())
}

/// Draws wrapped text inside `area` and returns the number of lines drawn.
///
/// Wrapping uses the built-in 6x10 mono font metrics. Text is clipped at the
/// bottom of `area`; words longer than the available width are split.
pub fn draw_wrapped_text<D>(
    target: &mut D,
    area: Rectangle,
    text: &str,
    style: TextBlockStyle,
) -> Result<usize, D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    if let Some(background) = style.background {
        area.into_styled(PrimitiveStyle::with_fill(background))
            .draw(target)?;
    }

    let max_chars = (area.size.width / 6).max(1) as usize;
    let max_lines = (area.size.height / style.line_height).max(1) as usize;
    let text_style = MonoTextStyleBuilder::new()
        .font(&FONT_6X10)
        .text_color(style.color)
        .build();
    let mut lines_drawn = 0usize;
    let mut remaining = text.trim();

    while !remaining.is_empty() && lines_drawn < max_lines {
        let (line, rest) = split_line(remaining, max_chars);
        let baseline = area.top_left.y + 10 + (lines_drawn as i32 * style.line_height as i32);
        Text::new(line, Point::new(area.top_left.x, baseline), text_style).draw(target)?;
        lines_drawn += 1;
        remaining = rest.trim_start();
    }

    Ok(lines_drawn)
}

fn split_line(text: &str, max_chars: usize) -> (&str, &str) {
    if text.chars().count() <= max_chars {
        return (text, "");
    }

    let mut split_byte = 0;
    let mut last_space = None;
    for (char_count, (byte_index, ch)) in text.char_indices().enumerate() {
        if char_count == max_chars {
            break;
        }
        split_byte = byte_index + ch.len_utf8();
        if ch.is_whitespace() {
            last_space = Some(byte_index);
        }
    }

    let split_at = match last_space {
        Some(space) if space > 0 => space,
        _ => split_byte,
    };
    (&text[..split_at], &text[split_at..])
}

fn normalize_degrees(degrees: i32) -> i32 {
    let normalized = degrees % 360;
    if normalized < 0 {
        normalized + 360
    } else {
        normalized
    }
}

fn point_degrees(x: i32, y: i32) -> i32 {
    let radians = libm::atan2f(y as f32, x as f32);
    normalize_degrees((radians * 180.0 / core::f32::consts::PI) as i32)
}

fn point_on_circle(center: Point, radius: i32, degrees: i32) -> Point {
    let radians = degrees as f32 * core::f32::consts::PI / 180.0;
    Point::new(
        center.x + (libm::cosf(radians) * radius as f32) as i32,
        center.y + (libm::sinf(radians) * radius as f32) as i32,
    )
}

fn angle_in_sweep(angle: i32, start: i32, end: i32, sweep: i32) -> bool {
    if sweep >= 360 || sweep <= -360 {
        return true;
    }

    if sweep > 0 {
        if start <= end {
            angle >= start && angle <= end
        } else {
            angle >= start || angle <= end
        }
    } else if end <= start {
        angle <= start && angle >= end
    } else {
        angle <= start || angle >= end
    }
}

/// Easing curve for simple frame-based transitions.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Easing {
    /// Linear interpolation.
    Linear,
    /// Smooth ease-in/ease-out interpolation.
    SmoothStep,
}

/// Integer transition helper for simple embedded animations.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Transition {
    from: i32,
    to: i32,
    frames: u16,
    current: u16,
    easing: Easing,
}

impl Transition {
    /// Creates a new integer transition.
    #[must_use]
    pub const fn new(from: i32, to: i32, frames: u16, easing: Easing) -> Self {
        Self {
            from,
            to,
            frames,
            current: 0,
            easing,
        }
    }

    /// Advances the transition by one frame and returns the current value.
    #[must_use]
    pub fn step(&mut self) -> i32 {
        if self.current < self.frames {
            self.current += 1;
        }
        self.value()
    }

    /// Returns the current interpolated value.
    #[must_use]
    pub fn value(&self) -> i32 {
        if self.frames == 0 {
            return self.to;
        }
        let progress = self.current.min(self.frames) as f32 / self.frames as f32;
        let t = match self.easing {
            Easing::Linear => progress,
            Easing::SmoothStep => progress * progress * (3.0 - 2.0 * progress),
        };
        self.from + ((self.to - self.from) as f32 * t) as i32
    }

    /// Returns true when the transition has reached its final frame.
    #[must_use]
    pub const fn is_finished(&self) -> bool {
        self.current >= self.frames
    }
}