Control Surface  1.2.0
MIDI Control Surface library for Arduino
BitArray.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 
7 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
8 
9 #include <AH/Error/Error.hpp>
11 #include <stdint.h>
12 
15 
17 
24 template <uint8_t N>
25 class BitArray {
26  public:
33  bool get(uint8_t bitIndex) const {
34  return buffer[getBufferIndex(bitIndex)] & getBufferMask(bitIndex);
35  }
36 
43  void set(uint8_t bitIndex) {
44  buffer[getBufferIndex(bitIndex)] |= getBufferMask(bitIndex);
45  }
46 
53  void clear(uint8_t bitIndex) {
54  buffer[getBufferIndex(bitIndex)] &= ~getBufferMask(bitIndex);
55  }
56 
65  void set(uint8_t bitIndex, bool state) {
66  state ? set(bitIndex) : clear(bitIndex);
67  }
68 
77  uint8_t safeIndex(uint8_t byteIndex) const {
78  if (byteIndex >= getBufferLength()) {
79  ERROR(F("Error: index out of bounds (")
80  << byteIndex << F(", length is ") << getBufferLength()
81  << ')',
82  0xFFFF);
83  return getBufferLength() - 1; // LCOV_EXCL_LINE
84  }
85  return byteIndex;
86  }
87 
99  uint8_t getByte(uint8_t byteIndex) const {
100  return buffer[byteIndex];
101  // return buffer[safeIndex(byteIndex)];
102  }
103 
107  uint8_t getBufferLength() const { return bufferLength; }
108 
109  private:
110  uint8_t getBufferIndex(uint8_t bitIndex) const {
111  return safeIndex(bitIndex / 8);
112  }
113  uint8_t getBufferBit(uint8_t bitIndex) const { return bitIndex % 8; }
114  uint8_t getBufferMask(uint8_t bitIndex) const {
115  return 1 << getBufferBit(bitIndex);
116  }
117 
118  constexpr static uint8_t bufferLength = (uint8_t)((N + 7) / 8);
119  uint8_t buffer[bufferLength] = {};
120 };
121 
123 
125 
Warnings.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
AH::BitArray::set
void set(uint8_t bitIndex)
Set the value of the given bit to 1.
Definition: BitArray.hpp:43
AH::BitArray::get
bool get(uint8_t bitIndex) const
Get the value of the given bit.
Definition: BitArray.hpp:33
AH::BitArray::getBufferMask
uint8_t getBufferMask(uint8_t bitIndex) const
Definition: BitArray.hpp:114
AH::BitArray::safeIndex
uint8_t safeIndex(uint8_t byteIndex) const
Check the given byte index, and return it if it is within the bounds of the array,...
Definition: BitArray.hpp:77
ERROR
#define ERROR(msg, errc)
Print the error message and error code, and stop the execution if FATAL_ERRORS are enabled.
Definition: Error.hpp:42
AH::BitArray::getBufferLength
uint8_t getBufferLength() const
Get the buffer length in bytes.
Definition: BitArray.hpp:107
AH::BitArray::getBufferBit
uint8_t getBufferBit(uint8_t bitIndex) const
Definition: BitArray.hpp:113
AH::BitArray::set
void set(uint8_t bitIndex, bool state)
Set the value of the given bit to the given state.
Definition: BitArray.hpp:65
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23
AH::BitArray::getByte
uint8_t getByte(uint8_t byteIndex) const
Get the byte at the given index.
Definition: BitArray.hpp:99
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::BitArray::clear
void clear(uint8_t bitIndex)
Clear the value of the given bit to 0.
Definition: BitArray.hpp:53
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
NamespaceSettings.hpp
AH::BitArray
A class for arrays of bits.
Definition: BitArray.hpp:25
AH::BitArray::getBufferIndex
uint8_t getBufferIndex(uint8_t bitIndex) const
Definition: BitArray.hpp:110