-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitArray.cpp
302 lines (266 loc) · 7.86 KB
/
BitArray.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "Arduino.h"
#include "BitArray.h"
#define __BITARRAY_DEBUG__() callback(buffer,rows,columns)
#define __HALT__() {while(1) {delay(3000); Serial.println(F("HALT.....")); }}
BitArray::BitArray(uint16_t _rows,uint16_t _columns,
void (*_callback)(uint8_t *p,uint16_t _rows,uint16_t _columns )) {
callback=_callback;
rows=_rows;
columns=(_columns+7) >> 3;
}
void BitArray::init() {
if( (rows+1)*columns > (int)sizeof(buffer)) {
__HALT__();
}
}
void BitArray::clear() {
init();
uint8_t *p=buffer;
for(uint8_t i=(columns*rows);i>0;i--) *p++=0;
__BITARRAY_DEBUG__();
}
void BitArray::inverse() {
uint8_t *p=buffer;
for(uint8_t i=(columns*rows);i>0;i--,p++) *p =~(*p);
__BITARRAY_DEBUG__();
}
byte BitArray::bitReverse( byte x ) {
// 01010101 | 10101010
x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
// 00110011 | 11001100
x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
// 00001111 | 11110000
x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
return x;
}
uint8_t BitArray::setDot(uint16_t row, uint16_t column, boolean b) {
uint8_t mask= ((uint8_t)0x80) >> ( column & 0x07);
column >>=3;
if(row>=rows || column >=columns) return 0;
if(b) buffer[row*columns+column] |= mask;
else buffer[row*columns+column] &= (~mask);
__BITARRAY_DEBUG__();
return mask;
}
void BitArray::load16x16(const byte *p,uint8_t rowOffset,uint8_t columnOffset) {
uint8_t byteOffset=columnOffset >> 3;
columnOffset &= 0x07;
uint8_t *pos,b;
uint8_t keepMask = ~(0xff >> columnOffset);
pos = buffer+(rowOffset)*columns + byteOffset;
if(columnOffset) {
for(uint8_t c=0; c< 16; c++, pos += (columns-2)) {
//if(rowOffset+c>=rows)break;
b=pgm_read_byte(p);
if( (byteOffset) < columns) {
*pos = (*pos & keepMask) | (b >> columnOffset);
}
pos++; p++;
if( (byteOffset +1) < columns) {
*pos = (b << (8-columnOffset));
b=pgm_read_byte(p);
*pos |= ( b >> columnOffset);
}
pos++; p++;
if( (byteOffset) +2 < columns) {
*pos = (b << (8-columnOffset)) | (*pos & (~keepMask));
}
}
} else {
for(uint8_t c=0; c< 16; c++, pos += (columns-2)) {
// if(rowOffset+c>=rows)break;
if( (byteOffset < columns))
*pos = pgm_read_byte(p);
pos++; p++;
if( (byteOffset+1 < columns))
*pos = pgm_read_byte(p);
pos++; p++;
}
}
__BITARRAY_DEBUG__();
}
void BitArray::swapV() {
for(int i=0;i<rows/2;i++) {
for(uint8_t c=0;c<columns;c++) {
uint8_t t=buffer[i*columns+c];
buffer[i*columns+c]=buffer[(rows+i-1)*columns+c];
buffer[(rows+i-1)*columns+c]=t;
}
}
__BITARRAY_DEBUG__();
}
void BitArray::swapH() {
uint8_t c;
for(uint8_t r=0;r<rows;r++) {
for(c=0;c<columns/2;c++) {
uint8_t t=bitReverse(buffer[r*columns+c]);
buffer[r*columns+c]=bitReverse(buffer[(r+1)*columns-c-1]);
buffer[(r+1)*columns-c-1]=t;
}
if(columns & 0x01) buffer[r*columns+c]=bitReverse(buffer[r*columns+c]);
}
__BITARRAY_DEBUG__();
}
void BitArray::setRow(uint8_t row, ...) {
if(row>=rows) return;
va_list argv;
va_start(argv, row);
for(uint8_t c=0;c<columns ;c++) {
buffer[row*columns+c]=va_arg(argv, unsigned int);
}
va_end(argv);
__BITARRAY_DEBUG__();
}
void BitArray::setColumn(uint8_t column, ...) {
uint8_t bit= 0x80 >> (column & 0x07);
uint8_t *pos=buffer +(column>>3);
if(column>=columns) return;
va_list argv; va_start(argv, column);
for(uint8_t r=rows/8; r>0 ;r--) {
uint8_t value=va_arg(argv, unsigned int);
for(uint8_t mask=0x80, k=0;k<8;k++,mask>>=1,pos+=columns) {
if(value & mask) *pos |= bit;
else *pos &= (~bit);
}
}
va_end(argv);
__BITARRAY_DEBUG__();
}
void BitArray::rotateUp(boolean fillIn,...) {
uint8_t *p,t;
va_list argv; va_start(argv, fillIn);
for(uint8_t c=0;c<columns;c++) {
p=buffer+c;
t=*p;
for(uint8_t r=1;r<rows;r++, p+=columns) {
*p=*(p+columns);
}
if(fillIn) *p=va_arg(argv, unsigned int);
else *p=t;
}
va_end(argv);
__BITARRAY_DEBUG__();
}
void BitArray::rotateDown(boolean fillIn,...) {
uint8_t *p,t;
va_list argv; va_start(argv, fillIn);
for(uint8_t c=0;c<columns;c++) {
p=buffer+((rows-1)*columns+c);
t=*p;
for(uint8_t r=1;r<rows;r++, p-=columns) {
*(p)=*(p-columns);
}
if(fillIn) *p=va_arg(argv, unsigned int);
else *p=t;
}
va_end(argv);
__BITARRAY_DEBUG__();
}
void BitArray::rotateLeft(boolean fillIn, ...) {
va_list argv; va_start(argv, fillIn);
uint8_t *p=buffer;
uint8_t mask=0,arg=0,b;
for(int i=0;i<rows;i++) {
if( (i & 0x07) ==0) {
mask=0x80; arg=va_arg(argv, unsigned int);
} else {
mask>>=1;
}
if(fillIn) b=arg & mask;
else b= (*p) & 0x80;
for( int c=0;c<columns;c++, p++) {
*p <<=1;
if(*(p+1) & 0x80) *p |= 0x01;
}
if(b) *(p-1) |= 0x01;
else *(p-1) &=0xfe;
}
va_end(argv);
__BITARRAY_DEBUG__();
}
void BitArray::rotateRight(boolean fillIn, ...) {
va_list argv; va_start(argv, fillIn);
uint8_t *p=buffer-1;
uint8_t mask=0,arg=0,b;
for(int i=0;i<rows;i++, p+=columns) {
if( (i & 0x07) ==0) {
mask=0x80; arg=va_arg(argv, unsigned int);
} else {
mask>>=1;
}
p+=columns;
if(fillIn) b=arg & mask;
else b= (*p) & 0x01;
for( int c=0; c<columns;c++, p--) {
*p >>= 1;
if(*(p-1) & 0x01) *p |= 0x80;
}
if(b) *(p+1) |= 0x80;
else *(p+1) &=0x7f;
}
va_end(argv);
__BITARRAY_DEBUG__();
}
//////////////////////////////////////////////
void BitArray::shiftUp16x16In(const byte *p,uint32_t delayTime) {
for(int i=0;i<16;i++) {
uint8_t v1=pgm_read_byte(p++);
uint8_t v2=pgm_read_byte(p++);
rotateUp(true,v1,v2,0);
delay(delayTime);
}
}
void BitArray::shiftDown16x16In(const byte *p,uint32_t delayTime) {
p+=32;
for(int i=0;i<16;i++) {
uint8_t v1=pgm_read_byte(--p);
uint8_t v2=pgm_read_byte(--p);
rotateDown(true,v2,v1,0);
delay(delayTime);
}
}
void BitArray::shiftRight16x16In(const byte *bitMap,uint32_t delayTime) {
uint8_t mask=0,loop,count;
uint8_t v1=0,v2=0;
for(bitMap+=2, loop=0;loop<16;loop++, bitMap-=32) {
if((loop & 0x07)==0) {
mask=0x01;
bitMap--;
} else {
mask <<= 1;
}
for(count=0;count<8;count++, bitMap+=2) {
v1 <<=1;
if(pgm_read_byte(bitMap) & mask) v1 |= 0x01;
}
for(count=0;count<8;count++, bitMap+=2) {
v2 <<=1;
if(pgm_read_byte(bitMap) & mask) v2 |= 0x01;
}
rotateRight(true,v1,v2,0);
delay(delayTime);
}
}
void BitArray::shiftLeft16x16In(const byte *bitMap,uint32_t delayTime) {
uint8_t mask=0,loop,count;
uint8_t v1=0,v2=0;
bitMap--;
for(loop=0;loop<16;loop++, bitMap-=32) {
if((loop & 0x07)==0) {
mask=0x80;
bitMap++;
} else {
mask >>= 1;
}
for(count=0;count<8;count++, bitMap+=2) {
v1 <<=1;
if(pgm_read_byte(bitMap) & mask) v1 |= 0x01;
}
for(count=0;count<8;count++, bitMap+=2) {
v2 <<=1;
if(pgm_read_byte(bitMap) & mask) v2 |= 0x01;
}
rotateLeft(true,v1,v2,0);
delay(delayTime);
}
}