Library for the MAX7219 LED display driver

Dependents:   MAXREFDES99_demo MAXREFDES99_RTC_Display nucleo_spi_max7219_led8x8 max7219 ... more

Committer:
j3
Date:
Tue Jan 12 23:40:36 2016 +0000
Revision:
0:798fb343e022
Child:
1:90a7cf4e7d26
Initial commit, still need to add comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:798fb343e022 1 /******************************************************************//**
j3 0:798fb343e022 2 * @file max7219.h
j3 0:798fb343e022 3 *
j3 0:798fb343e022 4 * @author Justin Jordan
j3 0:798fb343e022 5 *
j3 0:798fb343e022 6 * @version 0.0
j3 0:798fb343e022 7 *
j3 0:798fb343e022 8 * Started: 08JAN16
j3 0:798fb343e022 9 *
j3 0:798fb343e022 10 * Updated:
j3 0:798fb343e022 11 *
j3 0:798fb343e022 12 * @brief Header file for Max7219 class
j3 0:798fb343e022 13 ***********************************************************************
j3 0:798fb343e022 14 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:798fb343e022 15 *
j3 0:798fb343e022 16 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:798fb343e022 17 * copy of this software and associated documentation files (the "Software"),
j3 0:798fb343e022 18 * to deal in the Software without restriction, including without limitation
j3 0:798fb343e022 19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:798fb343e022 20 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:798fb343e022 21 * Software is furnished to do so, subject to the following conditions:
j3 0:798fb343e022 22 *
j3 0:798fb343e022 23 * The above copyright notice and this permission notice shall be included
j3 0:798fb343e022 24 * in all copies or substantial portions of the Software.
j3 0:798fb343e022 25 *
j3 0:798fb343e022 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:798fb343e022 27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:798fb343e022 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:798fb343e022 29 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:798fb343e022 30 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:798fb343e022 31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:798fb343e022 32 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:798fb343e022 33 *
j3 0:798fb343e022 34 * Except as contained in this notice, the name of Maxim Integrated
j3 0:798fb343e022 35 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:798fb343e022 36 * Products, Inc. Branding Policy.
j3 0:798fb343e022 37 *
j3 0:798fb343e022 38 * The mere transfer of this software does not imply any licenses
j3 0:798fb343e022 39 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:798fb343e022 40 * trademarks, maskwork rights, or any other form of intellectual
j3 0:798fb343e022 41 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:798fb343e022 42 * ownership rights.
j3 0:798fb343e022 43 **********************************************************************/
j3 0:798fb343e022 44
j3 0:798fb343e022 45
j3 0:798fb343e022 46 #ifndef MAX7219_H
j3 0:798fb343e022 47 #define MAX7219_H
j3 0:798fb343e022 48
j3 0:798fb343e022 49 #include "mbed.h"
j3 0:798fb343e022 50
j3 0:798fb343e022 51
j3 0:798fb343e022 52 typedef struct
j3 0:798fb343e022 53 {
j3 0:798fb343e022 54 uint8_t device_number;
j3 0:798fb343e022 55 uint8_t decode_mode;
j3 0:798fb343e022 56 uint8_t intensity;
j3 0:798fb343e022 57 uint8_t scan_limit;
j3 0:798fb343e022 58 }max7219_configuration_t;
j3 0:798fb343e022 59
j3 0:798fb343e022 60
j3 0:798fb343e022 61 class Max7219
j3 0:798fb343e022 62 {
j3 0:798fb343e022 63 public:
j3 0:798fb343e022 64
j3 0:798fb343e022 65 typedef enum
j3 0:798fb343e022 66 {
j3 0:798fb343e022 67 MAX7219_NO_OP = 0,
j3 0:798fb343e022 68 MAX7219_DIGIT_0,
j3 0:798fb343e022 69 MAX7219_DIGIT_1,
j3 0:798fb343e022 70 MAX7219_DIGIT_2,
j3 0:798fb343e022 71 MAX7219_DIGIT_3,
j3 0:798fb343e022 72 MAX7219_DIGIT_4,
j3 0:798fb343e022 73 MAX7219_DIGIT_5,
j3 0:798fb343e022 74 MAX7219_DIGIT_6,
j3 0:798fb343e022 75 MAX7219_DIGIT_7,
j3 0:798fb343e022 76 MAX7219_DECODE_MODE,
j3 0:798fb343e022 77 MAX7219_INTENSITY,
j3 0:798fb343e022 78 MAX7219_SCAN_LIMIT,
j3 0:798fb343e022 79 MAX7219_SHUTDOWN,
j3 0:798fb343e022 80 MAX7219_DISPLAY_TEST = 15
j3 0:798fb343e022 81 }max7219_register_e;
j3 0:798fb343e022 82
j3 0:798fb343e022 83
j3 0:798fb343e022 84 typedef enum
j3 0:798fb343e022 85 {
j3 0:798fb343e022 86 MAX7219_INTENSITY_0 = 0,
j3 0:798fb343e022 87 MAX7219_INTENSITY_1,
j3 0:798fb343e022 88 MAX7219_INTENSITY_2,
j3 0:798fb343e022 89 MAX7219_INTENSITY_3,
j3 0:798fb343e022 90 MAX7219_INTENSITY_4,
j3 0:798fb343e022 91 MAX7219_INTENSITY_5,
j3 0:798fb343e022 92 MAX7219_INTENSITY_6,
j3 0:798fb343e022 93 MAX7219_INTENSITY_7,
j3 0:798fb343e022 94 MAX7219_INTENSITY_8,
j3 0:798fb343e022 95 MAX7219_INTENSITY_9,
j3 0:798fb343e022 96 MAX7219_INTENSITY_A,
j3 0:798fb343e022 97 MAX7219_INTENSITY_B,
j3 0:798fb343e022 98 MAX7219_INTENSITY_C,
j3 0:798fb343e022 99 MAX7219_INTENSITY_D,
j3 0:798fb343e022 100 MAX7219_INTENSITY_E,
j3 0:798fb343e022 101 MAX7219_INTENSITY_F
j3 0:798fb343e022 102 }max7219_intensity_e;
j3 0:798fb343e022 103
j3 0:798fb343e022 104
j3 0:798fb343e022 105 typedef enum
j3 0:798fb343e022 106 {
j3 0:798fb343e022 107 MAX7219_SCAN_1 = 0,
j3 0:798fb343e022 108 MAX7219_SCAN_2,
j3 0:798fb343e022 109 MAX7219_SCAN_3,
j3 0:798fb343e022 110 MAX7219_SCAN_4,
j3 0:798fb343e022 111 MAX7219_SCAN_5,
j3 0:798fb343e022 112 MAX7219_SCAN_6,
j3 0:798fb343e022 113 MAX7219_SCAN_7,
j3 0:798fb343e022 114 MAX7219_SCAN_8
j3 0:798fb343e022 115 }max7219_scan_limit_e;
j3 0:798fb343e022 116
j3 0:798fb343e022 117
j3 0:798fb343e022 118 /**********************************************************//**
j3 0:798fb343e022 119 * @brief Constructor for Max7219 Class.
j3 0:798fb343e022 120 *
j3 0:798fb343e022 121 * @details Allows user to pass pointer to existing SPI bus
j3 0:798fb343e022 122 *
j3 0:798fb343e022 123 * On Entry:
j3 0:798fb343e022 124 * @param[in] spi_bus - pointer to existing SPI object
j3 0:798fb343e022 125 * @param[in] cs - pin to use for cs
j3 0:798fb343e022 126 *
j3 0:798fb343e022 127 * On Exit:
j3 0:798fb343e022 128 * @return none
j3 0:798fb343e022 129 **************************************************************/
j3 0:798fb343e022 130 Max7219(SPI *spi_bus, PinName cs);
j3 0:798fb343e022 131
j3 0:798fb343e022 132
j3 0:798fb343e022 133 /**********************************************************//**
j3 0:798fb343e022 134 * @brief Constructor for Max7219 Class.
j3 0:798fb343e022 135 *
j3 0:798fb343e022 136 * @details Allows user to specify SPI peripheral to use
j3 0:798fb343e022 137 *
j3 0:798fb343e022 138 * On Entry:
j3 0:798fb343e022 139 * @param[in] mosi - pin to use for mosi
j3 0:798fb343e022 140 * @param[in] miso - pin to use for miso
j3 0:798fb343e022 141 * @param[in] sclk - pin to use for sclk
j3 0:798fb343e022 142 * @param[in] cs - pin to use for cs
j3 0:798fb343e022 143 *
j3 0:798fb343e022 144 * On Exit:
j3 0:798fb343e022 145 * @return none
j3 0:798fb343e022 146 **************************************************************/
j3 0:798fb343e022 147 Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs);
j3 0:798fb343e022 148
j3 0:798fb343e022 149
j3 0:798fb343e022 150 /**********************************************************//**
j3 0:798fb343e022 151 * @brief Default destructor for Max7219 Class.
j3 0:798fb343e022 152 *
j3 0:798fb343e022 153 * @details Destroys SPI object if owner
j3 0:798fb343e022 154 *
j3 0:798fb343e022 155 * On Entry:
j3 0:798fb343e022 156 *
j3 0:798fb343e022 157 * On Exit:
j3 0:798fb343e022 158 * @return none
j3 0:798fb343e022 159 **************************************************************/
j3 0:798fb343e022 160 ~Max7219();
j3 0:798fb343e022 161
j3 0:798fb343e022 162
j3 0:798fb343e022 163 /**********************************************************//**
j3 0:798fb343e022 164 * @brief Sets the number of MAX7219 devices being used.
j3 0:798fb343e022 165 * Defaults to one
j3 0:798fb343e022 166 *
j3 0:798fb343e022 167 * @details
j3 0:798fb343e022 168 *
j3 0:798fb343e022 169 * On Entry:
j3 0:798fb343e022 170 * @param[in] num_devices - number of MAX7219 devices being
j3 0:798fb343e022 171 * used, max of 255
j3 0:798fb343e022 172 *
j3 0:798fb343e022 173 * On Exit:
j3 0:798fb343e022 174 * @return returns number of devices
j3 0:798fb343e022 175 **************************************************************/
j3 0:798fb343e022 176 int32_t set_num_devices(uint8_t num_devices);
j3 0:798fb343e022 177
j3 0:798fb343e022 178
j3 0:798fb343e022 179 /**********************************************************//**
j3 0:798fb343e022 180 * @brief Tests all devices being used
j3 0:798fb343e022 181 *
j3 0:798fb343e022 182 * @details Sets bit0 of DISPLAY_TEST regiser in all devices
j3 0:798fb343e022 183 *
j3 0:798fb343e022 184 * On Entry:
j3 0:798fb343e022 185 *
j3 0:798fb343e022 186 * On Exit:
j3 0:798fb343e022 187 * @return none
j3 0:798fb343e022 188 **************************************************************/
j3 0:798fb343e022 189 void set_display_test(void);
j3 0:798fb343e022 190
j3 0:798fb343e022 191
j3 0:798fb343e022 192 /**********************************************************//**
j3 0:798fb343e022 193 * @brief Stops test
j3 0:798fb343e022 194 *
j3 0:798fb343e022 195 * @details Clear bit0 of DISPLAY_TEST regiser in all devices
j3 0:798fb343e022 196 *
j3 0:798fb343e022 197 * On Entry:
j3 0:798fb343e022 198 *
j3 0:798fb343e022 199 * On Exit:
j3 0:798fb343e022 200 * @return none
j3 0:798fb343e022 201 **************************************************************/
j3 0:798fb343e022 202 void clear_display_test(void);
j3 0:798fb343e022 203
j3 0:798fb343e022 204
j3 0:798fb343e022 205 /**********************************************************//**
j3 0:798fb343e022 206 * @brief initializes specific device in display with given
j3 0:798fb343e022 207 * config data
j3 0:798fb343e022 208 *
j3 0:798fb343e022 209 * @details
j3 0:798fb343e022 210 *
j3 0:798fb343e022 211 * On Entry:
j3 0:798fb343e022 212 *
j3 0:798fb343e022 213 * On Exit:
j3 0:798fb343e022 214 * @return none
j3 0:798fb343e022 215 **************************************************************/
j3 0:798fb343e022 216 int32_t init_device(max7219_configuration_t config, uint8_t device_number);
j3 0:798fb343e022 217
j3 0:798fb343e022 218
j3 0:798fb343e022 219 /**********************************************************//**
j3 0:798fb343e022 220 * @brief initializes all devices with given config data
j3 0:798fb343e022 221 *
j3 0:798fb343e022 222 * @details
j3 0:798fb343e022 223 *
j3 0:798fb343e022 224 * On Entry:
j3 0:798fb343e022 225 *
j3 0:798fb343e022 226 * On Exit:
j3 0:798fb343e022 227 * @return none
j3 0:798fb343e022 228 **************************************************************/
j3 0:798fb343e022 229 void init_display(max7219_configuration_t config);
j3 0:798fb343e022 230
j3 0:798fb343e022 231
j3 0:798fb343e022 232 /**********************************************************//**
j3 0:798fb343e022 233 * @brief enables specific device in display
j3 0:798fb343e022 234 *
j3 0:798fb343e022 235 * @details
j3 0:798fb343e022 236 *
j3 0:798fb343e022 237 * On Entry:
j3 0:798fb343e022 238 *
j3 0:798fb343e022 239 * On Exit:
j3 0:798fb343e022 240 * @return none
j3 0:798fb343e022 241 **************************************************************/
j3 0:798fb343e022 242 int32_t enable_device(uint8_t device_number);
j3 0:798fb343e022 243
j3 0:798fb343e022 244
j3 0:798fb343e022 245 /**********************************************************//**
j3 0:798fb343e022 246 * @brief enables all device in display
j3 0:798fb343e022 247 *
j3 0:798fb343e022 248 * @details
j3 0:798fb343e022 249 *
j3 0:798fb343e022 250 * On Entry:
j3 0:798fb343e022 251 *
j3 0:798fb343e022 252 * On Exit:
j3 0:798fb343e022 253 * @return none
j3 0:798fb343e022 254 **************************************************************/
j3 0:798fb343e022 255 void enable_display(void);
j3 0:798fb343e022 256
j3 0:798fb343e022 257
j3 0:798fb343e022 258 /**********************************************************//**
j3 0:798fb343e022 259 * @brief disables specific device in display
j3 0:798fb343e022 260 *
j3 0:798fb343e022 261 * @details
j3 0:798fb343e022 262 *
j3 0:798fb343e022 263 * On Entry:
j3 0:798fb343e022 264 *
j3 0:798fb343e022 265 * On Exit:
j3 0:798fb343e022 266 * @return none
j3 0:798fb343e022 267 **************************************************************/
j3 0:798fb343e022 268 int32_t disable_device(uint8_t device_number);
j3 0:798fb343e022 269
j3 0:798fb343e022 270
j3 0:798fb343e022 271 /**********************************************************//**
j3 0:798fb343e022 272 * @brief disables all devices in display
j3 0:798fb343e022 273 *
j3 0:798fb343e022 274 * @details
j3 0:798fb343e022 275 *
j3 0:798fb343e022 276 * On Entry:
j3 0:798fb343e022 277 *
j3 0:798fb343e022 278 * On Exit:
j3 0:798fb343e022 279 * @return none
j3 0:798fb343e022 280 **************************************************************/
j3 0:798fb343e022 281 void disable_display(void);
j3 0:798fb343e022 282
j3 0:798fb343e022 283
j3 0:798fb343e022 284 /**********************************************************//**
j3 0:798fb343e022 285 * @brief writes digit of given device with given data, user
j3 0:798fb343e022 286 * must enter correct data for decode_mode chosen
j3 0:798fb343e022 287 *
j3 0:798fb343e022 288 * @details
j3 0:798fb343e022 289 *
j3 0:798fb343e022 290 * On Entry:
j3 0:798fb343e022 291 *
j3 0:798fb343e022 292 * On Exit:
j3 0:798fb343e022 293 * @return none
j3 0:798fb343e022 294 **************************************************************/
j3 0:798fb343e022 295 int32_t write_digit(uint8_t device_number, uint8_t digit, uint8_t data);
j3 0:798fb343e022 296
j3 0:798fb343e022 297
j3 0:798fb343e022 298 /**********************************************************//**
j3 0:798fb343e022 299 * @brief clears digit of given device
j3 0:798fb343e022 300 *
j3 0:798fb343e022 301 * @details
j3 0:798fb343e022 302 *
j3 0:798fb343e022 303 * On Entry:
j3 0:798fb343e022 304 *
j3 0:798fb343e022 305 * On Exit:
j3 0:798fb343e022 306 * @return none
j3 0:798fb343e022 307 **************************************************************/
j3 0:798fb343e022 308 int32_t clear_digit(uint8_t device_number, uint8_t digit);
j3 0:798fb343e022 309
j3 0:798fb343e022 310
j3 0:798fb343e022 311 /**********************************************************//**
j3 0:798fb343e022 312 * @brief turns on all segments/digits of given device
j3 0:798fb343e022 313 *
j3 0:798fb343e022 314 * @details
j3 0:798fb343e022 315 *
j3 0:798fb343e022 316 * On Entry:
j3 0:798fb343e022 317 *
j3 0:798fb343e022 318 * On Exit:
j3 0:798fb343e022 319 * @return none
j3 0:798fb343e022 320 **************************************************************/
j3 0:798fb343e022 321 int32_t device_all_on(uint8_t device_number);
j3 0:798fb343e022 322
j3 0:798fb343e022 323
j3 0:798fb343e022 324 /**********************************************************//**
j3 0:798fb343e022 325 * @brief turns off all segments/digits of given device
j3 0:798fb343e022 326 *
j3 0:798fb343e022 327 * @details
j3 0:798fb343e022 328 *
j3 0:798fb343e022 329 * On Entry:
j3 0:798fb343e022 330 *
j3 0:798fb343e022 331 * On Exit:
j3 0:798fb343e022 332 * @return none
j3 0:798fb343e022 333 **************************************************************/
j3 0:798fb343e022 334 int32_t device_all_off(uint8_t device_number);
j3 0:798fb343e022 335
j3 0:798fb343e022 336
j3 0:798fb343e022 337 /**********************************************************//**
j3 0:798fb343e022 338 * @brief turns on all segments/digits of display
j3 0:798fb343e022 339 *
j3 0:798fb343e022 340 * @details
j3 0:798fb343e022 341 *
j3 0:798fb343e022 342 * On Entry:
j3 0:798fb343e022 343 *
j3 0:798fb343e022 344 * On Exit:
j3 0:798fb343e022 345 * @return none
j3 0:798fb343e022 346 **************************************************************/
j3 0:798fb343e022 347 void display_all_on(void);
j3 0:798fb343e022 348
j3 0:798fb343e022 349
j3 0:798fb343e022 350 /**********************************************************//**
j3 0:798fb343e022 351 * @brief turns off all segments/digits of display
j3 0:798fb343e022 352 *
j3 0:798fb343e022 353 * @details
j3 0:798fb343e022 354 *
j3 0:798fb343e022 355 * On Entry:
j3 0:798fb343e022 356 *
j3 0:798fb343e022 357 * On Exit:
j3 0:798fb343e022 358 * @return none
j3 0:798fb343e022 359 **************************************************************/
j3 0:798fb343e022 360 void display_all_off(void);
j3 0:798fb343e022 361
j3 0:798fb343e022 362
j3 0:798fb343e022 363 private:
j3 0:798fb343e022 364
j3 0:798fb343e022 365 SPI *_p_spi;
j3 0:798fb343e022 366 DigitalOut *_p_cs;
j3 0:798fb343e022 367 bool _spi_owner;
j3 0:798fb343e022 368
j3 0:798fb343e022 369 uint8_t _num_devices;
j3 0:798fb343e022 370
j3 0:798fb343e022 371 };
j3 0:798fb343e022 372 #endif /* MAX7219_H*/