1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::{
    driver::Driver,
    modules::{
        adc::AdcModule,
        encoder::EncoderModule,
        gpio::{GpioModule, PinMode},
        neopixel::NeopixelModule,
        status::StatusModule,
        timer::TimerModule,
    },
    seesaw_device, HardwareId, SeesawDeviceInit,
};

/// All devices implement the status module
impl<D: Driver, T: super::SeesawDevice<Driver = D>> StatusModule<D> for T {}

seesaw_device! {
    #[doc(hidden)]
    name: GenericDevice,
    hardware_id: HardwareId::SAMD09,
    product_id: 0,
    default_addr: 0x49,
    modules: []
}

impl<D: Driver> SeesawDeviceInit<D> for GenericDevice<D> {
    fn init(mut self) -> Result<Self, Self::Error> {
        self.reset().map(|_| self)
    }
}

seesaw_device! {
    /// ArcadeButton1x4
    ///
    /// Button | Pin | GPIO
    /// ---|---|---
    /// SW1 | 18 | PA01
    /// SW2 | 19 | PA02
    /// SW3 | 20 | PA03
    /// SW4 | 2 | PA06
    ///
    /// LED | PIN | GPIO
    /// ---|---|---
    /// PWM1 | 12 | PC00
    /// PWM2 | 13 | PC01
    /// PWM3 | 0 | PA04
    /// PWM4 | 1 | PA05
    ///
    name: ArcadeButton1x4,
    hardware_id: HardwareId::ATTINY817,
    product_id: 5296,
    default_addr: 0x3A,
    modules: [
        GpioModule,
        TimerModule
    ]
}

impl<D: Driver> SeesawDeviceInit<D> for ArcadeButton1x4<D> {
    fn init(mut self) -> Result<Self, Self::Error> {
        self.reset_and_verify_seesaw()
            .and_then(|_| self.enable_buttons())
            .map(|_| self)
    }
}

impl<D: Driver> ArcadeButton1x4<D> {
    pub fn button_values(&mut self) -> Result<[bool; 4], crate::SeesawError<D::I2cError>> {
        [18, 19, 20, 2].try_map(|pin| self.digital_read(pin))
    }

    /// Set the pin mode of the 4 buttons to input pullup:
    pub fn enable_buttons(&mut self) -> Result<(), crate::SeesawError<D::I2cError>> {
        self.set_pin_mode(18, PinMode::InputPullup)?;
        self.set_pin_mode(19, PinMode::InputPullup)?;
        self.set_pin_mode(20, PinMode::InputPullup)?;
        self.set_pin_mode(2, PinMode::InputPullup)?;
        Ok(())
    }

    pub fn set_led_duty_cycles(
        &mut self,
        pwms: &[u8; 4],
    ) -> Result<(), crate::SeesawError<D::I2cError>> {
        [12u8, 13, 0, 1]
            .iter()
            .enumerate()
            .try_for_each(|(i, &pin)| self.analog_write(pin, pwms[i]))
    }
}

seesaw_device! {
    /// NeoKey1x4
    name: NeoKey1x4,
    hardware_id: HardwareId::SAMD09,
    product_id: 4980,
    default_addr: 0x30,
    modules: [
        GpioModule,
        NeopixelModule { num_leds: 4, pin: 3 },
    ]
}

impl<D: Driver> SeesawDeviceInit<D> for NeoKey1x4<D> {
    fn init(mut self) -> Result<Self, Self::Error> {
        self.reset_and_verify_seesaw()
            .and_then(|_| self.enable_neopixel())
            .and_then(|_| self.enable_button_pins())
            .map(|_| self)
    }
}

impl<D: Driver> NeoKey1x4<D> {
    pub fn enable_button_pins(&mut self) -> Result<(), crate::SeesawError<D::I2cError>> {
        self.set_pin_mode_bulk(
            (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7),
            PinMode::InputPullup,
        )
    }

    pub fn keys(&mut self) -> Result<u8, crate::SeesawError<D::I2cError>> {
        self.digital_read_bulk().map(|r| (r >> 4 & 0xF) as u8)
    }
}

seesaw_device!(
    /// NeoSlider
    name: NeoSlider,
    hardware_id: HardwareId::ATTINY817,
    product_id: 5295,
    default_addr: 0x30,
    modules: [
        AdcModule,
        GpioModule,
        NeopixelModule { num_leds: 4, pin: 14},
    ]
);

impl<D: Driver> SeesawDeviceInit<D> for NeoSlider<D> {
    fn init(mut self) -> Result<Self, Self::Error> {
        self.reset_and_verify_seesaw()
            .and_then(|_| self.enable_neopixel())
            .map(|_| self)
    }
}

impl<D: Driver> NeoSlider<D> {
    pub fn slider_value(&mut self) -> Result<u16, crate::SeesawError<D::I2cError>> {
        self.analog_read(18)
    }
}

seesaw_device! {
    /// RotaryEncoder
    name: RotaryEncoder,
    hardware_id: HardwareId::SAMD09,
    product_id: 4991,
    default_addr: 0x36,
    modules:  [
        EncoderModule { button_pin: 24 },
        GpioModule,
        NeopixelModule { num_leds: 1, pin: 6 },
    ]
}

impl<D: Driver> SeesawDeviceInit<D> for RotaryEncoder<D> {
    fn init(mut self) -> Result<Self, Self::Error> {
        self.reset_and_verify_seesaw()
            .and_then(|_| self.enable_button())
            .and_then(|_| self.enable_neopixel())
            .map(|_| self)
    }
}