This repository has been archived by the owner on Aug 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
channel.cpp
181 lines (159 loc) · 5.7 KB
/
channel.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2018 Michael Dahsler
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "channel.h"
#include "debug.h"
#include "ntp.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
/*
* Global variables
*/
uint8_t numOfChannels; // current number of used channels
uint32_t PWMFrequency; // frequency used to generate the PWM Signal
uint16_t PWMGenerator; // defines if the PWM signal is generated by the ESP8266 itself or the PCA9685
unsigned long millisAtLastPWMUpdate; // millis upime of the device since the last PWM Update
Channel channels[MAX_NUM_OF_CHANNELS]; // array storing all channels
Adafruit_PWMServoDriver PCA9685Shield = Adafruit_PWMServoDriver(); // Object representing the PCA9685 PWM Module
/*
* Constructor, Destructor
*/
Channel::Channel() {}
Channel::~Channel() {}
/*
* Prints all information of the channel
*/
void Channel::print() {
DEBUG_INFO("Channel: %d", channelNumber);
DEBUG_INFO("Name: %s", name);
DEBUG_INFO("Color: %s", color);
DEBUG_INFO("Pin: %d", pin);
DEBUG_INFO("Power: %f", power);
DEBUG_INFO("Manual: %d", manual);
DEBUG_INFO("Moonlight: %d", moonlight);
DEBUG_INFO("Max Moonlight value: %f", maxMoonlightValue);
DEBUG_INFO("value: %f", value);
DEBUG_INFO("Number of entries: %d", numOfEntries);
DEBUG_INFO("Entry | Time | Value [%]");
for(uint8_t i=0; i<numOfEntries; i++) DEBUG_INFO("%d | %d | %d\n",i,t[i],v[i]);
}
/*
* Updates the PWM signal every "MILLIS_BETWEEN_PWM_UPDATES"
* therefore the "value" is updated according to the lightschedule if the
* channel is in automatic mode ("manual" == false)
* Ff the channel is in manual mode ("manual" == true) "value" is not updated according to
* the lightschedule
* If the channel is in moonlight mode ("moonlight" == true), "value" is calculated according to
* the moonlight simulation
*
* In the end the ¨value" is defining the new duty cycle of the PWM signal generated by "PWMGenerator"
*/
void Channel::updatePWM() {
if(moonlight) {
// moonlight simulation TODO
value = 0;
DEBUG_INFO("[Channel::updatePWM {%d}] moonlight, value: %f", channelNumber, value);
}
else {
String m;
// calculate PWM value in automatic mode
if(!manual) {
m = "automatic";
uint32_t t_ = getLocalSecondsOfTheDay();
for(uint8_t i=0; i<numOfEntries; i++) {
uint32_t t1 = t[i];
uint32_t t2 = t[(i+1)%numOfEntries];
float v1 = v[i];
float v2 = v[(i+1)%numOfEntries];
if(i+1 == numOfEntries) {
t2 += 24*60*60;
if(t_<t1) t_ += 24*60*60;
}
if(t1<t_ && t_<= t2) {
value = v1 + (v2-v1)/float(t2-t1)*float(t_-t1);
}
}
}
// no update in automatic mode
if(manual) {
m = "manual";
}
DEBUG_INFO("[Channel::updatePWM {%d}] mode: %s, value: %f", channelNumber, m.c_str(), value);
}
// set pwm
switch(PWMGenerator) {
case PWM_GENERATOR_ESP8266:
analogWrite(pin, value/100. * 1023);
break;
case PWM_GENERATOR_PCA9685:
PCA9685Shield.setPWM(channelNumber, 0, float(value/100. * 4095));
break;
}
}
/*
* prints all channels also the not active ones
*/
void printAllChannels() {
for(uint8_t c=0; c<MAX_NUM_OF_CHANNELS; c++) channels[c].print();
}
/*
* Configures the PWMGenerator
* if the PWM is generated by the ESP8266 the pins are set as outputs
* if the PWM is generated by the PCA9685 the I2C module is configured
*/
void configurePWM() {
millisAtLastPWMUpdate = 0;
switch(PWMGenerator) {
case PWM_GENERATOR_ESP8266:
DEBUG_INFO("[configurePWM] pwm generated by ESP8266");
for(uint8_t c=0; c<numOfChannels; c++) {
pinMode(channels[c].pin, OUTPUT);
digitalWrite(channels[c].pin, 0);
}
break;
case PWM_GENERATOR_PCA9685:
DEBUG_INFO("[configurePWM] pwm generated by PCA9685");
PCA9685Shield.begin();
for(uint8_t c=0; c<numOfChannels; c++) PCA9685Shield.setPin(c, 0, false);
break;
}
setPWMFrequency(PWMFrequency);
}
/*
* sets a new PWM frequency in Hz
*/
void setPWMFrequency(const uint32_t f) {
DEBUG_INFO("[setPWMFrequency] new frequency: %d Hz",f);
PWMFrequency = f;
switch(PWMGenerator) {
case PWM_GENERATOR_ESP8266:
analogWriteFreq(f);
break;
case PWM_GENERATOR_PCA9685:
PCA9685Shield.setPWMFreq(f);
break;
}
}
/*
* handles the PWM generation in the main loop
* every "MILLIS_BETWEEN_PWM_UPDATES" the PWM values are updated
* if (force == true) the update is forced
*/
void handlePWM(const bool force) {
if(millis() - millisAtLastPWMUpdate > MILLIS_BETWEEN_PWM_UPDATES || force) {
for(uint8_t c=0; c<numOfChannels; c++) channels[c].updatePWM();
millisAtLastPWMUpdate = millis();
}
}