more efficient pwm wave access

This commit is contained in:
Alex Janka 2023-03-09 11:20:14 +11:00
parent 4d2f84e183
commit 66f7518bb0

View file

@ -95,13 +95,18 @@ impl From<u8> for DutyCycle {
} }
} }
const TWELVE_POINT_FIVE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 1];
const TWENTY_FIVE: [u8; 8] = [1, 0, 0, 0, 0, 0, 0, 1];
const FIFTY: [u8; 8] = [1, 0, 0, 0, 0, 1, 1, 1];
const SEVENTY_FIVE: [u8; 8] = [0, 1, 1, 1, 1, 1, 1, 0];
impl DutyCycle { impl DutyCycle {
fn as_waveform(&self) -> [u8; 8] { fn get_waveform(&self, index: usize) -> u8 {
match self { match self {
DutyCycle::TwelvePointFive => [0, 0, 0, 0, 0, 0, 0, 1], DutyCycle::TwelvePointFive => TWELVE_POINT_FIVE[index],
DutyCycle::TwentyFive => [1, 0, 0, 0, 0, 0, 0, 1], DutyCycle::TwentyFive => TWENTY_FIVE[index],
DutyCycle::Fifty => [1, 0, 0, 0, 0, 1, 1, 1], DutyCycle::Fifty => FIFTY[index],
DutyCycle::SeventyFive => [0, 1, 1, 1, 1, 1, 1, 0], DutyCycle::SeventyFive => SEVENTY_FIVE[index],
} }
} }
@ -167,7 +172,7 @@ impl PwmChannel {
self.wave_position = (self.wave_position + 1) % 8; self.wave_position = (self.wave_position + 1) % 8;
self.set_wave_timer(); self.set_wave_timer();
} }
self.dac(self.duty_cycle.as_waveform()[self.wave_position]) self.dac(self.duty_cycle.get_waveform(self.wave_position))
}) })
.collect() .collect()
} else { } else {