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;
2431bool light_state, in_transition;
2532int transitiontime, ct, hue, bri, sat;
2633float step_level[4 ], current_rgbw[4 ], x, y;
@@ -219,20 +226,22 @@ void apply_scene(uint8_t new_scene) {
219226}
220227
221228void 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() {
245254void 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