Max7219 7seg LED library
Dependents: Max7219_7segled_example Max7219_7LED_example
- example: https://developer.mbed.org/users/hotchpotch/code/Max7219_7segled_example/
Max7219.hpp@0:980df3fd4973, 2016-09-24 (annotated)
- Committer:
- hotchpotch
- Date:
- Sat Sep 24 08:20:49 2016 +0000
- Revision:
- 0:980df3fd4973
- Child:
- 1:367abb8e4cfc
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hotchpotch | 0:980df3fd4973 | 1 | /* |
hotchpotch | 0:980df3fd4973 | 2 | * Licence: MIT |
hotchpotch | 0:980df3fd4973 | 3 | * Author: Yuichi Tateno |
hotchpotch | 0:980df3fd4973 | 4 | * |
hotchpotch | 0:980df3fd4973 | 5 | * base code from https://lowreal.net/2016/03/18/1 <Author: cho45> |
hotchpotch | 0:980df3fd4973 | 6 | */ |
hotchpotch | 0:980df3fd4973 | 7 | |
hotchpotch | 0:980df3fd4973 | 8 | #ifndef _MAX_7129_HPP |
hotchpotch | 0:980df3fd4973 | 9 | #define _MAX_7129_HPP |
hotchpotch | 0:980df3fd4973 | 10 | |
hotchpotch | 0:980df3fd4973 | 11 | #include <mbed.h> |
hotchpotch | 0:980df3fd4973 | 12 | |
hotchpotch | 0:980df3fd4973 | 13 | class MAX7219 { |
hotchpotch | 0:980df3fd4973 | 14 | SPI spi; |
hotchpotch | 0:980df3fd4973 | 15 | DigitalOut cs; |
hotchpotch | 0:980df3fd4973 | 16 | |
hotchpotch | 0:980df3fd4973 | 17 | public: |
hotchpotch | 0:980df3fd4973 | 18 | typedef enum { |
hotchpotch | 0:980df3fd4973 | 19 | NOOP = 0b0000, |
hotchpotch | 0:980df3fd4973 | 20 | DIGIT0 = 1, |
hotchpotch | 0:980df3fd4973 | 21 | DIGIT1 = 2, |
hotchpotch | 0:980df3fd4973 | 22 | DIGIT2 = 3, |
hotchpotch | 0:980df3fd4973 | 23 | DIGIT3 = 4, |
hotchpotch | 0:980df3fd4973 | 24 | DIGIT4 = 5, |
hotchpotch | 0:980df3fd4973 | 25 | DIGIT5 = 6, |
hotchpotch | 0:980df3fd4973 | 26 | DIGIT6 = 7, |
hotchpotch | 0:980df3fd4973 | 27 | DIGIT7 = 8, |
hotchpotch | 0:980df3fd4973 | 28 | DECODE_MODE = 0b1001, |
hotchpotch | 0:980df3fd4973 | 29 | INTENSITY = 0b1010, |
hotchpotch | 0:980df3fd4973 | 30 | SCAN_LIMIT = 0b1011, |
hotchpotch | 0:980df3fd4973 | 31 | SHUTDOWN = 0b1100, |
hotchpotch | 0:980df3fd4973 | 32 | DISPLAY_TEST = 0b1111, |
hotchpotch | 0:980df3fd4973 | 33 | } ADDRESS; |
hotchpotch | 0:980df3fd4973 | 34 | |
hotchpotch | 0:980df3fd4973 | 35 | typedef enum { |
hotchpotch | 0:980df3fd4973 | 36 | SHUTDOWN_MODE_SHUTDOWN = 0b0, |
hotchpotch | 0:980df3fd4973 | 37 | SHUTDOWN_MODE_NORMAL = 0b1, |
hotchpotch | 0:980df3fd4973 | 38 | } SHUTDOWN_MODE; |
hotchpotch | 0:980df3fd4973 | 39 | |
hotchpotch | 0:980df3fd4973 | 40 | typedef enum { |
hotchpotch | 0:980df3fd4973 | 41 | NO_DECODE_FOR_DIGITS_7_0 = 0b00000000, |
hotchpotch | 0:980df3fd4973 | 42 | CODE_B_DECODE_FOR_DIGIT_0_NO_DECODE_FOR_DIGITS_7_1 = 0b00000001, |
hotchpotch | 0:980df3fd4973 | 43 | CODE_B_DECODE_FOR_DIGIT_3_0_NO_DECODE_FOR_DIGITS_7_4 = 0b00001111, |
hotchpotch | 0:980df3fd4973 | 44 | CODE_B_DECODE_FOR_DIGIT_7_0 = 0b11111111, |
hotchpotch | 0:980df3fd4973 | 45 | } DECODE_MODE_TYPE; |
hotchpotch | 0:980df3fd4973 | 46 | |
hotchpotch | 0:980df3fd4973 | 47 | MAX7219(SPI _spi, PinName _cs_pin) : |
hotchpotch | 0:980df3fd4973 | 48 | spi(_spi), cs(_cs_pin, 0) |
hotchpotch | 0:980df3fd4973 | 49 | { |
hotchpotch | 0:980df3fd4973 | 50 | } |
hotchpotch | 0:980df3fd4973 | 51 | |
hotchpotch | 0:980df3fd4973 | 52 | void write(uint8_t address, uint8_t data) { |
hotchpotch | 0:980df3fd4973 | 53 | cs = 0; |
hotchpotch | 0:980df3fd4973 | 54 | spi.write(address); |
hotchpotch | 0:980df3fd4973 | 55 | spi.write(data); |
hotchpotch | 0:980df3fd4973 | 56 | cs = 1; |
hotchpotch | 0:980df3fd4973 | 57 | } |
hotchpotch | 0:980df3fd4973 | 58 | |
hotchpotch | 0:980df3fd4973 | 59 | void begin() { |
hotchpotch | 0:980df3fd4973 | 60 | setDecodeMode(CODE_B_DECODE_FOR_DIGIT_7_0); |
hotchpotch | 0:980df3fd4973 | 61 | setScanLimit(7); |
hotchpotch | 0:980df3fd4973 | 62 | setShutdown(false); |
hotchpotch | 0:980df3fd4973 | 63 | |
hotchpotch | 0:980df3fd4973 | 64 | setIntensity(8); |
hotchpotch | 0:980df3fd4973 | 65 | |
hotchpotch | 0:980df3fd4973 | 66 | for (uint8_t i = 0; i < 8; i++) { |
hotchpotch | 0:980df3fd4973 | 67 | write(i + 1, 1); |
hotchpotch | 0:980df3fd4973 | 68 | } |
hotchpotch | 0:980df3fd4973 | 69 | } |
hotchpotch | 0:980df3fd4973 | 70 | |
hotchpotch | 0:980df3fd4973 | 71 | void setDecodeMode(DECODE_MODE_TYPE mode) { |
hotchpotch | 0:980df3fd4973 | 72 | write(DECODE_MODE, mode); |
hotchpotch | 0:980df3fd4973 | 73 | } |
hotchpotch | 0:980df3fd4973 | 74 | |
hotchpotch | 0:980df3fd4973 | 75 | void setScanLimit(uint8_t limit) { |
hotchpotch | 0:980df3fd4973 | 76 | write(SCAN_LIMIT, limit); |
hotchpotch | 0:980df3fd4973 | 77 | } |
hotchpotch | 0:980df3fd4973 | 78 | |
hotchpotch | 0:980df3fd4973 | 79 | void setIntensity(uint8_t intensity) { |
hotchpotch | 0:980df3fd4973 | 80 | write(INTENSITY, intensity); |
hotchpotch | 0:980df3fd4973 | 81 | } |
hotchpotch | 0:980df3fd4973 | 82 | |
hotchpotch | 0:980df3fd4973 | 83 | void setShutdown(bool shutdown) { |
hotchpotch | 0:980df3fd4973 | 84 | write(SHUTDOWN, shutdown ? SHUTDOWN_MODE_SHUTDOWN : SHUTDOWN_MODE_NORMAL); |
hotchpotch | 0:980df3fd4973 | 85 | } |
hotchpotch | 0:980df3fd4973 | 86 | |
hotchpotch | 0:980df3fd4973 | 87 | void print(const char* buf) { |
hotchpotch | 0:980df3fd4973 | 88 | size_t len = strlen(buf); |
hotchpotch | 0:980df3fd4973 | 89 | uint8_t digit = 0, dp = 0; |
hotchpotch | 0:980df3fd4973 | 90 | for (size_t i = 0; i < len && digit < 8; i++) { |
hotchpotch | 0:980df3fd4973 | 91 | const char c = buf[len - i - 1]; |
hotchpotch | 0:980df3fd4973 | 92 | if (c == '.') { |
hotchpotch | 0:980df3fd4973 | 93 | dp = 1<<7; |
hotchpotch | 0:980df3fd4973 | 94 | continue; |
hotchpotch | 0:980df3fd4973 | 95 | } |
hotchpotch | 0:980df3fd4973 | 96 | |
hotchpotch | 0:980df3fd4973 | 97 | uint8_t b; |
hotchpotch | 0:980df3fd4973 | 98 | if (c == '-') { |
hotchpotch | 0:980df3fd4973 | 99 | b = 0b1010; |
hotchpotch | 0:980df3fd4973 | 100 | } else |
hotchpotch | 0:980df3fd4973 | 101 | if (c == ' ') { |
hotchpotch | 0:980df3fd4973 | 102 | b = 0b1111; |
hotchpotch | 0:980df3fd4973 | 103 | } else |
hotchpotch | 0:980df3fd4973 | 104 | if ('0' <= c && c <= '9') { |
hotchpotch | 0:980df3fd4973 | 105 | b = static_cast<uint8_t>(c - '0'); |
hotchpotch | 0:980df3fd4973 | 106 | } else { |
hotchpotch | 0:980df3fd4973 | 107 | continue; |
hotchpotch | 0:980df3fd4973 | 108 | } |
hotchpotch | 0:980df3fd4973 | 109 | |
hotchpotch | 0:980df3fd4973 | 110 | write(digit + 1, b | dp); |
hotchpotch | 0:980df3fd4973 | 111 | dp = 0; |
hotchpotch | 0:980df3fd4973 | 112 | digit++; |
hotchpotch | 0:980df3fd4973 | 113 | } |
hotchpotch | 0:980df3fd4973 | 114 | |
hotchpotch | 0:980df3fd4973 | 115 | for (; digit < 8; digit++) { |
hotchpotch | 0:980df3fd4973 | 116 | write(digit + 1, 0b1111); |
hotchpotch | 0:980df3fd4973 | 117 | } |
hotchpotch | 0:980df3fd4973 | 118 | } |
hotchpotch | 0:980df3fd4973 | 119 | }; |
hotchpotch | 0:980df3fd4973 | 120 | |
hotchpotch | 0:980df3fd4973 | 121 | |
hotchpotch | 0:980df3fd4973 | 122 | #endif // _MAX_7129_HPP |