-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRow.cpp
More file actions
106 lines (94 loc) · 2.37 KB
/
Row.cpp
File metadata and controls
106 lines (94 loc) · 2.37 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
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
#include "Arduino.h"
#include "Row.h"
#include "Pixel.h"
#include <Adafruit_NeoPixel.h>
Row::Row(int rowOffset, bool reverse, Adafruit_NeoPixel& pixels)
{
_rowOffset = rowOffset;
_reverse = reverse;
_pixels = pixels;
_pixelList = new LinkedList<Pixel*>;
_onColor = _pixels.Color(25,25,25);
_offColor = _pixels.Color(0,0,0);
_brightColor = _pixels.Color(50,50,50);
_pixelOffTime = 0;
_delay = 0;
setIncrementor();
}
void Row::turnPixelOn(int pixelNum)
{
if(_reverse){
pixelNum = (_rowOffset) + (_rowOffset - pixelNum) - 1;
}
hideLastPixel();
// fix the incrementor so it wont be static.
_pixels.setPixelColor(pixelNum - _incrementor, _offColor);//hide the last one
_pixels.setPixelColor(pixelNum, _onColor);//show the current one
_pixels.show();
}
void Row::setIncrementor()
{
if(_reverse){
_incrementor = -1;
}else{
_incrementor = 1;
}
}
void Row::createPixel()
{
Pixel *newPixel = new Pixel(_rowOffset);
_pixelList->add(newPixel);
}
void Row::createPixelCondition(int size, int position)
{
int halfWayUp = _rowOffset + (ROWLENGTH / 2) - 1;
int halfWayDown = _rowOffset - (ROWLENGTH / 2) - 1;
if((position == halfWayUp || position == halfWayDown) && size <= 3){
int rand = random(100);
if(rand > 80 && size == 1){
createPixel();
}
if(rand > 90 && size == 2){
createPixel();
}
}
}
void Row::removePixel(int pixelPos, int i, int size)
{
int rowTerminationTop = _rowOffset + ROWLENGTH - 1;
int rowTerminationBottom = _rowOffset - ROWLENGTH;
if((pixelPos == rowTerminationTop || pixelPos == rowTerminationBottom)){
hideLastPixel();
Pixel *popped = _pixelList->remove(i);
delete(popped);
}
_pixelOffTime = millis();
_delay = random(0, 5000);
}
void Row::hideLastPixel()
{
if(_reverse){
_pixels.setPixelColor(_rowOffset - ROWLENGTH, _offColor);
return;
}
_pixels.setPixelColor(_rowOffset + ROWLENGTH - 1, _offColor);
}
// make void for final version. Int just for debugging.
int Row::handlePixel()
{
int size = _pixelList->size();
if(size == 0 && millis() > _pixelOffTime + _delay){
createPixel();
return 1;
}
for(int i = 0; i < size; i++){
if(_pixelList->get(i)->checkTime()){
int pixelPos = _pixelList->get(i)->position;
createPixelCondition(size, pixelPos);
turnPixelOn(pixelPos);
removePixel(pixelPos, i, size);
}
}
//remove after debugging
return 1;
}