-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightCtr.cpp
More file actions
153 lines (132 loc) · 4.07 KB
/
LightCtr.cpp
File metadata and controls
153 lines (132 loc) · 4.07 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
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
#include "LightCtr.h"
CRGB leds[NUM_LEDS]; // Define the array of leds
int light_state; //determine which state the light is in, auto, or manual
int light_gradient_ctr; //record down how many loops has the delay counter been through
RGBVal RGB_curr; //store current RGB values
RGBVal RGB_gradient[EFF_GRAD_STEPS]; //store RGB channel gradient in values and go through them
const uint64_t timer_const = 10000000;
uint64_t timer_off_val; //delay timer value
boolean timer_off_en; //store the timer state
boolean next_state; //boolean flag to store for the next state
const RGBVal RGB_high_manual = {255,255,255};
const RGBVal RGB_mid_manual = {127,127,127};
const RGBVal RGB_low_manual = {63,63,63};
const RGBVal RGB_off = {0,0,0};
void lightInit(void) {
FastLED.addLeds<WS2812B, LED_OUT_PIN, RGB>(leds, NUM_LEDS);
light_state = OFF;
light_gradient_ctr = EFF_GRAD_STEPS - 1;
}
void lightChg(void) {
if (light_gradient_ctr < EFF_GRAD_STEPS){
/*
for (int i = 0; i < NUM_LEDS; i++){
leds[i].setRGB(RGB_gradient[light_gradient_ctr].R, RGB_gradient[light_gradient_ctr].G, RGB_gradient[light_gradient_ctr].B);
}
FastLED.show();
*/
lightWrite(RGB_gradient[light_gradient_ctr]);
RGB_curr = RGB_gradient[light_gradient_ctr];
light_gradient_ctr++;
}
}
void lightWrite(RGBVal val){
for (int i = 0; i < NUM_LEDS; i++){
leds[i].setRGB(val.G, val.R, val.B);
}
RGB_curr = val;
FastLED.show();
Serial.print("RGB changed");
Serial.print(" R: ");
Serial.print(val.R);
Serial.print(" G: ");
Serial.print(val.G);
Serial.print(" B: ");
Serial.println(val.B);
}
void lightStateChg(void){
switch(light_state){
case OFF:
if(phone_state){
light_state = AUTO;
}else if(button_state){
light_state = HIGH_MA;
//lightWrite(RGB_high_manual);
//RGB_curr = RGB_high_manual;
lightGradient(RGB_high_manual);
}
break;
case HIGH_MA:
if(phone_state){
light_state = AUTO;
}else if(button_state){
light_state = MID_MA;
//lightWrite(RGB_mid_manual);
//RGB_curr = RGB_mid_manual;
lightGradient(RGB_mid_manual);
}
break;
case MID_MA:
if(phone_state){
light_state = AUTO;
}
if(button_state){
light_state = LOW_MA;
//lightWrite(RGB_low_manual);
//RGB_curr = RGB_low_manual;
lightGradient(RGB_low_manual);
}
break;
case LOW_MA:
if(phone_state){
light_state = AUTO;
}
if(button_state){
light_state = OFF;
//lightWrite(RGB_off);
//RGB_curr = RGB_off;
lightGradient(RGB_off);
}
break;
case AUTO:
if(!phone_state){
light_state = AUTO_OFF;
timer_off_en = true;
timer_off_val = esp_timer_get_time();
}
break;
case AUTO_OFF:
if(phone_state){
timer_off_en = false;
light_state = AUTO;
}else if(button_state){
timer_off_en = false;
light_state = OFF;
//lightWrite(RGB_off);
//RGB_curr = RGB_off;
lightGradient(RGB_off);
}
break;
}
button_state = false; //Clear button state buffer after the state machine
}
void lightNextState(void){
}
void lightAutoOff(void){
if(timer_off_en){
if (esp_timer_get_time()- timer_off_val > timer_const){
Serial.println("timed off");
button_state = true; //Enable flag of button pressed to signal a turn off
lightStateChg(); //Call lightStateChg to change the state machine
}
}
}
void lightGradient(RGBVal tarval){
for (int i; i< EFF_GRAD_STEPS; i++){
RGB_gradient[i].R = map(i,0,EFF_GRAD_STEPS,RGB_curr.R,tarval.R);
RGB_gradient[i].G = map(i,0,EFF_GRAD_STEPS,RGB_curr.G,tarval.G);
RGB_gradient[i].B = map(i,0,EFF_GRAD_STEPS,RGB_curr.B,tarval.B);
}
RGB_gradient[EFF_GRAD_STEPS-1] = tarval;
light_gradient_ctr = 0;
}