Arduino Helpers master
Utility library for Arduino
MAX7219_Base.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6
8#include <SPI.h>
10
12
21template <class SPIDriver = decltype(SPI) &>
23 public:
34 MAX7219_Base(SPIDriver spi, pin_t loadPin, uint8_t chainlength = 1)
35 : spi(std::forward<SPIDriver>(spi)), loadPin(loadPin),
36 chainlength(chainlength) {}
37
38 static constexpr uint8_t DECODEMODE = 9;
39 static constexpr uint8_t INTENSITY = 10;
40 static constexpr uint8_t SCANLIMIT = 11;
41 static constexpr uint8_t SHUTDOWN = 12;
42 static constexpr uint8_t DISPLAYTEST = 15;
43
47 void begin() {
48 ExtIO::digitalWrite(loadPin, HIGH);
49 ExtIO::pinMode(loadPin, OUTPUT);
50 spi.begin();
51 sendRawAll(DISPLAYTEST, 0); // Normal operation, no test mode
52 sendRawAll(SCANLIMIT, 7); // Scan all 8 digits
53 sendRawAll(DECODEMODE, 0); // Raw LED addressing
54 sendRawAll(INTENSITY, 0xF); // Maximum intensity
55 clear();
56 sendRawAll(SHUTDOWN, 1); // Enable the display
57 }
58
62 void clear() {
63 for (uint8_t j = 1; j < 8 + 1; j++)
64 sendRawAll(j, 0);
65 }
66
77 void send(uint8_t digit, uint8_t value, uint8_t chip = 0) {
78 sendRaw((digit & 0x7) + 1, value, chip);
79 }
80
112 void sendRowAll(uint8_t digit, const uint8_t *values,
113 uint8_t leading_dim = 1) {
114 uint8_t opcode = (digit & 0x7) + 1;
115 ExtIO::digitalWrite(loadPin, LOW);
116 spi.beginTransaction(settings);
117 for (uint8_t i = 0; i < chainlength; ++i) {
118 spi.transfer(opcode);
119 spi.transfer(values[uint16_t(i) * leading_dim]);
120 }
121 ExtIO::digitalWrite(loadPin, HIGH);
122 spi.endTransaction();
123 }
124
143 void sendAll(const uint8_t *values) {
144 for (uint8_t row = 0; row < 8; ++row)
145 sendRowAll(row, values + row, 8);
146 }
147
156 void sendRawAll(uint8_t opcode, uint8_t value) {
157 ExtIO::digitalWrite(loadPin, LOW);
158 spi.beginTransaction(settings);
159 for (uint8_t i = 0; i < chainlength; ++i) {
160 spi.transfer(opcode);
161 spi.transfer(value);
162 }
163 ExtIO::digitalWrite(loadPin, HIGH);
164 spi.endTransaction();
165 }
166
177 void sendRaw(uint8_t opcode, uint8_t value, uint8_t chip = 0) {
178 if (chip >= chainlength)
179 return; // Should I throw an error?
180 ExtIO::digitalWrite(loadPin, LOW);
181 spi.beginTransaction(settings);
182 uint8_t c = 0;
183 for (; c < chip; c++) {
184 spi.transfer(0x00); // No-Op
185 spi.transfer(0x00);
186 }
187 spi.transfer(opcode);
188 spi.transfer(value);
189 for (c++; c < chainlength; c++) {
190 spi.transfer(0x00); // No-Op
191 spi.transfer(0x00);
192 }
193 ExtIO::digitalWrite(loadPin, HIGH);
194 spi.endTransaction();
195 }
196
203 void setIntensity(uint8_t intensity) {
204 sendRawAll(INTENSITY, intensity & 0xF);
205 }
206
215 void setIntensity(uint8_t intensity, uint8_t chip) {
216 sendRaw(INTENSITY, intensity & 0xF, chip);
217 }
218
222 uint8_t getChainLength() const { return chainlength; }
223
224 private:
225 SPIDriver spi;
227 uint8_t chainlength;
228
229 public:
230 SPISettings settings{SPI_MAX_SPEED, MSBFIRST, SPI_MODE0};
231};
232
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
constexpr PinMode_t OUTPUT
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
static constexpr Frequency SPI_MAX_SPEED
Definition: Settings.hpp:92
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:51
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:50
A base class for classes that control MAX7219 LED drivers.
SPIDriver spi
void sendRawAll(uint8_t opcode, uint8_t value)
Send the same raw opcode and value to all chips in the chain.
uint8_t getChainLength() const
Get the number of daisy-chained chips.
void sendRaw(uint8_t opcode, uint8_t value, uint8_t chip=0)
Send a raw opcode and value to the given MAX7219.
void sendAll(const uint8_t *values)
Send different values to all digits/rows of all chips.
uint8_t chainlength
void begin()
Initialize the Arduino pins, SPI, and the MAX7219.
void clear()
Turn off all LEDs.
void sendRowAll(uint8_t digit, const uint8_t *values, uint8_t leading_dim=1)
Send values to the given digit or row, sending a different value for each chip.
void setIntensity(uint8_t intensity, uint8_t chip)
Set the intensity of the LEDs of a specific chip.
void setIntensity(uint8_t intensity)
Set the intensity of the LEDs of all chips.
void send(uint8_t digit, uint8_t value, uint8_t chip=0)
Send the value to the given digit or row.
MAX7219_Base(SPIDriver spi, pin_t loadPin, uint8_t chainlength=1)
Create a MAX7219_Base object.
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
Definition: vector.cpp:5