-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecoder.cpp
More file actions
63 lines (54 loc) · 1.73 KB
/
Decoder.cpp
File metadata and controls
63 lines (54 loc) · 1.73 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
#include "Decoder.h"
#include "Setup.h"
#include "Arduino.h"
Decoder::Decoder(uint8_t colorsPerLed, uint8_t xSize, uint8_t decoderPinsCount, uint8_t* decoderPins, uint8_t cathodePinsCount, uint8_t* cathodePins) {
_colorsPerLed = colorsPerLed;
_xSize = xSize;
_decoderPinsCount = decoderPinsCount;
_cathodePinsCount = cathodePinsCount;
_decoderPins = new uint8_t[_decoderPinsCount];
for (uint8_t pin = 0; pin < _decoderPinsCount; pin++) {
_decoderPins[pin] = decoderPins[pin];
}
_cathodePins = new uint8_t[_cathodePinsCount];
for (uint8_t pin = 0; pin < _cathodePinsCount; pin++) {
_cathodePins[pin] = cathodePins[pin];
}
for (int pin = 0; pin < _decoderPinsCount; pin++) {
pinMode(_decoderPins[pin], OUTPUT);
}
for (int pin = 0; pin < _cathodePinsCount; pin++) {
pinMode(_cathodePins[pin], OUTPUT);
}
}
Decoder::~Decoder() {
delete[] _decoderPins;
delete[] _cathodePins;
}
void Decoder::cathode(uint8_t layer) {
for (int pin = 0; pin < _cathodePinsCount; pin++) {
if (pin == layer || layer >= _cathodePinsCount) {
digitalWrite(_cathodePins[pin], HIGH);
}
else {
digitalWrite(_cathodePins[pin], LOW);
}
}
}
void Decoder::cathodesOff() {
for (int pin = 0; pin < _cathodePinsCount; pin++) {
digitalWrite(_cathodePins[pin], LOW);
}
}
void Decoder::displayNum(int brightness, uint8_t ledNum) {
ledNum = constrain(ledNum, 0, CUBE_SIZE);
for (int weight = 1, pin = 0; pin < _decoderPinsCount; weight = weight << 1, pin++)
digitalWrite(_decoderPins[pin], (ledNum & weight) >> pin);
delayMicroseconds(brightness);
cathodesOff();
}
void Decoder::display(int brightness, uint8_t color, uint8_t x, uint8_t y, uint8_t z) {
int lednum = y * _xSize + x;
cathode(z);
displayNum(brightness, _colorsPerLed * lednum + color);
}