Arduino Helpers master
Utility library for Arduino
MAX7219.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
8
10
29template <uint8_t NumChips = 1, class SPIDriver = decltype(SPI) &>
30class MAX7219 : public MAX7219_Base<SPIDriver>,
31 public StaticSizeExtendedIOElement<8 * 8 * NumChips> {
32 public:
42 : MAX7219_Base<SPIDriver>(std::forward<SPIDriver>(spi), loadPin,
43 NumChips) {}
44
48
49 private:
50 struct IndexMask {
51 uint8_t row;
52 uint8_t col;
53 uint8_t rowgrp;
54 uint8_t rowmask;
55 uint8_t colmask;
56 };
57
59 uint8_t row = pin / 8;
60 uint8_t col = pin % 8;
61 uint8_t rowgrp = row % 8;
62 uint8_t rowmask = 1 << rowgrp;
63 uint8_t colmask = 1 << col;
64 return {row, col, rowgrp, rowmask, colmask};
65 }
66
67 public:
72 void pinMode(pin_t pin, PinMode_t mode) override
73 __attribute__((deprecated)) {
74 (void)pin;
75 (void)mode;
76 }
77
81 void pinModeBuffered(pin_t pin, PinMode_t mode) override
82 __attribute__((deprecated)) {
83 (void)pin;
84 (void)mode;
85 }
86
96 void digitalWrite(pin_t pin, PinStatus_t val) override {
98 val ? buffer[i.row] |= i.colmask // set the pin (high)
99 : buffer[i.row] &= ~i.colmask; // clear the pin (low)
101 }
102
111 val ? buffer[i.row] |= i.colmask // set the pin (high)
112 : buffer[i.row] &= ~i.colmask; // clear the pin (low)
113 dirty_rows |= i.rowmask;
114 }
115
128 return bool(buffer[i.row] & i.colmask) ? HIGH : LOW;
129 }
130
136 return bool(buffer[i.row] & i.colmask) ? HIGH : LOW;
137 }
138
149 analog_t analogRead(pin_t pin) override __attribute__((deprecated)) {
150 return 1023 * digitalRead(pin);
151 }
152
157 __attribute__((deprecated)) {
158 return 1023 * digitalRead(pin);
159 }
160
174 void analogWrite(pin_t pin, analog_t val) override
175 __attribute__((deprecated)) {
176 digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
177 }
178
183 __attribute__((deprecated)) {
184 digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
185 }
186
188 this->sendRowAll(i.rowgrp, buffer.data + i.rowgrp, 8);
189 dirty_rows &= ~i.rowmask;
190 }
191
192 void updateBufferedOutputs() override {
193 if (dirty_rows == 0)
194 return;
195 uint8_t row = 8;
196 do {
197 --row;
198 if (dirty_rows & 0x80)
199 this->sendRowAll(row, buffer.data + row, 8);
200 dirty_rows <<= 1;
201 } while (row);
202 }
203
204 void updateBufferedInputs() override {}
205
206 private:
208 uint8_t dirty_rows = 0xFF;
209};
210
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
uint16_t analog_t
The type returned from analogRead and similar functions.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
pin_t pin(pin_t pin) const
Get the extended IO pin number of a given physical pin of this extended IO element.
A base class for classes that control MAX7219 LED drivers.
void begin()
Initialize the Arduino pins, SPI, and the MAX7219.
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.
A class for LED outputs using the MAX7219 LED display driver.
Definition: MAX7219.hpp:31
analog_t analogReadBuffered(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:156
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:174
void updateBufferedOutputRow(IndexMask i)
Definition: MAX7219.hpp:187
void updateBufferedOutputs() override
Write the internal state to the physical outputs.
Definition: MAX7219.hpp:192
PinStatus_t digitalReadBuffered(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:134
PinStatus_t digitalRead(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:126
void updateBufferedInputs() override
Read the physical state into the input buffers.
Definition: MAX7219.hpp:204
MAX7219(SPIDriver spi, pin_t loadPin)
Create a MAX7219 ExtendedIOElement.
Definition: MAX7219.hpp:41
void digitalWrite(pin_t pin, PinStatus_t val) override
Set the state of a given output pin.
Definition: MAX7219.hpp:96
void begin() override
Initialize.
Definition: MAX7219.hpp:47
static IndexMask pin2index(pin_t pin)
Definition: MAX7219.hpp:58
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:72
analog_t analogRead(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:149
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:81
Array< uint8_t, 8 *NumChips > buffer
Definition: MAX7219.hpp:207
uint8_t dirty_rows
Definition: MAX7219.hpp:208
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:182
void digitalWriteBuffered(pin_t pin, PinStatus_t val) override
Set the state of a given pin in the software buffer.
Definition: MAX7219.hpp:109
A class for ExtendedIOElements with a fixed size.
Definition: vector.cpp:5
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:32