Library for the MAX7219 LED display driver

Dependents:   MAXREFDES99_demo MAXREFDES99_RTC_Display nucleo_spi_max7219_led8x8 max7219 ... more

max7219.h

Committer:
j3
Date:
2016-01-12
Revision:
0:798fb343e022
Child:
1:90a7cf4e7d26

File content as of revision 0:798fb343e022:

/******************************************************************//**
* @file max7219.h
*
* @author Justin Jordan
*
* @version 0.0
*
* Started: 08JAN16
*
* Updated: 
*
* @brief Header file for Max7219 class
***********************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/


#ifndef MAX7219_H
#define MAX7219_H

#include "mbed.h"


typedef struct
{
    uint8_t device_number;
    uint8_t decode_mode;
    uint8_t intensity;
    uint8_t scan_limit;
}max7219_configuration_t;


class Max7219
{
    public:
    
    typedef enum
    {
        MAX7219_NO_OP = 0,
        MAX7219_DIGIT_0,
        MAX7219_DIGIT_1,
        MAX7219_DIGIT_2,
        MAX7219_DIGIT_3,
        MAX7219_DIGIT_4,
        MAX7219_DIGIT_5,
        MAX7219_DIGIT_6,
        MAX7219_DIGIT_7,
        MAX7219_DECODE_MODE,
        MAX7219_INTENSITY,
        MAX7219_SCAN_LIMIT,
        MAX7219_SHUTDOWN,
        MAX7219_DISPLAY_TEST = 15
    }max7219_register_e;
    
    
    typedef enum
    {
        MAX7219_INTENSITY_0 = 0,
        MAX7219_INTENSITY_1,
        MAX7219_INTENSITY_2,
        MAX7219_INTENSITY_3,
        MAX7219_INTENSITY_4,
        MAX7219_INTENSITY_5,
        MAX7219_INTENSITY_6,
        MAX7219_INTENSITY_7,
        MAX7219_INTENSITY_8,
        MAX7219_INTENSITY_9,
        MAX7219_INTENSITY_A,
        MAX7219_INTENSITY_B,
        MAX7219_INTENSITY_C,
        MAX7219_INTENSITY_D,
        MAX7219_INTENSITY_E,
        MAX7219_INTENSITY_F
    }max7219_intensity_e;
    
    
    typedef enum
    {
        MAX7219_SCAN_1 = 0,
        MAX7219_SCAN_2,
        MAX7219_SCAN_3,
        MAX7219_SCAN_4,
        MAX7219_SCAN_5,
        MAX7219_SCAN_6,
        MAX7219_SCAN_7,
        MAX7219_SCAN_8
    }max7219_scan_limit_e;
        
    
    /**********************************************************//**
    * @brief Constructor for Max7219 Class.  
    * 
    * @details Allows user to pass pointer to existing SPI bus
    *
    * On Entry:
    *     @param[in] spi_bus - pointer to existing SPI object
    *     @param[in] cs - pin to use for cs
    *
    * On Exit:
    *    @return none
    **************************************************************/
    Max7219(SPI *spi_bus, PinName cs);
    
    
    /**********************************************************//**
    * @brief Constructor for Max7219 Class.  
    * 
    * @details Allows user to specify SPI peripheral to use
    *
    * On Entry:
    *     @param[in] mosi - pin to use for mosi
    *     @param[in] miso - pin to use for miso
    *     @param[in] sclk - pin to use for sclk
    *     @param[in] cs - pin to use for cs
    *
    * On Exit:
    *    @return none
    **************************************************************/
    Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs);
    
    
    /**********************************************************//**
    * @brief Default destructor for Max7219 Class.  
    *
    * @details Destroys SPI object if owner 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    ~Max7219();
    
    
    /**********************************************************//**
    * @brief Sets the number of MAX7219 devices being used.
    *        Defaults to one
    *
    * @details 
    *
    * On Entry:
    *     @param[in] num_devices - number of MAX7219 devices being
    *                              used, max of 255 
    *
    * On Exit:
    *    @return returns number of devices
    **************************************************************/
    int32_t set_num_devices(uint8_t num_devices);
    
    
    /**********************************************************//**
    * @brief Tests all devices being used 
    *
    * @details Sets bit0 of DISPLAY_TEST regiser in all devices
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void set_display_test(void);
    
    
    /**********************************************************//**
    * @brief Stops test
    *
    * @details Clear bit0 of DISPLAY_TEST regiser in all devices
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void clear_display_test(void);
    
    
    /**********************************************************//**
    * @brief initializes specific device in display with given 
    *        config data
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t init_device(max7219_configuration_t config, uint8_t device_number);
    
    
    /**********************************************************//**
    * @brief initializes all devices with given config data
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void init_display(max7219_configuration_t config);
    
    
    /**********************************************************//**
    * @brief enables specific device in display
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t enable_device(uint8_t device_number);
    
    
    /**********************************************************//**
    * @brief enables all device in display 
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void enable_display(void);
    
    
    /**********************************************************//**
    * @brief disables specific device in display
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t disable_device(uint8_t device_number);
    
    
    /**********************************************************//**
    * @brief disables all devices in display
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void disable_display(void);
    
    
    /**********************************************************//**
    * @brief writes digit of given device with given data, user
    *        must enter correct data for decode_mode chosen
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t write_digit(uint8_t device_number, uint8_t digit, uint8_t data);
    
    
    /**********************************************************//**
    * @brief clears digit of given device
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t clear_digit(uint8_t device_number, uint8_t digit);
    
    
    /**********************************************************//**
    * @brief turns on all segments/digits of given device
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t device_all_on(uint8_t device_number);
    
    
    /**********************************************************//**
    * @brief turns off all segments/digits of given device
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    int32_t device_all_off(uint8_t device_number);
    
    
    /**********************************************************//**
    * @brief turns on all segments/digits of display
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void display_all_on(void);
    
    
    /**********************************************************//**
    * @brief turns off all segments/digits of display
    *
    * @details 
    *
    * On Entry:
    *
    * On Exit:
    *    @return none
    **************************************************************/
    void display_all_off(void);
    
      
    private:
    
    SPI *_p_spi;
    DigitalOut *_p_cs;
    bool _spi_owner;
    
    uint8_t _num_devices;
    
};
#endif /* MAX7219_H*/