Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
AH
Containers
BitArray.hpp
Go to the documentation of this file.
1
/* ✔ */
2
3
#pragma once
4
5
#include <
AH/Error/Error.hpp
>
6
#include <
AH/Settings/NamespaceSettings.hpp
>
7
#include <stdint.h>
8
9
BEGIN_AH_NAMESPACE
10
13
20
template
<u
int
16_t N>
21
class
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
}
99
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
142
END_AH_NAMESPACE
NamespaceSettings.hpp
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:11
Error.hpp
ERROR
#define ERROR(msg, errc)
Definition
Error.hpp:19
::BitArray
A class for arrays of bits.
Definition
BitArray.hpp:21
AH::BitArray::set
void set(uint16_t bitIndex)
Set the value of the given bit to 1.
Definition
BitArray.hpp:39
AH::BitArray::getBufferMask
uint8_t getBufferMask(uint16_t bitIndex) const
Definition
BitArray.hpp:132
AH::BitArray::getByte
uint8_t & getByte(uint16_t byteIndex)
Get the byte at the given index.
Definition
BitArray.hpp:100
AH::BitArray::safeIndex
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
AH::BitArray::setByte
void setByte(uint16_t byteIndex, uint8_t value)
Set the byte at the given index.
Definition
BitArray.hpp:118
AH::BitArray::getBufferBit
uint8_t getBufferBit(uint16_t bitIndex) const
Definition
BitArray.hpp:131
AH::BitArray::getBufferLength
uint16_t getBufferLength() const
Get the buffer length in bytes.
Definition
BitArray.hpp:125
AH::BitArray::getByte
const uint8_t & getByte(uint16_t byteIndex) const
Get the byte at the given index.
Definition
BitArray.hpp:95
AH::BitArray::buffer
uint8_t buffer[bufferLength]
Definition
BitArray.hpp:137
AH::BitArray::clear
void clear(uint16_t bitIndex)
Clear the value of the given bit to 0.
Definition
BitArray.hpp:49
AH::BitArray::getBufferIndex
uint16_t getBufferIndex(uint16_t bitIndex) const
Definition
BitArray.hpp:128
AH::BitArray::set
void set(uint16_t bitIndex, bool state)
Set the value of the given bit to the given state.
Definition
BitArray.hpp:61
AH::BitArray::bufferLength
static constexpr uint16_t bufferLength
Definition
BitArray.hpp:136
AH::BitArray::get
bool get(uint16_t bitIndex) const
Get the value of the given bit.
Definition
BitArray.hpp:29
Generated by
1.17.0