This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
BitArray.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <Helpers/Error.hpp>
6 #include <stdint.h>
7 
10 
17 template <uint8_t N>
18 class BitArray {
19  public:
26  bool get(uint8_t bitIndex) {
27  return buffer[getBufferIndex(bitIndex)] & getBufferMask(bitIndex);
28  }
29 
36  void set(uint8_t bitIndex) {
37  buffer[getBufferIndex(bitIndex)] |= getBufferMask(bitIndex);
38  }
39 
46  void clear(uint8_t bitIndex) {
47  buffer[getBufferIndex(bitIndex)] &= ~getBufferMask(bitIndex);
48  }
49 
58  void set(uint8_t bitIndex, bool state) {
59  state ? set(bitIndex) : clear(bitIndex);
60  }
61 
70  uint8_t safeIndex(uint8_t byteIndex) const {
71  if (byteIndex >= getBufferLength()) {
72  ERROR(F("Error: index out of bounds (")
73  << byteIndex << F(", length is ") << getBufferLength()
74  << ')',
75  0xFFFF);
76  return getBufferLength() - 1;
77  }
78  return byteIndex;
79  }
80 
92  uint8_t getByte(uint8_t byteIndex) const {
93  return buffer[byteIndex];
94  // return buffer[safeIndex(byteIndex)];
95  }
96 
100  uint8_t getBufferLength() const { return bufferLength; }
101 
102  private:
103  uint8_t getBufferIndex(uint8_t bitIndex) const {
104  return safeIndex(bitIndex / 8);
105  }
106  uint8_t getBufferBit(uint8_t bitIndex) const { return bitIndex % 8; }
107  uint8_t getBufferMask(uint8_t bitIndex) const {
108  return 1 << getBufferBit(bitIndex);
109  }
110 
111  constexpr static uint8_t bufferLength = (uint8_t)((N + 7) / 8);
112  uint8_t buffer[bufferLength] = {};
113 };
114 
115 /// @}
BitArray::getBufferMask
uint8_t getBufferMask(uint8_t bitIndex) const
Definition: BitArray.hpp:107
ERROR
#define ERROR(x, e)
Definition: Error.hpp:27
Error.hpp
BitArray::getByte
uint8_t getByte(uint8_t byteIndex) const
Get the byte at the given index.
Definition: BitArray.hpp:92
BitArray::getBufferBit
uint8_t getBufferBit(uint8_t bitIndex) const
Definition: BitArray.hpp:106
BitArray::getBufferIndex
uint8_t getBufferIndex(uint8_t bitIndex) const
Definition: BitArray.hpp:103
BitArray::set
void set(uint8_t bitIndex)
Set the value of the given bit to 1.
Definition: BitArray.hpp:36
BitArray::buffer
uint8_t buffer[bufferLength]
Definition: BitArray.hpp:112
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:70
BitArray::getBufferLength
uint8_t getBufferLength() const
Get the buffer length in bytes.
Definition: BitArray.hpp:100
BitArray::bufferLength
constexpr static uint8_t bufferLength
Definition: BitArray.hpp:111
BitArray::get
bool get(uint8_t bitIndex)
Get the value of the given bit.
Definition: BitArray.hpp:26
BitArray::clear
void clear(uint8_t bitIndex)
Clear the value of the given bit to 0.
Definition: BitArray.hpp:46
BitArray
A class for arrays of bits.
Definition: BitArray.hpp:18
BitArray::set
void set(uint8_t bitIndex, bool state)
Set the value of the given bit to the given state.
Definition: BitArray.hpp:58
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23