cu-sen0682 1.0.0-rc2

Copper source for the DFRobot SEN0682 / Wanyee WY6005 ToF lidar.
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

mod protocol;

use alloc::format;
use core::fmt;

#[cfg(feature = "std")]
use cu_linux_resources::LinuxSerialPort;
use cu_sensor_payloads::PointCloudSoa;
use cu29::prelude::*;
use cu29::resource::{Owned, ResourceBindingMap, ResourceBindings, ResourceManager};
use embedded_io::{ErrorKind, ErrorType, Read, Write};

pub use protocol::{MAX_FRAME_BYTES, MAX_POINTS};

const SERIAL_BUFFER_BYTES: usize = protocol::MAX_FRAME_BYTES * 2;
const DEFAULT_MIN_RANGE_M: f32 = 0.05;
const DEFAULT_ROW_ID: u8 = 0;
const DEFAULT_START_COLUMN: u8 = 1;
const DEFAULT_END_COLUMN: u8 = 64;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Sen0682ReadoutConfig {
    pub configure_device: bool,
    pub row_id: u8,
    pub start_column: u8,
    pub end_column: u8,
}

impl Sen0682ReadoutConfig {
    fn from_component_config(config: Option<&ComponentConfig>) -> CuResult<Self> {
        let configure_device = cfg_bool(config, "configure_device", true)?;
        let row_id = cfg_u8(config, "row_id", DEFAULT_ROW_ID)?;
        let start_column = cfg_u8(config, "start_column", DEFAULT_START_COLUMN)?;
        let end_column = cfg_u8(config, "end_column", DEFAULT_END_COLUMN)?;

        if row_id > 8 {
            return Err(CuError::from(format!(
                "sen0682 row_id must be between 0 and 8, got {row_id}"
            )));
        }
        if !(1..=64).contains(&start_column) {
            return Err(CuError::from(format!(
                "sen0682 start_column must be between 1 and 64, got {start_column}"
            )));
        }
        if !(1..=64).contains(&end_column) {
            return Err(CuError::from(format!(
                "sen0682 end_column must be between 1 and 64, got {end_column}"
            )));
        }
        if start_column > end_column {
            return Err(CuError::from(format!(
                "sen0682 start_column ({start_column}) must be <= end_column ({end_column})"
            )));
        }

        Ok(Self {
            configure_device,
            row_id,
            start_column,
            end_column,
        })
    }
}

fn cfg_bool(config: Option<&ComponentConfig>, key: &str, default: bool) -> CuResult<bool> {
    Ok(match config {
        Some(cfg) => cfg.get::<bool>(key)?.unwrap_or(default),
        None => default,
    })
}

fn cfg_u8(config: Option<&ComponentConfig>, key: &str, default: u8) -> CuResult<u8> {
    let raw = match config {
        Some(cfg) => cfg.get::<u64>(key)?.unwrap_or(default as u64),
        None => default as u64,
    };
    u8::try_from(raw).map_err(|_| {
        CuError::from(format!(
            "sen0682 config key `{key}` must fit in u8, got {raw}"
        ))
    })
}

fn cfg_f32(config: Option<&ComponentConfig>, key: &str, default: f32) -> CuResult<f32> {
    let raw = match config {
        Some(cfg) => cfg.get::<f64>(key)?.unwrap_or(default as f64),
        None => default as f64,
    };
    Ok(raw as f32)
}

trait FrameTransport {
    fn start(&mut self, readout: &Sen0682ReadoutConfig) -> CuResult<()>;
    fn read_frame(&mut self, out: &mut [u8]) -> CuResult<Option<usize>>;
    fn stop(&mut self) -> CuResult<()> {
        Ok(())
    }
}

struct Sen0682SourceCore<T> {
    transport: T,
    // The sensor publishes a small, fixed-size frame, so keeping the scratch
    // buffers inline is simpler and more deterministic than pulling a pool into
    // a 15 Hz source.
    frame_buffer: [u8; protocol::MAX_FRAME_BYTES],
    min_range_m: f32,
}

impl<T> Sen0682SourceCore<T>
where
    T: FrameTransport,
{
    fn new(transport: T, min_range_m: f32) -> Self {
        Self {
            transport,
            frame_buffer: [0u8; protocol::MAX_FRAME_BYTES],
            min_range_m,
        }
    }

    fn start(&mut self, readout: &Sen0682ReadoutConfig) -> CuResult<()> {
        self.transport.start(readout)
    }

    fn process(
        &mut self,
        ctx: &CuContext,
        output: &mut CuMsg<PointCloudSoa<MAX_POINTS>>,
    ) -> CuResult<()> {
        let Some(frame_len) = self.transport.read_frame(&mut self.frame_buffer)? else {
            output.metadata.set_status("waiting");
            output.clear_payload();
            return Ok(());
        };

        // Reusing the message-owned storage keeps the hot path from re-zeroing a
        // 512-point SoA every cycle just because the transport delivered another frame.
        let payload = output
            .payload_mut()
            .get_or_insert_with(PointCloudSoa::<MAX_POINTS>::default);

        let stats = protocol::decode_frame_into(
            &self.frame_buffer[..frame_len],
            ctx.now(),
            self.min_range_m,
            payload,
        )
        .map_err(|err| CuError::from(format!("sen0682 frame decode failed: {err}")))?;

        if payload.len == 0 {
            output.metadata.set_status("filtered");
            output.clear_payload();
            return Ok(());
        }

        output.metadata.set_status("streaming");
        output.tov = Tov::Time(ctx.now());

        if stats.frame_idx == 0 {
            debug!(
                "sen0682: first frame width={} height={} points={} device_index={}",
                stats.width, stats.height, stats.valid_points, stats.device_index
            );
        }

        Ok(())
    }

    fn stop(&mut self) -> CuResult<()> {
        self.transport.stop()
    }
}

struct SerialTransport<S> {
    serial: S,
    buffer: [u8; SERIAL_BUFFER_BYTES],
    buffered: usize,
    configured_by_driver: bool,
}

impl<S> SerialTransport<S> {
    fn new(serial: S) -> Self {
        Self {
            serial,
            buffer: [0u8; SERIAL_BUFFER_BYTES],
            buffered: 0,
            configured_by_driver: false,
        }
    }

    fn discard_prefix(&mut self, count: usize) {
        if count >= self.buffered {
            self.buffered = 0;
            return;
        }
        self.buffer.copy_within(count..self.buffered, 0);
        self.buffered -= count;
    }

    fn keep_tail(&mut self, count: usize) {
        if count >= self.buffered {
            return;
        }
        let start = self.buffered - count;
        self.buffer.copy_within(start..self.buffered, 0);
        self.buffered = count;
    }

    fn try_extract_frame(&mut self, out: &mut [u8]) -> CuResult<Option<usize>> {
        loop {
            if self.buffered < protocol::TAG.len() {
                return Ok(None);
            }

            let Some(tag_pos) = protocol::find_tag(&self.buffer[..self.buffered]) else {
                self.keep_tail(self.buffered.min(protocol::TAG.len() - 1));
                return Ok(None);
            };

            if tag_pos > 0 {
                self.discard_prefix(tag_pos);
            }

            if self.buffered < protocol::HEADER_BYTES {
                return Ok(None);
            }

            match protocol::frame_total_bytes_from_prefix(&self.buffer[..protocol::HEADER_BYTES]) {
                Ok(Some(total_bytes)) => {
                    if total_bytes > out.len() {
                        return Err(CuError::from(format!(
                            "sen0682 frame length {total_bytes} exceeds parser buffer {}",
                            out.len()
                        )));
                    }
                    if self.buffered < total_bytes {
                        return Ok(None);
                    }
                    out[..total_bytes].copy_from_slice(&self.buffer[..total_bytes]);
                    self.discard_prefix(total_bytes);
                    return Ok(Some(total_bytes));
                }
                Ok(None) => return Ok(None),
                Err(_) => {
                    self.discard_prefix(1);
                }
            }
        }
    }
}

impl<S> SerialTransport<S>
where
    S: Read + Write + ErrorType,
    <S as ErrorType>::Error: embedded_io::Error + fmt::Debug,
{
    fn send_command(&mut self, command: &str) -> CuResult<()> {
        write_all(&mut self.serial, command.as_bytes())?;
        self.serial
            .flush()
            .map_err(|err| CuError::from(format!("sen0682 command flush failed: {err:?}")))?;

        let mut saw_error = false;
        let mut scratch = [0u8; 256];
        loop {
            match self.serial.read(&mut scratch) {
                Ok(0) => break,
                Ok(n) => {
                    saw_error |= contains_ascii_token(&scratch[..n], b"ERROR");
                }
                Err(err) if is_idle_io_error(&err) => break,
                Err(err) => {
                    return Err(CuError::from(format!(
                        "sen0682 response read failed after `{}`: {err:?}",
                        command.trim()
                    )));
                }
            }
        }

        if saw_error {
            return Err(CuError::from(format!(
                "sen0682 rejected command `{}`",
                command.trim()
            )));
        }

        Ok(())
    }

    fn configure_streaming(&mut self, readout: &Sen0682ReadoutConfig) -> CuResult<()> {
        if !readout.configure_device {
            self.configured_by_driver = false;
            return Ok(());
        }

        self.send_command("AT+STREAM_CONTROL=0\n")?;
        self.send_command("AT+STREAM_DATA_TYPE=3\n")?;
        self.send_command("AT+SPAD_FRAME_MODE=0\n")?;
        self.send_command(
            format!(
                "AT+SPAD_OUTPUT_LINE_DATA={},{},{}\n",
                readout.row_id, readout.start_column, readout.end_column
            )
            .as_str(),
        )?;
        // A Copper source should not rewrite bench hardware state as a startup
        // side effect, so we intentionally stay away from AT+SAVE_CONFIG here.
        self.send_command("AT+STREAM_CONTROL=1\n")?;
        self.buffered = 0;
        self.configured_by_driver = true;
        Ok(())
    }
}

impl<S> FrameTransport for SerialTransport<S>
where
    S: Read + Write + ErrorType + Send + Sync + 'static,
    <S as ErrorType>::Error: embedded_io::Error + fmt::Debug + 'static,
{
    fn start(&mut self, readout: &Sen0682ReadoutConfig) -> CuResult<()> {
        self.configure_streaming(readout)
    }

    fn read_frame(&mut self, out: &mut [u8]) -> CuResult<Option<usize>> {
        if let Some(frame) = self.try_extract_frame(out)? {
            return Ok(Some(frame));
        }

        if self.buffered == self.buffer.len() {
            return Err(CuError::from(
                "sen0682 serial framing buffer saturated before a valid frame was found",
            ));
        }

        match self.serial.read(&mut self.buffer[self.buffered..]) {
            Ok(0) => Ok(None),
            Ok(n) => {
                self.buffered += n;
                self.try_extract_frame(out)
            }
            Err(err) if is_idle_io_error(&err) => Ok(None),
            Err(err) => Err(CuError::from(format!(
                "sen0682 serial read failed: {err:?}"
            ))),
        }
    }

    fn stop(&mut self) -> CuResult<()> {
        if self.configured_by_driver {
            let _ = self.send_command("AT+STREAM_CONTROL=0\n");
            self.configured_by_driver = false;
        }
        Ok(())
    }
}

fn write_all<S>(serial: &mut S, bytes: &[u8]) -> CuResult<()>
where
    S: Write + ErrorType,
    <S as ErrorType>::Error: fmt::Debug,
{
    let mut written = 0;
    while written < bytes.len() {
        let n = serial
            .write(&bytes[written..])
            .map_err(|err| CuError::from(format!("sen0682 command write failed: {err:?}")))?;
        if n == 0 {
            return Err(CuError::from(
                "sen0682 command write returned zero bytes before completion",
            ));
        }
        written += n;
    }
    Ok(())
}

fn contains_ascii_token(haystack: &[u8], token: &[u8]) -> bool {
    haystack.windows(token.len()).any(|window| window == token)
}

fn is_idle_io_error<E>(err: &E) -> bool
where
    E: embedded_io::Error,
{
    matches!(err.kind(), ErrorKind::TimedOut | ErrorKind::Interrupted)
}

/// Address-scoped I2C integration point for board bundles.
///
/// The driver expects the resource layer to own addressing and any burst-read
/// quirks so the task config stays about lidar behavior instead of hardware
/// wiring details.
pub trait Sen0682I2cBus: Send + Sync + 'static {
    type Error: fmt::Debug + Send + 'static;

    fn configure_stream(&mut self, readout: &Sen0682ReadoutConfig) -> Result<(), Self::Error>;
    fn read_frame(&mut self, out: &mut [u8]) -> Result<Option<usize>, Self::Error>;
    fn stop_stream(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

struct I2cTransport<B> {
    bus: B,
}

impl<B> I2cTransport<B> {
    fn new(bus: B) -> Self {
        Self { bus }
    }
}

impl<B> FrameTransport for I2cTransport<B>
where
    B: Sen0682I2cBus,
{
    fn start(&mut self, readout: &Sen0682ReadoutConfig) -> CuResult<()> {
        self.bus
            .configure_stream(readout)
            .map_err(|err| CuError::from(format!("sen0682 i2c configure failed: {err:?}")))
    }

    fn read_frame(&mut self, out: &mut [u8]) -> CuResult<Option<usize>> {
        self.bus
            .read_frame(out)
            .map_err(|err| CuError::from(format!("sen0682 i2c read failed: {err:?}")))
    }

    fn stop(&mut self) -> CuResult<()> {
        self.bus
            .stop_stream()
            .map_err(|err| CuError::from(format!("sen0682 i2c stop failed: {err:?}")))
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SerialBinding {
    Serial,
}

pub struct Sen0682SerialResourcesT<S> {
    pub serial: Owned<S>,
}

#[cfg(feature = "std")]
pub type Sen0682SerialResources = Sen0682SerialResourcesT<LinuxSerialPort>;

impl<'r, S: 'static + Send + Sync> ResourceBindings<'r> for Sen0682SerialResourcesT<S> {
    type Binding = SerialBinding;

    fn from_bindings(
        manager: &'r mut ResourceManager,
        mapping: Option<&ResourceBindingMap<Self::Binding>>,
    ) -> CuResult<Self> {
        let mapping = mapping.ok_or_else(|| {
            CuError::from("Sen0682SerialSourceTask requires a `serial` resource mapping")
        })?;
        let path = mapping.get(SerialBinding::Serial).ok_or_else(|| {
            CuError::from(
                "Sen0682SerialSourceTask resources must include `serial: <bundle.resource>`",
            )
        })?;
        let serial = manager
            .take::<S>(path.typed())
            .map_err(|e| e.add_cause("Failed to fetch SEN0682 serial resource"))?;
        Ok(Self { serial })
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum I2cBinding {
    I2c,
}

pub struct Sen0682I2cResourcesT<B> {
    pub i2c: Owned<B>,
}

impl<'r, B: 'static + Send + Sync> ResourceBindings<'r> for Sen0682I2cResourcesT<B> {
    type Binding = I2cBinding;

    fn from_bindings(
        manager: &'r mut ResourceManager,
        mapping: Option<&ResourceBindingMap<Self::Binding>>,
    ) -> CuResult<Self> {
        let mapping = mapping.ok_or_else(|| {
            CuError::from("Sen0682I2cSourceTask requires an `i2c` resource mapping")
        })?;
        let path = mapping.get(I2cBinding::I2c).ok_or_else(|| {
            CuError::from("Sen0682I2cSourceTask resources must include `i2c: <bundle.resource>`")
        })?;
        let i2c = manager
            .take::<B>(path.typed())
            .map_err(|e| e.add_cause("Failed to fetch SEN0682 I2C resource"))?;
        Ok(Self { i2c })
    }
}

#[derive(Reflect)]
#[reflect(no_field_bounds, from_reflect = false, type_path = false)]
pub struct Sen0682SerialSourceTask<S> {
    #[reflect(ignore)]
    core: Sen0682SourceCore<SerialTransport<S>>,
    configure_device: bool,
    row_id: u8,
    start_column: u8,
    end_column: u8,
    min_range_m: f32,
}

#[cfg(feature = "std")]
pub type Sen0682SerialSource = Sen0682SerialSourceTask<LinuxSerialPort>;

impl<S: 'static> TypePath for Sen0682SerialSourceTask<S> {
    fn type_path() -> &'static str {
        "cu_sen0682::Sen0682SerialSourceTask"
    }

    fn short_type_path() -> &'static str {
        "Sen0682SerialSourceTask"
    }

    fn type_ident() -> Option<&'static str> {
        Some("Sen0682SerialSourceTask")
    }

    fn crate_name() -> Option<&'static str> {
        Some("cu_sen0682")
    }

    fn module_path() -> Option<&'static str> {
        Some("cu_sen0682")
    }
}

impl<S> fmt::Debug for Sen0682SerialSourceTask<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Sen0682SerialSourceTask")
            .field("configure_device", &self.configure_device)
            .field("row_id", &self.row_id)
            .field("start_column", &self.start_column)
            .field("end_column", &self.end_column)
            .field("min_range_m", &self.min_range_m)
            .finish()
    }
}

impl<S> Freezable for Sen0682SerialSourceTask<S> {}

impl<S> Sen0682SerialSourceTask<S> {
    fn readout_config(&self) -> Sen0682ReadoutConfig {
        Sen0682ReadoutConfig {
            configure_device: self.configure_device,
            row_id: self.row_id,
            start_column: self.start_column,
            end_column: self.end_column,
        }
    }
}

impl<S> CuSrcTask for Sen0682SerialSourceTask<S>
where
    S: Read + Write + ErrorType + Send + Sync + 'static,
    <S as ErrorType>::Error: embedded_io::Error + fmt::Debug + 'static,
{
    type Resources<'r> = Sen0682SerialResourcesT<S>;
    type Output<'m> = output_msg!(PointCloudSoa<MAX_POINTS>);

    fn new(config: Option<&ComponentConfig>, resources: Self::Resources<'_>) -> CuResult<Self>
    where
        Self: Sized,
    {
        let readout = Sen0682ReadoutConfig::from_component_config(config)?;
        let min_range_m = cfg_f32(config, "min_range_m", DEFAULT_MIN_RANGE_M)?;

        Ok(Self {
            core: Sen0682SourceCore::new(SerialTransport::new(resources.serial.0), min_range_m),
            configure_device: readout.configure_device,
            row_id: readout.row_id,
            start_column: readout.start_column,
            end_column: readout.end_column,
            min_range_m,
        })
    }

    fn start(&mut self, _ctx: &CuContext) -> CuResult<()> {
        self.core.start(&self.readout_config())
    }

    fn process(&mut self, ctx: &CuContext, output: &mut Self::Output<'_>) -> CuResult<()> {
        self.core.process(ctx, output)
    }

    fn stop(&mut self, _ctx: &CuContext) -> CuResult<()> {
        self.core.stop()
    }
}

#[derive(Reflect)]
#[reflect(no_field_bounds, from_reflect = false, type_path = false)]
pub struct Sen0682I2cSourceTask<B> {
    #[reflect(ignore)]
    core: Sen0682SourceCore<I2cTransport<B>>,
    configure_device: bool,
    row_id: u8,
    start_column: u8,
    end_column: u8,
    min_range_m: f32,
}

impl<B: 'static> TypePath for Sen0682I2cSourceTask<B> {
    fn type_path() -> &'static str {
        "cu_sen0682::Sen0682I2cSourceTask"
    }

    fn short_type_path() -> &'static str {
        "Sen0682I2cSourceTask"
    }

    fn type_ident() -> Option<&'static str> {
        Some("Sen0682I2cSourceTask")
    }

    fn crate_name() -> Option<&'static str> {
        Some("cu_sen0682")
    }

    fn module_path() -> Option<&'static str> {
        Some("cu_sen0682")
    }
}

impl<B> fmt::Debug for Sen0682I2cSourceTask<B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Sen0682I2cSourceTask")
            .field("configure_device", &self.configure_device)
            .field("row_id", &self.row_id)
            .field("start_column", &self.start_column)
            .field("end_column", &self.end_column)
            .field("min_range_m", &self.min_range_m)
            .finish()
    }
}

impl<B> Freezable for Sen0682I2cSourceTask<B> {}

impl<B> Sen0682I2cSourceTask<B> {
    fn readout_config(&self) -> Sen0682ReadoutConfig {
        Sen0682ReadoutConfig {
            configure_device: self.configure_device,
            row_id: self.row_id,
            start_column: self.start_column,
            end_column: self.end_column,
        }
    }
}

impl<B> CuSrcTask for Sen0682I2cSourceTask<B>
where
    B: Sen0682I2cBus,
{
    type Resources<'r> = Sen0682I2cResourcesT<B>;
    type Output<'m> = output_msg!(PointCloudSoa<MAX_POINTS>);

    fn new(config: Option<&ComponentConfig>, resources: Self::Resources<'_>) -> CuResult<Self>
    where
        Self: Sized,
    {
        let readout = Sen0682ReadoutConfig::from_component_config(config)?;
        let min_range_m = cfg_f32(config, "min_range_m", DEFAULT_MIN_RANGE_M)?;

        Ok(Self {
            core: Sen0682SourceCore::new(I2cTransport::new(resources.i2c.0), min_range_m),
            configure_device: readout.configure_device,
            row_id: readout.row_id,
            start_column: readout.start_column,
            end_column: readout.end_column,
            min_range_m,
        })
    }

    fn start(&mut self, _ctx: &CuContext) -> CuResult<()> {
        self.core.start(&self.readout_config())
    }

    fn process(&mut self, ctx: &CuContext, output: &mut Self::Output<'_>) -> CuResult<()> {
        self.core.process(ctx, output)
    }

    fn stop(&mut self, _ctx: &CuContext) -> CuResult<()> {
        self.core.stop()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::collections::VecDeque;
    use alloc::vec::Vec;

    fn build_test_frame() -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(b"wyld");
        bytes.extend_from_slice(&2u16.to_le_bytes());
        bytes.extend_from_slice(&1u16.to_le_bytes());
        bytes.extend_from_slice(&16u32.to_le_bytes());
        bytes.extend_from_slice(&16u16.to_le_bytes());
        bytes.extend_from_slice(&3u16.to_le_bytes());
        bytes.extend_from_slice(&7u32.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&0u32.to_le_bytes());
        bytes.extend_from_slice(&1000i16.to_le_bytes());
        bytes.extend_from_slice(&0i16.to_le_bytes());
        bytes.extend_from_slice(&2000i16.to_le_bytes());
        bytes.extend_from_slice(&123u16.to_le_bytes());
        bytes.extend_from_slice(&1500i16.to_le_bytes());
        bytes.extend_from_slice(&0i16.to_le_bytes());
        bytes.extend_from_slice(&2500i16.to_le_bytes());
        bytes.extend_from_slice(&456u16.to_le_bytes());
        bytes
    }

    #[derive(Default)]
    struct FakeSerial {
        reads: VecDeque<Result<Vec<u8>, std::io::Error>>,
        writes: Vec<Vec<u8>>,
    }

    impl FakeSerial {
        fn with_reads(reads: impl IntoIterator<Item = Result<Vec<u8>, std::io::Error>>) -> Self {
            Self {
                reads: reads.into_iter().collect(),
                writes: Vec::new(),
            }
        }
    }

    impl ErrorType for FakeSerial {
        type Error = std::io::Error;
    }

    impl Read for FakeSerial {
        fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
            let Some(next) = self.reads.pop_front() else {
                return Err(std::io::Error::from(std::io::ErrorKind::TimedOut));
            };
            match next {
                Ok(chunk) => {
                    let len = chunk.len().min(buf.len());
                    buf[..len].copy_from_slice(&chunk[..len]);
                    Ok(len)
                }
                Err(err) => Err(err),
            }
        }
    }

    impl Write for FakeSerial {
        fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
            self.writes.push(buf.to_vec());
            Ok(buf.len())
        }

        fn flush(&mut self) -> Result<(), Self::Error> {
            Ok(())
        }
    }

    #[test]
    fn serial_transport_resynchronizes_after_text_noise() {
        let frame = build_test_frame();
        let serial = FakeSerial::with_reads([
            Ok(b"OK\r\n".to_vec()),
            Ok(frame[..11].to_vec()),
            Ok(frame[11..].to_vec()),
        ]);
        let mut transport = SerialTransport::new(serial);
        let mut out = [0u8; protocol::MAX_FRAME_BYTES];

        let mut extracted = None;
        for _ in 0..3 {
            extracted = transport.read_frame(&mut out).expect("read should succeed");
            if extracted.is_some() {
                break;
            }
        }

        assert!(extracted.is_some());
        assert_eq!(extracted.unwrap(), frame.len());
        assert_eq!(&out[..frame.len()], frame.as_slice());
    }
}