Bird Techstep / MAX72XX

Fork of MAX72XX by Bird Techstep

Committer:
techstep
Date:
Mon Aug 04 11:55:15 2014 +0000
Revision:
1:dfa1f4cac4c9
Parent:
0:8bdc990b1b57
Child:
2:8662e8d7d8fa
MAX7219/MAX7221

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