Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed PWM invert issue #436

Merged
merged 15 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ESPixelStick/src/ConstNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const char CN_File [] = "File";
const char CN_file [] = "file";
const char CN_filename [] = "filename";
const char CN_files [] = "files";
const char CN_Frequency [] = "Frequency";
const char CN_fseqfilename [] = "fseqfilename";
const char CN_g [] = "g";
const char CN_gamma [] = "gamma";
Expand Down
1 change: 1 addition & 0 deletions ESPixelStick/src/ConstNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extern const char CN_File[];
extern const char CN_file[];
extern const char CN_filename[];
extern const char CN_files[];
extern const char CN_Frequency[];
extern const char CN_fseqfilename[];
extern const char CN_gateway[];
extern const char CN_g[];
Expand Down
7 changes: 6 additions & 1 deletion ESPixelStick/src/GPIO_Defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ typedef enum
// #define ESP32_CAM
// #define ESP32_TTGO_T8

// Platform specific GPIO definitions
#define SUPPORT_OutputType_UCS1903

// Platform specific GPIO definitions
#if defined (ESP32_CAM)
# include "GPIO_Defs_ESP32_CAM.hpp"
#elif defined (ESP32_TTGO_T8)
# include "GPIO_Defs_ESP32_TTGO_T8.hpp"
#elif defined (ESP32_LOLIN_D32_PRO)
# include "GPIO_Defs_ESP32_generic.hpp"
#elif defined (ESP32_D1_MINI)
# include "GPIO_Defs_ESP32_D1_MINI.hpp"
#elif defined (BOARD_ESPS_V3)
# include "GPIO_Defs_ESP8266_ESPS_V3.hpp"
#elif defined (ARDUINO_ARCH_ESP8266)
Expand Down
52 changes: 52 additions & 0 deletions ESPixelStick/src/GPIO_Defs_ESP32_D1_MINI.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once
/*
* GPIO_Defs_ESP32_D1_MINI.hpp - Output Management class
*
* Project: ESPixelStick - An ESP8266 / ESP32 and E1.31 based pixel driver
* Copyright (c) 2021 Shelby Merrick
* http://www.forkineye.com
*
* This program is provided free for you to use in any way that you wish,
* subject to the laws and regulations where you are using it. Due diligence
* is strongly suggested before using this code. Please give credit where due.
*
* The Author makes no warranty of any kind, express or implied, with regard
* to this program or the documentation contained in this document. The
* Author shall not be liable in any event for incidental or consequential
* damages in connection with, or arising out of, the furnishing, performance
* or use of these programs.
*
*/

//Output Manager
#define DEFAULT_UART_1_GPIO gpio_num_t::GPIO_NUM_2
#define DEFAULT_UART_2_GPIO gpio_num_t::GPIO_NUM_4
#define UART_LAST OutputChannelId_UART_1

#define SUPPORT_RMT_OUTPUT
#define DEFAULT_RMT_0_GPIO gpio_num_t::GPIO_NUM_0
#define DEFAULT_RMT_1_GPIO gpio_num_t::GPIO_NUM_5
#define DEFAULT_RMT_2_GPIO gpio_num_t::GPIO_NUM_16
#define RMT_LAST OutputChannelId_RMT_3

// #define SUPPORT_OutputType_WS2801
// #define SUPPORT_OutputType_APA102
// #define SUPPORT_OutputType_TM1814
// #define SUPPORT_OutputType_TLS3001

// #define SUPPORT_RELAY_OUTPUT

#if defined(SUPPORT_OutputType_WS2801) || defined(SUPPORT_OutputType_APA102)
# define SUPPORT_SPI_OUTPUT

// SPI Output
#define DEFAULT_SPI_DATA_GPIO gpio_num_t::GPIO_NUM_15
#define DEFAULT_SPI_CLOCK_GPIO gpio_num_t::GPIO_NUM_25

#endif // defined(SUPPORT_OutputType_WS2801) || defined(SUPPORT_OutputType_TM1814)

// File Manager
#define SD_CARD_MISO_PIN gpio_num_t::GPIO_NUM_12
#define SD_CARD_MOSI_PIN gpio_num_t::GPIO_NUM_13
#define SD_CARD_CLK_PIN gpio_num_t::GPIO_NUM_14
#define SD_CARD_CS_PIN gpio_num_t::GPIO_NUM_15
8 changes: 4 additions & 4 deletions ESPixelStick/src/GPIO_Defs_ESP32_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#define DEFAULT_RMT_3_GPIO gpio_num_t::GPIO_NUM_33
#define RMT_LAST OutputChannelId_RMT_4

// #define SUPPORT_OutputType_WS2801
// #define SUPPORT_OutputType_APA102
// #define SUPPORT_OutputType_TM1814
// #define SUPPORT_OutputType_TLS3001
// #define SUPPORT_OutputType_WS2801 // requires a change in the html directory
// #define SUPPORT_OutputType_APA102 // requires a change in the html directory
// #define SUPPORT_OutputType_TM1814 // requires a change in the html directory
// #define SUPPORT_OutputType_TLS3001 // requires a change in the html directory

#define SUPPORT_RELAY_OUTPUT

Expand Down
1 change: 0 additions & 1 deletion ESPixelStick/src/WebMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ void c_WebMgr::onWsEvent (AsyncWebSocket* server, AsyncWebSocketClient * client,
OutputMgr.PauseOutputs ();

// convert the input data into a json structure (use json read only mode)
size_t docSize = strlen ((const char*)(&WebSocketFrameCollectionBuffer[0])) * 3;
webJsonDoc.clear ();
DeserializationError error = deserializeJson (webJsonDoc, (const char *)(&WebSocketFrameCollectionBuffer[0]));

Expand Down
48 changes: 41 additions & 7 deletions ESPixelStick/src/output/OutputMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
//-----------------------------------------------------------------------------
// bring in driver definitions
#include "OutputDisabled.hpp"
#include "OutputAPA102Spi.hpp"
#include "OutputGECE.hpp"
#include "OutputSerial.hpp"
#include "OutputWS2811Uart.hpp"
#include "OutputTM1814Uart.hpp"
#include "OutputTM1814Rmt.hpp"
#include "OutputRelay.hpp"
#include "OutputSerial.hpp"
#include "OutputServoPCA9685.hpp"
#include "OutputWS2811Rmt.hpp"
#include "OutputTM1814Rmt.hpp"
#include "OutputTM1814Uart.hpp"
#include "OutputUCS1903Rmt.hpp"
#include "OutputUCS1903Uart.hpp"
#include "OutputWS2801Spi.hpp"
#include "OutputAPA102Spi.hpp"
#include "OutputWS2811Rmt.hpp"
#include "OutputWS2811Uart.hpp"
// needs to be last
#include "OutputMgr.hpp"

Expand Down Expand Up @@ -64,6 +66,10 @@ static const OutputTypeXlateMap_t OutputTypeXlateMap[c_OutputMgr::e_OutputType::
#endif // def SUPPORT_RELAY_OUTPUT
{c_OutputMgr::e_OutputType::OutputType_Disabled, "Disabled" },

#ifdef SUPPORT_OutputType_UCS1903
{c_OutputMgr::e_OutputType::OutputType_UCS1903, "UCS1903" },
#endif // def SUPPORT_OutputType_TM1814

#ifdef SUPPORT_OutputType_TM1814
{c_OutputMgr::e_OutputType::OutputType_TM1814, "TM1814" },
#endif // def SUPPORT_OutputType_TM1814
Expand Down Expand Up @@ -594,6 +600,34 @@ void c_OutputMgr::InstantiateNewOutputChannel (e_OutputChannelIds ChannelIndex,
break;
}

#ifdef SUPPORT_OutputType_UCS1903
case e_OutputType::OutputType_UCS1903:
{
#ifdef SUPPORT_RMT
if (OM_IS_RMT)
{
// logcon (CN_stars + String (F (" Starting TM1814 RMT for channel '")) + ChannelIndex + "'. " + CN_stars);
pOutputChannelDrivers[ChannelIndex] = new c_OutputUCS1903Rmt (ChannelIndex, dataPin, UartId, OutputType_UCS1903);
// DEBUG_V ("");
break;
}
#endif // def SUPPORT_RMT
// DEBUG_V ("");
if (OM_IS_UART)
{
// logcon (CN_stars + String (F (" Starting TM1814 UART for channel '")) + ChannelIndex + "'. " + CN_stars);
pOutputChannelDrivers[ChannelIndex] = new c_OutputUCS1903Uart (ChannelIndex, dataPin, UartId, OutputType_UCS1903);
// DEBUG_V ("");
break;
}

if (!BuildingNewConfig) { logcon (CN_stars + String (F (" Cannot Start UCS1903 for channel '")) + ChannelIndex + "'. " + CN_stars); }
pOutputChannelDrivers[ChannelIndex] = new c_OutputDisabled (ChannelIndex, dataPin, UartId, OutputType_Disabled);
// DEBUG_V ("");
break;
}
#endif // def SUPPORT_OutputType_UCS1903

#ifdef SUPPORT_OutputType_TM1814
case e_OutputType::OutputType_TM1814:
{
Expand All @@ -615,7 +649,7 @@ void c_OutputMgr::InstantiateNewOutputChannel (e_OutputChannelIds ChannelIndex,
break;
}

if (!BuildingNewConfig) { logcon (CN_stars + String (F (" Cannot Start WS2811 for channel '")) + ChannelIndex + "'. " + CN_stars); }
if (!BuildingNewConfig) { logcon (CN_stars + String (F (" Cannot Start TM1814 for channel '")) + ChannelIndex + "'. " + CN_stars); }
pOutputChannelDrivers[ChannelIndex] = new c_OutputDisabled (ChannelIndex, dataPin, UartId, OutputType_Disabled);
// DEBUG_V ("");
break;
Expand Down
3 changes: 3 additions & 0 deletions ESPixelStick/src/output/OutputMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class c_OutputMgr
OutputType_Relay,
OutputType_Servo_PCA9685,
#endif // def SUPPORT_RELAY_OUTPUT
#ifdef SUPPORT_OutputType_UCS1903
OutputType_UCS1903,
#endif // def SUPPORT_OutputType_TM1814
#ifdef SUPPORT_OutputType_TM1814
OutputType_TM1814,
#endif // def SUPPORT_OutputType_TM1814
Expand Down
83 changes: 58 additions & 25 deletions ESPixelStick/src/output/OutputRelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ GNU General Public License for more details.
#define Relay_OUTPUT_NOT_PWM false
#define Relay_DEFAULT_TRIGGER_LEVEL 128
#define Relay_DEFAULT_GPIO_ID ((gpio_num_t)0)
#define RelayPwmHigh 255
#define RelayPwmLow 0
#if defined(ARDUINO_ARCH_ESP32)
#define RelayPwmFrequency , 12000
#else
#define RelayPwmFrequency
#endif // defined(ARDUINO_ARCH_ESP32)

static const c_OutputRelay::RelayChannel_t RelayChannelDefaultSettings[] =
{
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
{Relay_OUTPUT_DISABLED, Relay_OUTPUT_INVERTED, Relay_OUTPUT_NOT_PWM, Relay_DEFAULT_TRIGGER_LEVEL, Relay_DEFAULT_GPIO_ID, LOW, HIGH, HIGH RelayPwmFrequency},
};

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -140,21 +147,37 @@ bool c_OutputRelay::validate ()
#if defined(ARDUINO_ARCH_ESP32)
// assign GPIO to a channel and set the pwm 12 Khz frequency, 8 bit
ledcAttachPin(currentRelay.GpioId, Channel);
ledcSetup(Channel, 12000, 8);
ledcSetup(Channel, currentRelay.PwmFrequency, 8);
#else
pinMode (currentRelay.GpioId, OUTPUT);
#endif
}

if (currentRelay.InvertOutput)
if (currentRelay.Pwm)
{
currentRelay.OffValue = HIGH;
currentRelay.OnValue = LOW;
if (currentRelay.InvertOutput)
{
currentRelay.OffValue = RelayPwmHigh;
currentRelay.OnValue = RelayPwmLow;
}
else
{
currentRelay.OffValue = RelayPwmLow;
currentRelay.OnValue = RelayPwmHigh;
}
}
else
{
currentRelay.OffValue = LOW;
currentRelay.OnValue = HIGH;
if (currentRelay.InvertOutput)
{
currentRelay.OffValue = HIGH;
currentRelay.OnValue = LOW;
}
else
{
currentRelay.OffValue = LOW;
currentRelay.OnValue = HIGH;
}
}

// DEBUGV (String ("CurrentRelayChanIndex: ") + String (CurrentRelayChanIndex++));
Expand Down Expand Up @@ -219,6 +242,9 @@ bool c_OutputRelay::SetConfig (ArduinoJson::JsonObject & jsonConfig)
setFromJSON (CurrentOutputChannel->InvertOutput, JsonChannelData, OM_RELAY_CHANNEL_INVERT_NAME);
setFromJSON (CurrentOutputChannel->Pwm, JsonChannelData, OM_RELAY_CHANNEL_PWM_NAME);
setFromJSON (CurrentOutputChannel->OnOffTriggerLevel, JsonChannelData, CN_trig);
#if defined(ARDUINO_ARCH_ESP32)
setFromJSON (CurrentOutputChannel->PwmFrequency, JsonChannelData, CN_Frequency);
#endif // defined(ARDUINO_ARCH_ESP32)

// DEBUGV (String ("currentRelay.GpioId: ") + String (CurrentOutputChannel->GpioId));
temp = CurrentOutputChannel->GpioId;
Expand All @@ -233,14 +259,17 @@ bool c_OutputRelay::SetConfig (ArduinoJson::JsonObject & jsonConfig)
}
CurrentOutputChannel->GpioId = (gpio_num_t)temp;

// DEBUGV (String ("CurrentRelayChanIndex: ") + String (ChannelId));
// DEBUGV (String ("currentRelay.OnValue: ") + String (CurrentOutputChannel->OnValue));
// DEBUGV (String ("currentRelay.OffValue: ") + String (CurrentOutputChannel->OffValue));
// DEBUGV (String ("currentRelay.Enabled: ") + String (CurrentOutputChannel->Enabled));
// DEBUGV (String ("currentRelay.InvertOutput: ") + String (CurrentOutputChannel->InvertOutput));
// DEBUGV (String (" CurrentRelayChanIndex: ") + String (ChannelId));
// DEBUGV (String (" currentRelay.OnValue: ") + String (CurrentOutputChannel->OnValue));
// DEBUGV (String (" currentRelay.OffValue: ") + String (CurrentOutputChannel->OffValue));
// DEBUGV (String (" currentRelay.Enabled: ") + String (CurrentOutputChannel->Enabled));
// DEBUGV (String (" currentRelay.InvertOutput: ") + String (CurrentOutputChannel->InvertOutput));
// DEBUGV (String ("currentRelay.OnOffTriggerLevel: ") + String (CurrentOutputChannel->OnOffTriggerLevel));
// DEBUGV (String ("currentRelay.GpioId: ") + String (CurrentOutputChannel->GpioId));
// DEBUGV (String ("currentRelay.Pwm: ") + String (CurrentOutputChannel->Pwm));
// DEBUGV (String (" currentRelay.GpioId: ") + String (CurrentOutputChannel->GpioId));
// DEBUGV (String (" currentRelay.Pwm: ") + String (CurrentOutputChannel->Pwm));
#if defined(ARDUINO_ARCH_ESP32)
// DEBUGV (String (" currentRelay.PwmFrequency: ") + String (CurrentOutputChannel->Pwm));
#endif // defined(ARDUINO_ARCH_ESP32)

++ChannelId;
}
Expand Down Expand Up @@ -271,12 +300,16 @@ void c_OutputRelay::GetConfig (ArduinoJson::JsonObject & jsonConfig)
{
JsonObject JsonChannelData = JsonChannelList.createNestedObject ();

JsonChannelData[CN_id] = ChannelId;
JsonChannelData[CN_id] = ChannelId;
JsonChannelData[OM_RELAY_CHANNEL_ENABLED_NAME] = currentRelay.Enabled;
JsonChannelData[OM_RELAY_CHANNEL_INVERT_NAME] = currentRelay.InvertOutput;
JsonChannelData[OM_RELAY_CHANNEL_PWM_NAME] = currentRelay.Pwm;
JsonChannelData[CN_trig] = currentRelay.OnOffTriggerLevel;
JsonChannelData[CN_gid] = currentRelay.GpioId;
JsonChannelData[CN_trig] = currentRelay.OnOffTriggerLevel;
JsonChannelData[CN_gid] = currentRelay.GpioId;

#if defined(ARDUINO_ARCH_ESP32)
JsonChannelData[CN_Frequency] = currentRelay.PwmFrequency;
#endif // defined(ARDUINO_ARCH_ESP32)

// DEBUGV (String ("CurrentRelayChanIndex: ") + String (ChannelId));
// DEBUGV (String ("currentRelay.OnValue: ") + String (currentRelay.OnValue));
Expand Down Expand Up @@ -310,7 +343,7 @@ void c_OutputRelay::Render ()
// DEBUG_V (String("OutputDataIndex: ") + String(OutputDataIndex));
if (currentRelay.Enabled)
{
uint8_t newOutputValue = pOutputBuffer[OutputDataIndex];
uint8_t newOutputValue = map (pOutputBuffer[OutputDataIndex], 0, 255, currentRelay.OffValue, currentRelay.OnValue);
if (currentRelay.Pwm)
{
#if defined(ARDUINO_ARCH_ESP32)
Expand Down
3 changes: 3 additions & 0 deletions ESPixelStick/src/output/OutputRelay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class c_OutputRelay : public c_OutputCommon
uint8_t OnValue;
uint8_t OffValue;
uint8_t previousValue;
#if defined(ARDUINO_ARCH_ESP32)
uint16_t PwmFrequency;
#endif // defined(ARDUINO_ARCH_ESP32)

} RelayChannel_t;

Expand Down
Loading