Control Surface main
MIDI Control Surface library for Arduino
BitArray.hpp
Go to the documentation of this file.
1/* ✔ */
2
3#pragma once
4
6
7AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
8
9#include <AH/Error/Error.hpp>
11#include <stdint.h>
12
14
17
24template <uint16_t N>
25class BitArray {
26 public:
33 bool get(uint16_t bitIndex) const {
34 return buffer[getBufferIndex(bitIndex)] & getBufferMask(bitIndex);
35 }
36
43 void set(uint16_t bitIndex) {
44 buffer[getBufferIndex(bitIndex)] |= getBufferMask(bitIndex);
45 }
46
53 void clear(uint16_t bitIndex) {
54 buffer[getBufferIndex(bitIndex)] &= ~getBufferMask(bitIndex);
55 }
56
65 void set(uint16_t bitIndex, bool state) {
66 state ? set(bitIndex) : clear(bitIndex);
67 }
68
77 uint16_t safeIndex(uint16_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 const uint8_t &getByte(uint16_t byteIndex) const {
100 return buffer[byteIndex];
101 // return buffer[safeIndex(byteIndex)];
102 }
104 uint8_t &getByte(uint16_t byteIndex) {
105 return buffer[byteIndex];
106 // return buffer[safeIndex(byteIndex)];
107 }
108
122 void setByte(uint16_t byteIndex, uint8_t value) {
123 buffer[byteIndex] = value;
124 }
125
129 uint16_t getBufferLength() const { return bufferLength; }
130
131 private:
132 uint16_t getBufferIndex(uint16_t bitIndex) const {
133 return safeIndex(bitIndex / 8);
134 }
135 uint8_t getBufferBit(uint16_t bitIndex) const { return bitIndex % 8; }
136 uint8_t getBufferMask(uint16_t bitIndex) const {
137 return 1 << getBufferBit(bitIndex);
138 }
139
140 constexpr static uint16_t bufferLength = (uint16_t)((N + 7) / 8);
141 uint8_t buffer[bufferLength] = {};
142};
143
145
147
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define ERROR(msg, errc)
Definition: Error.hpp:22
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:53
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:52
A class for arrays of bits.
Definition: BitArray.hpp:25
void set(uint16_t bitIndex)
Set the value of the given bit to 1.
Definition: BitArray.hpp:43
uint8_t getBufferMask(uint16_t bitIndex) const
Definition: BitArray.hpp:136
uint8_t & getByte(uint16_t byteIndex)
Get the byte at the given index.
Definition: BitArray.hpp:104
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:77
void setByte(uint16_t byteIndex, uint8_t value)
Set the byte at the given index.
Definition: BitArray.hpp:122
uint8_t getBufferBit(uint16_t bitIndex) const
Definition: BitArray.hpp:135
uint16_t getBufferLength() const
Get the buffer length in bytes.
Definition: BitArray.hpp:129
const uint8_t & getByte(uint16_t byteIndex) const
Get the byte at the given index.
Definition: BitArray.hpp:99
void clear(uint16_t bitIndex)
Clear the value of the given bit to 0.
Definition: BitArray.hpp:53
uint16_t getBufferIndex(uint16_t bitIndex) const
Definition: BitArray.hpp:132
void set(uint16_t bitIndex, bool state)
Set the value of the given bit to the given state.
Definition: BitArray.hpp:65
bool get(uint16_t bitIndex) const
Get the value of the given bit.
Definition: BitArray.hpp:33