-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitBox.cpp
163 lines (138 loc) · 5.04 KB
/
CircuitBox.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "CircuitBox.h"
#include <FastLED.h>
CircuitBox::CircuitBox() {
//Potantiometer for controlling most of box
pinMode(POTANTIOMETER_PIN, INPUT);
//Indicator led for virsuals
pinMode(INDICATOR_LED_PIN, OUTPUT);
indicatorLed = new Led(INDICATOR_LED_PIN);
//Switch pins 1 and 2 is for Brightness/Palette/Pattern mode controls
pinMode(MOD_CONTROLLER_SWITCH_1_PIN, INPUT_PULLUP);
pinMode(MOD_CONTROLLER_SWITCH_2_PIN, INPUT_PULLUP);
//Switch SECURITY_SWITCH_PIN is for Wifi/IR Remote safe switch
pinMode(SECURITY_SWITCH_PIN, INPUT_PULLUP);
}
void CircuitBox::print() const {
short int paletteCounterDisplay = abs(paletteCounter) % paletteCount;
short int patternCounterDisplay = abs(patternCounter) % patternCounter;
//Serial.printf("Brightness: %3d\tPalette: %d\tPattern: %d\n", current_Brightness , lightSensorStatus ? "-" : "+", paletteCounterDisplay, patternCounterDisplay);
Serial.printf("Brightness: %3d%s\tPalette: %d\tPattern: %d\n", current_Brightness, lightSensorStatus ? "-" : "+", paletteCounter, patternCounter);
}
uint8_t CircuitBox::getControlSwitchStatus() const {
bool isUp = digitalRead(MOD_CONTROLLER_SWITCH_1_PIN) && !digitalRead(MOD_CONTROLLER_SWITCH_2_PIN);
bool isDown = !digitalRead(MOD_CONTROLLER_SWITCH_1_PIN) && digitalRead(MOD_CONTROLLER_SWITCH_2_PIN);
bool isMiddle = digitalRead(MOD_CONTROLLER_SWITCH_1_PIN) && digitalRead(MOD_CONTROLLER_SWITCH_2_PIN);
return isUp ? 1 : isMiddle ? 2
: isDown ? 3
: 0;
};
bool CircuitBox::getSecuritySwitchStatus() const {
return digitalRead(SECURITY_SWITCH_PIN);
};
void CircuitBox::announceSecuritySwitchChange() {
uint8_t newSecuritySwitchStatus = getSecuritySwitchStatus();
if (securitySwitchStatus != newSecuritySwitchStatus) {
securitySwitchStatus = newSecuritySwitchStatus;
Serial.print("Security switch switched:");
if (securitySwitchStatus) {
//reciver.resume();
Serial.println("ON");
} else {
//reciver.pause();
Serial.println("OFF");
}
indicatorLed->on_off(200);
}
};
void CircuitBox::announceControlSwitchChange() {
uint8_t newControlSwitchStatus = getControlSwitchStatus();
if (controlSwitchStatus != newControlSwitchStatus) {
controlSwitchStatus = newControlSwitchStatus;
switch (getControlSwitchStatus()) {
case 1:
Serial.println("Potantiometer mode changed to Brightness");
break;
case 2:
Serial.println("Potantiometer mode changed to Palette");
break;
case 3:
Serial.println("Potantiometer mode changed to Pattern");
break;
}
indicatorLed->on_off(100);
};
};
void CircuitBox::handleBrightnessChange() {
int potValue = analogRead(POTANTIOMETER_PIN);
int newBrightness = map(potValue, 0, 1023, 0, max_Brightness);
if (newBrightness > max_Brightness) newBrightness = max_Brightness;
if (current_Brightness != newBrightness) {
current_Brightness = newBrightness;
FastLED.setBrightness(current_Brightness);
//Filters brightness change log
if (!(newBrightness % 10)) {
print();
indicatorLed->on_off(100);
}
}
}
void CircuitBox::handlePatternChange() {
int potValue = analogRead(POTANTIOMETER_PIN);
uint8_t newPattern = map(potValue, 0, 1023, 0, patternCount - 1);
if (newPattern != patternCounter) {
patternCounter = newPattern;
print();
indicatorLed->on_off(100);
}
}
void CircuitBox::handlePaletteChange() {
int potValue = analogRead(POTANTIOMETER_PIN);
int newPatternID = map(potValue, 0, 1023, 0, paletteCount - 1);
if (paletteCounter != newPatternID) {
paletteCounter = newPatternID;
print();
indicatorLed->on_off(100);
};
;
}
void CircuitBox::handleLightSensor(int lightChange) {
bool newLightSensorStatus = !digitalRead(LIGHT_SENSOR_PIN);
if (lightSensorStatus != newLightSensorStatus) {
lightSensorStatus = newLightSensorStatus;
if (!newLightSensorStatus) {
Serial.println("Brightness Limit Decreased.");
//TODO Dim the brigtness slowly
max_Brightness -= lightChange;
current_Brightness -= lightChange;
} else {
Serial.println("Brightness Limit Increased.");
max_Brightness += lightChange;
current_Brightness += lightChange;
}
FastLED.setBrightness(current_Brightness);
Serial.println(current_Brightness);
}
}
bool CircuitBox::didPotChange() {
int current_PotValue = !analogRead(POTANTIOMETER_PIN) ? 1 : analogRead(POTANTIOMETER_PIN);
int diff = abs(potValue - current_PotValue);
if (potValue != current_PotValue) {
potValue = current_PotValue;
}
return 2 <= diff;
};
void CircuitBox::modifyBrightness(short int amount) {
if (current_Brightness + amount < 0) {
current_Brightness = 0;
} else if (current_Brightness + amount > max_Brightness) {
current_Brightness = max_Brightness;
} else {
current_Brightness += amount;
}
}
void CircuitBox::modifyPattern(short change) {
patternCounter = patternCounter + change % patternCount;
}
void CircuitBox::modifyPalette(short change) {
paletteCounter = paletteCounter + change % paletteCount;
}