Control Surface  1.1.0
MIDI Control Surface library for Arduino
MAX7219_Base.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 <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
9 
11 #include <SPI.h>
13 
15 
16 /**
17  * @brief A base class for classes that control MAX7219 LED drivers.
18  *
19  * The SPI interface is used.
20  *
21  * @todo Wiring diagram for SPI connection.
22  */
23 class MAX7219_Base {
24  public:
25  /**
26  * @brief Create a MAX7219_Base object.
27  *
28  * @param loadPin
29  * The pin connected to the load pin (C̄S̄) of the MAX7219.
30  */
31  MAX7219_Base(pin_t loadPin) : loadPin(loadPin) {}
32 
33  static constexpr uint8_t DECODEMODE = 9;
34  static constexpr uint8_t INTENSITY = 10;
35  static constexpr uint8_t SCANLIMIT = 11;
36  static constexpr uint8_t SHUTDOWN = 12;
37  static constexpr uint8_t DISPLAYTEST = 15;
38 
39  /**
40  * @brief Initialize the Arduino pins, SPI, and the MAX7219.
41  * @todo Rename to `update`.
42  */
43  void init() {
44  ExtIO::digitalWrite(loadPin, HIGH);
45  ExtIO::pinMode(loadPin, OUTPUT);
46  SPI.begin();
47  sendRaw(DISPLAYTEST, 0); // Normal operation, no test mode
48  sendRaw(SCANLIMIT, 7); // Scan all 8 digits
49  sendRaw(DECODEMODE, 0); // Raw LED addressing
50  sendRaw(INTENSITY, 0xF); // Maximum intensity
51  clear();
52  sendRaw(SHUTDOWN, 1); // Enable the display
53  }
54 
55  /**
56  * @brief Turn off all LEDs.
57  */
58  void clear() {
59  for (uint8_t j = 1; j < 8 + 1; j++)
60  sendRaw(j, 0);
61  }
62 
63  /**
64  * @brief Send the value to the given digit.
65  *
66  * @param digit
67  * The digit or row to set [0, 7].
68  * @param value
69  * The value to set the row to.
70  */
71  void send(uint8_t digit, uint8_t value) {
72  sendRaw((digit & 0x7) + 1, value);
73  }
74 
75  /**
76  * @brief Send a raw opcode and value to the MAX7219.
77  *
78  * @param opcode
79  * The opcode to send.
80  * @param value
81  * The value to send.
82  */
83  void sendRaw(uint8_t opcode, uint8_t value) {
84  ExtIO::digitalWrite(loadPin, LOW);
85  SPI.beginTransaction(settings);
86  SPI.transfer(opcode);
87  SPI.transfer(value);
88  ExtIO::digitalWrite(loadPin, HIGH);
89  SPI.endTransaction();
90  }
91 
92  /**
93  * @brief Set the intensity of the LEDs.
94  *
95  * @param intensity
96  * The intensity [0, 15].
97  */
98  void setIntensity(uint8_t intensity) {
99  sendRaw(INTENSITY, intensity & 0xF);
100  }
101 
102  private:
104  SPISettings settings = {SPI_MAX_SPEED, MSBFIRST, SPI_MODE0};
105 };
106 
108 
AH::MAX7219_Base
A base class for classes that control MAX7219 LED drivers.
Definition: MAX7219_Base.hpp:23
Warnings.hpp
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:46
AH::MAX7219_Base::init
void init()
Initialize the Arduino pins, SPI, and the MAX7219.
Definition: MAX7219_Base.hpp:43
AH::MAX7219_Base::MAX7219_Base
MAX7219_Base(pin_t loadPin)
Create a MAX7219_Base object.
Definition: MAX7219_Base.hpp:31
AH::MAX7219_Base::setIntensity
void setIntensity(uint8_t intensity)
Set the intensity of the LEDs.
Definition: MAX7219_Base.hpp:98
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:47
AH_DIAGNOSTIC_EXTERNAL_HEADER
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:18
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::MAX7219_Base::loadPin
pin_t loadPin
Definition: MAX7219_Base.hpp:103
AH::MAX7219_Base::sendRaw
void sendRaw(uint8_t opcode, uint8_t value)
Send a raw opcode and value to the MAX7219.
Definition: MAX7219_Base.hpp:83
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
OUTPUT
const uint8_t OUTPUT
Definition: ExtendedInputOutput.hpp:50
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::MAX7219_Base::clear
void clear()
Turn off all LEDs.
Definition: MAX7219_Base.hpp:58
AH::MAX7219_Base::send
void send(uint8_t digit, uint8_t value)
Send the value to the given digit.
Definition: MAX7219_Base.hpp:71
AH::SPI_MAX_SPEED
constexpr static Frequency SPI_MAX_SPEED
Definition: AH/Settings/Settings.hpp:85
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:47