Bird Techstep / MAX72XX

Fork of MAX72XX by Bird Techstep

Committer:
techstep
Date:
Mon Aug 04 12:11:51 2014 +0000
Revision:
2:8662e8d7d8fa
Parent:
1:dfa1f4cac4c9
Child:
3:2bcf4c5b47d4
MAX72xx; MAX7219/MAX7221 Library; 7 SEGMENT LED; Software SPI use digitalOut

Who changed what in which revision?

UserRevisionLine numberNew contents of line
techstep 0:8bdc990b1b57 1 /*****************************************************************************
techstep 0:8bdc990b1b57 2 * Type : Header
techstep 0:8bdc990b1b57 3 * File : MAX72XX.h
techstep 0:8bdc990b1b57 4 * Dec. : MAX7219 & MAX7221 Software SPI library
techstep 0:8bdc990b1b57 5 * Copyright (c) 2013-2014, Bird Techstep, tbird_th@hotmail.com
techstep 2:8662e8d7d8fa 6 *
techstep 1:dfa1f4cac4c9 7 * Remark Original codr from LedControl Library [Arduino]
techstep 2:8662e8d7d8fa 8 *
techstep 0:8bdc990b1b57 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
techstep 0:8bdc990b1b57 10 * of this software and associated documentation files (the "Software"), to deal
techstep 0:8bdc990b1b57 11 * in the Software without restriction, including without limitation the rights
techstep 0:8bdc990b1b57 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
techstep 0:8bdc990b1b57 13 * copies of the Software, and to permit persons to whom the Software is
techstep 0:8bdc990b1b57 14 * furnished to do so, subject to the following conditions:
techstep 0:8bdc990b1b57 15 *
techstep 0:8bdc990b1b57 16 * The above copyright notice and this permission notice shall be included in
techstep 0:8bdc990b1b57 17 * all copies or substantial portions of the Software.
techstep 0:8bdc990b1b57 18 *
techstep 0:8bdc990b1b57 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
techstep 0:8bdc990b1b57 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
techstep 0:8bdc990b1b57 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
techstep 0:8bdc990b1b57 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
techstep 0:8bdc990b1b57 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
techstep 0:8bdc990b1b57 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
techstep 0:8bdc990b1b57 25 * THE SOFTWARE.
techstep 0:8bdc990b1b57 26 *****************************************************************************/
techstep 0:8bdc990b1b57 27 #ifndef MAX72XX_H
techstep 0:8bdc990b1b57 28 #define MAX72XX_H
techstep 0:8bdc990b1b57 29
techstep 0:8bdc990b1b57 30 // Segments to be switched on for characters and digits on 7-Segment Displays
techstep 0:8bdc990b1b57 31 const static char charTable[128] = {
techstep 0:8bdc990b1b57 32 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, // 0, 1, 2, 3, 4, 5, 6, 7,
techstep 0:8bdc990b1b57 33 0x7F, 0x7B, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47, // 8, 9, A, b, c, d, E, F,
techstep 0:8bdc990b1b57 34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 37 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x80, 0x00, // , , , , ., -, ., ,
techstep 0:8bdc990b1b57 38 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, // 0, 1, 2, 3, 4, 5, 6, 7,
techstep 0:8bdc990b1b57 39 0x7F, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8, 9, , , , , , ,
techstep 0:8bdc990b1b57 40 0x00, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47, 0x00, // , A, b, c, d, E, F, ,
techstep 0:8bdc990b1b57 41 0x37, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, // H, , , , L, , , ,
techstep 0:8bdc990b1b57 42 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // P, , , , , , , ,
techstep 0:8bdc990b1b57 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, // , , , , , , , _,
techstep 0:8bdc990b1b57 44 0x00, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47, 0x00, // , A, b, c, d, E, F, ,
techstep 0:8bdc990b1b57 45 0x37, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, // H, , , , L, , , ,
techstep 0:8bdc990b1b57 46 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // P, , , , , , , ,
techstep 0:8bdc990b1b57 47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 48 };
techstep 0:8bdc990b1b57 49
techstep 0:8bdc990b1b57 50 class MAX72XX {
techstep 0:8bdc990b1b57 51 public:
techstep 0:8bdc990b1b57 52 /*************************************************************************
techstep 0:8bdc990b1b57 53 * Create MAX72XX Object.
techstep 0:8bdc990b1b57 54 *************************************************************************
techstep 0:8bdc990b1b57 55 * @param mosi_pin : MOSI Digital pin. [Master Out Slave In]
techstep 0:8bdc990b1b57 56 * @param miso_pin : MISO Digital pin. [Master In Slave Out]
techstep 0:8bdc990b1b57 57 * @param sclk_pin : SCLK Digital pin. [SPI Clock]
techstep 0:8bdc990b1b57 58 * @param cs_pin : CS Digital pin. [Chip Select]
techstep 0:8bdc990b1b57 59 * @param numDevices : Maximum number of devices that can be controled.
techstep 0:8bdc990b1b57 60 *************************************************************************/
techstep 0:8bdc990b1b57 61 MAX72XX(PinName mosi_pin, PinName miso_pin, PinName sclk_pin, PinName cs_pin, int numDevices=1);
techstep 0:8bdc990b1b57 62
techstep 0:8bdc990b1b57 63 /*************************************************************************
techstep 0:8bdc990b1b57 64 * Destructor.
techstep 0:8bdc990b1b57 65 *************************************************************************/
techstep 0:8bdc990b1b57 66 ~MAX72XX();
techstep 0:8bdc990b1b57 67
techstep 0:8bdc990b1b57 68 /*************************************************************************
techstep 0:8bdc990b1b57 69 * Specify SPI format.
techstep 0:8bdc990b1b57 70 *************************************************************************
techstep 0:8bdc990b1b57 71 * @param bits : 8 or 16 are typical values.
techstep 0:8bdc990b1b57 72 * @param mode : 0, 1, 2, or 3 phase (bit1) and idle clock (bit0).
techstep 0:8bdc990b1b57 73 *************************************************************************/
techstep 0:8bdc990b1b57 74 void format(int bits, int mode = 0);
techstep 0:8bdc990b1b57 75
techstep 0:8bdc990b1b57 76 /*************************************************************************
techstep 0:8bdc990b1b57 77 * Specify SPI clock frequency.
techstep 0:8bdc990b1b57 78 *************************************************************************
techstep 0:8bdc990b1b57 79 * @param hz : frequency (optional, defaults to 10000000).
techstep 0:8bdc990b1b57 80 *************************************************************************/
techstep 0:8bdc990b1b57 81 void frequency(int hz = 10000000);
techstep 0:8bdc990b1b57 82
techstep 0:8bdc990b1b57 83 /*************************************************************************
techstep 0:8bdc990b1b57 84 * Write data and read result.
techstep 0:8bdc990b1b57 85 *************************************************************************
techstep 0:8bdc990b1b57 86 * @param value : data to write (see format for bit size).
techstep 0:8bdc990b1b57 87 * returns value read from device.
techstep 0:8bdc990b1b57 88 *************************************************************************/
techstep 0:8bdc990b1b57 89 int write(int value);
techstep 0:8bdc990b1b57 90
techstep 0:8bdc990b1b57 91 /*************************************************************************
techstep 0:8bdc990b1b57 92 * Display a character on a 7-Segment display.
techstep 0:8bdc990b1b57 93 *************************************************************************
techstep 0:8bdc990b1b57 94 * Dec. : There are only a few characters that make sense here :
techstep 0:8bdc990b1b57 95 * '0','1','2','3','4','5','6','7','8','9','0',
techstep 0:8bdc990b1b57 96 * 'A','b','c','d','E','F','H','L','P',
techstep 0:8bdc990b1b57 97 * '.','-','_',' '.
techstep 0:8bdc990b1b57 98 * @param addr : Number of device.
techstep 0:8bdc990b1b57 99 * @param digit : The position of the character on the display (0..7).
techstep 0:8bdc990b1b57 100 * @param value : The character to be displayed.
techstep 0:8bdc990b1b57 101 * @param dp : Sets the decimal point.
techstep 0:8bdc990b1b57 102 *************************************************************************/
techstep 0:8bdc990b1b57 103 void setChar(int addr, int digit, char value, bool dp);
techstep 0:8bdc990b1b57 104
techstep 0:8bdc990b1b57 105 /*************************************************************************
techstep 0:8bdc990b1b57 106 * Display a hexadecimal digit on a 7-Segment Display.
techstep 0:8bdc990b1b57 107 *************************************************************************
techstep 0:8bdc990b1b57 108 * @param addr : Number of device.
techstep 0:8bdc990b1b57 109 * @param digit : The position of the digit on the display (0..7)
techstep 0:8bdc990b1b57 110 * @param value : The value to be displayed. (0x00..0x0F)
techstep 0:8bdc990b1b57 111 * @param dp : Tets the decimal point.
techstep 0:8bdc990b1b57 112 *************************************************************************/
techstep 0:8bdc990b1b57 113 void setDigit(int addr, int digit, uint8_t value, bool dp);
techstep 0:8bdc990b1b57 114
techstep 0:8bdc990b1b57 115 /*************************************************************************
techstep 0:8bdc990b1b57 116 * Switch all Leds on the display off.
techstep 0:8bdc990b1b57 117 *************************************************************************
techstep 0:8bdc990b1b57 118 * @param addr : Number of device.
techstep 0:8bdc990b1b57 119 *************************************************************************/
techstep 0:8bdc990b1b57 120 void clearDisplay(int addr);
techstep 0:8bdc990b1b57 121
techstep 0:8bdc990b1b57 122 /*************************************************************************
techstep 0:8bdc990b1b57 123 * Switch all Leds on the display off.
techstep 0:8bdc990b1b57 124 *************************************************************************
techstep 0:8bdc990b1b57 125 * @param addr : Number of device.
techstep 0:8bdc990b1b57 126 * @param digit : 0-7
techstep 0:8bdc990b1b57 127 *************************************************************************/
techstep 0:8bdc990b1b57 128 void clearDigit(int addr, int digit);
techstep 0:8bdc990b1b57 129
techstep 0:8bdc990b1b57 130 /*************************************************************************
techstep 0:8bdc990b1b57 131 * Set the brightness of the display.
techstep 0:8bdc990b1b57 132 *************************************************************************
techstep 0:8bdc990b1b57 133 * @param addr : Number of device.
techstep 0:8bdc990b1b57 134 * @param intensity : Brightness of the display. (0..15)
techstep 0:8bdc990b1b57 135 *************************************************************************/
techstep 0:8bdc990b1b57 136 void setIntensity(int addr, int intensity);
techstep 0:8bdc990b1b57 137
techstep 0:8bdc990b1b57 138 /*************************************************************************
techstep 0:8bdc990b1b57 139 * Set the number of digits to be displayed.
techstep 0:8bdc990b1b57 140 *************************************************************************
techstep 0:8bdc990b1b57 141 * @param addr : Number of device.
techstep 0:8bdc990b1b57 142 * @param limit : number of digits to be displayed (1..8)
techstep 0:8bdc990b1b57 143 *************************************************************************/
techstep 0:8bdc990b1b57 144 void setScanLimit(int addr, int limit);
techstep 0:8bdc990b1b57 145
techstep 0:8bdc990b1b57 146 /*************************************************************************
techstep 0:8bdc990b1b57 147 * Set the shutdown (power saving) mode for the device.
techstep 0:8bdc990b1b57 148 *************************************************************************
techstep 0:8bdc990b1b57 149 * @param addr : Number of device.
techstep 0:8bdc990b1b57 150 * @param status : If true the device goes into power-down mode.
techstep 0:8bdc990b1b57 151 * Set to false for normal operation
techstep 0:8bdc990b1b57 152 *************************************************************************/
techstep 0:8bdc990b1b57 153 void shutdown(int addr, bool status);
techstep 0:8bdc990b1b57 154
techstep 0:8bdc990b1b57 155 /*************************************************************************
techstep 0:8bdc990b1b57 156 * Display test .
techstep 0:8bdc990b1b57 157 *************************************************************************
techstep 0:8bdc990b1b57 158 * @param addr : Number of device.
techstep 0:8bdc990b1b57 159 * @param status : If true test false for mormal
techstep 0:8bdc990b1b57 160 *************************************************************************/
techstep 0:8bdc990b1b57 161 void displayTest(int addr, bool status);
techstep 0:8bdc990b1b57 162
techstep 0:8bdc990b1b57 163 /*************************************************************************
techstep 0:8bdc990b1b57 164 * Decode Mode.
techstep 0:8bdc990b1b57 165 *************************************************************************
techstep 0:8bdc990b1b57 166 * @param addr : Number of device.
techstep 0:8bdc990b1b57 167 * @param mode : 0x00, 0x01, 0x0F, 0xFF
techstep 0:8bdc990b1b57 168 *************************************************************************/
techstep 0:8bdc990b1b57 169 void decodeMode(int addr, uint8_t mode);
techstep 0:8bdc990b1b57 170
techstep 0:8bdc990b1b57 171 private:
techstep 0:8bdc990b1b57 172 /*************************************************************************
techstep 0:8bdc990b1b57 173 * Send out a single command to the device.
techstep 0:8bdc990b1b57 174 *************************************************************************
techstep 0:8bdc990b1b57 175 * @param addr : Number of Devices.
techstep 0:8bdc990b1b57 176 * @param opcode : Register Address Map.
techstep 0:8bdc990b1b57 177 * @param data : The value to be displayed. (0x00..0x0F)
techstep 0:8bdc990b1b57 178 *************************************************************************/
techstep 0:8bdc990b1b57 179 void spiTransfer(int addr, uint8_t opcode, uint8_t data);
techstep 0:8bdc990b1b57 180
techstep 0:8bdc990b1b57 181 // mosi_pin, miso_pin, sclk_pin, cs_pin, numDevices
techstep 0:8bdc990b1b57 182 DigitalOut* mosi;
techstep 0:8bdc990b1b57 183 DigitalIn* miso;
techstep 0:8bdc990b1b57 184 DigitalOut* sclk;
techstep 0:8bdc990b1b57 185 int numDevices;
techstep 0:8bdc990b1b57 186 int port;
techstep 0:8bdc990b1b57 187 int bits;
techstep 0:8bdc990b1b57 188 int mode;
techstep 0:8bdc990b1b57 189 int polarity; // idle clock value
techstep 0:8bdc990b1b57 190 int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
techstep 0:8bdc990b1b57 191 int freq;
techstep 0:8bdc990b1b57 192
techstep 0:8bdc990b1b57 193 uint8_t spidata[16]; // The array for shifting the data to the devices.
techstep 0:8bdc990b1b57 194 uint8_t status[64]; // We keep track of the led-status for all 8 devices in this array.
techstep 0:8bdc990b1b57 195 int maxDevices; // The maximum number of devices we use.
techstep 0:8bdc990b1b57 196
techstep 0:8bdc990b1b57 197 protected:
techstep 0:8bdc990b1b57 198 //SPI _spi;
techstep 0:8bdc990b1b57 199 DigitalOut cs;
techstep 0:8bdc990b1b57 200
techstep 0:8bdc990b1b57 201 };
techstep 0:8bdc990b1b57 202
techstep 0:8bdc990b1b57 203 #endif // MAX72XX_H