-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_device.cpp
76 lines (65 loc) · 1.91 KB
/
led_device.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
#include "device.h"
LEDDevice::LEDDevice(void) {
this->active = false;
}
std::string LEDDevice::name(void) {
return "LEDDevice";
}
Device *LEDDevice::create(void) {
return new LEDDevice();
}
std::string LEDDevice::prefix(void) {
return "led";
}
// A valid LEDDevice must be empty (black).
bool LEDDevice::sub_parse(AspngSurface *surface, int32_t min_x, int32_t min_y, int32_t max_x, int32_t max_y, std::string param) {
if (param != "") {
return false;
}
for (int32_t x = min_x; x <= max_x; x++) {
for (int32_t y = min_y; y <= max_y; y++) {
if (surface->get_pixel(x, y) != Rgb(0, 0, 0)) {
return false;
}
this->sub_patch.insert(Coord(x, y));
}
}
return true;
}
bool LEDDevice::link(void) {
return true;
}
std::list<std::shared_ptr<Port>> LEDDevice::propagate(std::shared_ptr<Port>) {
std::list<std::shared_ptr<Port>> empty;
return empty;
}
ElectricalValue LEDDevice::get_value_at_port(std::shared_ptr<Port>) {
return EmptyElectricalValue;
}
void LEDDevice::apply_new_value(std::shared_ptr<Port>, ElectricalValue v) {
switch (v) {
case HiElectricalValue:
case PullHiElectricalValue:
this->active = true;
break;
case LoElectricalValue:
case PullLoElectricalValue:
case EmptyElectricalValue:
this->active = false;
break;
}
}
std::list<Patch *> LEDDevice::sub_patches(void) {
std::list<Patch *> sub_patches;
sub_patches.push_back(&(this->sub_patch));
return sub_patches;
}
// Bright red if active, otherwise dark.
void LEDDevice::sub_draw(AspngSurface *surface, int32_t min_x, int32_t min_y, int32_t max_x, int32_t max_y) {
Rgb color = this->active ? Rgb(0xff, 0, 0) : Rgb(0x30, 0, 0);
for (int32_t x = min_x; x <= max_x; x++) {
for (int32_t y = min_y; y <= max_y; y++) {
surface->set_pixel(x, y, color);
}
}
}