-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nibbleArray.cpp
86 lines (65 loc) · 1.4 KB
/
nibbleArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// FILE: nibbleArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.6
// PURPOSE: Arduino library for a compact array of nibbles (4 bits)
// URL: https://github.com/RobTillaart/nibbleArray
#include "nibbleArray.h"
nibbleArray::nibbleArray(const uint16_t size)
{
_size = size;
if (_size > (uint16_t) NIBBLEARRAY_MAXSIZE)
{
_size = (uint16_t) NIBBLEARRAY_MAXSIZE;
}
_bytes = (_size + 1)/2;
_arr = (uint8_t *) malloc(_bytes);
}
nibbleArray::~nibbleArray()
{
if (_arr != NULL)
{
free(_arr);
}
}
uint8_t nibbleArray::get(const uint16_t index)
{
// disable this check for more speed
if (index > _size)
{
return NIBBLEARRAY_ERROR_INDEX;
}
if (index & 1) return _arr[index/2] & 0x0F;
return _arr[index/2] >> 4;
}
uint8_t nibbleArray::set(const uint16_t index, uint8_t value)
{
// disable this check for more speed
if (index > _size)
{
return NIBBLEARRAY_ERROR_INDEX;
}
uint8_t v = value & 0x0F;
if (index & 1) _arr[index/2] = (_arr[index/2] & 0xF0) | v;
else _arr[index/2] = (_arr[index/2] & 0x0F) | (v << 4);
return NIBBLEARRAY_OK;
}
uint16_t nibbleArray::size()
{
return _size;
};
uint16_t nibbleArray::memory()
{
return _bytes;
};
void nibbleArray::clear()
{
memset(_arr, 0, (_size + 1)/2);
}
void nibbleArray::setAll(uint8_t value)
{
uint8_t v = value & 0x0F;
v |= (v << 4);
memset(_arr, v, (_size + 1)/2);
}
// -- END OF FILE --