adafruit_seesaw/modules/timer.rs
1use super::{Modules, Reg};
2use crate::{devices::SeesawDevice, Driver, DriverExt, modules::HardwareId, SeesawError};
3
4/// WO - 16 bits
5/// The first byte of the register indicates which PWM pin will have its value
6/// set The second byte is the actual PWM value
7const PWM_VAL: &Reg = &[Modules::Timer.into_u8(), 0x01];
8
9/// The PWM module provides up to 4 8-bit PWM outputs.
10/// The module base register address for the PWM module is 0x08.
11/// PWM outputs are available on pins PA04, PA05, PA06, and PA07.
12pub trait TimerModule<D: Driver>: SeesawDevice<Driver = D> {
13    /// Write a PWM value to a PWM-enabled pin
14    ///
15    /// On the SAMD09 breakout, the pin corresponds to the number on the
16    /// silkscreen. On the default seesaw firmware on the SAMD09 breakout,
17    /// pins 5, 6, and 7 are PWM enabled.
18    fn analog_write(&mut self, pin: u8, value: u8) -> Result<(), SeesawError<D::Error>> {
19        let mapped_pin = match Self::HARDWARE_ID {
20            HardwareId::SAMD09 => match pin {
21                4 => 0,
22                5 => 1,
23                6 => 2,
24                7 => 3,
25                _ => 0,
26            },
27            _ => pin,
28        };
29
30        let addr = self.addr();
31        self.driver()
32            .write_u16(addr, PWM_VAL, u16::from_be_bytes([mapped_pin, value]))
33            .map_err(SeesawError::I2c)
34    }
35}