Browse Source

Using my9291 external library reference

fastled
Xose Pérez 7 years ago
parent
commit
af40b8858b
5 changed files with 19 additions and 314 deletions
  1. +1
    -1
      code/espurna/config/general.h
  2. +16
    -31
      code/espurna/relay.ino
  3. +0
    -187
      code/lib/my9291/my9291.cpp
  4. +0
    -94
      code/lib/my9291/my9291.h
  5. +2
    -1
      code/platformio.ini

+ 1
- 1
code/espurna/config/general.h View File

@ -113,7 +113,7 @@
// of the web interface by running "gulp buildfs_embed"
#ifndef EMBEDDED_WEB
#define EMBEDDED_WEB 0
#define EMBEDDED_WEB 1
#endif
// -----------------------------------------------------------------------------


+ 16
- 31
code/espurna/relay.ino View File

@ -27,15 +27,7 @@ unsigned char _dual_status = 0;
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
#include <my9291.h>
typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char white;
} color_t;
my9291 _my9291 = my9291(MY9291_DI_PIN, MY9291_DCKI_PIN, MY9291_COMMAND);
bool _my9291_status = false;
color_t _my9291_color = {0, 0, 0, 0};
Ticker colorTicker;
#endif
@ -47,13 +39,8 @@ Ticker colorTicker;
void setLightColor(unsigned char red, unsigned char green, unsigned char blue, unsigned char white) {
_my9291_color.red = red;
_my9291_color.green = green;
_my9291_color.blue = blue;
_my9291_color.white = white;
// Force color change
relayProviderStatus(0, _my9291_status);
// Set new color (if light is open it will automatically change)
_my9291.setColor((my9291_color_t) { red, green, blue, white });
// Delay saving to EEPROM 5 seconds to avoid wearing it out unnecessarily
colorTicker.once(5, saveLightColor);
@ -62,23 +49,26 @@ void setLightColor(unsigned char red, unsigned char green, unsigned char blue, u
String getLightColor() {
char buffer[16];
sprintf(buffer, "%d,%d,%d,%d", _my9291_color.red, _my9291_color.green, _my9291_color.blue, _my9291_color.white);
my9291_color_t color = _my9291.getColor();
sprintf(buffer, "%d,%d,%d,%d", color.red, color.green, color.blue, color.white);
return String(buffer);
}
void saveLightColor() {
setSetting("colorRed", _my9291_color.red);
setSetting("colorGreen", _my9291_color.green);
setSetting("colorBlue", _my9291_color.blue);
setSetting("colorWhite", _my9291_color.white);
my9291_color_t color = _my9291.getColor();
setSetting("colorRed", color.red);
setSetting("colorGreen", color.green);
setSetting("colorBlue", color.blue);
setSetting("colorWhite", color.white);
saveSettings();
}
void retrieveLightColor() {
_my9291_color.red = getSetting("colorRed", MY9291_COLOR_RED).toInt();
_my9291_color.green = getSetting("colorGreen", MY9291_COLOR_GREEN).toInt();
_my9291_color.blue = getSetting("colorBlue", MY9291_COLOR_BLUE).toInt();
_my9291_color.white = getSetting("colorWhite", MY9291_COLOR_WHITE).toInt();
unsigned int red = getSetting("colorRed", MY9291_COLOR_RED).toInt();
unsigned int green = getSetting("colorGreen", MY9291_COLOR_GREEN).toInt();
unsigned int blue = getSetting("colorBlue", MY9291_COLOR_BLUE).toInt();
unsigned int white = getSetting("colorWhite", MY9291_COLOR_WHITE).toInt();
_my9291.setColor((my9291_color_t) { red, green, blue, white });
}
#endif
@ -96,12 +86,7 @@ void relayProviderStatus(unsigned char id, bool status) {
#endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
if (status) {
_my9291.send(_my9291_color.red,_my9291_color.green,_my9291_color.blue,_my9291_color.white);
} else {
_my9291.send(0, 0, 0, 0);
}
_my9291_status = status;
_my9291.setState(status);
#endif
#if RELAY_PROVIDER == RELAY_PROVIDER_RELAY
@ -118,7 +103,7 @@ bool relayProviderStatus(unsigned char id) {
#endif
#if RELAY_PROVIDER == RELAY_PROVIDER_MY9291
return _my9291_status;
return _my9291.getState();
#endif
#if RELAY_PROVIDER == RELAY_PROVIDER_RELAY


+ 0
- 187
code/lib/my9291/my9291.cpp View File

@ -1,187 +0,0 @@
/*
MY9291 LED Driver Arduino library 0.1.0
Copyright (c) 2016 - 2026 MaiKe Labs
Copyright (C) 2017 - Xose Pérez (for the library)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "my9291.h"
extern "C" {
void os_delay_us(unsigned int);
}
void my9291::_di_pulse(unsigned int times) {
unsigned int i;
for (i = 0; i < times; i++) {
digitalWrite(_pin_di, HIGH);
digitalWrite(_pin_di, LOW);
}
}
void my9291::_dcki_pulse(unsigned int times) {
unsigned int i;
for (i = 0; i < times; i++) {
digitalWrite(_pin_dcki, HIGH);
digitalWrite(_pin_dcki, LOW);
}
}
void my9291::_set_cmd(my9291_cmd_t command) {
unsigned char i;
unsigned char command_data = *(unsigned char *) (&command);
_command = command;
// ets_intr_lock();
// TStop > 12us.
os_delay_us(12);
// Send 12 DI pulse, after 6 pulse's falling edge store duty data, and 12
// pulse's rising edge convert to command mode.
_di_pulse(12);
// Delay >12us, begin send CMD data
os_delay_us(12);
// Send CMD data
for (i = 0; i < 4; i++) {
// DCK = 0;
digitalWrite(_pin_dcki, LOW);
if (command_data & 0x80) {
// DI = 1;
digitalWrite(_pin_di, HIGH);
} else {
// DI = 0;
digitalWrite(_pin_di, LOW);
}
// DCK = 1;
digitalWrite(_pin_dcki, HIGH);
command_data = command_data << 1;
if (command_data & 0x80) {
// DI = 1;
digitalWrite(_pin_di, HIGH);
} else {
// DI = 0;
digitalWrite(_pin_di, LOW);
}
// DCK = 0;
digitalWrite(_pin_dcki, LOW);
// DI = 0;
digitalWrite(_pin_di, LOW);
command_data = command_data << 1;
}
// TStart > 12us. Delay 12 us.
os_delay_us(12);
// Send 16 DI pulse,at 14 pulse's falling edge store CMD data, and
// at 16 pulse's falling edge convert to duty mode.
_di_pulse(16);
// TStop > 12us.
os_delay_us(12);
// ets_intr_unlock();
}
void my9291::send(unsigned int duty_r, unsigned int duty_g, unsigned int duty_b, unsigned int duty_w) {
unsigned char i = 0;
unsigned char channel = 0;
unsigned char bit_length = 8;
unsigned int duty_current = 0;
// Definition for RGBW channels
unsigned int duty[4] = { duty_r, duty_g, duty_b, duty_w };
switch (_command.bit_width) {
case MY9291_CMD_BIT_WIDTH_16:
bit_length = 16;
break;
case MY9291_CMD_BIT_WIDTH_14:
bit_length = 14;
break;
case MY9291_CMD_BIT_WIDTH_12:
bit_length = 12;
break;
case MY9291_CMD_BIT_WIDTH_8:
bit_length = 8;
break;
default:
bit_length = 8;
break;
}
// ets_intr_lock();
// TStop > 12us.
os_delay_us(12);
for (channel = 0; channel < 4; channel++) { //RGBW 4CH
// RGBW Channel
duty_current = duty[channel];
// Send 8bit/12bit/14bit/16bit Data
for (i = 0; i < bit_length / 2; i++) {
// DCK = 0;
digitalWrite(_pin_dcki, LOW);
if (duty_current & (0x01 << (bit_length - 1))) {
// DI = 1;
digitalWrite(_pin_di, HIGH);
} else {
// DI = 0;
digitalWrite(_pin_di, LOW);
}
// DCK = 1;
digitalWrite(_pin_dcki, HIGH);
duty_current = duty_current << 1;
if (duty_current & (0x01 << (bit_length - 1))) {
// DI = 1;
digitalWrite(_pin_di, HIGH);
} else {
// DI = 0;
digitalWrite(_pin_di, LOW);
}
//DCK = 0;
digitalWrite(_pin_dcki, LOW);
//DI = 0;
digitalWrite(_pin_di, LOW);
duty_current = duty_current << 1;
}
}
// TStart > 12us. Ready for send DI pulse.
os_delay_us(12);
// Send 8 DI pulse. After 8 pulse falling edge, store old data.
_di_pulse(8);
// TStop > 12us.
os_delay_us(12);
// ets_intr_unlock();
}
my9291::my9291(unsigned char di, unsigned char dcki, my9291_cmd_t command) {
_pin_di = di;
_pin_dcki = dcki;
pinMode(_pin_di, OUTPUT);
pinMode(_pin_dcki, OUTPUT);
digitalWrite(_pin_di, LOW);
digitalWrite(_pin_dcki, LOW);
// Clear all duty register
_dcki_pulse(64 / 2);
_set_cmd(command);
}

+ 0
- 94
code/lib/my9291/my9291.h View File

@ -1,94 +0,0 @@
/*
MY9291 LED Driver Arduino library 0.1.0
Copyright (c) 2016 - 2026 MaiKe Labs
Copyright (C) 2017 - Xose Pérez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _my9291_h
#define _my9291_h
#include <Arduino.h>
typedef enum my9291_cmd_one_shot_t {
MY9291_CMD_ONE_SHOT_DISABLE = 0X00,
MY9291_CMD_ONE_SHOT_ENFORCE = 0X01,
} my9291_cmd_one_shot_t;
typedef enum my9291_cmd_reaction_t {
MY9291_CMD_REACTION_FAST = 0X00,
MY9291_CMD_REACTION_SLOW = 0X01,
} my9291_cmd_reaction_t;
typedef enum my9291_cmd_bit_width_t {
MY9291_CMD_BIT_WIDTH_16 = 0X00,
MY9291_CMD_BIT_WIDTH_14 = 0X01,
MY9291_CMD_BIT_WIDTH_12 = 0X02,
MY9291_CMD_BIT_WIDTH_8 = 0X03,
} my9291_cmd_bit_width_t;
typedef enum my9291_cmd_frequency_t {
MY9291_CMD_FREQUENCY_DIVIDE_1 = 0X00,
MY9291_CMD_FREQUENCY_DIVIDE_4 = 0X01,
MY9291_CMD_FREQUENCY_DIVIDE_16 = 0X02,
MY9291_CMD_FREQUENCY_DIVIDE_64 = 0X03,
} my9291_cmd_frequency_t;
typedef enum my9291_cmd_scatter_t {
MY9291_CMD_SCATTER_APDM = 0X00,
MY9291_CMD_SCATTER_PWM = 0X01,
} my9291_cmd_scatter_t;
typedef struct {
my9291_cmd_scatter_t scatter:1;
my9291_cmd_frequency_t frequency:2;
my9291_cmd_bit_width_t bit_width:2;
my9291_cmd_reaction_t reaction:1;
my9291_cmd_one_shot_t one_shot:1;
unsigned char resv:1;
} __attribute__ ((aligned(1), packed)) my9291_cmd_t;
#define MY9291_COMMAND_DEFAULT { \
.scatter = MY9291_CMD_SCATTER_APDM, \
.frequency = MY9291_CMD_FREQUENCY_DIVIDE_1, \
.bit_width = MY9291_CMD_BIT_WIDTH_8, \
.reaction = MY9291_CMD_REACTION_FAST, \
.one_shot = MY9291_CMD_ONE_SHOT_DISABLE, \
.resv = 0 \
}
class my9291 {
public:
my9291(unsigned char di, unsigned char dcki, my9291_cmd_t command);
void send(unsigned int duty_r, unsigned int duty_g, unsigned int duty_b, unsigned int duty_w);
private:
void _di_pulse(unsigned int times);
void _dcki_pulse(unsigned int times);
void _set_cmd(my9291_cmd_t command);
my9291_cmd_t _command;
unsigned char _pin_di;
unsigned char _pin_dcki;
};
#endif

+ 2
- 1
code/platformio.ini View File

@ -26,6 +26,7 @@ lib_deps =
https://bitbucket.org/xoseperez/nofuss.git
https://bitbucket.org/xoseperez/emonliteesp.git
https://bitbucket.org/xoseperez/debounceevent.git
https://github.com/xoseperez/my9291#1.0.0
https://github.com/xoseperez/RemoteSwitch-arduino-library.git
lib_ignore =
@ -404,4 +405,4 @@ extra_script = pio_hooks.py
build_flags = ${common.build_flags_1m128} -DAI_LIGHT
upload_speed = 115200
upload_port = "192.168.4.1"
upload_flags = --auth=Algernon1 --port 8266
upload_flags = --auth=fibonacci --port 8266

Loading…
Cancel
Save