Skip to content

Commit d51185e

Browse files
authored
Merge pull request #17 from mariusmotea/develop
Switch generic lights to new PWM library + support for CCT and RGB_CCT lights
2 parents 4c891b8 + 3701be7 commit d51185e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+7425
-58
lines changed
815 KB
Loading

Lights/Arduino/Generic_CCT_Light/Generic_CCT_Light.ino

Lines changed: 413 additions & 0 deletions
Large diffs are not rendered by default.

Lights/Arduino/Generic_CCT_Light/pwm.c

Lines changed: 449 additions & 0 deletions
Large diffs are not rendered by default.

Lights/Arduino/GenericWifiHueLight/GenericWifiHueLight.ino renamed to Lights/Arduino/Generic_RGBW_Light/Generic_RGBW_Light.ino

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
#include <ESP8266WebServer.h>
66
#include <WiFiManager.h>
77
#include <EEPROM.h>
8+
#include "pwm.c"
89

10+
#define PWM_CHANNELS 4
11+
const uint32_t period = 1024;
912

13+
//define pins
14+
uint32 io_info[PWM_CHANNELS][3] = {
15+
// MUX, FUNC, PIN
16+
{PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, 12},
17+
{PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13, 13},
18+
{PERIPHS_IO_MUX_MTMS_U, FUNC_GPIO14, 14},
19+
{PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5 , 5},
20+
};
1021

11-
#define red_pin 12
12-
#define green_pin 13
13-
#define blue_pin 14
14-
#define white_pin 5
15-
16-
#define pwm_channels 4 // put 3 for rgb, 4 for rgbw
22+
// initial duty: all off
23+
uint32 pwm_duty_init[PWM_CHANNELS] = {0, 0, 0, 0};
1724

1825
// if you want to setup static ip uncomment these 3 lines and line 72
1926
//IPAddress strip_ip ( 192, 168, 10, 95);
2027
//IPAddress gateway_ip ( 192, 168, 10, 1);
2128
//IPAddress subnet_mask(255, 255, 255, 0);
2229

23-
uint8_t rgbw[4], color_mode, scene, pins[4];
30+
uint8_t rgbw[4], color_mode, scene;
2431
bool light_state, in_transition;
2532
int transitiontime, ct, hue, bri, sat;
2633
float step_level[4], current_rgbw[4], x, y;
@@ -219,20 +226,22 @@ void apply_scene(uint8_t new_scene) {
219226
}
220227

221228
void lightEngine() {
222-
for (uint8_t color = 0; color < pwm_channels; color++) {
229+
for (uint8_t color = 0; color < PWM_CHANNELS; color++) {
223230
if (light_state) {
224231
if (rgbw[color] != current_rgbw[color] ) {
225232
in_transition = true;
226233
current_rgbw[color] += step_level[color];
227234
if ((step_level[color] > 0.0f && current_rgbw[color] > rgbw[color]) || (step_level[color] < 0.0f && current_rgbw[color] < rgbw[color])) current_rgbw[color] = rgbw[color];
228-
analogWrite(pins[color], (int)(current_rgbw[color] * 4));
235+
pwm_set_duty((int)(current_rgbw[color] * 4), color);
236+
pwm_start();
229237
}
230238
} else {
231239
if (current_rgbw[color] != 0) {
232240
in_transition = true;
233241
current_rgbw[color] -= step_level[color];
234242
if (current_rgbw[color] < 0.0f) current_rgbw[color] = 0;
235-
analogWrite(pins[color], (int)(current_rgbw[color] * 4));
243+
pwm_set_duty((int)(current_rgbw[color] * 4), color);
244+
pwm_start();
236245
}
237246
}
238247
}
@@ -245,12 +254,12 @@ void lightEngine() {
245254
void setup() {
246255
EEPROM.begin(512);
247256

248-
pins[0] = red_pin;
249-
pins[1] = green_pin;
250-
pins[2] = blue_pin;
251-
pins[3] = white_pin;
252-
analogWriteRange(1024);
253-
analogWriteFreq(4096);
257+
for (uint8_t ch = 0; ch < PWM_CHANNELS; ch++) {
258+
pinMode(io_info[ch][2], OUTPUT);
259+
}
260+
261+
pwm_init(period, pwm_duty_init, PWM_CHANNELS, io_info);
262+
pwm_start();
254263

255264
//WiFi.config(strip_ip, gateway_ip, subnet_mask);
256265

@@ -266,18 +275,13 @@ void setup() {
266275
WiFiManager wifiManager;
267276
wifiManager.autoConnect("New Hue Light");
268277
if (! light_state) {
269-
while (WiFi.status() != WL_CONNECTED) {
270-
analogWrite(pins[0], 10);
271-
delay(250);
272-
analogWrite(pins[0], 0);
273-
delay(250);
274-
}
275278
// Show that we are connected
276-
analogWrite(pins[1], 10);
279+
pwm_set_duty(100, 1);
280+
pwm_start();
277281
delay(500);
278-
analogWrite(pins[1], 0);
282+
pwm_set_duty(0, 1);
283+
pwm_start();
279284
}
280-
281285
WiFi.macAddress(mac);
282286

283287
// Port defaults to 8266
@@ -335,7 +339,7 @@ void setup() {
335339
} else if (button == 4000) {
336340
light_state = false;
337341
}
338-
for (uint8_t color = 0; color < pwm_channels; color++) {
342+
for (uint8_t color = 0; color < PWM_CHANNELS; color++) {
339343
if (light_state) {
340344
step_level[color] = (rgbw[color] - current_rgbw[color]) / 54;
341345
} else {
@@ -430,7 +434,7 @@ void setup() {
430434
convert_hue();
431435
}
432436
transitiontime *= 16;
433-
for (uint8_t color = 0; color < pwm_channels; color++) {
437+
for (uint8_t color = 0; color < PWM_CHANNELS; color++) {
434438
if (light_state) {
435439
step_level[color] = (rgbw[color] - current_rgbw[color]) / transitiontime;
436440
} else {
@@ -515,7 +519,7 @@ void setup() {
515519
current_rgbw[3] = 255;
516520
}
517521
}
518-
for (uint8_t color = 0; color < pwm_channels; color++) {
522+
for (uint8_t color = 0; color < PWM_CHANNELS; color++) {
519523
if (light_state) {
520524
step_level[color] = ((float)rgbw[color] - current_rgbw[color]) / transitiontime;
521525
} else {

0 commit comments

Comments
 (0)