Control Surface  1.1.0
MIDI Control Surface library for Arduino
ShiftRegisterOutBase.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"
10 
12 
13 /**
14  * @brief A class for serial-in/parallel-out shift registers,
15  * like the 74HC595.
16  *
17  * @tparam N
18  * The number of bits in total. Usually, shift registers (e.g. the
19  * 74HC595) have eight bits per chip, so `length = 8 * k` where `k`
20  * is the number of cascaded chips.
21  *
22  * @ingroup AH_ExtIO
23  */
24 template <uint8_t N>
26  public:
27 #if defined(__SAM3X8E__) || defined(__SAMD21G18A__)
28  using BitOrder_t = BitOrder;
29 #else
30  using BitOrder_t = uint8_t;
31 #endif
32 
33  protected:
34  /**
35  * @brief Create a new ShiftRegisterOutBase object with a given bit order,
36  * and a given number of outputs.
37  *
38  * @param latchPin
39  * The digital output pin connected to the latch pin (ST_CP or
40  * RCLK) of the shift register.
41  * @param bitOrder
42  * Either `MSBFIRST` (most significant bit first) or `LSBFIRST`
43  * (least significant bit first).
44  */
45  ShiftRegisterOutBase(pin_t latchPin, BitOrder_t bitOrder);
46 
47  public:
48  /**
49  * @brief The pinMode function is not implemented because the mode is
50  * `OUTPUT` by definition.
51  */
52  void pinMode(pin_t pin, uint8_t mode) override __attribute__((deprecated)) {
53  (void)pin;
54  (void)mode;
55  }
56 
57  /**
58  * @brief Set the state of a given output pin.
59  *
60  * @param pin
61  * The shift register pin to set.
62  * @param val
63  * The value to set the pin to.
64  * (Either `HIGH` (1) or `LOW` (0))
65  */
66  void digitalWrite(pin_t pin, uint8_t val) override;
67 
68  /**
69  * @brief Get the current state of a given output pin.
70  *
71  * @param pin
72  * The shift register pin to read from.
73  * @retval 0
74  * The state of the pin is `LOW`.
75  * @retval 1
76  * The state of the pin is `HIGH`.
77  */
78  int digitalRead(pin_t pin) override;
79 
80  /**
81  * @brief The analogRead function is deprecated because a shift
82  * is always digital.
83  * @param pin
84  * The shift register pin to read from.
85  * @retval 0
86  * The state of the pin is `LOW`.
87  * @retval 1023
88  * The state of the pin is `HIGH`.
89  */
90  analog_t analogRead(pin_t pin) override __attribute__((deprecated)) {
91  return 1023 * digitalRead(pin);
92  }
93 
94  /**
95  * @brief The analogWrite function is not deprecated because a shift
96  * is always digital.
97  * @param pin
98  * The shift register pin to set.
99  * @param val
100  * The value to set the pin to. A value greater or equal to 0x80
101  * will set the pin to a `HIGH` state, a value less than 0x80 will
102  * set the pin to a `LOW` state.
103  */
104  void analogWrite(pin_t pin, analog_t val) override
105  __attribute__((deprecated)) {
106  digitalWrite(pin, val >= 0x80);
107  }
108 
109  /**
110  * @brief Get the red output pin of the given LED.
111  *
112  * @param id
113  * The zero-based LED number.
114  */
115  pin_t red(pin_t id);
116 
117  /**
118  * @brief Get an array containing all pins with red LEDs.
119  */
120  Array<pin_t, N / 3> redPins();
121 
122  /**
123  * @brief Get the green output pin of the given LED.
124  *
125  * @param id
126  * The zero-based LED number.
127  */
128  pin_t green(pin_t id);
129 
130  /**
131  * @brief Get an array containing all pins with green LEDs.
132  */
133  Array<pin_t, N / 3> greenPins();
134 
135  /**
136  * @brief Get the blue output pin of the given LED.
137  *
138  * @param id
139  * The zero-based LED number.
140  */
141  pin_t blue(pin_t id);
142 
143  /**
144  * @brief Get an array containing all pins with blue LEDs.
145  */
146  Array<pin_t, N / 3> bluePins();
147 
148  protected:
151 
153  bool dirty = true;
154 };
155 
157 
158 #include "ShiftRegisterOutBase.ipp"
159 
AH::ShiftRegisterOutBase::analogWrite
void analogWrite(pin_t pin, analog_t val) override __attribute__((deprecated))
The analogWrite function is not deprecated because a shift is always digital.
Definition: ShiftRegisterOutBase.hpp:104
Warnings.hpp
AH::ShiftRegisterOutBase::buffer
BitArray< N > buffer
Definition: ShiftRegisterOutBase.hpp:152
AH::ShiftRegisterOutBase
A class for serial-in/parallel-out shift registers, like the 74HC595.
Definition: ShiftRegisterOutBase.hpp:25
AH::analog_t
uint16_t analog_t
The type returned from analogRead and similar functions.
Definition: Hardware-Types.hpp:15
BitArray.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36
ShiftRegisterOutBase.ipp
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:58
AH::StaticSizeExtendedIOElement
A class for ExtendedIOElements with a fixed size.
Definition: StaticSizeExtendedIOElement.hpp:19
AH::ShiftRegisterOutBase::BitOrder_t
uint8_t BitOrder_t
Definition: ShiftRegisterOutBase.hpp:30
AH::ShiftRegisterOutBase::analogRead
analog_t analogRead(pin_t pin) override __attribute__((deprecated))
The analogRead function is deprecated because a shift is always digital.
Definition: ShiftRegisterOutBase.hpp:90
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::ShiftRegisterOutBase::latchPin
const pin_t latchPin
Definition: ShiftRegisterOutBase.hpp:149
AH::BitArray
A class for arrays of bits.
Definition: BitArray.hpp:25
AH::ShiftRegisterOutBase::bitOrder
const BitOrder_t bitOrder
Definition: ShiftRegisterOutBase.hpp:150
AH::ShiftRegisterOutBase::pinMode
void pinMode(pin_t pin, uint8_t mode) override __attribute__((deprecated))
The pinMode function is not implemented because the mode is OUTPUT by definition.
Definition: ShiftRegisterOutBase.hpp:52
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:47