Skip to content

Commit

Permalink
Fixed multicast issues and add universe boundary config
Browse files Browse the repository at this point in the history
  • Loading branch information
forkineye committed Aug 4, 2017
1 parent 3bcbcfa commit 437aa6c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 24 deletions.
5 changes: 3 additions & 2 deletions ESPixelStick.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
#endif

/* Name and version */
const char VERSION[] = "3.0-rc1 (20170729)";
const char VERSION[] = "3.0-rc2 (20170804)";

#define HTTP_PORT 80 /* Default web server port */
#define MQTT_PORT 1883 /* Default MQTT port */
#define DATA_PIN 2 /* Pixel output - GPIO2 */
#define EEPROM_BASE 0 /* EEPROM configuration base address */
#define UNIVERSE_LIMIT 512 /* Universe boundary - 512 Channels */
#define UNIVERSE_MAX 512 /* Max channels in a DMX Universe */
#define PIXEL_LIMIT 1360 /* Total pixel limit - 40.85ms for 8 universes */
#define RENARD_LIMIT 2048 /* Channel limit for serial outputs */
#define E131_TIMEOUT 1000 /* Force refresh every second an E1.31 packet is not seen */
Expand Down Expand Up @@ -101,6 +101,7 @@ typedef struct {

/* E131 */
uint16_t universe; /* Universe to listen for */
uint16_t universe_limit; /* Universe boundary limit */
uint16_t channel_start; /* Channel to start listening at - 1 based */
uint16_t channel_count; /* Number of channels */
bool multicast; /* Enable multicast listener */
Expand Down
47 changes: 36 additions & 11 deletions ESPixelStick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,24 @@ void onWiFiDisconnect(const WiFiEventStationModeDisconnected &event) {
wifiTicker.once(2, connectWifi);
}

// Subscribe to "n" universes, starting at "universe"
void multiSub() {
uint8_t count;
ip_addr_t ifaddr;
ip_addr_t multicast_addr;

count = uniLast - config.universe + 1;
ifaddr.addr = static_cast<uint32_t>(WiFi.localIP());
for (uint8_t i = 0; i < count; i++) {
multicast_addr.addr = static_cast<uint32_t>(IPAddress(239, 255,
(((config.universe + i) >> 8) & 0xff),
(((config.universe + i) >> 0) & 0xff)));
igmp_joingroup(&ifaddr, &multicast_addr);
}
}

/////////////////////////////////////////////////////////
//
//
// MQTT Section
//
/////////////////////////////////////////////////////////
Expand Down Expand Up @@ -457,10 +473,13 @@ void validateConfig() {
if (config.universe < 1)
config.universe = 1;

if (config.universe_limit > UNIVERSE_MAX || config.universe_limit < 1)
config.universe_limit = UNIVERSE_MAX;

if (config.channel_start < 1)
config.channel_start = 1;
else if (config.channel_start > UNIVERSE_LIMIT)
config.channel_start = UNIVERSE_LIMIT;
else if (config.channel_start > config.universe_limit)
config.channel_start = config.universe_limit;

// Set default MQTT port if missing
if (config.mqtt_port == 0)
Expand Down Expand Up @@ -504,8 +523,8 @@ void validateConfig() {
else if (config.channel_count < 1)
config.channel_count = 1;

if (config.serial_type == SerialType::DMX512 && config.channel_count > UNIVERSE_LIMIT)
config.channel_count = UNIVERSE_LIMIT;
if (config.serial_type == SerialType::DMX512 && config.channel_count > UNIVERSE_MAX)
config.channel_count = UNIVERSE_MAX;

// Baud rate check
if (config.baudrate > BaudRate::BR_460800)
Expand All @@ -521,10 +540,10 @@ void updateConfig() {

/* Find the last universe we should listen for */
uint16_t span = config.channel_start + config.channel_count - 1;
if (span % UNIVERSE_LIMIT)
uniLast = config.universe + span / UNIVERSE_LIMIT;
if (span % config.universe_limit)
uniLast = config.universe + span / config.universe_limit;
else
uniLast = config.universe + span / UNIVERSE_LIMIT - 1;
uniLast = config.universe + span / config.universe_limit - 1;

/* Setup the sequence error tracker */
uint8_t uniTotal = (uniLast + 1) - config.universe;
Expand Down Expand Up @@ -553,6 +572,10 @@ void updateConfig() {
LOG_PORT.print(config.universe);
LOG_PORT.print(F(" to "));
LOG_PORT.println(uniLast);

// Setup IGMP subscriptions if multicast is enabled
if (config.multicast)
multiSub();
}

// De-Serialize Network config
Expand Down Expand Up @@ -591,6 +614,7 @@ void dsDeviceConfig(JsonObject &json) {

// E131
config.universe = json["e131"]["universe"];
config.universe_limit = json["e131"]["universe_limit"];
config.channel_start = json["e131"]["channel_start"];
config.channel_count = json["e131"]["channel_count"];
config.multicast = json["e131"]["multicast"];
Expand Down Expand Up @@ -696,6 +720,7 @@ void serializeConfig(String &jsonString, bool pretty, bool creds) {
// E131
JsonObject &e131 = json.createNestedObject("e131");
e131["universe"] = config.universe;
e131["universe_limit"] = config.universe_limit;
e131["channel_start"] = config.channel_start;
e131["channel_count"] = config.channel_count;
e131["multicast"] = config.multicast;
Expand Down Expand Up @@ -796,12 +821,12 @@ void loop() {
offset = config.channel_start - 1;

// Find start of data based off the Universe
int16_t dataStart = uniOffset * UNIVERSE_LIMIT - offset;
int16_t dataStart = uniOffset * config.universe_limit - offset;

// Calculate how much data we need for this buffer
uint16_t dataStop = config.channel_count;
if ((dataStart + UNIVERSE_LIMIT) < dataStop)
dataStop = dataStart + UNIVERSE_LIMIT;
if ((dataStart + config.universe_limit) < dataStop)
dataStop = dataStart + config.universe_limit;

// Set the data
uint16_t buffloc = 0;
Expand Down
1 change: 1 addition & 0 deletions data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

"e131": {
"universe": 1,
"universe_limit": 512,
"channel_start": 1,
"channel_count": 512,
"multicast": false
Expand Down
5 changes: 5 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
<label class="control-label col-sm-2" for="channel_start">Start Channel</label>
<div class="col-sm-10"><input type="text" class="form-control" id="channel_start" name="channel_start" placeholder="Enter Starting Channel"></div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="universe_limit">Universe Boundary</label>
<div class="col-sm-10"><input type="text" class="form-control" id="universe_limit" name="universe_limit" placeholder="Enter number of channels to handle per universe"></div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox"><label><input type="checkbox" id="multicast" name="multicast"> Enable Multicast</label></div>
Expand Down
2 changes: 2 additions & 0 deletions html/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ function getConfig(data) {

// E1.31 Config
$('#universe').val(config.e131.universe);
$('#universe_limit').val(config.e131.universe_limit);
$('#channel_start').val(config.e131.channel_start);
$('#multicast').prop('checked', config.e131.multicast);

Expand Down Expand Up @@ -571,6 +572,7 @@ function submitConfig() {
},
'e131': {
'universe': parseInt($('#universe').val()),
'universe_limit': parseInt($('#universe_limit').val()),
'channel_start': parseInt($('#channel_start').val()),
'channel_count': channels,
'multicast': $('#multicast').prop('checked')
Expand Down
21 changes: 10 additions & 11 deletions wshandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,22 @@ void procS(uint8_t *data, AsyncWebSocketClient *client) {
client->text("S1");
break;
case '2': // Set Device Config
// Reboot if MQTT changed
bool reboot = false;
if (config.mqtt != json["mqtt"]["enabled"])
reboot = true;

dsDeviceConfig(json);
saveConfig();
client->text("S2");

if (reboot)
client->text("S1");
else
client->text("S2");
break;
}
}

/*
enum class TestMode : uint8_t {
DISABLED,
STATIC,
CHASE,
RAINBOW,
VIEW_STREAM
};
*/

void procT(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '0':
Expand Down

0 comments on commit 437aa6c

Please sign in to comment.