autoselect IC range

This commit is contained in:
Sebastien Bourdeauducq 2017-05-13 15:40:41 +08:00
parent dc4534eb00
commit 246839f153
1 changed files with 54 additions and 13 deletions

View File

@ -2,6 +2,8 @@ use board;
pub struct Electrometer { pub struct Electrometer {
range: board::ElectrometerRange, range: board::ElectrometerRange,
out_of_range_count: u8,
ignore_count: u8,
ic_buffer: [f32; 64], ic_buffer: [f32; 64],
ic_buffer_count: usize, ic_buffer_count: usize,
last_ic: Option<f32> last_ic: Option<f32>
@ -16,6 +18,8 @@ impl Electrometer {
pub const fn new() -> Electrometer { pub const fn new() -> Electrometer {
Electrometer { Electrometer {
range: board::ElectrometerRange::Med, range: board::ElectrometerRange::Med,
out_of_range_count: 0,
ignore_count: 0,
ic_buffer: [0.0; 64], ic_buffer: [0.0; 64],
ic_buffer_count: 0, ic_buffer_count: 0,
last_ic: None last_ic: None
@ -23,6 +27,41 @@ impl Electrometer {
} }
pub fn adc_input(&mut self, ic_sample: u16) { pub fn adc_input(&mut self, ic_sample: u16) {
if self.ignore_count > 0 {
self.ignore_count -= 1;
} else {
let mut new_range = if ic_sample > 3350 {
match self.range {
board::ElectrometerRange::Low => Some(board::ElectrometerRange::Med),
board::ElectrometerRange::Med => Some(board::ElectrometerRange::High),
board::ElectrometerRange::High => None
}
} else if ic_sample < 400 {
match self.range {
board::ElectrometerRange::Low => None,
board::ElectrometerRange::Med => Some(board::ElectrometerRange::Low),
board::ElectrometerRange::High => Some(board::ElectrometerRange::Med)
}
} else {
None
};
if new_range.is_some() {
self.out_of_range_count += 1;
if self.out_of_range_count < 10 {
new_range = None;
}
} else {
self.out_of_range_count = 0;
}
if new_range.is_some() {
self.ignore_count = 20;
self.ic_buffer_count = 0;
self.last_ic = None;
self.range = new_range.unwrap();
board::set_electrometer_range(self.range);
} else {
let gain = match self.range { let gain = match self.range {
board::ElectrometerRange::Low => board::IC_ADC_GAIN_LOW, board::ElectrometerRange::Low => board::IC_ADC_GAIN_LOW,
board::ElectrometerRange::Med => board::IC_ADC_GAIN_MED, board::ElectrometerRange::Med => board::IC_ADC_GAIN_MED,
@ -39,6 +78,8 @@ impl Electrometer {
self.ic_buffer_count = 0; self.ic_buffer_count = 0;
} }
} }
}
}
pub fn get_status(&self) -> ElectrometerStatus { pub fn get_status(&self) -> ElectrometerStatus {
ElectrometerStatus { ElectrometerStatus {