|
| 1 | +/* |
| 2 | + * MAX7219.cpp - A library for controling Leds with a MAX7219/MAX7221 |
| 3 | + * Copyright (c) 2007 Eberhard Fahle |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person |
| 6 | + * obtaining a copy of this software and associated documentation |
| 7 | + * files (the "Software"), to deal in the Software without |
| 8 | + * restriction, including without limitation the rights to use, |
| 9 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the |
| 11 | + * Software is furnished to do so, subject to the following |
| 12 | + * conditions: |
| 13 | + * |
| 14 | + * This permission notice shall be included in all copies or |
| 15 | + * substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 2
10000
8 | + * Modified by Neil Kolban (2017) for ESP32 support. |
| 29 | + */ |
| 30 | + |
| 31 | +#include "MAX7219.h" |
| 32 | +#include <math.h> |
| 33 | + |
| 34 | +//the opcodes for the MAX7221 and MAX7219 |
| 35 | +#define OP_NOOP 0x00 |
| 36 | +#define OP_DIGIT0 0x01 |
| 37 | +#define OP_DIGIT1 0x02 |
| 38 | +#define OP_DIGIT2 0x03 |
| 39 | +#define OP_DIGIT3 0x04 |
| 40 | +#define OP_DIGIT4 0x05 |
| 41 | +#define OP_DIGIT5 0x06 |
| 42 | +#define OP_DIGIT6 0x07 |
| 43 | +#define OP_DIGIT7 0x08 |
| 44 | +#define OP_DECODEMODE 0x09 |
| 45 | +#define OP_INTENSITY 0x0a |
| 46 | +#define OP_SCANLIMIT 0x0b |
| 47 | +#define OP_SHUTDOWN 0x0c |
| 48 | +#define OP_DISPLAYTEST 0x0f |
| 49 | + |
| 50 | +/* |
| 51 | + * Segments to be switched on for characters and digits on |
| 52 | + * 7-Segment Displays |
| 53 | + */ |
| 54 | +const static uint8_t charTable[] = { |
| 55 | + 0b01111110, 0b00110000, 0b01101101, |
| 56 | + 0b01111001, 0b00110011, 0b01011011, 0b01011111, 0b01110000, 0b01111111, |
| 57 | + 0b01111011, 0b01110111, 0b00011111, 0b00001101, 0b00111101, 0b01001111, |
| 58 | + 0b01000111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 59 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 60 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 61 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 62 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b10000000, |
| 63 | + 0b00000001, 0b10000000, 0b00000000, 0b01111110, 0b00110000, 0b01101101, |
| 64 | + 0b01111001, 0b00110011, 0b01011011, 0b01011111, 0b01110000, 0b01111111, |
| 65 | + 0b01111011, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 66 | + 0b00000000, 0b00000000, 0b01110111, 0b00011111, 0b00001101, 0b00111101, |
| 67 | + 0b01001111, 0b01000111, 0b00000000, 0b00110111, 0b00000000, 0b00000000, |
| 68 | + 0b00000000, 0b00001110, 0b00000000, 0b00000000, 0b00000000, 0b01100111, |
| 69 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 70 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 71 | + 0b00000000, 0b00000000, 0b00001000, 0b00000000, 0b01110111, 0b00011111, |
| 72 | + 0b00001101, 0b00111101, 0b01001111, 0b01000111, 0b00000000, 0b00110111, |
| 73 | + 0b00000000, 0b00000000, 0b00000000, 0b00001110, 0b00000000, 0b00010101, |
| 74 | + 0b00011101, 0b01100111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 75 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, |
| 76 | + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 }; |
| 77 | + |
| 78 | + |
| 79 | +MAX7219::MAX7219(SPI *spi, int numDevices) { |
| 80 | + assert(spi != nullptr); |
| 81 | + this->spi = spi; |
| 82 | + if (numDevices <= 0 || numDevices > 8) { |
| 83 | + numDevices = 8; |
| 84 | + } |
| 85 | + maxDevices = numDevices; |
| 86 | + |
| 87 | + for (int i = 0; i < 64; i++) { |
| 88 | + status[i] = 0x00; |
| 89 | + } |
| 90 | + for (int i = 0; i < maxDevices; i++) { |
| 91 | + spiTransfer(i, OP_DISPLAYTEST, 0); |
| 92 | + //scanlimit is set to max on startup |
| 93 | + setScanLimit(7, i); |
| 94 | + //decode is done in source |
| 95 | + spiTransfer(i, OP_DECODEMODE, 0); |
| 96 | + clearDisplay(i); |
| 97 | + //we go into shutdown-mode on startup |
| 98 | + shutdown(true, i); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +int MAX7219::getDeviceCount() { |
| 104 | + return maxDevices; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +void MAX7219::shutdown(bool b, int addr) { |
| 109 | + if (addr < 0 || addr >= maxDevices) |
| 110 | + return; |
| 111 | + if (b) { |
| 112 | + spiTransfer(addr, OP_SHUTDOWN, 0); |
| 113 | + } |
| 114 | + else { |
| 115 | + spiTransfer(addr, OP_SHUTDOWN, 1); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void MAX7219::setScanLimit(int limit, int addr) { |
| 121 | + if (addr < 0 || addr >= maxDevices) { |
| 122 | + return; |
| 123 | + } |
| 124 | + if (limit >= 0 && limit < 8) { |
| 125 | + spiTransfer(addr, OP_SCANLIMIT, limit); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +void MAX7219::setIntensity(int intensity, int addr) { |
| 131 | + if (addr < 0 || addr >= maxDevices) { |
| 132 | + return; |
| 133 | + } |
| 134 | + if (intensity >= 0 && intensity < 16) { |
| 135 | + spiTransfer(addr, OP_INTENSITY, intensity); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +void MAX7219::clearDisplay(int addr) { |
| 141 | + int offset; |
| 142 | + |
| 143 | + if (addr < 0 || addr >= maxDevices) { |
| 144 | + return; |
| 145 | + } |
| 146 | + offset = addr * 8; |
| 147 | + for (int i = 0; i < 8; i++) { |
| 148 | + status[offset + i] = 0; |
| 149 | + spiTransfer(addr, i + 1, status[offset + i]); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +void MAX7219::setLed(int row, int column, bool state, int addr) { |
| 155 | + int offset; |
| 156 | + uint8_t val = 0x00; |
| 157 | + |
| 158 | + if (addr < 0 || addr >= maxDevices) { |
| 159 | + return; |
| 160 | + } |
| 161 | + if (row < 0 || row > 7 || column < 0 || column > 7) { |
| 162 | + return; |
| 163 | + } |
| 164 | + offset = addr * 8; |
| 165 | + val = 0b10000000 >> column; |
| 166 | + if (state) { |
| 167 | + status[offset + row] = status[offset + row] | val; |
| 168 | + } |
| 169 | + else { |
| 170 | + val = ~val; |
| 171 | + status[offset + row] = status[offset + row] & val; |
| 172 | + } |
| 173 | + spiTransfer(addr, row + 1, status[offset + row]); |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +void MAX7219::setRow(int row, uint8_t value, int addr) { |
| 178 | + int offset; |
| 179 | + if (addr < 0 || addr >= maxDevices) { |
| 180 | + return; |
| 181 | + } |
| 182 | + if (row < 0 || row > 7) { |
| 183 | + return; |
| 184 | + } |
| 185 | + offset = addr * 8; |
| 186 | + status[offset + row] = value; |
| 187 | + spiTransfer(addr, row + 1, status[offset + row]); |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +void MAX7219::setColumn(int col, uint8_t value, int addr) { |
| 192 | + uint8_t val; |
| 193 | + |
| 194 | + if (addr < 0 || addr >= maxDevices) { |
| 195 | + return; |
| 196 | + } |
| 197 | + if (col < 0 || col > 7) { |
| 198 | + return; |
| 199 | + } |
| 200 | + for (int row = 0; row < 8; row++) { |
| 201 | + val = value >> (7 - row); |
| 202 | + val = val & 0x01; |
| 203 | + setLed(row, col, val, addr); |
| 204 | + } |
| 205 | +} |
| 206 | +
10000
div> |
| 207 | + |
| 208 | +void MAX7219::setDigit(int digit, uint8_t value, bool dp, int addr) { |
| 209 | + int offset; |
| 210 | + uint8_t v; |
| 211 | + |
| 212 | + if (addr < 0 || addr >= maxDevices) { |
| 213 | + return; |
| 214 | + } |
| 215 | + if (digit < 0 || digit > 7 || value > 15) { |
| 216 | + return; |
| 217 | + } |
| 218 | + offset = addr * 8; |
| 219 | + v = charTable[value]; |
| 220 | + if (dp) { |
| 221 | + v |= 0b10000000; |
| 222 | + } |
| 223 | + status[offset + digit] = v; |
| 224 | + spiTransfer(addr, digit + 1, v); |
| 225 | +} |
| 226 | + |
| 227 | + |
| 228 | +void MAX7219::setChar(int digit, char value, bool dp, int addr) { |
| 229 | + int offset; |
| 230 | + uint8_t index, v; |
| 231 | + |
| 232 | + if (addr < 0 || addr >= maxDevices) { |
| 233 | + return; |
| 234 | + } |
| 235 | + if (digit < 0 || digit > 7) { |
| 236 | + return; |
| 237 | + } |
| 238 | + offset = addr * 8; |
| 239 | + index = (uint8_t) value; |
| 240 | + if (index > 127) { |
| 241 | + //no defined beyond index 127, so we use the space char |
| 242 | + index = 32; |
| 243 | + } |
| 244 | + v = charTable[index]; |
| 245 | + if (dp) { |
| 246 | + v |= 0b10000000; |
| 247 | + } |
| 248 | + status[offset + digit] = v; |
| 249 | + spiTransfer(addr, digit + 1, v); |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | +void MAX7219::spiTransfer(int addr, volatile uint8_t opcode, volatile uint8_t data) { |
| 254 | + //Create an array with the data to shift out |
| 255 | + int offset = addr * 2; |
| 256 | + int maxbytes = maxDevices * 2; |
| 257 | + |
| 258 | + /* The array for shifting the data to the devices */ |
| 259 | + uint8_t spidata[16]; |
| 260 | + |
| 261 | + for (int i = 0; i < maxbytes; i++) { |
| 262 | + spidata[i] = (uint8_t) 0; |
| 263 | + } |
| 264 | + //put our device data into the array |
| 265 | + spidata[offset] = opcode; |
| 266 | + spidata[offset + 1] = data; |
| 267 | + spi->transfer(spidata, maxbytes); |
| 268 | +} |
| 269 | + |
| 270 | +void MAX7219::setNumber(uint32_t number, int addr) { |
| 271 | + //number = number % (uint32_t)pow(10, maxDevices); |
| 272 | + for (auto i=0; i<8; i++) { |
| 273 | + if (number == 0 && i > 0){ |
| 274 | + setChar(i, ' ', addr); |
| 275 | + } else { |
| 276 | + setDigit(i, number%10, addr); |
| 277 | + number = number/10; |
| 278 | + } |
| 279 | + } |
| 280 | +} |
0 commit comments