Arduino Helpers master
Utility library for Arduino
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
39 void set(uint16_t bitIndex) {
40 buffer[getBufferIndex(bitIndex)] |= getBufferMask(bitIndex);
41 }
42
49 void clear(uint16_t bitIndex) {
50 buffer[getBufferIndex(bitIndex)] &= ~getBufferMask(bitIndex);
51 }
52
61 void set(uint16_t bitIndex, bool state) {
62 state ? set(bitIndex) : clear(bitIndex);
63 }
64
73 uint16_t safeIndex(uint16_t byteIndex) const {
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
95 const uint8_t &getByte(uint16_t byteIndex) const {
96 return buffer[byteIndex];
97 // return buffer[safeIndex(byteIndex)];
98 }
100 uint8_t &getByte(uint16_t byteIndex) {
101 return buffer[byteIndex];
102 // return buffer[safeIndex(byteIndex)];
103 }
104
118 void setByte(uint16_t byteIndex, uint8_t value) {
119 buffer[byteIndex] = value;
120 }
121
125 uint16_t getBufferLength() const { return bufferLength; }
126
127 private:
128 uint16_t getBufferIndex(uint16_t bitIndex) const {
129 return safeIndex(bitIndex / 8);
130 }
131 uint8_t getBufferBit(uint16_t bitIndex) const { return bitIndex % 8; }
132 uint8_t getBufferMask(uint16_t bitIndex) const {
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
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
uint8_t buffer[bufferLength]
Definition: BitArray.hpp:137
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
static constexpr uint16_t bufferLength
Definition: BitArray.hpp:136
bool get(uint16_t bitIndex) const
Get the value of the given bit.
Definition: BitArray.hpp:29
#define ERROR(msg, errc)
Print the error message and error code, and stop the execution if FATAL_ERRORS are enabled.
Definition: Error.hpp:39