autocore-std 3.3.28

Standard library for AutoCore control programs - shared memory, IPC, and logging utilities
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
//! Banner WLS15P IO-Link multi-color light strip.
//!
//! This module provides enums for the WLS15P's PDO fields and two function blocks:
//!
//! - [`Wls15RunMode`] — IO-Link run mode control with preset animations
//!   (alert, knight rider, pulse, spectrum, etc.)
//! - [`Wls15Digital`] — Simple digital (two-wire) control with red/green/blue
//!   selection and optional blinking
//!
//! # IO-Link Run Mode Example
//!
//! ```ignore
//! use autocore_std::banner::wls15::{Wls15RunMode, Animation, Color, ColorIntensity, Speed};
//!
//! let mut light = Wls15RunMode::new();
//!
//! // Red alert — scrolls out from center
//! light.alert(Color::Red, ColorIntensity::High, Speed::Medium);
//!
//! // Knight Rider scanner effect
//! light.knight_rider(Color::Red);
//!
//! // Breathing pulse
//! light.pulse(Color::Green, ColorIntensity::High, Speed::Slow);
//!
//! // Rainbow spectrum
//! light.spectrum(Speed::Fast);
//!
//! // Turn off
//! light.off();
//! ```
//!
//! # Digital Control Example
//!
//! ```
//! use autocore_std::banner::wls15::Wls15Digital;
//! use std::time::Duration;
//!
//! let mut light = Wls15Digital::new();
//!
//! light.green();
//! light.red();
//! light.blue();
//! light.off();
//!
//! // Blink at 500ms interval
//! light.blink_on(Duration::from_millis(500));
//! light.call(); // call every scan cycle
//!
//! light.blink_off();
//! ```

use std::time::Duration;
use crate::fb::{RTrig, FTrig, Ton};

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

/// Animation modes for the WLS15P IO-Link run mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Animation {
    Off = 0,
    Steady = 1,
    Flash = 2,
    TwoColorFlash = 3,
    TwoColorShift = 4,
    EndsSteady = 5,
    EndsFlash = 6,
    Scroll = 7,
    CenterScroll = 8,
    Bounce = 9,
    CenterBounce = 10,
    /// Breathing effect — smooth intensity sweep.
    IntensitySweep = 11,
    TwoColorSweep = 12,
    /// All colors sweeping across the strip.
    Spectrum = 13,
    SingleEndSteady = 14,
    SingleEndFlash = 15,
}

/// Color codes for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Color {
    Green = 0,
    Red = 1,
    Orange = 2,
    Amber = 3,
    Yellow = 4,
    LimeGreen = 5,
    SpringGreen = 6,
    Cyan = 7,
    SkyBlue = 8,
    Blue = 9,
    Violet = 10,
    Magenta = 11,
    Rose = 12,
    DaylightWhite = 13,
    Custom1 = 14,
    Custom2 = 15,
    IncandescentWhite = 16,
    WarmWhite = 17,
    FluorescentWhite = 18,
    NeutralWhite = 19,
    CoolWhite = 20,
}

/// Color intensity levels for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ColorIntensity {
    High = 0,
    Low = 1,
    Medium = 2,
    Off = 3,
    Custom = 4,
}

/// Animation direction for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Direction {
    Up = 0,
    Down = 1,
}

/// Pulse pattern codes for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PulsePattern {
    Normal = 0,
    Strobe = 1,
    ThreePulse = 2,
    Sos = 3,
    Random = 4,
}

/// Scroll or bounce style for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ScrollStyle {
    Solid = 0,
    Tail = 1,
    Ripple = 2,
}

/// Animation speed codes for the WLS15P IO-Link PDOs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Speed {
    Medium = 0,
    Fast = 1,
    Slow = 2,
    CustomFlashRate = 3,
}

// ---------------------------------------------------------------------------
// Wls15RunMode — IO-Link run mode control
// ---------------------------------------------------------------------------

/// IO-Link run mode controller for the Banner WLS15P light strip.
///
/// Each output field corresponds to a PDO byte that should be linked to the
/// device's IO-Link process data. Call the preset methods (`alert`, `pulse`,
/// `knight_rider`, `spectrum`, `off`) to configure the animation, or set the
/// fields directly for full control.
#[derive(Debug, Clone)]
pub struct Wls15RunMode {
    /// PDO: Animation mode.
    pub animation: u8,
    /// PDO: Color 1.
    pub color1: u8,
    /// PDO: Color 1 intensity.
    pub color1_intensity: u8,
    /// PDO: Color 2.
    pub color2: u8,
    /// PDO: Color 2 intensity.
    pub color2_intensity: u8,
    /// PDO: Speed.
    pub speed: u8,
    /// PDO: Pulse pattern.
    pub pulse_pattern: u8,
    /// PDO: Scroll/bounce style.
    pub scroll_bounce_style: u8,
    /// PDO: Percent width of color 1 (0-100).
    pub percent_width_color1: u8,
    /// PDO: Direction.
    pub direction: u8,
}

impl Wls15RunMode {
    /// Create a new WLS15 run mode controller with all outputs at zero (off).
    pub fn new() -> Self {
        Self {
            animation: 0,
            color1: 0,
            color1_intensity: 0,
            color2: 0,
            color2_intensity: 0,
            speed: 0,
            pulse_pattern: 0,
            scroll_bounce_style: 0,
            percent_width_color1: 0,
            direction: 0,
        }
    }

    /// Turn the light off.
    pub fn off(&mut self) {
        self.animation = Animation::Off as u8;
    }

    /// Red alert — scrolls out from center.
    ///
    /// Classic sci-fi alert indication. Uses center-scroll animation
    /// with a single color (color 2 intensity is off).
    pub fn alert(&mut self, color: Color, intensity: ColorIntensity, speed: Speed) {
        self.color1 = color as u8;
        self.speed = speed as u8;
        self.color1_intensity = intensity as u8;
        self.color2_intensity = ColorIntensity::Off as u8;
        self.percent_width_color1 = 0;
        self.animation = Animation::CenterScroll as u8;
    }

    /// Knight Rider — bouncing scanner with a tail over a dark background.
    ///
    /// Classic 80's scanner effect. The primary color bounces across the
    /// strip with a tail effect.
    pub fn knight_rider(&mut self, color: Color) {
        self.color2 = Color::WarmWhite as u8;
        self.scroll_bounce_style = ScrollStyle::Tail as u8;
        self.color2_intensity = ColorIntensity::Off as u8;
        self.percent_width_color1 = 40;
        self.color1 = color as u8;
        self.color1_intensity = ColorIntensity::High as u8;
        self.speed = Speed::Medium as u8;
        self.animation = Animation::Bounce as u8;
    }

    /// Pulse — smooth breathing effect.
    ///
    /// The light smoothly fades in and out with the selected color.
    pub fn pulse(&mut self, color: Color, intensity: ColorIntensity, speed: Speed) {
        self.color1 = color as u8;
        self.speed = speed as u8;
        self.color1_intensity = intensity as u8;
        self.color2_intensity = ColorIntensity::Off as u8;
        self.percent_width_color1 = 0;
        self.animation = Animation::TwoColorSweep as u8;
    }

    /// Spectrum — all colors flowing across the strip.
    ///
    /// A rainbow effect where every color sweeps across the light.
    pub fn spectrum(&mut self, speed: Speed) {
        self.animation = Animation::Spectrum as u8;
        self.speed = speed as u8;
        self.percent_width_color1 = 0;
    }

    /// Steady — solid single color.
    pub fn steady(&mut self, color: Color, intensity: ColorIntensity) {
        self.color1 = color as u8;
        self.color1_intensity = intensity as u8;
        self.animation = Animation::Steady as u8;
    }

    /// Flash — single color flashing.
    pub fn flash(&mut self, color: Color, intensity: ColorIntensity, speed: Speed) {
        self.color1 = color as u8;
        self.color1_intensity = intensity as u8;
        self.speed = speed as u8;
        self.animation = Animation::Flash as u8;
    }
}

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

// ---------------------------------------------------------------------------
// Wls15Digital — Two-wire digital control
// ---------------------------------------------------------------------------

/// Digital (two-wire) controller for the Banner WLS15P light strip.
///
/// Controls the light using two digital outputs (Q1, Q2) to select colors:
///
/// | Q1 | Q2 | Color |
/// |----|----|-------|
/// | false | false | Off |
/// | true | false | Red |
/// | false | true | Green |
/// | true | true | Blue |
///
/// Supports optional blinking via an internal timer. Call [`call()`](Self::call)
/// every scan cycle to update the outputs.
#[derive(Debug, Clone)]
pub struct Wls15Digital {
    /// Output: connect to channel/input 1 of the light strip.
    pub q1: bool,
    /// Output: connect to channel/input 2 of the light strip.
    pub q2: bool,

    req_q1: bool,
    req_q2: bool,
    enable_blink: bool,
    blink_time: Duration,
    blink_timer: Ton,
    blink_bit: bool,
    rt_blink: RTrig,
    ft_blink: FTrig,
}

impl Wls15Digital {
    /// Create a new digital light controller (off, no blink).
    pub fn new() -> Self {
        Self {
            q1: false,
            q2: false,
            req_q1: false,
            req_q2: false,
            enable_blink: false,
            blink_time: Duration::from_secs(1),
            blink_timer: Ton::new(),
            blink_bit: false,
            rt_blink: RTrig::new(),
            ft_blink: FTrig::new(),
        }
    }

    /// Set color to off.
    pub fn off(&mut self) {
        self.req_q1 = false;
        self.req_q2 = false;
    }

    /// Set color to red.
    pub fn red(&mut self) {
        self.req_q1 = true;
        self.req_q2 = false;
    }

    /// Set color to green.
    pub fn green(&mut self) {
        self.req_q1 = false;
        self.req_q2 = true;
    }

    /// Set color to blue.
    pub fn blue(&mut self) {
        self.req_q1 = true;
        self.req_q2 = true;
    }

    /// Enable blinking at the specified interval.
    pub fn blink_on(&mut self, interval: Duration) {
        self.enable_blink = true;
        self.blink_time = interval;
    }

    /// Disable blinking — outputs follow the requested color directly.
    pub fn blink_off(&mut self) {
        self.enable_blink = false;
    }

    /// Update outputs. Call once per scan cycle.
    ///
    /// When blinking is enabled, the outputs toggle between the requested
    /// color and off at the configured interval. When blinking is disabled,
    /// the outputs track the requested color directly.
    pub fn call(&mut self) {
        // Blink timer — toggles blink_bit when elapsed
        if self.blink_timer.call(true, self.blink_time) {
            self.blink_bit = !self.blink_bit;
            self.blink_timer.reset();
        }

        let rising = self.rt_blink.call(self.blink_bit);
        let falling = self.ft_blink.call(self.blink_bit);

        if self.enable_blink {
            if rising {
                self.q1 = self.req_q1;
                self.q2 = self.req_q2;
            }
            if falling {
                self.q1 = false;
                self.q2 = false;
            }
        } else {
            self.q1 = self.req_q1;
            self.q2 = self.req_q2;
        }
    }
}

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

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

    #[test]
    fn test_run_mode_off() {
        let mut light = Wls15RunMode::new();
        light.steady(Color::Red, ColorIntensity::High);
        assert_eq!(light.animation, Animation::Steady as u8);
        light.off();
        assert_eq!(light.animation, Animation::Off as u8);
    }

    #[test]
    fn test_run_mode_alert() {
        let mut light = Wls15RunMode::new();
        light.alert(Color::Red, ColorIntensity::High, Speed::Medium);
        assert_eq!(light.animation, Animation::CenterScroll as u8);
        assert_eq!(light.color1, Color::Red as u8);
        assert_eq!(light.color1_intensity, ColorIntensity::High as u8);
        assert_eq!(light.color2_intensity, ColorIntensity::Off as u8);
    }

    #[test]
    fn test_run_mode_knight_rider() {
        let mut light = Wls15RunMode::new();
        light.knight_rider(Color::Red);
        assert_eq!(light.animation, Animation::Bounce as u8);
        assert_eq!(light.scroll_bounce_style, ScrollStyle::Tail as u8);
        assert_eq!(light.color1, Color::Red as u8);
        assert_eq!(light.percent_width_color1, 40);
    }

    #[test]
    fn test_run_mode_pulse() {
        let mut light = Wls15RunMode::new();
        light.pulse(Color::Green, ColorIntensity::High, Speed::Slow);
        assert_eq!(light.animation, Animation::TwoColorSweep as u8);
        assert_eq!(light.color1, Color::Green as u8);
        assert_eq!(light.speed, Speed::Slow as u8);
    }

    #[test]
    fn test_run_mode_spectrum() {
        let mut light = Wls15RunMode::new();
        light.spectrum(Speed::Fast);
        assert_eq!(light.animation, Animation::Spectrum as u8);
        assert_eq!(light.speed, Speed::Fast as u8);
    }

    #[test]
    fn test_digital_colors() {
        let mut light = Wls15Digital::new();

        light.red();
        light.call();
        assert_eq!((light.q1, light.q2), (true, false));

        light.green();
        light.call();
        assert_eq!((light.q1, light.q2), (false, true));

        light.blue();
        light.call();
        assert_eq!((light.q1, light.q2), (true, true));

        light.off();
        light.call();
        assert_eq!((light.q1, light.q2), (false, false));
    }

    #[test]
    fn test_digital_blink_off_tracks_color() {
        let mut light = Wls15Digital::new();
        light.red();
        light.blink_off();
        light.call();
        assert_eq!((light.q1, light.q2), (true, false));
    }

    #[test]
    fn test_enum_values() {
        assert_eq!(Animation::Off as u8, 0);
        assert_eq!(Animation::Spectrum as u8, 13);
        assert_eq!(Color::Green as u8, 0);
        assert_eq!(Color::Red as u8, 1);
        assert_eq!(Color::CoolWhite as u8, 20);
        assert_eq!(ColorIntensity::High as u8, 0);
        assert_eq!(ColorIntensity::Off as u8, 3);
        assert_eq!(Direction::Up as u8, 0);
        assert_eq!(Direction::Down as u8, 1);
        assert_eq!(PulsePattern::Sos as u8, 3);
        assert_eq!(ScrollStyle::Tail as u8, 1);
        assert_eq!(Speed::Fast as u8, 1);
    }
}