Control Surface  1.2.0
MIDI Control Surface library for Arduino
MAX7219.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7 
8 #include "StaticSizeExtendedIOElement.hpp"
11 
13 
24 template <uint8_t NumChips = 1>
25 class MAX7219 : public MAX7219_Base,
26  public StaticSizeExtendedIOElement<8 * 8 * NumChips> {
27  public:
34  MAX7219(pin_t loadPin) : MAX7219_Base(loadPin, NumChips) {}
35 
38  void begin() override { init(); }
39 
40  private:
41  struct IndexMask {
42  uint8_t row;
43  uint8_t col;
44  uint8_t rowgrp;
45  uint8_t rowmask;
46  uint8_t colmask;
47  };
48 
49  static IndexMask pin2index(pin_t pin) {
50  uint8_t row = pin / 8;
51  uint8_t col = pin % 8;
52  uint8_t rowgrp = row % 8;
53  uint8_t rowmask = 1 << rowgrp;
54  uint8_t colmask = 1 << col;
55  return {row, col, rowgrp, rowmask, colmask};
56  }
57 
58  public:
63  void pinMode(pin_t pin, PinMode_t mode) override
64  __attribute__((deprecated)) {
65  (void)pin;
66  (void)mode;
67  }
68 
72  void pinModeBuffered(pin_t pin, PinMode_t mode) override
73  __attribute__((deprecated)) {
74  (void)pin;
75  (void)mode;
76  }
77 
87  void digitalWrite(pin_t pin, PinStatus_t val) override {
88  IndexMask i = pin2index(pin);
89  val ? buffer[i.row] |= i.colmask // set the pin (high)
90  : buffer[i.row] &= ~i.colmask; // clear the pin (low)
91  updateBufferedOutputRow(i);
92  }
93 
100  void digitalWriteBuffered(pin_t pin, PinStatus_t val) override {
101  IndexMask i = pin2index(pin);
102  val ? buffer[i.row] |= i.colmask // set the pin (high)
103  : buffer[i.row] &= ~i.colmask; // clear the pin (low)
104  dirty_rows |= i.rowmask;
105  }
106 
117  int digitalRead(pin_t pin) override {
118  IndexMask i = pin2index(pin);
119  return bool(buffer[i.row] & i.colmask);
120  }
121 
125  int digitalReadBuffered(pin_t pin) override {
126  IndexMask i = pin2index(pin);
127  return bool(buffer[i.row] & i.colmask);
128  }
129 
140  analog_t analogRead(pin_t pin) override __attribute__((deprecated)) {
141  return 1023 * digitalRead(pin);
142  }
143 
148  __attribute__((deprecated)) {
149  return 1023 * digitalRead(pin);
150  }
151 
165  void analogWrite(pin_t pin, analog_t val) override
166  __attribute__((deprecated)) {
167  digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
168  }
169 
173  void analogWriteBuffered(pin_t pin, analog_t val) override
174  __attribute__((deprecated)) {
175  digitalWrite(pin, val >= 0x80 ? HIGH : LOW);
176  }
177 
179  sendRowAll(i.rowgrp, buffer.data + i.rowgrp, 8);
180  dirty_rows &= ~i.rowmask;
181  }
182 
183  void updateBufferedOutputs() override {
184  if (dirty_rows == 0)
185  return;
186  uint8_t row = 8;
187  do {
188  --row;
189  if (dirty_rows & 0x80)
190  sendRowAll(row, buffer.data + row, 8);
191  dirty_rows <<= 1;
192  } while (row);
193  }
194 
195  void updateBufferedInputs() override {}
196 
197  private:
199  uint8_t dirty_rows = 0xFF;
200 };
201 
203 
AH::MAX7219::digitalWrite
void digitalWrite(pin_t pin, PinStatus_t val) override
Set the state of a given output pin.
Definition: MAX7219.hpp:87
AH::MAX7219::updateBufferedInputs
void updateBufferedInputs() override
Read the physical state into the input buffers.
Definition: MAX7219.hpp:195
LOW
const PinStatus_t LOW
Definition: ExtendedInputOutput.hpp:57
AH::MAX7219_Base
A base class for classes that control MAX7219 LED drivers.
Definition: MAX7219_Base.hpp:23
Warnings.hpp
AH::MAX7219::begin
void begin() override
Initialize.
Definition: MAX7219.hpp:38
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
AH::MAX7219::IndexMask
Definition: MAX7219.hpp:41
AH::MAX7219::updateBufferedOutputRow
void updateBufferedOutputRow(IndexMask i)
Definition: MAX7219.hpp:178
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::MAX7219::IndexMask::colmask
uint8_t colmask
Definition: MAX7219.hpp:46
BitArray.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
AH::MAX7219::IndexMask::rowgrp
uint8_t rowgrp
Definition: MAX7219.hpp:44
AH::MAX7219::updateBufferedOutputs
void updateBufferedOutputs() override
Write the internal state to the physical outputs.
Definition: MAX7219.hpp:183
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:48
AH::MAX7219::pin2index
static IndexMask pin2index(pin_t pin)
Definition: MAX7219.hpp:49
AH::MAX7219::pinMode
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:63
HIGH
const PinStatus_t HIGH
Definition: ExtendedInputOutput.hpp:56
AH::MAX7219::digitalReadBuffered
int digitalReadBuffered(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:125
AH::Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36
AH::MAX7219::MAX7219
MAX7219(pin_t loadPin)
Create a MAX7219 ExtendedIOElement.
Definition: MAX7219.hpp:34
AH::MAX7219::pinModeBuffered
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:72
PinStatus_t
uint8_t PinStatus_t
Definition: ExtendedInputOutput.hpp:48
AH::analog_t
uint16_t analog_t
The type returned from analogRead and similar functions.
Definition: Hardware-Types.hpp:15
AH::MAX7219
A class for LED outputs using the MAX7219 LED display driver.
Definition: MAX7219.hpp:26
AH::StaticSizeExtendedIOElement
A class for ExtendedIOElements with a fixed size.
Definition: StaticSizeExtendedIOElement.hpp:19
AH::MAX7219::buffer
Array< uint8_t, 8 *NumChips > buffer
Definition: MAX7219.hpp:198
AH::MAX7219::analogRead
analog_t analogRead(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:140
AH::MAX7219::IndexMask::row
uint8_t row
Definition: MAX7219.hpp:42
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
AH::MAX7219::analogWrite
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:165
AH::MAX7219::digitalRead
int digitalRead(pin_t pin) override
Get the current state of a given output.
Definition: MAX7219.hpp:117
AH::MAX7219::IndexMask::col
uint8_t col
Definition: MAX7219.hpp:43
AH::MAX7219::IndexMask::rowmask
uint8_t rowmask
Definition: MAX7219.hpp:45
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
PinMode_t
uint8_t PinMode_t
Definition: ExtendedInputOutput.hpp:49
AH::MAX7219::digitalWriteBuffered
void digitalWriteBuffered(pin_t pin, PinStatus_t val) override
Set the state of a given pin in the software buffer.
Definition: MAX7219.hpp:100
MAX7219_Base.hpp
AH::MAX7219::analogWriteBuffered
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:173
AH::MAX7219::analogReadBuffered
analog_t analogReadBuffered(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a MAX7219 is always digital.
Definition: MAX7219.hpp:147