Control Surface master
MIDI Control Surface library for Arduino
MAX7219_Base.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
9
11#include <SPI.h>
13
15
24template <class SPIDriver = decltype(SPI) &>
26 public:
37 MAX7219_Base(SPIDriver spi, pin_t loadPin, uint8_t chainlength = 1)
38 : spi(std::forward<SPIDriver>(spi)), loadPin(loadPin),
39 chainlength(chainlength) {}
40
41 static constexpr uint8_t DECODEMODE = 9;
42 static constexpr uint8_t INTENSITY = 10;
43 static constexpr uint8_t SCANLIMIT = 11;
44 static constexpr uint8_t SHUTDOWN = 12;
45 static constexpr uint8_t DISPLAYTEST = 15;
46
50 void begin() {
51 ExtIO::digitalWrite(loadPin, HIGH);
52 ExtIO::pinMode(loadPin, OUTPUT);
53 spi.begin();
54 sendRawAll(DISPLAYTEST, 0); // Normal operation, no test mode
55 sendRawAll(SCANLIMIT, 7); // Scan all 8 digits
56 sendRawAll(DECODEMODE, 0); // Raw LED addressing
57 sendRawAll(INTENSITY, 0xF); // Maximum intensity
58 clear();
59 sendRawAll(SHUTDOWN, 1); // Enable the display
60 }
61
65 void clear() {
66 for (uint8_t j = 1; j < 8 + 1; j++)
67 sendRawAll(j, 0);
68 }
69
80 void send(uint8_t digit, uint8_t value, uint8_t chip = 0) {
81 sendRaw((digit & 0x7) + 1, value, chip);
82 }
83
115 void sendRowAll(uint8_t digit, const uint8_t *values,
116 uint8_t leading_dim = 1) {
117 uint8_t opcode = (digit & 0x7) + 1;
118 ExtIO::digitalWrite(loadPin, LOW);
119 spi.beginTransaction(settings);
120 for (uint8_t i = 0; i < chainlength; ++i) {
121 spi.transfer(opcode);
122 spi.transfer(values[uint16_t(i) * leading_dim]);
123 }
124 ExtIO::digitalWrite(loadPin, HIGH);
125 spi.endTransaction();
126 }
127
146 void sendAll(const uint8_t *values) {
147 for (uint8_t row = 0; row < 8; ++row)
148 sendRowAll(row, values + row, 8);
149 }
150
159 void sendRawAll(uint8_t opcode, uint8_t value) {
160 ExtIO::digitalWrite(loadPin, LOW);
161 spi.beginTransaction(settings);
162 for (uint8_t i = 0; i < chainlength; ++i) {
163 spi.transfer(opcode);
164 spi.transfer(value);
165 }
166 ExtIO::digitalWrite(loadPin, HIGH);
167 spi.endTransaction();
168 }
169
180 void sendRaw(uint8_t opcode, uint8_t value, uint8_t chip = 0) {
181 if (chip >= chainlength)
182 return; // Should I throw an error?
183 ExtIO::digitalWrite(loadPin, LOW);
184 spi.beginTransaction(settings);
185 uint8_t c = 0;
186 for (; c < chip; c++) {
187 spi.transfer(0x00); // No-Op
188 spi.transfer(0x00);
189 }
190 spi.transfer(opcode);
191 spi.transfer(value);
192 for (c++; c < chainlength; c++) {
193 spi.transfer(0x00); // No-Op
194 spi.transfer(0x00);
195 }
196 ExtIO::digitalWrite(loadPin, HIGH);
197 spi.endTransaction();
198 }
199
206 void setIntensity(uint8_t intensity) {
207 sendRawAll(INTENSITY, intensity & 0xF);
208 }
209
218 void setIntensity(uint8_t intensity, uint8_t chip) {
219 sendRaw(INTENSITY, intensity & 0xF, chip);
220 }
221
225 uint8_t getChainLength() const { return chainlength; }
226
227 private:
228 SPIDriver spi;
230 uint8_t chainlength;
231
232 public:
233 SPISettings settings{SPI_MAX_SPEED, MSBFIRST, SPI_MODE0};
234};
235
237
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
constexpr PinMode_t OUTPUT
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A base class for classes that control MAX7219 LED drivers.
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.
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.
static constexpr Frequency SPI_MAX_SPEED
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).