-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeMatrix.cpp
More file actions
75 lines (66 loc) · 2.06 KB
/
FakeMatrix.cpp
File metadata and controls
75 lines (66 loc) · 2.06 KB
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
#include "Arduino.h"
#include "FakeMatrix.h"
FakeBit::FakeBit(byte* addr,byte bitPos) : addr(addr), bitPos(bitPos) {}
FakeBit& FakeBit::operator=(const byte other) {
if(other != 1 && other)
return *this;
if(other) {
(*this->addr) |= (1 << this->bitPos);
}
else {
if(this->check()){ // remove 1 from bit position
(*this->addr) ^= (1 << this->bitPos);
}
}
return *this;
}
FakeBit& FakeBit::operator=(const FakeBit& other) {
if(other.check()) {
(*this->addr) |= (1 << this->bitPos);
}
else {
if(this->check()) {
(*this->addr) ^= (1 << this->bitPos);
}
}
return *this;
}
FakeMatrix::FakeMatrix(byte numRows, byte numCols) : numRows(numRows), numCols(numCols) {
this->matrix = (byte*)malloc(numRows * numCols * sizeof(byte));
for(int i = 0; i < numRows * numCols; ++i) {
this->matrix[i] = 0;
}
}
FakeMatrix::FakeMatrix(const FakeMatrix& other) {
this->numRows = other.numRows;
this->numCols = other.numCols;
this->matrix = (byte*)malloc(this->numRows * this->numCols * sizeof(byte));
for(int i = 0; i < this->numRows * this->numCols; ++i){
this->matrix[i] = other.matrix[i];
}
}
FakeMatrix& FakeMatrix::operator=(const FakeMatrix& other) {
this->numRows = other.numRows;
this->numCols = other.numCols;
this->matrix = (byte*)malloc(this->numRows * this->numCols * sizeof(byte));
for(int i = 0; i < this->numRows * this->numCols; ++i){
this->matrix[i] = other.matrix[i];
}
return *this;
}
FakeMatrix::~FakeMatrix() {
free(this->matrix);
}
void FakeMatrix::setByte(const Pos& pos, const byte& b) {
int i = pos.i, j = pos.j;
int index = i * this->numCols + j / 8;
this->matrix[index] = b;
}
FakeBit FakeMatrix::operator[](const Pos& pos) {
int i = pos.i, j = pos.j;
if(i < 0 || j < 0)
return FakeBit(nullptr, 0);
int index = i * this->numCols + j / 8;
// return this->matrix[index] & (1 << ( 7 - (j % 8)));
return FakeBit(this->matrix + index, (7 - (j % 8)));
}