v5.8.0g - Fix some PWM issues

5.8.0g
 * Fix inverted PWM index (#960)
 * Fix some PWM related issues
(#967)
This commit is contained in:
arendst 2017-10-05 13:28:31 +02:00
parent c82fe7f46e
commit 9e845f3c1e
5 changed files with 44 additions and 33 deletions

View File

@ -1,7 +1,7 @@
## Sonoff-Tasmota ## Sonoff-Tasmota
Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE. Provide ESP8266 based Sonoff by [iTead Studio](https://www.itead.cc/) and ElectroDragon IoT Relay with Serial, Web and MQTT control allowing 'Over the Air' or OTA firmware updates using Arduino IDE.
Current version is **5.8.0f** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information. Current version is **5.8.0g** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information.
### ATTENTION All versions ### ATTENTION All versions

View File

@ -1,4 +1,8 @@
/* 5.8.0f /* 5.8.0g
* Fix inverted PWM index (#960)
* Fix some PWM related issues (#967)
*
* 5.8.0f
* Set all saved power settings to Off when SetOption0 (SaveState) = 0 (#955) * Set all saved power settings to Off when SetOption0 (SaveState) = 0 (#955)
* Allow PWM initialization after restart (#955) * Allow PWM initialization after restart (#955)
* Add support for inverted PWM (#960) * Add support for inverted PWM (#960)

View File

@ -25,7 +25,7 @@
- Select IDE Tools - Flash Size: "1M (no SPIFFS)" - Select IDE Tools - Flash Size: "1M (no SPIFFS)"
====================================================*/ ====================================================*/
#define VERSION 0x05080006 // 5.8.0f #define VERSION 0x05080007 // 5.8.0g
enum week_t {Last, First, Second, Third, Fourth}; enum week_t {Last, First, Second, Third, Fourth};
enum dow_t {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat}; enum dow_t {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
@ -290,9 +290,7 @@ uint8_t dht_flg = 0; // DHT configured
uint8_t hlw_flg = 0; // Power monitor configured uint8_t hlw_flg = 0; // Power monitor configured
uint8_t i2c_flg = 0; // I2C configured uint8_t i2c_flg = 0; // I2C configured
uint8_t spi_flg = 0; // SPI configured uint8_t spi_flg = 0; // SPI configured
uint8_t pwm_flg = 0; // PWM configured uint8_t sfl_flg = 0; // Led flag (0 = No led, 1 = BN-SZ01, 2 = Sonoff Led, 3 = H801/MagicHome, 4 = H801/MagicHome, 5 = H801, 11 = WS2812, 12 = AiLight, 13 = Sonoff B1)
uint8_t sfl_flg = 0; // Sonoff Led flag (0 = No led, 1 = BN-SZ01, 2 = Sonoff Led, 3 = H801/MagicHome, 4 = H801/MagicHome, 5 = H801, 11 = WS2812, 12 = AiLight, 13 = Sonoff B1)
uint8_t pwm_idxoffset = 0; // Allowed PWM command offset (change for Sonoff Led)
boolean mDNSbegun = false; boolean mDNSbegun = false;
@ -1251,7 +1249,7 @@ void mqttDataCb(char* topic, byte* data, unsigned int data_len)
} }
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
} }
else if (!strcasecmp_P(type, PSTR(D_CMND_PWM)) && (index > pwm_idxoffset) && (index <= 5)) { else if (!sfl_flg && !strcasecmp_P(type, PSTR(D_CMND_PWM)) && (index > 0) && (index <= 5)) {
if ((payload >= 0) && (payload <= PWM_RANGE) && (pin[GPIO_PWM1 + index -1] < 99)) { if ((payload >= 0) && (payload <= PWM_RANGE) && (pin[GPIO_PWM1 + index -1] < 99)) {
sysCfg.pwmvalue[index -1] = payload; sysCfg.pwmvalue[index -1] = payload;
analogWrite(pin[GPIO_PWM1 + index -1], pwm_inverted[index -1] ? PWM_RANGE - payload : payload); analogWrite(pin[GPIO_PWM1 + index -1], pwm_inverted[index -1] ? PWM_RANGE - payload : payload);
@ -2596,15 +2594,15 @@ void GPIO_init()
if (mpin) { if (mpin) {
if ((mpin >= GPIO_REL1_INV) && (mpin <= GPIO_REL4_INV)) { if ((mpin >= GPIO_REL1_INV) && (mpin <= GPIO_REL4_INV)) {
rel_inverted[mpin - GPIO_REL1_INV] = 1; rel_inverted[mpin - GPIO_REL1_INV] = 1;
mpin -= 4; mpin -= (GPIO_REL1_INV - GPIO_REL1);
} }
else if ((mpin >= GPIO_LED1_INV) && (mpin <= GPIO_LED4_INV)) { else if ((mpin >= GPIO_LED1_INV) && (mpin <= GPIO_LED4_INV)) {
led_inverted[mpin - GPIO_LED1_INV] = 1; led_inverted[mpin - GPIO_LED1_INV] = 1;
mpin -= 4; mpin -= (GPIO_LED1_INV - GPIO_LED1);
} }
else if ((mpin >= GPIO_PWM1_INV) && (mpin <= GPIO_PWM5_INV)) { else if ((mpin >= GPIO_PWM1_INV) && (mpin <= GPIO_PWM5_INV)) {
pwm_inverted[mpin - GPIO_PWM1_INV] = 1; pwm_inverted[mpin - GPIO_PWM1_INV] = 1;
mpin -= 5; mpin -= (GPIO_PWM1_INV - GPIO_PWM1);
} }
#ifdef USE_DHT #ifdef USE_DHT
else if ((mpin >= GPIO_DHT11) && (mpin <= GPIO_DHT22)) { else if ((mpin >= GPIO_DHT11) && (mpin <= GPIO_DHT22)) {
@ -2626,19 +2624,18 @@ void GPIO_init()
Serial.set_tx(2); Serial.set_tx(2);
} }
analogWriteRange(PWM_RANGE); // Default is 1023 (Arduino.h) analogWriteRange(PWM_RANGE); // Default is 1023 (Arduino.h)
analogWriteFreq(PWM_FREQ); // Default is 1000 (core_esp8266_wiring_pwm.c) analogWriteFreq(PWM_FREQ); // Default is 1000 (core_esp8266_wiring_pwm.c)
Maxdevice = 1;
if (sysCfg.flag.pwm_control) { if (sysCfg.flag.pwm_control) {
sfl_flg = 0; sfl_flg = 0;
for (byte i = 0; i < 5; i++) { for (byte i = 0; i < 5; i++) {
if (pin[GPIO_PWM1 +i] < 99) { if (pin[GPIO_PWM1 +i] < 99) {
sfl_flg++; sfl_flg++; // Use Dimmer/Color control for all PWM as SetOption15 = 1
} }
} }
} }
Maxdevice = 1;
if (SONOFF_BRIDGE == sysCfg.module) { if (SONOFF_BRIDGE == sysCfg.module) {
Baudrate = 19200; Baudrate = 19200;
} }
@ -2656,7 +2653,7 @@ void GPIO_init()
} }
else if ((H801 == sysCfg.module) || (MAGICHOME == sysCfg.module)) { // PWM RGBCW led else if ((H801 == sysCfg.module) || (MAGICHOME == sysCfg.module)) { // PWM RGBCW led
if (!sysCfg.flag.pwm_control) { if (!sysCfg.flag.pwm_control) {
sfl_flg = 0; sfl_flg = 0; // Use basic PWM control if SetOption15 = 0
} }
} }
else if (SONOFF_BN == sysCfg.module) { // PWM Single color led (White) else if (SONOFF_BN == sysCfg.module) { // PWM Single color led (White)
@ -2672,7 +2669,9 @@ void GPIO_init()
sfl_flg = 13; sfl_flg = 13;
} }
else { else {
Maxdevice = 0; if (!sfl_flg) {
Maxdevice = 0;
}
for (byte i = 0; i < 4; i++) { for (byte i = 0; i < 4; i++) {
if (pin[GPIO_REL1 +i] < 99) { if (pin[GPIO_REL1 +i] < 99) {
pinMode(pin[GPIO_REL1 +i], OUTPUT); pinMode(pin[GPIO_REL1 +i], OUTPUT);
@ -2703,17 +2702,14 @@ void GPIO_init()
sfl_flg = 11; sfl_flg = 11;
} }
#endif // USE_WS2812 #endif // USE_WS2812
if (sfl_flg) { // Sonoff B1, AiLight, Sonoff Led or BN-SZ01, WS2812 if (sfl_flg) { // Any Led light under Dimmer/Color control
if (sfl_flg < 6) {
pwm_idxoffset = sfl_flg; // 1 for BN-SZ01, 2 for Sonoff Led, 3,4,5 for H801 and MagicHome
}
sl_init(); sl_init();
} } else {
for (byte i = pwm_idxoffset; i < 5; i++) { for (byte i = 0; i < 5; i++) {
if (pin[GPIO_PWM1 +i] < 99) { if (pin[GPIO_PWM1 +i] < 99) {
pwm_flg = 1; pinMode(pin[GPIO_PWM1 +i], OUTPUT);
pinMode(pin[GPIO_PWM1 +i], OUTPUT); analogWrite(pin[GPIO_PWM1 +i], pwm_inverted[i] ? PWM_RANGE - sysCfg.pwmvalue[i] : sysCfg.pwmvalue[i]);
analogWrite(pin[GPIO_PWM1 +i], pwm_inverted[i] ? PWM_RANGE - sysCfg.pwmvalue[i] : sysCfg.pwmvalue[i]); }
} }
} }

View File

@ -632,10 +632,16 @@ boolean inModule(byte val, uint8_t *arr)
} }
#endif #endif
if (((val >= GPIO_REL1) && (val <= GPIO_REL4)) || ((val >= GPIO_LED1) && (val <= GPIO_LED4))) { if (((val >= GPIO_REL1) && (val <= GPIO_REL4)) || ((val >= GPIO_LED1) && (val <= GPIO_LED4))) {
offset = 4; offset = (GPIO_REL1_INV - GPIO_REL1);
} }
if (((val >= GPIO_REL1_INV) && (val <= GPIO_REL4_INV)) || ((val >= GPIO_LED1_INV) && (val <= GPIO_LED4_INV))) { if (((val >= GPIO_REL1_INV) && (val <= GPIO_REL4_INV)) || ((val >= GPIO_LED1_INV) && (val <= GPIO_LED4_INV))) {
offset = -4; offset = -(GPIO_REL1_INV - GPIO_REL1);
}
if ((val >= GPIO_PWM1) && (val <= GPIO_PWM5)) {
offset = (GPIO_PWM1_INV - GPIO_PWM1);
}
if ((val >= GPIO_PWM1_INV) && (val <= GPIO_PWM5_INV)) {
offset = -(GPIO_PWM1_INV - GPIO_PWM1);
} }
for (byte i = 0; i < MAX_GPIO_PIN; i++) { for (byte i = 0; i < MAX_GPIO_PIN; i++) {
if (arr[i] == val) { if (arr[i] == val) {

View File

@ -161,19 +161,22 @@ void sl_init(void)
{ {
if (sfl_flg < 6) { // PWM if (sfl_flg < 6) { // PWM
for (byte i = 0; i < sfl_flg; i++) { for (byte i = 0; i < sfl_flg; i++) {
sysCfg.pwmvalue[i] = 0; // Disable direct PWM control sysCfg.pwmvalue[i] = 0; // Disable direct PWM control
}
if (1 == sfl_flg) {
sysCfg.led_color[0] = 255; // One PWM channel only supports Dimmer but needs max color
} }
if (SONOFF_LED == sysCfg.module) { // Fix Sonoff Led instabilities if (SONOFF_LED == sysCfg.module) { // Fix Sonoff Led instabilities
if (!my_module.gp.io[4]) { if (!my_module.gp.io[4]) {
pinMode(4, OUTPUT); // Stop floating outputs pinMode(4, OUTPUT); // Stop floating outputs
digitalWrite(4, LOW); digitalWrite(4, LOW);
} }
if (!my_module.gp.io[5]) { if (!my_module.gp.io[5]) {
pinMode(5, OUTPUT); // Stop floating outputs pinMode(5, OUTPUT); // Stop floating outputs
digitalWrite(5, LOW); digitalWrite(5, LOW);
} }
if (!my_module.gp.io[14]) { if (!my_module.gp.io[14]) {
pinMode(14, OUTPUT); // Stop floating outputs pinMode(14, OUTPUT); // Stop floating outputs
digitalWrite(14, LOW); digitalWrite(14, LOW);
} }
} }
@ -408,6 +411,8 @@ void sl_animate()
if (sfl_flg < 6) { if (sfl_flg < 6) {
if (pin[GPIO_PWM1 +i] < 99) { if (pin[GPIO_PWM1 +i] < 99) {
uint16_t curcol = cur_col[i] * (PWM_RANGE / 255); uint16_t curcol = cur_col[i] * (PWM_RANGE / 255);
// snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "Cur_Col%d %d, CurCol %d"), i, cur_col[i], curcol);
// addLog(LOG_LEVEL_DEBUG);
analogWrite(pin[GPIO_PWM1 +i], pwm_inverted[i] ? PWM_RANGE - curcol : curcol); analogWrite(pin[GPIO_PWM1 +i], pwm_inverted[i] ? PWM_RANGE - curcol : curcol);
} }
} }