-
Notifications
You must be signed in to change notification settings - Fork 0
/
DimmerModule.cpp
88 lines (74 loc) · 2.81 KB
/
DimmerModule.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
/**
* iotinator Dimmer Agent module
* Xavier Grosjean 2018
* Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
*/
#include "DimmerModule.h"
#include <FunctionalInterrupt.h>
DimmerModule::DimmerModule(DimmerConfigClass* config, int displayAddr, int displaySda, int displayScl, int intPin, int ctrlPinParam):XIOTModule(config, displayAddr, displaySda, displayScl) {
pinMode(intPin, INPUT_PULLUP);
_intPin = intPin;
pinMode(ctrlPinParam, OUTPUT);
_ctrlPin = ctrlPinParam;
_oledDisplay->setLineAlignment(2, TEXT_ALIGN_CENTER);
setLevel(config->getDefaultLevel());
}
void DimmerModule::customOnStaGotIpHandler(WiFiEventStationModeGotIP ipInfo) {
Serial.println("Init interrupt handler");
attachInterrupt(digitalPinToInterrupt(_intPin), std::bind(&DimmerModule::intHandler, this), RISING);
}
// Handler for the interruption received when crossing 0
// Code found on Amazon description of triac + zero crossing detection module
void DimmerModule::intHandler() {
// Firing angle calculation : 1 full 50Hz wave =1/50=20ms
// Every zerocrossing : (50Hz)-> 10ms (1/2 Cycle) For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us
if(_lightLevelDelay > 9600) {
digitalWrite(_ctrlPin, LOW); // triac Off
return;
}
delayMicroseconds(_lightLevelDelay); // Off cycle
digitalWrite(_ctrlPin, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(_ctrlPin, LOW); // triac Off
}
bool DimmerModule::customBeforeOTA() {
Serial.println("Detaching interrupts for OTA");
detachInterrupt(digitalPinToInterrupt(_intPin));
return true;
}
char* DimmerModule::_customData() {
// Serial.println("DimmerModule::_customData");
char* data = (char *)malloc(100);
sprintf(data, "{\"level\":%d}", _level);
return data;
}
char* DimmerModule::useData(char* data, int* httpCode) {
Serial.println("dimmer");
Serial.println(data);
const int bufferSize = 2*JSON_OBJECT_SIZE(1);
StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(data);
if (!root.success()) {
_oledDisplay->setLine(1, "Got bad data", TRANSIENT, NOT_BLINKING);
} else {
int level = (int)root["level"];
Serial.printf("New level: %d\n", level);
setLevel(level);
}
*httpCode = 200;
return emptyMallocedResponse();
}
void DimmerModule::setLevel(int levelParam) {
_level = levelParam;
_lightLevelDelay = (100 - _level) * 100; // Delay in ms before triggering triac
Serial.printf("LevelDelay %d\n", _lightLevelDelay);
refreshDisplay();
}
void DimmerModule::refreshDisplay() {
char message[100];
sprintf(message, "Level %d", _level);
_oledDisplay->setLine(2, message, NOT_TRANSIENT, NOT_BLINKING);
sprintf(message, "FCPU %d", F_CPU);
_oledDisplay->setLine(3, message, NOT_TRANSIENT, NOT_BLINKING);
}