Control Surface master
MIDI Control Surface library for Arduino
MAX7219.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include "StaticSizeExtendedIOElement.hpp"
11
13
26template <uint8_t NumChips = 1, class SPIDriver = decltype(SPI) &>
27class MAX7219 : public MAX7219_Base<SPIDriver>,
28 public StaticSizeExtendedIOElement<8 * 8 * NumChips> {
29 public:
38 MAX7219(SPIDriver spi, pin_t loadPin)
39 : MAX7219_Base<SPIDriver>(std::forward<SPIDriver>(spi), loadPin,
40 NumChips) {}
41
45
46 private:
47 struct IndexMask {
48 uint8_t row;
49 uint8_t col;
50 uint8_t rowgrp;
51 uint8_t rowmask;
52 uint8_t colmask;
53 };
54
56 uint8_t row = pin / 8;
57 uint8_t col = pin % 8;
58 uint8_t rowgrp = row % 8;
59 uint8_t rowmask = 1 << rowgrp;
60 uint8_t colmask = 1 << col;
61 return {row, col, rowgrp, rowmask, colmask};
62 }
63
64 public:
69 void pinMode(pin_t pin, PinMode_t mode) override
70 __attribute__((deprecated)) {
71 (void)pin;
72 (void)mode;
73 }
74
78 void pinModeBuffered(pin_t pin, PinMode_t mode) override
79 __attribute__((deprecated)) {
80 (void)pin;
81 (void)mode;
82 }
83
93 void digitalWrite(pin_t pin, PinStatus_t val) override {
94 IndexMask i = pin2index(pin);
95 val ? buffer[i.row] |= i.colmask // set the pin (high)
96 : buffer[i.row] &= ~i.colmask; // clear the pin (low)
97 updateBufferedOutputRow(i);
98 }
99
106 void digitalWriteBuffered(pin_t pin, PinStatus_t val) override {
107 IndexMask i = pin2index(pin);
108 val ? buffer[i.row] |= i.colmask // set the pin (high)
109 : buffer[i.row] &= ~i.colmask; // clear the pin (low)
110 dirty_rows |= i.rowmask;
111 }
112
124 IndexMask i = pin2index(pin);
125 return bool(buffer[i.row] & i.colmask) ? HIGH : LOW;
126 }
127
132 IndexMask i = pin2index(pin);
133 return bool(buffer[i.row] & i.colmask) ? HIGH : LOW;
134 }
135
146 analog_t analogRead(pin_t pin) override __attribute__((deprecated)) {
147 return 1023 * digitalRead(pin);
148 }
149
154 __attribute__((deprecated)) {
155 return 1023 * digitalRead(pin);
156 }
157
171 void analogWrite(pin_t pin, analog_t val) override
172 __attribute__((deprecated)) {
173 digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
174 }
175
179 void analogWriteBuffered(pin_t pin, analog_t val) override
180 __attribute__((deprecated)) {
181 digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
182 }
183
185 this->sendRowAll(i.rowgrp, buffer.data + i.rowgrp, 8);
186 dirty_rows &= ~i.rowmask;
187 }
188
189 void updateBufferedOutputs() override {
190 if (dirty_rows == 0)
191 return;
192 uint8_t row = 8;
193 do {
194 --row;
195 if (dirty_rows & 0x80)
196 this->sendRowAll(row, buffer.data + row, 8);
197 dirty_rows <<= 1;
198 } while (row);
199 }
200
201 void updateBufferedInputs() override {}
202
203 private:
205 uint8_t dirty_rows = 0xFF;
206};
207
209
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
AH::function_traits< decltype(::pinMode)>::argument_t< 1 > PinMode_t
AH::function_traits< decltype(::digitalWrite)>::argument_t< 1 > PinStatus_t
#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.
A class for LED outputs using the MAX7219 LED display driver.
Definition: MAX7219.hpp:28
analog_t analogReadBuffered(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:153
void analogWrite(pin_t pin, analog_t val) override __attribute__((deprecated))
The analogWrite function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:171
void updateBufferedOutputRow(IndexMask i)
Definition: MAX7219.hpp:184
void updateBufferedOutputs() override
Write the internal state to the physical outputs.
Definition: MAX7219.hpp:189
PinStatus_t digitalReadBuffered(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:131
PinStatus_t digitalRead(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:123
void updateBufferedInputs() override
Read the physical state into the input buffers.
Definition: MAX7219.hpp:201
MAX7219(SPIDriver spi, pin_t loadPin)
Create a MAX7219 ExtendedIOElement.
Definition: MAX7219.hpp:38
void digitalWrite(pin_t pin, PinStatus_t val) override
Set the state of a given output pin.
Definition: MAX7219.hpp:93
void begin() override
Initialize.
Definition: MAX7219.hpp:44
static IndexMask pin2index(pin_t pin)
Definition: MAX7219.hpp:55
void pinMode(pin_t pin, PinMode_t mode) override __attribute__((deprecated))
The pinMode function is not implemented because the mode is OUTPUT by definition.
Definition: MAX7219.hpp:69
analog_t analogRead(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:146
void pinModeBuffered(pin_t pin, PinMode_t mode) override __attribute__((deprecated))
The pinMode function is not implemented because the mode is OUTPUT by definition.
Definition: MAX7219.hpp:78
Array< uint8_t, 8 *NumChips > buffer
Definition: MAX7219.hpp:204
void analogWriteBuffered(pin_t pin, analog_t val) override __attribute__((deprecated))
The analogWrite function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:179
void digitalWriteBuffered(pin_t pin, PinStatus_t val) override
Set the state of a given pin in the software buffer.
Definition: MAX7219.hpp:106
A class for ExtendedIOElements with a fixed size.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
uint16_t analog_t
The type returned from analogRead and similar functions.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36