Library for the MAX7219 LED display driver

Dependents:   MAXREFDES99_demo MAXREFDES99_RTC_Display nucleo_spi_max7219_led8x8 max7219 ... more

Committer:
j3
Date:
Tue Jan 26 02:17:20 2016 +0000
Revision:
1:90a7cf4e7d26
Parent:
0:798fb343e022
Child:
2:9150a0dc77a3
Updated comments in header file

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 1:90a7cf4e7d26 212 * @param[in] config - Structure containing configuration
j3 1:90a7cf4e7d26 213 * data of device
j3 0:798fb343e022 214 *
j3 0:798fb343e022 215 * On Exit:
j3 1:90a7cf4e7d26 216 * @return returns 0 on success
j3 1:90a7cf4e7d26 217 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 218 * returns -2 if device number is 0
j3 0:798fb343e022 219 **************************************************************/
j3 1:90a7cf4e7d26 220 int32_t init_device(max7219_configuration_t config);
j3 0:798fb343e022 221
j3 0:798fb343e022 222
j3 0:798fb343e022 223 /**********************************************************//**
j3 0:798fb343e022 224 * @brief initializes all devices with given config data
j3 0:798fb343e022 225 *
j3 1:90a7cf4e7d26 226 * @details all devices are configured with given data
j3 0:798fb343e022 227 *
j3 0:798fb343e022 228 * On Entry:
j3 1:90a7cf4e7d26 229 * @param[in] config - Structure containing configuration
j3 1:90a7cf4e7d26 230 * data
j3 0:798fb343e022 231 * On Exit:
j3 0:798fb343e022 232 * @return none
j3 0:798fb343e022 233 **************************************************************/
j3 0:798fb343e022 234 void init_display(max7219_configuration_t config);
j3 0:798fb343e022 235
j3 0:798fb343e022 236
j3 0:798fb343e022 237 /**********************************************************//**
j3 0:798fb343e022 238 * @brief enables specific device in display
j3 0:798fb343e022 239 *
j3 0:798fb343e022 240 * @details
j3 0:798fb343e022 241 *
j3 0:798fb343e022 242 * On Entry:
j3 1:90a7cf4e7d26 243 * @param[in] device_number - device to enable
j3 0:798fb343e022 244 *
j3 0:798fb343e022 245 * On Exit:
j3 1:90a7cf4e7d26 246 * @return returns 0 on success
j3 1:90a7cf4e7d26 247 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 248 * returns -2 if device number is 0
j3 0:798fb343e022 249 **************************************************************/
j3 0:798fb343e022 250 int32_t enable_device(uint8_t device_number);
j3 0:798fb343e022 251
j3 0:798fb343e022 252
j3 0:798fb343e022 253 /**********************************************************//**
j3 0:798fb343e022 254 * @brief enables all device in display
j3 0:798fb343e022 255 *
j3 0:798fb343e022 256 * @details
j3 0:798fb343e022 257 *
j3 0:798fb343e022 258 * On Entry:
j3 0:798fb343e022 259 *
j3 0:798fb343e022 260 * On Exit:
j3 0:798fb343e022 261 * @return none
j3 0:798fb343e022 262 **************************************************************/
j3 0:798fb343e022 263 void enable_display(void);
j3 0:798fb343e022 264
j3 0:798fb343e022 265
j3 0:798fb343e022 266 /**********************************************************//**
j3 0:798fb343e022 267 * @brief disables specific device in display
j3 0:798fb343e022 268 *
j3 0:798fb343e022 269 * @details
j3 0:798fb343e022 270 *
j3 0:798fb343e022 271 * On Entry:
j3 1:90a7cf4e7d26 272 * @param[in] device_number - device to disable
j3 0:798fb343e022 273 *
j3 0:798fb343e022 274 * On Exit:
j3 1:90a7cf4e7d26 275 * @return returns 0 on success,
j3 1:90a7cf4e7d26 276 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 277 * returns -2 if device number is 0
j3 0:798fb343e022 278 **************************************************************/
j3 0:798fb343e022 279 int32_t disable_device(uint8_t device_number);
j3 0:798fb343e022 280
j3 0:798fb343e022 281
j3 0:798fb343e022 282 /**********************************************************//**
j3 0:798fb343e022 283 * @brief disables all devices in display
j3 0:798fb343e022 284 *
j3 0:798fb343e022 285 * @details
j3 0:798fb343e022 286 *
j3 0:798fb343e022 287 * On Entry:
j3 0:798fb343e022 288 *
j3 0:798fb343e022 289 * On Exit:
j3 0:798fb343e022 290 * @return none
j3 0:798fb343e022 291 **************************************************************/
j3 0:798fb343e022 292 void disable_display(void);
j3 0:798fb343e022 293
j3 0:798fb343e022 294
j3 0:798fb343e022 295 /**********************************************************//**
j3 0:798fb343e022 296 * @brief writes digit of given device with given data, user
j3 0:798fb343e022 297 * must enter correct data for decode_mode chosen
j3 0:798fb343e022 298 *
j3 0:798fb343e022 299 * @details
j3 0:798fb343e022 300 *
j3 0:798fb343e022 301 * On Entry:
j3 1:90a7cf4e7d26 302 * @param[in] device_number - device to write too
j3 1:90a7cf4e7d26 303 * @param[in] digit - digit to write
j3 1:90a7cf4e7d26 304 * @param[in] data - data to write
j3 0:798fb343e022 305 *
j3 0:798fb343e022 306 * On Exit:
j3 1:90a7cf4e7d26 307 * @return returns 0 on success,
j3 1:90a7cf4e7d26 308 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 309 * returns -2 if device number is 0
j3 1:90a7cf4e7d26 310 * returns -3 if digit > 8
j3 1:90a7cf4e7d26 311 * returns -4 if digit < 1
j3 0:798fb343e022 312 **************************************************************/
j3 0:798fb343e022 313 int32_t write_digit(uint8_t device_number, uint8_t digit, uint8_t data);
j3 0:798fb343e022 314
j3 0:798fb343e022 315
j3 0:798fb343e022 316 /**********************************************************//**
j3 0:798fb343e022 317 * @brief clears digit of given device
j3 0:798fb343e022 318 *
j3 0:798fb343e022 319 * @details
j3 0:798fb343e022 320 *
j3 0:798fb343e022 321 * On Entry:
j3 1:90a7cf4e7d26 322 * @param[in] device_number - device to write too
j3 1:90a7cf4e7d26 323 * @param[in] digit - digit to clear
j3 0:798fb343e022 324 *
j3 0:798fb343e022 325 * On Exit:
j3 1:90a7cf4e7d26 326 * @return returns 0 on success,
j3 1:90a7cf4e7d26 327 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 328 * returns -2 if device number is 0
j3 1:90a7cf4e7d26 329 * returns -3 if digit > 8
j3 1:90a7cf4e7d26 330 * returns -4 if digit < 1
j3 0:798fb343e022 331 **************************************************************/
j3 0:798fb343e022 332 int32_t clear_digit(uint8_t device_number, uint8_t digit);
j3 0:798fb343e022 333
j3 0:798fb343e022 334
j3 0:798fb343e022 335 /**********************************************************//**
j3 0:798fb343e022 336 * @brief turns on all segments/digits of given device
j3 0:798fb343e022 337 *
j3 0:798fb343e022 338 * @details
j3 0:798fb343e022 339 *
j3 0:798fb343e022 340 * On Entry:
j3 1:90a7cf4e7d26 341 * @param[in] device_number - device to write too
j3 0:798fb343e022 342 *
j3 0:798fb343e022 343 * On Exit:
j3 1:90a7cf4e7d26 344 * @return returns 0 on success,
j3 1:90a7cf4e7d26 345 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 346 * returns -2 if device number is 0
j3 0:798fb343e022 347 **************************************************************/
j3 0:798fb343e022 348 int32_t device_all_on(uint8_t device_number);
j3 0:798fb343e022 349
j3 0:798fb343e022 350
j3 0:798fb343e022 351 /**********************************************************//**
j3 0:798fb343e022 352 * @brief turns off all segments/digits of given device
j3 0:798fb343e022 353 *
j3 0:798fb343e022 354 * @details
j3 0:798fb343e022 355 *
j3 0:798fb343e022 356 * On Entry:
j3 1:90a7cf4e7d26 357 * @param[in] device_number - device to write too
j3 0:798fb343e022 358 *
j3 0:798fb343e022 359 * On Exit:
j3 1:90a7cf4e7d26 360 * @return returns 0 on success,
j3 1:90a7cf4e7d26 361 * returns -1 if device number is > _num_devices
j3 1:90a7cf4e7d26 362 * returns -2 if device number is 0
j3 0:798fb343e022 363 **************************************************************/
j3 0:798fb343e022 364 int32_t device_all_off(uint8_t device_number);
j3 0:798fb343e022 365
j3 0:798fb343e022 366
j3 0:798fb343e022 367 /**********************************************************//**
j3 0:798fb343e022 368 * @brief turns on all segments/digits of display
j3 0:798fb343e022 369 *
j3 0:798fb343e022 370 * @details
j3 0:798fb343e022 371 *
j3 0:798fb343e022 372 * On Entry:
j3 0:798fb343e022 373 *
j3 0:798fb343e022 374 * On Exit:
j3 0:798fb343e022 375 * @return none
j3 0:798fb343e022 376 **************************************************************/
j3 0:798fb343e022 377 void display_all_on(void);
j3 0:798fb343e022 378
j3 0:798fb343e022 379
j3 0:798fb343e022 380 /**********************************************************//**
j3 0:798fb343e022 381 * @brief turns off all segments/digits of display
j3 0:798fb343e022 382 *
j3 0:798fb343e022 383 * @details
j3 0:798fb343e022 384 *
j3 0:798fb343e022 385 * On Entry:
j3 0:798fb343e022 386 *
j3 0:798fb343e022 387 * On Exit:
j3 0:798fb343e022 388 * @return none
j3 0:798fb343e022 389 **************************************************************/
j3 0:798fb343e022 390 void display_all_off(void);
j3 0:798fb343e022 391
j3 0:798fb343e022 392
j3 0:798fb343e022 393 private:
j3 0:798fb343e022 394
j3 0:798fb343e022 395 SPI *_p_spi;
j3 0:798fb343e022 396 DigitalOut *_p_cs;
j3 0:798fb343e022 397 bool _spi_owner;
j3 0:798fb343e022 398
j3 0:798fb343e022 399 uint8_t _num_devices;
j3 0:798fb343e022 400
j3 0:798fb343e022 401 };
j3 0:798fb343e022 402 #endif /* MAX7219_H*/