/
car.cpp
96 lines (76 loc) · 1.95 KB
/
car.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "car.h"
Car::Car(Adafruit_DotStar* strip) {
this->strip = strip;
// hex Green Red Blue
// 0x 00 00 00
uint32_t colorList[] = { 0x000000
, 0x010101
, 0x020202
, 0x030303
, 0x040404
, 0x050505
};
this->colorLength = sizeof(colorList)/sizeof(uint32_t);
this->colors = new uint32_t[this->colorLength];
this->colors = colorList;
this->create();
}
bool Car::create() {
randomSeed(analogRead(0));
this->startLed = random(0, 60);
this->endLed = random(0, 60);
this->head = this->startLed;
if (this->startLed < this->endLed) {
this->directionDifference = 1;
this->tail = this->head - 2;
} else {
this->directionDifference = -1;
this->tail = this->head + 2;
}
this->lightStep = 0;
this->active = false;
return true;
}
bool Car::isActive() {
return this->active;
}
bool Car::setActive(bool active) {
this->active = active;
return active;
}
int Car::getStart() {
return this->startLed;
}
int Car::getEnd() {
return this->endLed;
}
int Car::getHead() {
return this->head;
}
int Car::getTail() {
return this->tail;
}
int Car::getPathLength() {
if (this->directionDifference == 1)
return this->endLed - this->startLed;
else
return this->startLed - this->endLed;
}
bool Car::nextStep() {
Serial.println((int)this->strip);
this->strip->setPixelColor(25, 0x101010);
// if (this->active) {
// if ((this->directionDifference > 0) && (this->lightStep == this->colorLength)) {
// this->lightStep = 0;
// this->head++;
// this->tail++;
// } else {
// this->lightStep++;
// this->strip->setPixelColor(head, this->colors[this->lightStep]);
// this->strip->setPixelColor(tail, this->colors[this->colorLength - this->lightStep - 1]);
// }
// if ((this->tail == this->endLed) && (this->lightStep == this->colorLength))
// this->active = false;
// }
return this->active;
}