Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
BitArray.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
5#include <AH/Error/Error.hpp>
7#include <stdint.h>
8
10
13
20template <uint16_t N>
21class BitArray {
22 public:
29 bool get(uint16_t bitIndex) const {
30 return buffer[getBufferIndex(bitIndex)] & getBufferMask(bitIndex);
31 }
32
40 buffer[getBufferIndex(bitIndex)] |= getBufferMask(bitIndex);
41 }
42
50 buffer[getBufferIndex(bitIndex)] &= ~getBufferMask(bitIndex);
51 }
52
61 void set(uint16_t bitIndex, bool state) {
62 state ? set(bitIndex) : clear(bitIndex);
63 }
64
74 if (byteIndex >= getBufferLength()) {
75 ERROR(F("Error: index out of bounds (")
76 << byteIndex << F(", length is ") << getBufferLength()
77 << ')',
78 0xFFFF);
79 return getBufferLength() - 1; // LCOV_EXCL_LINE
80 }
81 return byteIndex;
82 }
83
96 return buffer[byteIndex];
97 // return buffer[safeIndex(byteIndex)];
98 }
101 return buffer[byteIndex];
102 // return buffer[safeIndex(byteIndex)];
103 }
104
119 buffer[byteIndex] = value;
120 }
121
125 uint16_t getBufferLength() const { return bufferLength; }
126
127 private:
129 return safeIndex(bitIndex / 8);
130 }
133 return 1 << getBufferBit(bitIndex);
134 }
135
136 constexpr static uint16_t bufferLength = (uint16_t)((N + 7) / 8);
137 uint8_t buffer[bufferLength] = {};
138};
139
141
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define ERROR(msg, errc)
Definition Error.hpp:19
A class for arrays of bits.
Definition BitArray.hpp:21
void set(uint16_t bitIndex)
Set the value of the given bit to 1.
Definition BitArray.hpp:39
uint8_t getBufferMask(uint16_t bitIndex) const
Definition BitArray.hpp:132
uint8_t & getByte(uint16_t byteIndex)
Get the byte at the given index.
Definition BitArray.hpp:100
uint16_t safeIndex(uint16_t byteIndex) const
Check the given byte index, and return it if it is within the bounds of the array,...
Definition BitArray.hpp:73
void setByte(uint16_t byteIndex, uint8_t value)
Set the byte at the given index.
Definition BitArray.hpp:118
uint8_t getBufferBit(uint16_t bitIndex) const
Definition BitArray.hpp:131
uint16_t getBufferLength() const
Get the buffer length in bytes.
Definition BitArray.hpp:125
const uint8_t & getByte(uint16_t byteIndex) const
Get the byte at the given index.
Definition BitArray.hpp:95
void clear(uint16_t bitIndex)
Clear the value of the given bit to 0.
Definition BitArray.hpp:49
uint16_t getBufferIndex(uint16_t bitIndex) const
Definition BitArray.hpp:128
void set(uint16_t bitIndex, bool state)
Set the value of the given bit to the given state.
Definition BitArray.hpp:61
bool get(uint16_t bitIndex) const
Get the value of the given bit.
Definition BitArray.hpp:29
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...