ferriswheel 0.5.0

RGB LED ring effects and animations
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
//! Breathing animation effect for LED rings.
//!
//! All LEDs display the same color with brightness oscillating smoothly
//! over a full symmetric sine cycle — no flat-bottom plateau.

use crate::effect::{validate_buffer, validate_num_leds, validate_speed, Effect, EffectError};
use crate::util::{scale_brightness, sine_full};
use rgb::RGB8;

/// A smooth breathing animation effect.
///
/// All LEDs share the same color, with brightness oscillating smoothly
/// between configurable minimum and maximum values using a full symmetric
/// sine wave. The brightness rises continuously from minimum to maximum
/// and back again, with no pause at the floor.
///
/// # Comparison with [`PulseEffect`]
///
/// [`PulseEffect`] uses a half-wave rectified sine: brightness rises to the
/// peak, falls to zero, then *pauses at zero* for roughly a quarter of the
/// cycle before rising again — producing a heartbeat or pulse character.
///
/// `BreatheEffect` uses a full symmetric sine wave: brightness rises to the
/// peak and descends symmetrically back to the minimum without any pause,
/// giving a continuous in-out breathing rhythm.
///
/// # Example
///
/// ```
/// use ferriswheel::{BreatheEffect, Effect};
/// use ferriswheel::RGB8;
///
/// let mut breathe = BreatheEffect::new(12).unwrap()
///     .with_color(RGB8::new(0, 128, 255));
/// let mut buffer = [RGB8::default(); 12];
///
/// breathe.update(&mut buffer).unwrap();
/// ```
///
/// [`PulseEffect`]: crate::PulseEffect
#[derive(Debug, Clone, PartialEq)]
pub struct BreatheEffect {
    num_leds: usize,
    color: RGB8,
    phase: u8,
    speed: u8,
    min_brightness: u8,
    max_brightness: u8,
}

impl BreatheEffect {
    /// Creates a new breathe effect for the specified number of LEDs.
    ///
    /// # Errors
    ///
    /// Returns `EffectError::ZeroLeds` if `num_leds` is 0.
    /// Returns `EffectError::TooManyLeds` if `num_leds` exceeds `MAX_LEDS`.
    ///
    /// # Default Configuration
    ///
    /// - Color: white (255, 255, 255)
    /// - Speed: 2
    /// - Min brightness: 0
    /// - Max brightness: 255
    pub fn new(num_leds: usize) -> Result<Self, EffectError> {
        validate_num_leds(num_leds)?;
        Ok(Self {
            num_leds,
            color: RGB8::new(255, 255, 255),
            phase: 0,
            speed: 2,
            min_brightness: 0,
            max_brightness: 255,
        })
    }

    /// Sets the breathe color.
    pub fn with_color(mut self, color: RGB8) -> Self {
        self.color = color;
        self
    }

    /// Sets the animation speed (phase increment per update).
    ///
    /// Higher values make the breathing cycle faster.
    ///
    /// # Errors
    ///
    /// Returns `EffectError::ZeroStep` if `speed` is 0.
    pub fn with_speed(mut self, speed: u8) -> Result<Self, EffectError> {
        validate_speed(speed)?;
        self.speed = speed;
        Ok(self)
    }

    /// Sets the minimum brightness (0–255).
    pub fn with_min_brightness(mut self, min: u8) -> Self {
        self.min_brightness = min;
        self
    }

    /// Sets the maximum brightness (0–255).
    pub fn with_max_brightness(mut self, max: u8) -> Self {
        self.max_brightness = max;
        self
    }

    /// Sets the breathe color without resetting the animation phase.
    ///
    /// Use this to change the color live (e.g., from a rotary encoder)
    /// without restarting the breathing cycle.
    pub fn set_color(&mut self, color: RGB8) {
        self.color = color;
    }

    /// Returns the number of LEDs this effect is configured for.
    pub fn num_leds(&self) -> usize {
        self.num_leds
    }

    fn current_brightness(&self) -> u8 {
        let lo = self.min_brightness.min(self.max_brightness) as u16;
        let hi = self.min_brightness.max(self.max_brightness) as u16;
        let sine_val = sine_full(self.phase) as u16;
        (lo + (sine_val * (hi - lo)) / 255) as u8
    }

    /// Fills the buffer with the current breathe colors without advancing.
    pub fn current(&self, buffer: &mut [RGB8]) -> Result<(), EffectError> {
        validate_buffer(buffer, self.num_leds)?;
        let brightness = self.current_brightness();
        let pixel = scale_brightness(self.color, brightness);
        for led in buffer.iter_mut().take(self.num_leds) {
            *led = pixel;
        }
        Ok(())
    }

    /// Fills the buffer with breathe colors and advances the animation.
    pub fn update(&mut self, buffer: &mut [RGB8]) -> Result<(), EffectError> {
        self.current(buffer)?;
        self.phase = self.phase.wrapping_add(self.speed);
        Ok(())
    }

    /// Resets the animation to its initial state.
    pub fn reset(&mut self) {
        self.phase = 0;
    }
}

impl Effect for BreatheEffect {
    fn update(&mut self, buffer: &mut [RGB8]) -> Result<(), EffectError> {
        BreatheEffect::update(self, buffer)
    }

    fn current(&self, buffer: &mut [RGB8]) -> Result<(), EffectError> {
        BreatheEffect::current(self, buffer)
    }

    fn reset(&mut self) {
        BreatheEffect::reset(self);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::effect::MAX_LEDS;

    #[test]
    fn test_new_with_zero_leds_returns_error() {
        assert_eq!(BreatheEffect::new(0).unwrap_err(), EffectError::ZeroLeds);
    }

    #[test]
    fn test_new_with_too_many_leds_returns_error() {
        assert_eq!(
            BreatheEffect::new(MAX_LEDS + 1).unwrap_err(),
            EffectError::TooManyLeds {
                requested: MAX_LEDS + 1,
                max: MAX_LEDS
            }
        );
    }

    #[test]
    fn test_new_with_valid_leds_succeeds() {
        let effect = BreatheEffect::new(12).unwrap();
        assert_eq!(effect.num_leds(), 12);
    }

    #[test]
    fn test_with_speed_zero_returns_error() {
        let result = BreatheEffect::new(12).unwrap().with_speed(0);
        assert_eq!(result.unwrap_err(), EffectError::ZeroStep);
    }

    #[test]
    fn test_buffer_too_small_returns_error() {
        let effect = BreatheEffect::new(12).unwrap();
        let mut buffer = [RGB8::default(); 8];
        assert_eq!(
            effect.current(&mut buffer).unwrap_err(),
            EffectError::BufferTooSmall {
                required: 12,
                actual: 8
            }
        );
    }

    #[test]
    fn test_all_leds_same_color() {
        let mut effect = BreatheEffect::new(6)
            .unwrap()
            .with_color(RGB8::new(255, 0, 0));
        let mut buffer = [RGB8::default(); 6];

        for _ in 0..10 {
            effect.update(&mut buffer).unwrap();
        }

        for i in 1..6 {
            assert_eq!(buffer[0], buffer[i], "LED {} should match LED 0", i);
        }
    }

    #[test]
    fn test_breathing_changes_brightness() {
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 0, 0))
            .with_speed(20)
            .unwrap();

        let mut buffer = [RGB8::default(); 1];
        let mut values = Vec::new();
        for _ in 0..20 {
            effect.update(&mut buffer).unwrap();
            values.push(buffer[0].r);
        }

        let min = *values.iter().min().unwrap();
        let max = *values.iter().max().unwrap();
        assert!(
            max > min,
            "brightness should vary: min={}, max={}",
            min,
            max
        );
    }

    #[test]
    fn test_no_extended_brightness_plateau() {
        // With min_brightness=0 (default), count zero-brightness phases across a full
        // 256-step cycle. BreatheEffect uses a symmetric full sine: only phases at
        // exactly 0 and 255 map to zero, so at most 3 zero phases are expected.
        // PulseEffect has ~19 zero-brightness phases due to its half-wave plateau.
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 255, 255))
            .with_speed(1)
            .unwrap();

        let mut buffer = [RGB8::default(); 1];
        let mut zero_count = 0usize;
        for _ in 0..256 {
            effect.update(&mut buffer).unwrap();
            let brightness = buffer[0].r.max(buffer[0].g).max(buffer[0].b);
            if brightness == 0 {
                zero_count += 1;
            }
        }
        assert!(
            zero_count <= 3,
            "BreatheEffect should have at most 3 zero-brightness phases, got {}",
            zero_count
        );
    }

    #[test]
    fn test_inverted_brightness_range_does_not_underflow() {
        // min > max would underflow with naive u16 subtraction; the clamp must
        // treat this as the same range as (max, min) and never produce nonsense.
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 255, 255))
            .with_min_brightness(200)
            .with_max_brightness(100)
            .with_speed(1)
            .unwrap();

        let mut buffer = [RGB8::default(); 1];
        for _ in 0..256 {
            effect.update(&mut buffer).unwrap();
            let brightness = buffer[0].r.max(buffer[0].g).max(buffer[0].b);
            assert!(
                brightness >= 99 && brightness <= 201,
                "brightness {} should be within [100, 200] (allowing rounding)",
                brightness
            );
        }
    }

    #[test]
    fn test_min_brightness_floor() {
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 255, 255))
            .with_min_brightness(100)
            .with_speed(1)
            .unwrap();

        let mut buffer = [RGB8::default(); 1];
        for _ in 0..256 {
            effect.update(&mut buffer).unwrap();
            let brightness = buffer[0].r.max(buffer[0].g).max(buffer[0].b);
            assert!(
                brightness >= 99,
                "brightness {} should be >= min 100 (allowing rounding)",
                brightness
            );
        }
    }

    #[test]
    fn test_max_brightness_ceiling() {
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 255, 255))
            .with_max_brightness(100)
            .with_speed(1)
            .unwrap();

        let mut buffer = [RGB8::default(); 1];
        for _ in 0..256 {
            effect.update(&mut buffer).unwrap();
            let brightness = buffer[0].r.max(buffer[0].g).max(buffer[0].b);
            assert!(
                brightness <= 101,
                "brightness {} should be <= max 100 (allowing rounding)",
                brightness
            );
        }
    }

    #[test]
    fn test_reset_restores_initial_state() {
        let mut effect = BreatheEffect::new(4)
            .unwrap()
            .with_color(RGB8::new(0, 255, 0))
            .with_speed(10)
            .unwrap();

        let mut initial = [RGB8::default(); 4];
        effect.current(&mut initial).unwrap();

        let mut temp = [RGB8::default(); 4];
        for _ in 0..20 {
            effect.update(&mut temp).unwrap();
        }

        effect.reset();
        let mut after_reset = [RGB8::default(); 4];
        effect.current(&mut after_reset).unwrap();

        assert_eq!(initial, after_reset);
    }

    #[test]
    fn test_current_does_not_advance() {
        let effect = BreatheEffect::new(4)
            .unwrap()
            .with_color(RGB8::new(0, 255, 0));

        let mut buf1 = [RGB8::default(); 4];
        let mut buf2 = [RGB8::default(); 4];

        effect.current(&mut buf1).unwrap();
        effect.current(&mut buf2).unwrap();

        assert_eq!(buf1, buf2);
    }

    #[test]
    fn test_set_color_does_not_reset_phase() {
        let mut effect = BreatheEffect::new(4)
            .unwrap()
            .with_color(RGB8::new(255, 0, 0))
            .with_speed(20)
            .unwrap();

        let mut buffer = [RGB8::default(); 4];
        for _ in 0..5 {
            effect.update(&mut buffer).unwrap();
        }

        let mut before = [RGB8::default(); 4];
        effect.current(&mut before).unwrap();

        effect.set_color(RGB8::new(0, 255, 0));

        let mut after = [RGB8::default(); 4];
        effect.current(&mut after).unwrap();

        // Brightness should be unchanged (same phase), only hue differs.
        let brightness_before = before[0].r.max(before[0].g).max(before[0].b);
        let brightness_after = after[0].r.max(after[0].g).max(after[0].b);
        assert_eq!(
            brightness_before, brightness_after,
            "phase should be unchanged after set_color"
        );
        assert_ne!(before[0], after[0], "color should have changed");
    }

    #[test]
    fn test_trait_object_update() {
        let mut effect = BreatheEffect::new(4)
            .unwrap()
            .with_color(RGB8::new(255, 0, 0))
            .with_speed(50)
            .unwrap();

        let effect_ref: &mut dyn Effect = &mut effect;

        let mut buf1 = [RGB8::default(); 4];
        let mut buf2 = [RGB8::default(); 4];

        effect_ref.update(&mut buf1).unwrap();
        effect_ref.update(&mut buf2).unwrap();
        // At speed=50, phase 0 (buf1) is black; phase 50 (buf2) maps to a
        // non-zero sine value — successive updates must produce different output.
        assert_ne!(
            buf1, buf2,
            "successive updates should produce different output"
        );
    }

    #[test]
    fn test_new_with_max_leds_succeeds() {
        assert!(BreatheEffect::new(MAX_LEDS).is_ok());
    }

    #[test]
    fn test_update_advances_state() {
        // At phase=0, sine_full=0 so output is black.
        // After one update at speed=20, phase advances to 20, which maps to a
        // non-zero sine value and therefore a brighter output.
        let mut effect = BreatheEffect::new(1)
            .unwrap()
            .with_color(RGB8::new(255, 255, 255))
            .with_speed(20)
            .unwrap();

        let mut buf = [RGB8::default(); 1];
        effect.current(&mut buf).unwrap();
        let at_phase_0 = buf[0];

        effect.update(&mut buf).unwrap(); // advances phase 0 → 20
        effect.current(&mut buf).unwrap();
        let at_phase_20 = buf[0];

        assert_eq!(at_phase_0, RGB8::new(0, 0, 0), "phase=0 should be black");
        assert_ne!(
            at_phase_0, at_phase_20,
            "phase=20 should differ from phase=0"
        );
    }

    #[test]
    fn test_with_color_affects_output() {
        // Advance past phase=0 (where output is black regardless of color).
        // At phase=64 the sine value is non-zero, so the chosen color is visible.
        let make_output = |color: RGB8| {
            let mut e = BreatheEffect::new(1)
                .unwrap()
                .with_color(color)
                .with_speed(64)
                .unwrap();
            let mut buf = [RGB8::default(); 1];
            e.update(&mut buf).unwrap(); // advance phase 0 → 64
            e.current(&mut buf).unwrap(); // read at phase=64
            buf[0]
        };

        let red = make_output(RGB8::new(255, 0, 0));
        let blue = make_output(RGB8::new(0, 0, 255));

        assert_ne!(
            red, blue,
            "different colors should produce different output"
        );
        assert_eq!(red.g, 0, "red effect should have no green channel");
        assert_eq!(red.b, 0, "red effect should have no blue channel");
        assert_eq!(blue.r, 0, "blue effect should have no red channel");
        assert_eq!(blue.g, 0, "blue effect should have no green channel");
    }

    #[test]
    fn test_trait_reset_path() {
        let mut effect = BreatheEffect::new(4)
            .unwrap()
            .with_color(RGB8::new(0, 255, 0))
            .with_speed(10)
            .unwrap();

        let mut initial = [RGB8::default(); 4];
        effect.current(&mut initial).unwrap();

        let effect_dyn: &mut dyn Effect = &mut effect;
        let mut buf = [RGB8::default(); 4];
        for _ in 0..10 {
            effect_dyn.update(&mut buf).unwrap();
        }

        effect_dyn.reset();

        let mut after_reset = [RGB8::default(); 4];
        effect_dyn.current(&mut after_reset).unwrap();

        assert_eq!(
            initial, after_reset,
            "reset via trait object should restore initial state"
        );
    }

    #[test]
    fn test_oversized_buffer_accepted() {
        let sentinel = RGB8::new(0xDE, 0xAD, 0xFF);
        let effect = BreatheEffect::new(4).unwrap();
        let mut buffer = [sentinel; 8];
        effect.current(&mut buffer).unwrap();
        for i in 4..8 {
            assert_eq!(
                buffer[i], sentinel,
                "LED {} beyond num_leds must not be modified",
                i
            );
        }
    }
}