Cargo.lock
Cargo.lock
This file has not been changed.
Cargo.toml
Cargo.toml
This file has not been changed.
+1
-1
README.md
+1
-1
README.md
···
10
10
11
11
## Licenses
12
12
13
-
The sensor hardware designs are licensed under the [CERN Open Hardware Licence Version 2 - Strongly Reciprocal](./kicad/LICENSE-CERN-OHL-S) license.
13
+
The sensor hardware designs are licensed under the [CERN Open Hardware Licence Version 2 - Strongly Reciprocal](./kicad/LICENSE-CERN-OHL-S) license.
embassy-strike-driver/Cargo.toml
embassy-strike-driver/Cargo.toml
This file has not been changed.
+135
-19
embassy-strike-driver/src/lib.rs
+135
-19
embassy-strike-driver/src/lib.rs
···
1
+
//! A generalised Embassy driver for the Strike Sensor v1
2
+
//!
1
3
#![no_std]
2
4
3
5
use core::cell::Cell;
···
25
27
fn set_duty(&mut self, duty: u16);
26
28
}
27
29
30
+
/// A Mutable Buffer trait
28
31
pub trait BufferMut<T> {
29
32
fn push(&mut self, value: T);
30
33
fn clear(&mut self);
···
296
299
#[cfg(test)]
297
300
mod tests {
298
301
use core::future::poll_fn;
299
-
300
302
use embassy_time::MockDriver;
301
303
304
+
extern crate alloc;
302
305
use super::*;
303
306
304
307
#[derive(Debug, Default)]
···
343
346
}
344
347
}
345
348
346
-
async fn tune_detector_manually<'a>(detector: &mut DetectorDriver<MockTimeSource, MockPwm<'a>, MockAdc<'a>>, buf: &mut [u16], driver: &MockDriver) {
349
+
impl BufferMut<usize> for alloc::vec::Vec<usize> {
350
+
fn push(&mut self, value: usize) {
351
+
self.push(value);
352
+
}
353
+
354
+
fn clear(&mut self) {
355
+
self.clear();
356
+
}
357
+
358
+
fn len(&self) -> usize {
359
+
self.len()
360
+
}
361
+
362
+
fn is_empty(&self) -> bool {
363
+
self.is_empty()
364
+
}
365
+
366
+
fn as_slice(&self) -> &[usize] {
367
+
self
368
+
}
369
+
}
370
+
371
+
async fn tune_detector_manually<'a>(
372
+
detector: &mut DetectorDriver<MockTimeSource, MockPwm<'a>, MockAdc<'a>>,
373
+
buf: &mut [u16],
374
+
driver: &MockDriver,
375
+
) {
347
376
let mut tuning = core::pin::pin!(detector.tune(buf));
348
377
349
378
poll_fn(|cx| match tuning.as_mut().poll(cx) {
···
384
413
let pwm = MockPwm(&device);
385
414
let adc = MockAdc(&device);
386
415
387
-
let mut detector = DetectorDriver::new(Default::default(), MockTimeSource, pwm, adc);
388
-
let mut buf = [0; 16];
416
+
let detector = DetectorDriver::new(Default::default(), MockTimeSource, pwm, adc);
389
417
390
-
tune_detector_manually(&mut detector, &mut buf, driver).await;
418
+
detector.state.max_duty.set(98);
419
+
detector.state.duty.set(64);
420
+
detector.state.avg.set(896);
391
421
392
422
{
393
423
let mut tick = core::pin::pin!(detector.tick(|a| {
394
-
assert_eq!(a, DetectorUpdate::Tick { timestamp: 0, level: 300 });
424
+
assert_eq!(
425
+
a,
426
+
DetectorUpdate::Tick {
427
+
timestamp: 0,
428
+
level: 300
429
+
}
430
+
);
395
431
}));
396
432
397
-
poll_fn(|cx| {
398
-
match tick.as_mut().poll(cx) {
399
-
core::task::Poll::Ready(_) => core::task::Poll::Ready(()),
400
-
core::task::Poll::Pending => {
401
-
if detector.state.warn_level.get() > 255 {
402
-
return core::task::Poll::Ready(());
403
-
}
404
-
detector.state.strikes.set(300);
405
-
driver.advance(Duration::from_secs(1));
406
-
cx.waker().wake_by_ref();
407
-
core::task::Poll::Pending
408
-
},
433
+
poll_fn(|cx| match tick.as_mut().poll(cx) {
434
+
core::task::Poll::Ready(_) => core::task::Poll::Ready(()),
435
+
core::task::Poll::Pending => {
436
+
if detector.state.warn_level.get() > 255 {
437
+
return core::task::Poll::Ready(());
438
+
}
439
+
detector.state.strikes.set(300);
440
+
driver.advance(Duration::from_secs(1));
441
+
cx.waker().wake_by_ref();
442
+
core::task::Poll::Pending
409
443
}
410
-
}).await;
444
+
})
445
+
.await;
411
446
}
412
447
413
448
assert_eq!(detector.state.warn_level.get(), 553);
449
+
}
450
+
451
+
#[pollster::test]
452
+
async fn detection_updates_only_on_large_enough_signals() {
453
+
let driver = embassy_time::MockDriver::get();
454
+
driver.reset();
455
+
let device = MockMachine::default();
456
+
let pwm = MockPwm(&device);
457
+
let adc = MockAdc(&device);
458
+
459
+
let detector = DetectorDriver::new(Default::default(), MockTimeSource, pwm, adc);
460
+
detector.pwm.0.pwm.set(64);
461
+
detector.state.max_duty.set(98);
462
+
detector.state.duty.set(64);
463
+
detector.state.avg.set(896);
464
+
465
+
let mut samples = alloc::vec![0; BLOCK_SIZE];
466
+
467
+
detector.adc.sample(&mut samples).await;
468
+
469
+
samples[5] -= 3;
470
+
samples[6] -= 1;
471
+
472
+
let mut peaks = alloc::vec::Vec::with_capacity(512);
473
+
474
+
let update = |_update: DetectorUpdate<'_>| {
475
+
panic!("This update function shouldn't be called");
476
+
};
477
+
478
+
detector.detect_from_sample(0, &samples, &mut peaks, update);
479
+
480
+
assert_eq!(peaks.len(), 0);
481
+
482
+
// Example voltage drop signal
483
+
samples[5] -= 60;
484
+
samples[6] -= 55;
485
+
samples[7] -= 50;
486
+
samples[8] -= 45;
487
+
samples[9] -= 40;
488
+
samples[10] -= 35;
489
+
samples[11] -= 30;
490
+
samples[12] -= 28;
491
+
samples[13] -= 26;
492
+
samples[14] -= 24;
493
+
samples[15] -= 22;
494
+
samples[16] -= 20;
495
+
samples[17] -= 18;
496
+
samples[18] -= 16;
497
+
samples[19] -= 14;
498
+
samples[20] -= 12;
499
+
samples[21] -= 11;
500
+
samples[22] -= 10;
501
+
samples[23] -= 9;
502
+
samples[24] -= 8;
503
+
samples[25] -= 7;
504
+
samples[26] -= 6;
505
+
samples[27] -= 5;
506
+
samples[28] -= 4;
507
+
samples[29] -= 3;
508
+
samples[30] -= 2;
509
+
samples[31] -= 1;
510
+
511
+
let expected_peaks = alloc::vec![0, 8];
512
+
let called = Cell::new(false);
513
+
514
+
let update = |update: DetectorUpdate<'_>| {
515
+
called.set(true);
516
+
assert_eq!(
517
+
update,
518
+
DetectorUpdate::Detection {
519
+
timestamp: 0,
520
+
samples: &samples,
521
+
peaks: expected_peaks.as_slice()
522
+
}
523
+
)
524
+
};
525
+
526
+
detector.detect_from_sample(0, &samples, &mut peaks, update);
527
+
528
+
assert_eq!(peaks.len(), 2);
529
+
assert!(called.get());
414
530
}
415
531
}
History
10 rounds
0 comments
1 commit
expand
collapse
feat: Embassy driver
1/1 success
expand
collapse
expand 0 comments
pull request successfully merged
1 commit
expand
collapse
feat: Embassy driver
1/1 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
feat: Embassy driver
1/1 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
feat: Embassy driver
1/1 failed
expand
collapse
expand 0 comments
1 commit
expand
collapse
feat: Embassy driver
1/1 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
feat: Embassy driver
1/1 success
expand
collapse
expand 0 comments
1 commit
expand
collapse
feat: Embassy driver