-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightController.cpp
86 lines (77 loc) · 2.35 KB
/
LightController.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
#include "Arduino.h"
#include "LightController.h"
#include "config.h"
LightController::LightController(int lightPin, int buttonPin, String id){
this->lightPin = lightPin;
this->buttonPin = buttonPin;
this->id = id;
this->lightState = LOW;
this->readCount = 0;
this->pressed = false;
this->offDelay = 30000;
pinMode(this->lightPin, OUTPUT);
pinMode(this->buttonPin, INPUT);
};
void LightController::setLed() {
digitalWrite(this->lightPin, this->lightState);
};
void LightController::turnOn(){
this->lightState = HIGH;
this->setLed();
};
void LightController::turnOff(){
this->lightState = LOW;
this->setLed();
};
void LightController::toggleLight(){
this->lightState = ! this->lightState;
this->setLed();
};
void LightController::checkButton(){
// auto Off
if(this->lightState == HIGH && this->readCount == 0 && !this->pressed ){
unsigned long offTime = this->onTime + this->offDelay;
unsigned long now = millis();
if( offTime < now ){
this->debug("AUTOOFF", "state: "+String(this->lightState)+" OnTime: "
+String(this->onTime)+" OffTime: "+String(offTime)+" now:"+String(now));
this->lightState = LOW;
digitalWrite(this->lightPin, this->lightState );
}
}
//check for button presses
int reading = digitalRead(this->buttonPin);
//debounce
if ( reading == HIGH ){
if(this->readCount < 100){
this->readCount++;
}
}else if(this->readCount > 0){
this->readCount--;
}else if (this->readCount == 0){
this->pressed = false;
}
//turn on if sufficiently debounced
if(!this->pressed && this->readCount >= 50 ){
unsigned long now = millis();
this->debug("PRESS","state:"+String(this->lightState)+" pressed:"+String(this->pressed)+" count:"+String(this->readCount)+" now:"+String(now)+" offIn: "+String(this->offDelay)+"ms");
this->pressed=true;
toggleLight();
if(this->lightState == HIGH) {
this->onTime = now;
}
}
};
void LightController::setTimeout(int seconds){
this->offDelay=(unsigned long)seconds *1000;
}
void LightController::debug(String action, String s){
#ifdef DEBUG
//TODO: debug ifdef?
//TODO: This is probably horrendously inefficient, use a fixed size char[] instead? Or as its debug, perhaps rely on ifdefing it out for production?
while(action.length() < 10){
action += " ";
}
Serial.println(action+"("+id+"): "+s);
#endif
};