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

Pwm support with web config #66

Closed
wants to merge 78 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
7ab8609
hostname and udpraw support
penfold42 Aug 20, 2016
a32bb6a
bounds checking and fill remaining leds with 0
penfold42 Aug 21, 2016
9f527eb
why did github desktop miss this change?
penfold42 Aug 21, 2016
6bdab6e
started adding direct colour control
penfold42 Aug 22, 2016
73f6a17
added last_clientIP and port to e131 status
penfold42 Aug 25, 2016
42a2152
admin and same origin
penfold42 Aug 28, 2016
7ce4c6f
random stuff
penfold42 Aug 29, 2016
f66812c
Merge pull request #2 from forkineye/master
penfold42 Sep 23, 2016
8682719
Initial commit of new web interface
forkineye Sep 28, 2016
4370728
Merge branch 'master' of https://github.com/forkineye/ESPixelStick in…
penfold42 Sep 29, 2016
0f4b5d1
readd udpraw support
penfold42 Sep 29, 2016
4a1a08f
Merge branch 'master' of https://github.com/forkineye/ESPixelStick in…
penfold42 Sep 30, 2016
3b61b46
Added flash info, freeheap
penfold42 Sep 30, 2016
35ff55d
minor cleanups
penfold42 Sep 30, 2016
e904f78
adding colour status
penfold42 Oct 9, 2016
9104fe5
Merge pull request #3 from penfold42/201+raw
penfold42 Nov 26, 2016
0f003bb
move globals to
penfold42 Jun 20, 2017
154257e
start adding pwm support
penfold42 Aug 5, 2017
262b494
make pwm conditional
penfold42 Aug 6, 2017
b4423cd
PWM: added json serialize deserialize
penfold42 Aug 6, 2017
261b5da
PWM: html config options
penfold42 Aug 7, 2017
c29c779
PWM: added gpio 1,12,13,14 to webUI
penfold42 Aug 9, 2017
9dcb7d4
PWM: added labels and gpio15
penfold42 Aug 11, 2017
ff6a117
Merge remote-tracking branch 'refs/remotes/forkineye/master' into pwm
penfold42 Aug 11, 2017
b56c9aa
PWM: fixed up gpio15
penfold42 Aug 12, 2017
6ba4d69
PWM: re-do layout html
penfold42 Aug 14, 2017
8c83ddd
PWM: gamma, invert
penfold42 Aug 14, 2017
78fa2bd
PWM: fix conditional build options
penfold42 Aug 15, 2017
6b5a59d
PWM: updated gpio labels
penfold42 Aug 17, 2017
43f6117
PWM: added pwm frequency adjustment
penfold42 Aug 17, 2017
914dfcc
Device Mode is now a bitfield
penfold42 Aug 19, 2017
18f1e37
PWM: use bitmask for valid_gpio list
penfold42 Aug 20, 2017
c03554f
PWM: force disable invalid gpios
penfold42 Aug 20, 2017
d1acfd2
PWM: add gpio16, range check DMX channel
penfold42 Aug 21, 2017
66bb241
Added adjustable gamma value
penfold42 Oct 9, 2017
9c6867c
fix gamma calculation error
penfold42 Oct 9, 2017
529e0dd
validate gamma value. 2.2 is default
penfold42 Oct 10, 2017
97a65eb
Merge remote-tracking branch 'refs/remotes/forkineye/master'
penfold42 Oct 11, 2017
11990b4
Merge branch 'master' of https://github.com/penfold42/ESPixelStick
penfold42 Oct 11, 2017
8f6375b
Merge remote-tracking branch 'refs/remotes/origin/gamma' into pwm
penfold42 Oct 11, 2017
32f6de0
user gamma into pwm cleanup
penfold42 Oct 11, 2017
a067e7b
MQTT: apply brightness
penfold42 Oct 12, 2017
2305094
Added direct GPIO manipulation via http
XfableX Oct 14, 2017
e9712c7
simple gpio web interface
XfableX Oct 14, 2017
47b4a9c
Fixed Toggle function
XfableX Oct 15, 2017
4bfaabf
split pwm into separate files
penfold42 Oct 15, 2017
82e63af
Updated UI for Phone and Desktop
XfableX Oct 19, 2017
4d914d0
added /uptime
penfold42 Oct 19, 2017
c4077b5
gpio0 was deemed invalid
penfold42 Oct 20, 2017
bc54ab7
CORS default header
penfold42 Oct 20, 2017
ea0eb88
Merge pull request #1 from penfold42/gpio_access
XfableX Oct 20, 2017
ed99df9
gamma svg graph
penfold42 Oct 22, 2017
a4bc142
added link to gamma graph in index.html
penfold42 Oct 22, 2017
d9f4ab0
gamma graph tidying
penfold42 Oct 22, 2017
d28631d
cleanup junk files
penfold42 Oct 23, 2017
a8fb729
cleanout more junk
penfold42 Oct 23, 2017
ff4361f
gamma updates
penfold42 Oct 23, 2017
c3ae825
incorporate udpraw support (#8)
penfold42 Oct 24, 2017
e5cb28c
PWM: optimise heap usage
penfold42 Oct 26, 2017
5a4cc7a
Cleanup headers
penfold42 Oct 26, 2017
2aea9d2
fixups for building with make
penfold42 Oct 26, 2017
e89d87d
split pwm into separate files
penfold42 Oct 15, 2017
0ea38e4
PWM: optimise heap usage
penfold42 Oct 26, 2017
99ad2e6
fixups for building with make
penfold42 Oct 26, 2017
3d98e32
Merge pull request #9 from penfold42/pwm
penfold42 Oct 26, 2017
2388723
Merge pull request #2 from penfold42/master
XfableX Oct 27, 2017
9c89f2d
Merge pull request #3 from penfold42/gpio_access
XfableX Oct 27, 2017
30973c8
PWM: update gpio16 use on h801
penfold42 Nov 2, 2017
5007fa7
PWM: change scaling maths
penfold42 Nov 3, 2017
adcd573
Merge branch 'master' of https://github.com/forkineye/ESPixelStick in…
penfold42 Nov 12, 2017
b4aff1f
Alternate approach for websocket delay on test modes
penfold42 Nov 12, 2017
9d4e38d
gamma svg graph
penfold42 Oct 22, 2017
c1c6ccb
added link to gamma graph in index.html
penfold42 Oct 22, 2017
61a65b4
gamma graph tidying
penfold42 Oct 22, 2017
5ab8a2f
Add brightness setting to gamma calculation
penfold42 Nov 15, 2017
4b9e57f
PWM: added digital output option
penfold42 Nov 16, 2017
e9ef93b
Merge branch 'gpio_access' into pwm
XfableX Nov 16, 2017
901d476
Merge branch 'gpio_access' into pwm
XfableX Nov 16, 2017
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
51 changes: 48 additions & 3 deletions ESPixelStick.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@ const char BUILD_DATE[] = __DATE__ " " __TIME__;
#define ESPS_MODE_PIXEL
//#define ESPS_MODE_SERIAL

/* Include support for PWM */
#define ESPS_SUPPORT_PWM

/*****************************************/
/* END - Configuration */
/*****************************************/

#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncUDP.h>
#include <ESPAsyncWebServer.h>

#if defined(ESPS_MODE_PIXEL)
#include "PixelDriver.h"
#elif defined(ESPS_MODE_SERIAL)
Expand Down Expand Up @@ -64,11 +75,22 @@ const char BUILD_DATE[] = __DATE__ " " __TIME__;
#define CONFIG_MAX_SIZE 2048 /* Sanity limit for config file */

/* Pixel Types */
class DevCap {
public:
bool MPIXEL : 1;
bool MSERIAL : 1;
bool MPWM : 1;
uint8_t toInt() {
return (MPWM<<2 | MSERIAL<<1 | MPIXEL);
}

};
/*
enum class DevMode : uint8_t {
MPIXEL,
MSERIAL
};

*/
/* Test Modes */
enum class TestMode : uint8_t {
DISABLED,
Expand All @@ -89,7 +111,7 @@ typedef struct {
typedef struct {
/* Device */
String id; /* Device ID */
DevMode devmode; /* Device Mode - used for reporting mode, can't be set */
DevCap devmode; /* Device Mode - used for reporting mode, can't be set */
TestMode testmode; /* Testing mode */

/* Network */
Expand Down Expand Up @@ -122,12 +144,22 @@ typedef struct {
PixelType pixel_type; /* Pixel type */
PixelColor pixel_color; /* Pixel color order */
bool gamma; /* Use gamma map? */

float gammaVal; /* gamma value to use */
float briteVal; /* brightness lto use */
#elif defined(ESPS_MODE_SERIAL)
/* Serial */
SerialType serial_type; /* Serial type */
BaudRate baudrate; /* Baudrate */
#endif
#if defined(ESPS_SUPPORT_PWM)
bool pwm_global_enabled; /* is pwm runtime enabled? */
int pwm_freq; /* pwm frequency */
bool pwm_gamma; /* is pwm runtime enabled? */
uint16_t pwm_gpio_dmx[17]; /* which dmx channel is gpio[n] mapped to? */
uint32_t pwm_gpio_enabled; /* is gpio[n] enabled? */
uint32_t pwm_gpio_invert; /* is gpio[n] active high or active low? */
uint32_t pwm_gpio_digital; /* is gpio[n] digital or "analog"? */
#endif
} config_t;

/* Forward Declarations */
Expand All @@ -136,4 +168,17 @@ void dsNetworkConfig(JsonObject &json);
void dsDeviceConfig(JsonObject &json);
void saveConfig();

void connectWifi();
void onWifiConnect(const WiFiEventStationModeGotIP &event);
void onWiFiDisconnect(const WiFiEventStationModeDisconnected &event);
void connectToMqtt();
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttMessage(char* topic, char* p_payload,
AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void publishRGBState();
void publishRGBBrightness();
void publishRGBColor();
void setStatic(uint8_t r, uint8_t g, uint8_t b);

#endif /* ESPIXELSTICK_H_ */
125 changes: 116 additions & 9 deletions ESPixelStick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

/* Output Mode has been moved to ESPixelStick.h */


/* Fallback configuration if config.json is empty or fails */
const char ssid[] = "ENTER_SSID_HERE";
const char passphrase[] = "ENTER_PASSPHRASE_HERE";
Expand All @@ -43,8 +44,12 @@ const char passphrase[] = "ENTER_PASSPHRASE_HERE";
#include <Hash.h>
#include <SPI.h>
#include "ESPixelStick.h"
#include "udpraw.h"
#include "EFUpdate.h"
#include "wshandler.h"
#include "pwm.h"
#include "gamma.h"
#include "gpio.h"

extern "C" {
#include <user_interface.h>
Expand Down Expand Up @@ -122,11 +127,14 @@ SerialDriver serial; // Serial object
#error "No valid output mode defined."
#endif



/////////////////////////////////////////////////////////
//
// Forward Declarations
//
/////////////////////////////////////////////////////////

void loadConfig();
void initWifi();
void initWeb();
Expand All @@ -142,7 +150,7 @@ RF_PRE_INIT() {
void setup() {
// Configure SDK params
wifi_set_sleep_type(NONE_SLEEP_T);

ToggleSetup();
// Initial pin states
pinMode(DATA_PIN, OUTPUT);
digitalWrite(DATA_PIN, LOW);
Expand Down Expand Up @@ -228,6 +236,11 @@ void setup() {
}

// Configure the outputs
#if defined (ESPS_SUPPORT_PWM)
setupPWM();

#endif

#if defined (ESPS_MODE_PIXEL)
pixels.setPin(DATA_PIN);
updateConfig();
Expand Down Expand Up @@ -395,7 +408,7 @@ Serial.println(payload);
config.testmode = TestMode::MQTT;
if (m_rgb_state != true) {
m_rgb_state = true;
setStatic(m_rgb_red, m_rgb_green, m_rgb_blue);
setStatic(m_rgb_red*m_rgb_brightness/100, m_rgb_green*m_rgb_brightness/100, m_rgb_blue*m_rgb_brightness/100);
publishRGBState();
}
} else if (payload.equals(String(LIGHT_OFF))) {
Expand All @@ -412,7 +425,7 @@ Serial.println(payload);
return;
} else {
m_rgb_brightness = brightness;
setStatic(m_rgb_red, m_rgb_green, m_rgb_blue);
setStatic(m_rgb_red*m_rgb_brightness/100, m_rgb_green*m_rgb_brightness/100, m_rgb_blue*m_rgb_brightness/100);
publishRGBBrightness();
}
} else if (String(config.mqtt_topic + MQTT_LIGHT_RGB_COMMAND_TOPIC).equals(topic)) {
Expand All @@ -424,7 +437,7 @@ Serial.println(payload);
m_rgb_green = payload.substring(firstIndex + 1, lastIndex).toInt();
m_rgb_blue = payload.substring(lastIndex + 1).toInt();

setStatic(m_rgb_red, m_rgb_green, m_rgb_blue);
setStatic(m_rgb_red*m_rgb_brightness/100, m_rgb_green*m_rgb_brightness/100, m_rgb_blue*m_rgb_brightness/100);
publishRGBColor();
}
}
Expand Down Expand Up @@ -478,22 +491,42 @@ void initWeb() {
// JSON Config Handler
web.on("/conf", HTTP_GET, [](AsyncWebServerRequest *request) {
String jsonString;
serializeConfig(jsonString);
serializeConfig(jsonString, true);
request->send(200, "text/json", jsonString);
});

// gamma debugging Config Handler
web.on("/gamma", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("text/plain");
for (int i=0; i<256; i++) {
response->printf ("%5d,", GAMMA_TABLE[i]);
if (i%16 == 15) {
response->printf("\r\n");
}
}
request->send(response);
});

// Firmware upload handler
web.on("/updatefw", HTTP_POST, [](AsyncWebServerRequest *request) {
ws.textAll("X6");
}, handle_fw_upload);

// Static Handler
web.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
web.serveStatic("/config.json", SPIFFS, "/config.json");

web.onNotFound([](AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Page not found");
if (request->method() == HTTP_OPTIONS) {
AsyncWebServerResponse *response = request->beginResponse(200);
request->send(response);
} else {
request->send(404, "text/plain", "Page not found");
}
});

DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), "*");

web.begin();

LOG_PORT.print(F("- Web Server started on port "));
Expand Down Expand Up @@ -531,10 +564,15 @@ void validateConfig() {
config.mqtt_topic = "diy/esps/" + String(chipId);
}

#if defined(ESPS_SUPPORT_PWM)
config.devmode.MPWM = true;
#endif

#if defined(ESPS_MODE_PIXEL)
// Set Mode
config.devmode = DevMode::MPIXEL;
// config.devmode = DevMode::MPIXEL;
config.devmode.MPIXEL = true;
config.devmode.MSERIAL = false;

// Generic channel limits for pixels
if (config.channel_count % 3)
Expand All @@ -552,9 +590,19 @@ void validateConfig() {
config.channel_count = 63 * 3;
}

// gamma value
if (config.gammaVal <= 0) {
config.gammaVal = 2.2;
}
// default gamma value
if (config.briteVal <= 0) {
config.briteVal = 1.0;
}
#elif defined(ESPS_MODE_SERIAL)
// Set Mode
config.devmode = DevMode::MSERIAL;
// config.devmode = DevMode::MSERIAL;
config.devmode.MPIXEL = false;
config.devmode.MSERIAL = true;

// Generic serial channel limits
if (config.channel_count > RENARD_LIMIT)
Expand Down Expand Up @@ -602,6 +650,8 @@ void updateConfig() {
#if defined(ESPS_MODE_PIXEL)
pixels.begin(config.pixel_type, config.pixel_color, config.channel_count / 3);
pixels.setGamma(config.gamma);
updateGammaTable(config.gammaVal, config.briteVal);

#elif defined(ESPS_MODE_SERIAL)
serial.begin(&SEROUT_PORT, config.serial_type, config.channel_count, config.baudrate);
#endif
Expand Down Expand Up @@ -671,12 +721,38 @@ void dsDeviceConfig(JsonObject &json) {
config.pixel_type = PixelType(static_cast<uint8_t>(json["pixel"]["type"]));
config.pixel_color = PixelColor(static_cast<uint8_t>(json["pixel"]["color"]));
config.gamma = json["pixel"]["gamma"];
config.gammaVal = json["pixel"]["gammaVal"];
config.briteVal = json["pixel"]["briteVal"];

#elif defined(ESPS_MODE_SERIAL)
/* Serial */
config.serial_type = SerialType(static_cast<uint8_t>(json["serial"]["type"]));
config.baudrate = BaudRate(static_cast<uint32_t>(json["serial"]["baudrate"]));
#endif

#if defined(ESPS_SUPPORT_PWM)
config.pwm_global_enabled = json["pwm"]["enabled"];
config.pwm_freq = json["pwm"]["freq"];
config.pwm_gamma = json["pwm"]["gamma"];
config.pwm_gpio_invert = 0;
config.pwm_gpio_digital = 0;
config.pwm_gpio_enabled = 0;
for (int gpio=0; gpio < NUM_GPIO; gpio++ ) {
if ( valid_gpio_mask & 1<<gpio ) {
config.pwm_gpio_dmx[gpio] = json["pwm"]["gpio" + (String)gpio + "_channel"];
if (json["pwm"]["gpio" + (String)gpio + "_invert"]) {
config.pwm_gpio_invert |= 1<<gpio;
}
if (json["pwm"]["gpio" + (String)gpio + "_digital"]) {
config.pwm_gpio_digital |= 1<<gpio;
}
if (json["pwm"]["gpio" + (String)gpio + "_enabled"]) {
config.pwm_gpio_enabled |= 1<<gpio;
}
}
}
#endif

}

// Load configugration JSON file
Expand Down Expand Up @@ -728,7 +804,7 @@ void serializeConfig(String &jsonString, bool pretty, bool creds) {
// Device
JsonObject &device = json.createNestedObject("device");
device["id"] = config.id.c_str();
device["mode"] = static_cast<uint8_t>(config.devmode);
device["mode"] = config.devmode.toInt();

// Network
JsonObject &network = json.createNestedObject("network");
Expand Down Expand Up @@ -770,6 +846,8 @@ void serializeConfig(String &jsonString, bool pretty, bool creds) {
pixel["type"] = static_cast<uint8_t>(config.pixel_type);
pixel["color"] = static_cast<uint8_t>(config.pixel_color);
pixel["gamma"] = config.gamma;
pixel["gammaVal"] = config.gammaVal;
pixel["briteVal"] = config.briteVal;

#elif defined(ESPS_MODE_SERIAL)
// Serial
Expand All @@ -778,6 +856,22 @@ void serializeConfig(String &jsonString, bool pretty, bool creds) {
serial["baudrate"] = static_cast<uint32_t>(config.baudrate);
#endif

#if defined(ESPS_SUPPORT_PWM)
JsonObject &pwm = json.createNestedObject("pwm");
pwm["enabled"] = config.pwm_global_enabled;
pwm["freq"] = config.pwm_freq;
pwm["gamma"] = config.pwm_gamma;

for (int gpio=0; gpio < NUM_GPIO; gpio++ ) {
if ( valid_gpio_mask & 1<<gpio ) {
pwm["gpio" + (String)gpio + "_channel"] = static_cast<uint16_t>(config.pwm_gpio_dmx[gpio]);
pwm["gpio" + (String)gpio + "_enabled"] = static_cast<bool>(config.pwm_gpio_enabled & 1<<gpio);
pwm["gpio" + (String)gpio + "_invert"] = static_cast<bool>(config.pwm_gpio_invert & 1<<gpio);
pwm["gpio" + (String)gpio + "_digital"] = static_cast<bool>(config.pwm_gpio_digital & 1<<gpio);
}
}
#endif

if (pretty)
json.prettyPrintTo(jsonString);
else
Expand Down Expand Up @@ -833,6 +927,9 @@ void setStatic(uint8_t r, uint8_t g, uint8_t b) {
//
/////////////////////////////////////////////////////////
void loop() {
/* check for raw packets on port 2801 */
handle_raw_port();

e131_packet_t packet;

// Reboot handler
Expand Down Expand Up @@ -977,6 +1074,8 @@ void loop() {
}
}

ToggleTime();


/* Streaming refresh */
#if defined(ESPS_MODE_PIXEL)
Expand All @@ -986,4 +1085,12 @@ void loop() {
if (serial.canRefresh())
serial.show();
#endif


/* update the PWM outputs */
#if defined(ESPS_SUPPORT_PWM)
handlePWM();
#endif
}


Loading