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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! A platform agnostic driver to interface with MAX6955 LED Display Driver
//!
//! This driver was built using [`embedded-hal`] traits.
//!
//! [`embedded-hal`]: https://docs.rs/embedded-hal/~0.2
//!
//! ### Datasheets
//! - [MAX6955](https://datasheets.maximintegrated.com/en/ds/MAX6955.pdf)
//!

// #![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

extern crate bit_field;
extern crate embedded_hal as hal;

use bit_field::BitField;
use hal::blocking::i2c::{Write, WriteRead};

/// Default address
pub const DEFAULT_SLAVE_ADDR: u8 = 0x60;

/// MAX6955 driver
pub struct Max6955<I2C> {
    i2c: I2C,
    addr: u8,
}

impl<I2C, E> Max6955<I2C>
where
    I2C: WriteRead<Error = E> + Write<Error = E>,
{
    /// Construct a new MAX6955 driver instance with I2C peripheral and default address of `0x60`.
    ///
    /// # Arguments
    ///
    /// * `i2c` - I2C interface
    ///
    /// # Errors
    ///
    /// * `E` - returned in case there was an error reading/writing to the device
    ///
    pub fn new(i2c: I2C) -> Result<Self, E> {
        let max6955 = Max6955 {
            i2c,
            addr: DEFAULT_SLAVE_ADDR,
        };
        Ok(max6955)
    }

    /// Construct a new MAX6955 driver instance with I2C peripheral and address.
    ///
    /// # Arguments
    ///
    /// * `i2c` - I2C interface
    /// * `addr` - device address. This can be `0x60` ~ `0x6F`. See table 5 in the datasheet.
    ///
    /// # Errors
    ///
    /// * `E` - returned in case there was an error reading/writing to the device
    ///
    pub fn with_address(i2c: I2C, addr: u8) -> Result<Self, E> {
        let max6955 = Max6955 { i2c, addr };
        Ok(max6955)
    }

    /// Set device address
    /// # Arguments
    ///
    /// * `addr` - device address. This can be `0x60` ~ `0x6F`. See table 5 in the datasheet.
    pub fn set_address(&mut self, addr: u8) {
        self.addr = addr;
    }

    /// Set Global Intensity
    /// # Arguments
    ///
    /// * `intensity` - intensity level `0`: lowest ~ `15`: highest
    pub fn set_global_intensity(&mut self, intensity: u8) -> Result<(), E> {
        self.write_register(Register::GlobalIntensity, intensity)?;
        Ok(())
    }

    /// Control Blinking
    /// # Arguments
    ///
    /// * `mode` - `BlinkMode::Enable`: blink, `BlinkMode::Disable`: not blink
    /// * `rate` - `BlinkRate::Fast`: 0.5s cycle, `BlinkRate::Slow`: 1.0s cycle
    pub fn set_blink(&mut self, mode: BlinkMode, rate: BlinkRate) -> Result<(), E> {
        self.set_configuration_bit(ConfigBitFlag::Blink, mode.value())?;
        self.set_configuration_bit(ConfigBitFlag::BlinkRate, rate.value())
    }

    /// Power up Display
    pub fn powerup(&mut self) -> Result<(), E> {
        self.set_configuration_bit(ConfigBitFlag::Shutdown, true)
    }

    /// Shutdown Display
    pub fn shutdown(&mut self) -> Result<(), E> {
        self.set_configuration_bit(ConfigBitFlag::Shutdown, false)
    }

    /// Configure Digit Type
    /// # Arguments
    ///
    /// * `digit_type` - one of four `DigitType`s
    pub fn set_digit_type(&mut self, digit_type: DigitType) -> Result<(), E> {
        self.write_register(Register::DigitType, digit_type.value())
    }

    /// Configure Pin Mode
    /// # Arguments
    ///
    /// * `port` - `0` ~ `4`
    /// * `pin_mode`
    pub fn set_pin_mode(&mut self, port: usize, pin_mode: PinMode) -> Result<(), E> {
        let mut port_config: u8 = self.read_register(Register::PortConfiguration)?;
        let config = match pin_mode {
            PinMode::Input => *port_config.set_bit(port, true),
            PinMode::Output => *port_config.set_bit(port, false),
        };
        self.write_register(Register::PortConfiguration, config)
    }

    /// Configure Decode Mode
    /// # Arguments
    /// * `mode` - `DecodeMode`
    pub fn set_decode_mode(&mut self, mode: DecodeMode) -> Result<(), E> {
        self.write_register(Register::DecodeMode, mode.value())
    }

    /// Display Test function
    /// # Arguments
    /// * `enable` - `true`: enable test
    pub fn test(&mut self, enable: bool) -> Result<(), E> {
        if enable {
            self.write_register(Register::DisplayTest, 0x01)
        } else {
            self.write_register(Register::DisplayTest, 0x00)
        }
    }

    /// Clear Display
    pub fn clear_display(&mut self) -> Result<(), E> {
        self.write_str("")
    }

    /// Write Text
    /// # Arguments
    /// * `text` - text to write
    pub fn write_str(&mut self, text: &str) -> Result<(), E> {
        let mut row: [u8; 9] = [b' '; 9];
        row[0] = Register::Digit0Plane0.addr();
        for (i, c) in text.chars().enumerate() {
            row[i + 1] = match c {
                ' '..='~' => c as u8,
                _ => b' ',
            }
        }
        self.i2c.write(self.addr, &row)
    }

    fn write_register(&mut self, reg: Register, byte: u8) -> Result<(), E> {
        self.i2c.write(self.addr, &[reg.addr(), byte])
    }

    fn set_configuration_bit(&mut self, bit: ConfigBitFlag, set: bool) -> Result<(), E> {
        let mut config: u8 = self.read_register(Register::Configuration)?;
        config.set_bit(bit.value(), set);
        self.write_register(Register::Configuration, config)
    }

    fn read_register(&mut self, reg: Register) -> Result<u8, E> {
        let mut buffer: [u8; 8] = [0; 8];
        self.read_registers(reg, &mut buffer)?;
        Ok(buffer[0])
    }

    fn read_registers(&mut self, reg: Register, buffer: &mut [u8; 8]) -> Result<(), E> {
        self.i2c.write_read(self.addr, &[reg.addr()], buffer)
    }
}

/// Register address. see Table 7
pub enum Register {
    NoOp = 0x00,
    DecodeMode = 0x01,
    GlobalIntensity = 0x02,
    ScanLimit = 0x03,
    Configuration = 0x04,
    GpioData = 0x05,
    PortConfiguration = 0x06,
    DisplayTest = 0x07,
    KeyAMaskDebounce = 0x08,
    KeyBMaskDebounce = 0x09,
    KeyCMaskDebounce = 0x0A,
    KeyDMaskDebounce = 0x0B,
    DigitType = 0x0C,
    KeyBPressed = 0x0D,
    KeyCPressed = 0x0E,
    KeyDPressed = 0x0F,
    Intensity10 = 0x10,
    Intensity32 = 0x11,
    Intensity54 = 0x12,
    Intensity76 = 0x13,
    Intensity10a = 0x14,
    Intensity32a = 0x15,
    Intensity54a = 0x16,
    Intensity76a = 0x17,
    Digit0Plane0 = 0x20,
    Digit1Plane0 = 0x21,
    Digit2Plane0 = 0x22,
    Digit3Plane0 = 0x23,
    Digit4Plane0 = 0x24,
    Digit5Plane0 = 0x25,
    Digit6Plane0 = 0x26,
    Digit7Plane0 = 0x27,
    Digit0Plane1 = 0x40,
    Digit1Plane1 = 0x41,
    Digit2Plane1 = 0x42,
    Digit3Plane1 = 0x43,
    Digit4Plane1 = 0x44,
    Digit5Plane1 = 0x45,
    Digit6Plane1 = 0x46,
    Digit7Plane1 = 0x47,
}

impl Register {
    /// return register address as u8
    pub fn addr(self) -> u8 {
        self as u8
    }
}

/// Configuration Register bits. see Table 17
pub enum ConfigBitFlag {
    Shutdown = 0x00,
    BlinkRate = 0x02,
    Blink = 0x03,
    BlinkTiming = 0x04,
    ClearDigit = 0x05,
    Intensity = 0x06,
    BlinkPhase = 0x07,
}

impl ConfigBitFlag {
    /// return enum value as usize
    pub fn value(self) -> usize {
        self as usize
    }
}

/// Display Digit Configuration. see Table 14
pub enum DigitType {
    /// Digits 7 to 0 are 16-segment or 7- segment digits.
    Seg7_16 = 0x00,
    /// Digit 0 is a 14-segment digit, digits 7 to 1 are 16-segment or 7- segment digits.
    D0_14 = 0x01,
    /// Digits 2 to 0 are 14-segment digits, digits 7 to 3 are 16- segment or 7-segment digits.
    D0D2_14 = 0x07,
    /// Digits 7 to 0 are 14-segment digits.
    Seg14 = 0xFF,
}

impl DigitType {
    /// return enum value as u8
    pub fn value(self) -> u8 {
        self as u8
    }
}

/// Decode Mode. see Table 15
pub enum DecodeMode {
    /// No decode for digit pairs 7 to 0.
    NoDecode = 0x00,
    /// Hexadecimal decode for digit pair 0, no decode for digit pairs 7 to 1.
    HexD0 = 0x01,
    /// Hexadecimal decode for digit pairs 2 to 0, no decode for digit pairs 7 to 3.
    HexD0D2 = 0x07,
    /// Hexadecimal decode for digit pairs 7 to 0.
    Hex = 0xFF,
}

impl DecodeMode {
    /// return enum value as u8
    pub fn value(self) -> u8 {
        self as u8
    }
}

/// Pin Mode Input/Output
pub enum PinMode {
    Input,
    Output,
}

/// Blink Mode Enable/Disable
pub enum BlinkMode {
    Disable,
    Enable,
}

impl BlinkMode {
    /// return enum value as bool
    pub fn value(self) -> bool {
        match self {
            BlinkMode::Disable => false,
            BlinkMode::Enable => true,
        }
    }
}

/// Blink Rate Fast/Slow
pub enum BlinkRate {
    Fast,
    Slow,
}

impl BlinkRate {
    /// return enum value as bool
    pub fn value(self) -> bool {
        match self {
            BlinkRate::Slow => false,
            BlinkRate::Fast => true,
        }
    }
}