using iterator for scanning
This commit is contained in:
97
src/main.rs
97
src/main.rs
@@ -2,10 +2,17 @@
|
||||
#![no_main]
|
||||
|
||||
use arduino_hal::{
|
||||
adc::channel::{ADC6, ADC7},
|
||||
hal::port::{
|
||||
PB0, PB1, PB2, PB3, PB4, PC0, PC1, PC2, PC3, PC4, PC5, PD2, PD3, PD4, PD5, PD6, PD7,
|
||||
adc::{
|
||||
channel::{ADC6, ADC7},
|
||||
AdcChannel,
|
||||
},
|
||||
hal::{
|
||||
port::{
|
||||
PB0, PB1, PB2, PB3, PB4, PC0, PC1, PC2, PC3, PC4, PC5, PD2, PD3, PD4, PD5, PD6, PD7,
|
||||
},
|
||||
Atmega,
|
||||
},
|
||||
pac::ADC,
|
||||
port::{mode::Output, Pin},
|
||||
prelude::*,
|
||||
Adc,
|
||||
@@ -118,6 +125,20 @@ impl SegmentPins {
|
||||
}
|
||||
}
|
||||
|
||||
struct IOSelect<'p> {
|
||||
display_no: usize,
|
||||
pin: &'p mut Pin<Output>,
|
||||
}
|
||||
|
||||
impl IOSelect<'_> {
|
||||
fn set_on(&mut self) {
|
||||
self.pin.set_high()
|
||||
}
|
||||
fn set_off(&mut self) {
|
||||
self.pin.set_low()
|
||||
}
|
||||
}
|
||||
|
||||
struct IOPins([Pin<Output>; 9]);
|
||||
|
||||
impl IOPins {
|
||||
@@ -145,23 +166,11 @@ impl IOPins {
|
||||
])
|
||||
}
|
||||
|
||||
fn scan_output(&mut self, mut segment: impl FnMut(usize) -> ()) {
|
||||
for (no, d) in self.0.iter_mut().enumerate() {
|
||||
segment(no);
|
||||
d.set_high();
|
||||
arduino_hal::delay_ms(1);
|
||||
d.set_low();
|
||||
}
|
||||
}
|
||||
|
||||
fn scan_input(&mut self, mut segment: impl FnMut(usize) -> ()) {
|
||||
for (no, d) in self.0.iter_mut().enumerate() {
|
||||
d.set_high();
|
||||
arduino_hal::delay_ms(1);
|
||||
segment(no);
|
||||
arduino_hal::delay_ms(1);
|
||||
d.set_low();
|
||||
}
|
||||
fn iter_mut(&mut self) -> impl Iterator<Item = IOSelect> {
|
||||
self.0
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.map(|(display_no, pin)| IOSelect { display_no, pin })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,35 +285,24 @@ impl Default for DispalyState {
|
||||
}
|
||||
}
|
||||
|
||||
impl DispalyState {
|
||||
fn scan(&self, dp: &mut IOPins, seg: &mut SegmentPins) {
|
||||
dp.scan_output(|no| {
|
||||
self.0[no].apply(seg);
|
||||
});
|
||||
seg.set_off();
|
||||
}
|
||||
}
|
||||
|
||||
struct Input<KN, KO> {
|
||||
kn: KN,
|
||||
ko: KO,
|
||||
}
|
||||
|
||||
impl<KN, KO> Input<KN, KO> {
|
||||
impl<KN: AdcChannel<Atmega, ADC>, KO: AdcChannel<Atmega, ADC>> Input<KN, KO> {
|
||||
fn new(kn: KN, ko: KO) -> Input<KN, KO> {
|
||||
Input { kn, ko }
|
||||
}
|
||||
|
||||
fn scan(&self, io: &mut IOPins, adc: &mut Adc) -> Option<(usize, u16, u16)> {
|
||||
let mut out = None;
|
||||
io.scan_input(|no| {
|
||||
let kn = adc.read_blocking(&ADC7);
|
||||
let ko = adc.read_blocking(&ADC6);
|
||||
if kn > 500 || ko > 500 {
|
||||
out = Some((no, kn, ko));
|
||||
}
|
||||
});
|
||||
out
|
||||
fn read(&self, adc: &mut Adc) -> Option<(u16, u16)> {
|
||||
let kn = adc.read_blocking(&self.kn);
|
||||
let ko = adc.read_blocking(&self.ko);
|
||||
if kn > 500 || ko > 500 {
|
||||
Some((kn, ko))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,9 +357,22 @@ fn main() -> ! {
|
||||
display.0[8] = SegmentState::Num(8, false);
|
||||
|
||||
loop {
|
||||
display.scan(&mut io, &mut seg);
|
||||
arduino_hal::delay_ms(10);
|
||||
if let Some((no, kn, ko)) = input.scan(&mut io, &mut adc) {
|
||||
let mut key: Option<(usize, u16, u16)> = None;
|
||||
for (mut io_select, ss) in io.iter_mut().zip(display.0.iter()) {
|
||||
ss.apply(&mut seg);
|
||||
io_select.set_on();
|
||||
arduino_hal::delay_ms(1);
|
||||
io_select.set_off();
|
||||
seg.set_off();
|
||||
io_select.set_on();
|
||||
arduino_hal::delay_ms(1);
|
||||
if let Some((kn, ko)) = input.read(&mut adc) {
|
||||
key = Some((io_select.display_no, kn, ko))
|
||||
}
|
||||
io_select.set_off();
|
||||
}
|
||||
|
||||
if let Some((no, kn, ko)) = key {
|
||||
ufmt::uwriteln!(&mut serial, "no: {} kn: {} ko: {}", no, kn, ko).unwrap_infallible();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user