Bird Techstep / MAX72XX

Fork of MAX72XX by Bird Techstep

Committer:
techstep
Date:
Wed Aug 06 03:14:28 2014 +0000
Revision:
3:2bcf4c5b47d4
Parent:
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 3:2bcf4c5b47d4 4 * Dec. : MAX7219 & MAX7221 Software SPI library [7 SEGMENT]
techstep 0:8bdc990b1b57 5 * Copyright (c) 2013-2014, Bird Techstep, tbird_th@hotmail.com
techstep 2:8662e8d7d8fa 6 *
techstep 3:2bcf4c5b47d4 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 3:2bcf4c5b47d4 30 #include "mbed.h"
techstep 3:2bcf4c5b47d4 31
techstep 0:8bdc990b1b57 32 // Segments to be switched on for characters and digits on 7-Segment Displays
techstep 0:8bdc990b1b57 33 const static char charTable[128] = {
techstep 0:8bdc990b1b57 34 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, // 0, 1, 2, 3, 4, 5, 6, 7,
techstep 0:8bdc990b1b57 35 0x7F, 0x7B, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47, // 8, 9, A, b, c, d, E, F,
techstep 0:8bdc990b1b57 36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 39 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x80, 0x00, // , , , , ., -, ., ,
techstep 0:8bdc990b1b57 40 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, // 0, 1, 2, 3, 4, 5, 6, 7,
techstep 0:8bdc990b1b57 41 0x7F, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8, 9, , , , , , ,
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, 0x08, // , , , , , , , _,
techstep 0:8bdc990b1b57 46 0x00, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47, 0x00, // , A, b, c, d, E, F, ,
techstep 0:8bdc990b1b57 47 0x37, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, // H, , , , L, , , ,
techstep 0:8bdc990b1b57 48 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // P, , , , , , , ,
techstep 0:8bdc990b1b57 49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // , , , , , , , ,
techstep 0:8bdc990b1b57 50 };
techstep 0:8bdc990b1b57 51
techstep 0:8bdc990b1b57 52 class MAX72XX {
techstep 0:8bdc990b1b57 53 public:
techstep 0:8bdc990b1b57 54 /*************************************************************************
techstep 0:8bdc990b1b57 55 * Create MAX72XX Object.
techstep 0:8bdc990b1b57 56 *************************************************************************
techstep 0:8bdc990b1b57 57 * @param mosi_pin : MOSI Digital pin. [Master Out Slave In]
techstep 0:8bdc990b1b57 58 * @param miso_pin : MISO Digital pin. [Master In Slave Out]
techstep 0:8bdc990b1b57 59 * @param sclk_pin : SCLK Digital pin. [SPI Clock]
techstep 0:8bdc990b1b57 60 * @param cs_pin : CS Digital pin. [Chip Select]
techstep 0:8bdc990b1b57 61 * @param numDevices : Maximum number of devices that can be controled.
techstep 0:8bdc990b1b57 62 *************************************************************************/
techstep 0:8bdc990b1b57 63 MAX72XX(PinName mosi_pin, PinName miso_pin, PinName sclk_pin, PinName cs_pin, int numDevices=1);
techstep 0:8bdc990b1b57 64
techstep 0:8bdc990b1b57 65 /*************************************************************************
techstep 0:8bdc990b1b57 66 * Destructor.
techstep 0:8bdc990b1b57 67 *************************************************************************/
techstep 0:8bdc990b1b57 68 ~MAX72XX();
techstep 0:8bdc990b1b57 69
techstep 0:8bdc990b1b57 70 /*************************************************************************
techstep 0:8bdc990b1b57 71 * Specify SPI format.
techstep 0:8bdc990b1b57 72 *************************************************************************
techstep 0:8bdc990b1b57 73 * @param bits : 8 or 16 are typical values.
techstep 0:8bdc990b1b57 74 * @param mode : 0, 1, 2, or 3 phase (bit1) and idle clock (bit0).
techstep 0:8bdc990b1b57 75 *************************************************************************/
techstep 0:8bdc990b1b57 76 void format(int bits, int mode = 0);
techstep 0:8bdc990b1b57 77
techstep 0:8bdc990b1b57 78 /*************************************************************************
techstep 0:8bdc990b1b57 79 * Specify SPI clock frequency.
techstep 0:8bdc990b1b57 80 *************************************************************************
techstep 0:8bdc990b1b57 81 * @param hz : frequency (optional, defaults to 10000000).
techstep 0:8bdc990b1b57 82 *************************************************************************/
techstep 0:8bdc990b1b57 83 void frequency(int hz = 10000000);
techstep 0:8bdc990b1b57 84
techstep 0:8bdc990b1b57 85 /*************************************************************************
techstep 0:8bdc990b1b57 86 * Write data and read result.
techstep 0:8bdc990b1b57 87 *************************************************************************
techstep 0:8bdc990b1b57 88 * @param value : data to write (see format for bit size).
techstep 0:8bdc990b1b57 89 * returns value read from device.
techstep 0:8bdc990b1b57 90 *************************************************************************/
techstep 0:8bdc990b1b57 91 int write(int value);
techstep 0:8bdc990b1b57 92
techstep 0:8bdc990b1b57 93 /*************************************************************************
techstep 0:8bdc990b1b57 94 * Display a character on a 7-Segment display.
techstep 0:8bdc990b1b57 95 *************************************************************************
techstep 0:8bdc990b1b57 96 * Dec. : There are only a few characters that make sense here :
techstep 0:8bdc990b1b57 97 * '0','1','2','3','4','5','6','7','8','9','0',
techstep 0:8bdc990b1b57 98 * 'A','b','c','d','E','F','H','L','P',
techstep 0:8bdc990b1b57 99 * '.','-','_',' '.
techstep 0:8bdc990b1b57 100 * @param addr : Number of device.
techstep 0:8bdc990b1b57 101 * @param digit : The position of the character on the display (0..7).
techstep 0:8bdc990b1b57 102 * @param value : The character to be displayed.
techstep 0:8bdc990b1b57 103 * @param dp : Sets the decimal point.
techstep 0:8bdc990b1b57 104 *************************************************************************/
techstep 0:8bdc990b1b57 105 void setChar(int addr, int digit, char value, bool dp);
techstep 0:8bdc990b1b57 106
techstep 0:8bdc990b1b57 107 /*************************************************************************
techstep 0:8bdc990b1b57 108 * Display a hexadecimal digit on a 7-Segment Display.
techstep 0:8bdc990b1b57 109 *************************************************************************
techstep 0:8bdc990b1b57 110 * @param addr : Number of device.
techstep 0:8bdc990b1b57 111 * @param digit : The position of the digit on the display (0..7)
techstep 0:8bdc990b1b57 112 * @param value : The value to be displayed. (0x00..0x0F)
techstep 0:8bdc990b1b57 113 * @param dp : Tets the decimal point.
techstep 0:8bdc990b1b57 114 *************************************************************************/
techstep 0:8bdc990b1b57 115 void setDigit(int addr, int digit, uint8_t value, bool dp);
techstep 0:8bdc990b1b57 116
techstep 0:8bdc990b1b57 117 /*************************************************************************
techstep 0:8bdc990b1b57 118 * Switch all Leds on the display off.
techstep 0:8bdc990b1b57 119 *************************************************************************
techstep 0:8bdc990b1b57 120 * @param addr : Number of device.
techstep 0:8bdc990b1b57 121 *************************************************************************/
techstep 0:8bdc990b1b57 122 void clearDisplay(int addr);
techstep 0:8bdc990b1b57 123
techstep 0:8bdc990b1b57 124 /*************************************************************************
techstep 0:8bdc990b1b57 125 * Switch all Leds on the display off.
techstep 0:8bdc990b1b57 126 *************************************************************************
techstep 0:8bdc990b1b57 127 * @param addr : Number of device.
techstep 0:8bdc990b1b57 128 * @param digit : 0-7
techstep 0:8bdc990b1b57 129 *************************************************************************/
techstep 0:8bdc990b1b57 130 void clearDigit(int addr, int digit);
techstep 0:8bdc990b1b57 131
techstep 0:8bdc990b1b57 132 /*************************************************************************
techstep 0:8bdc990b1b57 133 * Set the brightness of the display.
techstep 0:8bdc990b1b57 134 *************************************************************************
techstep 0:8bdc990b1b57 135 * @param addr : Number of device.
techstep 0:8bdc990b1b57 136 * @param intensity : Brightness of the display. (0..15)
techstep 0:8bdc990b1b57 137 *************************************************************************/
techstep 0:8bdc990b1b57 138 void setIntensity(int addr, int intensity);
techstep 0:8bdc990b1b57 139
techstep 0:8bdc990b1b57 140 /*************************************************************************
techstep 0:8bdc990b1b57 141 * Set the number of digits to be displayed.
techstep 0:8bdc990b1b57 142 *************************************************************************
techstep 0:8bdc990b1b57 143 * @param addr : Number of device.
techstep 0:8bdc990b1b57 144 * @param limit : number of digits to be displayed (1..8)
techstep 0:8bdc990b1b57 145 *************************************************************************/
techstep 0:8bdc990b1b57 146 void setScanLimit(int addr, int limit);
techstep 0:8bdc990b1b57 147
techstep 0:8bdc990b1b57 148 /*************************************************************************
techstep 0:8bdc990b1b57 149 * Set the shutdown (power saving) mode for the device.
techstep 0:8bdc990b1b57 150 *************************************************************************
techstep 0:8bdc990b1b57 151 * @param addr : Number of device.
techstep 0:8bdc990b1b57 152 * @param status : If true the device goes into power-down mode.
techstep 0:8bdc990b1b57 153 * Set to false for normal operation
techstep 0:8bdc990b1b57 154 *************************************************************************/
techstep 0:8bdc990b1b57 155 void shutdown(int addr, bool status);
techstep 0:8bdc990b1b57 156
techstep 0:8bdc990b1b57 157 /*************************************************************************
techstep 0:8bdc990b1b57 158 * Display test .
techstep 0:8bdc990b1b57 159 *************************************************************************
techstep 0:8bdc990b1b57 160 * @param addr : Number of device.
techstep 0:8bdc990b1b57 161 * @param status : If true test false for mormal
techstep 0:8bdc990b1b57 162 *************************************************************************/
techstep 0:8bdc990b1b57 163 void displayTest(int addr, bool status);
techstep 0:8bdc990b1b57 164
techstep 0:8bdc990b1b57 165 /*************************************************************************
techstep 0:8bdc990b1b57 166 * Decode Mode.
techstep 0:8bdc990b1b57 167 *************************************************************************
techstep 0:8bdc990b1b57 168 * @param addr : Number of device.
techstep 0:8bdc990b1b57 169 * @param mode : 0x00, 0x01, 0x0F, 0xFF
techstep 0:8bdc990b1b57 170 *************************************************************************/
techstep 0:8bdc990b1b57 171 void decodeMode(int addr, uint8_t mode);
techstep 0:8bdc990b1b57 172
techstep 0:8bdc990b1b57 173 private:
techstep 0:8bdc990b1b57 174 /*************************************************************************
techstep 0:8bdc990b1b57 175 * Send out a single command to the device.
techstep 0:8bdc990b1b57 176 *************************************************************************
techstep 0:8bdc990b1b57 177 * @param addr : Number of Devices.
techstep 0:8bdc990b1b57 178 * @param opcode : Register Address Map.
techstep 0:8bdc990b1b57 179 * @param data : The value to be displayed. (0x00..0x0F)
techstep 0:8bdc990b1b57 180 *************************************************************************/
techstep 0:8bdc990b1b57 181 void spiTransfer(int addr, uint8_t opcode, uint8_t data);
techstep 0:8bdc990b1b57 182
techstep 0:8bdc990b1b57 183 // mosi_pin, miso_pin, sclk_pin, cs_pin, numDevices
techstep 0:8bdc990b1b57 184 DigitalOut* mosi;
techstep 0:8bdc990b1b57 185 DigitalIn* miso;
techstep 0:8bdc990b1b57 186 DigitalOut* sclk;
techstep 0:8bdc990b1b57 187 int numDevices;
techstep 0:8bdc990b1b57 188 int port;
techstep 0:8bdc990b1b57 189 int bits;
techstep 0:8bdc990b1b57 190 int mode;
techstep 0:8bdc990b1b57 191 int polarity; // idle clock value
techstep 0:8bdc990b1b57 192 int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
techstep 0:8bdc990b1b57 193 int freq;
techstep 0:8bdc990b1b57 194
techstep 0:8bdc990b1b57 195 uint8_t spidata[16]; // The array for shifting the data to the devices.
techstep 0:8bdc990b1b57 196 uint8_t status[64]; // We keep track of the led-status for all 8 devices in this array.
techstep 0:8bdc990b1b57 197 int maxDevices; // The maximum number of devices we use.
techstep 0:8bdc990b1b57 198
techstep 0:8bdc990b1b57 199 protected:
techstep 0:8bdc990b1b57 200 //SPI _spi;
techstep 0:8bdc990b1b57 201 DigitalOut cs;
techstep 0:8bdc990b1b57 202
techstep 0:8bdc990b1b57 203 };
techstep 0:8bdc990b1b57 204
techstep 0:8bdc990b1b57 205 #endif // MAX72XX_H