-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix8.cpp
64 lines (57 loc) · 1.43 KB
/
matrix8.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
// Developed by Mark Vodyanitskiy (@mvodya) 2018
#include "matrix8.h"
Matrix8::Matrix8(int latch_pin, int clock_pin, int data_pin) {
// Set initialization vars
latch_pin_ = latch_pin;
clock_pin_ = clock_pin;
data_pin_ = data_pin;
row_ = 0;
// Set pins to output
initPins();
// Create buffer
buffer_ = new byte[MATRIX_SIZE];
// Clear buffer after (shift registers may contain any data)
#if MATRIX_REGISTERS_INIT_CLEAR
clear();
#endif
}
void Matrix8::draw() {
// Go to function with zero delay
draw(0);
}
void Matrix8::draw(int del) {
row_ = 1;
for (int i = 0; i < MATRIX_SIZE; i++) {
// Lock latch pin
digitalWrite(latch_pin_, 0);
// Write row shift register
shiftOut(data_pin_, clock_pin_, LSBFIRST, ~row_);
// Write column shift register
shiftOut(data_pin_, clock_pin_, LSBFIRST, buffer_[i]);
// Unlock latch pin
digitalWrite(latch_pin_, 1);
// Switch to next row
row_ <<= 1;
// Delay
delay(del);
}
}
byte* Matrix8::getBuffer() { return buffer_; }
void Matrix8::set(int row, int column, bool value) {
if (value)
// LED ON
buffer_[row] |= (1 << column);
else
// LED OFF
buffer_[row] &= ~(1 << column);
}
void Matrix8::clear() {
// Set all bytes to zero
for (int i = 0; i < MATRIX_SIZE; i++) buffer_[i] = 0;
}
void Matrix8::initPins() {
// Set pins to output
pinMode(latch_pin_, OUTPUT);
pinMode(clock_pin_, OUTPUT);
pinMode(data_pin_, OUTPUT);
}