-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPulsePattern.cpp
76 lines (64 loc) · 1.85 KB
/
PulsePattern.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
//
// PulsePattern.cpp
//
//
// Created by Danny Machak on 10/28/16.
//
//
#include <Adafruit_NeoPixel.h>
#include <PulsePattern.h>
PulsePattern::PulsePattern(Adafruit_NeoPixel* strip, ColorFunction* function, int nPulses) {
this->strip = strip;
this->function = function;
this->nPulses = nPulses;
updateLength(strip->numPixels());
initializePulses();
}
void PulsePattern::updateLength(uint16_t n) {
flags = (uint8_t *)malloc(n);
step = (uint8_t *)malloc(n);
memset(flags, 0, n);
memset(step, 0, n);
}
void PulsePattern::initializePulses() {
pulseStep = (uint8_t *)malloc(nPulses);
pulseDelay = (uint8_t *)malloc(nPulses);
pulseIndex = (uint16_t *)malloc(2*nPulses);
memset(pulseStep, 0, nPulses);
memset(pulseDelay, 0, nPulses);
memset(pulseIndex, 0, 2*nPulses);
for (uint16_t i = 0; i < nPulses; i++) {
resetPulse(i, i*100/nPulses);
}
}
void PulsePattern::increment(void) {
for (uint16_t i = 0; i < nPulses; i++) {
if (isComplete(i)) {
resetPulse(i, 0);
} else {
if (pulseDelay[i] > 0) {
pulseDelay[i]--;
} else {
strip->setPixelColor(pulseIndex[i], function->getColor(0, pulseStep[i]));
pulseStep[i]++;
}
}
}
strip->show();
}
void PulsePattern::resetPulse(uint16_t pIndex, uint8_t delay) {
turnPixelOff(pulseIndex[pIndex]);
pulseIndex[pIndex] = 0;
pulseDelay[pIndex] = 0;
pulseStep[pIndex] = 0;
uint16_t pix = 0;
do {pix = rand() % strip->numPixels();}
while (isPixelOn(pix));
startPixel(pix, pIndex, delay);
}
void PulsePattern::startPixel(uint16_t index, uint16_t pIndex, uint8_t delay) {
turnPixelOn(index);
pulseIndex[pIndex] = index;
pulseDelay[pIndex] = delay;
pulseStep[pIndex] = 0;
}