Skip to content

Instantly share code, notes, and snippets.

@GeorgeHahn
Created April 15, 2017 17:46
Show Gist options
  • Save GeorgeHahn/7adecc9bc960ba16a741330fd323c0a2 to your computer and use it in GitHub Desktop.
Save GeorgeHahn/7adecc9bc960ba16a741330fd323c0a2 to your computer and use it in GitHub Desktop.
ESP8266 OPC LED Display
/*
ESPOPC -- Open Pixel Control server for ESP8266.
TODO
Support OTA
Notes
Gamma should be corrected on sender side
The MIT License (MIT)
Copyright (c) 2015-2017 by george@genericmaker.com
Based on code by bbx10node@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Esp.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
const char *ssid = "";
const char *password = "";
// Actual name will be *.local
const char myDNSName[] = "lights";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(7890);
#include <NeoPixelBus.h>
#define PixelCount 480 // Do I have an off by one error somewhere here?
#define COLORS 4 // RGB (3) or RGBW (4)
const int PixelPin = 2; // Pin # is decided by the chosen pixel output method
#define BufferCount (12)
#define OSCDEBUG (1)
#if OSCDEBUG
#define d(x) Serial.println(x)
#define dn(x) Serial.print(x)
#define df(x, ...) Serial.printf(x, __VA_ARGS__)
#else
#define d(x)
#define dn(x)
#define df(x, y)
#endif
// You can also use one of these for Esp8266, each having their own restrictions
//
// NOTE: These will ignore the PIN and use GPI03 pin
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount, PixelPin);
//NeoPixelBus<NeoRgbFeature, NeoEsp8266Dma400KbpsMethod> strip(PixelCount, PixelPin);
// Uart method is good for the Esp-01 or other pin restricted modules
// NOTE: These will ignore the PIN and use GPI02 pin
#if COLORS == 4
NeoPixelBus<NeoRgbwFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
#else
NeoPixelBus<NeoRgbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
#endif
// four element pixels, RGBW
//NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
#if COLORS == 4
RgbwColor Buffer[BufferCount][PixelCount];
#else
RgbColor Buffer[BufferCount][PixelCount];
#endif
volatile uint8_t FrontBuffer = 0;
volatile uint8_t BufferedFrames = 0;
uint8_t BackBuffer = 0;
static void frame_timer_interrupt() ICACHE_RAM_ATTR;
int timer_ticks = 0;
int one_ms;
volatile bool ready = false;
WiFiClient client;
MDNSResponder mdns;
#define minsize(x, y) (((x) < (y)) ? (x) : (y))
uint32_t usToTicks(uint32_t us)
{
return (clockCyclesPerMicrosecond() * us);
}
void setup()
{
Serial.begin(115200);
d();
// Reset all the neopixels to an off state
strip.Begin();
strip.Show();
// Connect to WiFi network
d();
d();
dn("Connecting to ");
d(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
dn(".");
}
d("");
d("WiFi connected");
// Print the IP address
d(WiFi.localIP());
// Set up mDNS responder:
if (!mdns.begin(myDNSName, WiFi.localIP())) {
d("Error setting up MDNS responder!");
} else {
d("mDNS responder started");
df("My name is [%s]\r\n", myDNSName);
}
// Start the server listening for incoming client connections
server.begin();
d("Server listening on port 7890");
// ESP.wdtDisable();
one_ms = usToTicks(1000);
// Setup 30FPS timer
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(frame_timer_interrupt);
timer_ticks = usToTicks(1000000 / 30); // FPS
timer0_write(ESP.getCycleCount() + timer_ticks);
interrupts();
}
#if OSCDEBUG
unsigned long previousFrameTime;
unsigned long TotalFrames = 0;
#endif
// TODO: Just set a flag here
static void frame_timer_interrupt()
{
// Set an interrupt for 1/30s from now
timer0_write(ESP.getCycleCount() + timer_ticks);
// No frame to show, come again soon
if (BufferedFrames == 0) {
return;
}
// We're about to overlap the (active) back buffer
if (FrontBuffer == BackBuffer) {
return;
}
// Display current frame (TODO: use memcpy)
for (int i = 0; i < PixelCount; i++) {
strip.SetPixelColor(i, Buffer[FrontBuffer][i]);
}
strip.Show();
// Advance front buffer
BufferedFrames--;
FrontBuffer++;
if (FrontBuffer >= BufferCount)
FrontBuffer = 0;
#if OSCDEBUG
d(BufferedFrames);
// Show frame-frame time (probably shouldn't print print here)
df("%lu\r\n", micros() - previousFrameTime);
previousFrameTime = micros();
TotalFrames++;
#endif
}
void loop()
{
static int packetParseState = 0;
static uint8_t pktChannel, pktCommand;
static uint16_t pktLength, pktLengthAdjusted, bytesIn;
static uint8_t pktData[PixelCount * COLORS];
uint16_t bytesRead;
// Check if a client has connected
if (!client) {
client = server.available();
if (!client) {
return;
}
d("new OPC client");
}
// Check to see if the client has disconnected
if (!client.connected()) {
d("OPC client disconnected");
client.stop();
client = server.available();
if (!client) {
return;
}
}
// TODO: Make this loop less blocking, and move frame display code to the main loop
while (client.available()) {
switch (packetParseState) {
case 0: // Get pktChannel
pktChannel = client.read();
packetParseState++;
#if OSCDEBUG
df("pktChannel %u\r\n", pktChannel);
#endif
break;
case 1: // Get pktCommand
pktCommand = client.read();
packetParseState++;
#if OSCDEBUG
df("pktCommand %u\r\n", pktCommand);
#endif
break;
case 2: // Get pktLength (high byte)
pktLength = client.read() << 8;
packetParseState++;
#if OSCDEBUG
df("pktLength high byte %u\r\n", pktLength);
#endif
break;
case 3: // Get pktLength (low byte)
pktLength = pktLength | client.read();
packetParseState++;
bytesIn = 0;
#if OSCDEBUG
df("pktLength %u\r\n", pktLength);
#endif
if (pktLength > sizeof(pktData)) {
d("Packet length exceeds size of buffer! Data discarded");
pktLengthAdjusted = sizeof(pktData);
} else {
pktLengthAdjusted = pktLength;
}
break;
case 4: // Read pktLengthAdjusted bytes into pktData
bytesRead = client.read(&pktData[bytesIn], minsize(sizeof(pktData), pktLengthAdjusted) - bytesIn);
bytesIn += bytesRead;
if (bytesIn >= pktLengthAdjusted) {
if ((pktCommand == 0) && (pktChannel <= 1)) {
int i;
uint8_t *pixrgbw;
pixrgbw = pktData;
// Don't allow buffer to overflow; don't drop frames
// int safety = 0;
// while (BufferedFrames >= BufferCount - 1) // (need 1 buffer for the front buffer)
// {
// delay(0);
// if (safety++ > 100000) {
// d("Trig.");
// break;
// }
// }
// Drop frames if we've overflowed
if (BufferedFrames >= BufferCount - 1) { // (need 1 buffer for the front buffer)
if (pktLength == pktLengthAdjusted) {
packetParseState = 0;
} else {
packetParseState++;
}
break;
}
// Advance back buffer
noInterrupts();
BackBuffer++;
if (BackBuffer >= BufferCount) {
BackBuffer = 0;
}
interrupts();
for (i = 0; i < minsize((pktLengthAdjusted / COLORS), PixelCount); i++) {
uint8_t g = *pixrgbw++; //GammaLUT[*pixrgbw++];
uint8_t r = *pixrgbw++;
uint8_t b = *pixrgbw++;
#if COLORS == 4
uint8_t w = *pixrgbw++;
Buffer[BackBuffer][i] = RgbwColor(r, g, b, w);
#else
Buffer[BackBuffer][i] = RgbColor(r, g, b);
#endif
}
noInterrupts();
BufferedFrames++;
// Dynamically adjust frame timer
// if(BufferedFrames > (High + Low) * (3/4)) {
// timer_ticks -= one_ms/100; // Overrun, play faster
// } else if(BufferedFrames < (High + Low) * (1/4)) {
// timer_ticks += one_ms/100; // Running low, play slower
// }
interrupts();
}
if (pktLength == pktLengthAdjusted)
packetParseState = 0;
else
packetParseState++;
}
break;
default: // Discard data that does not fit in pktData
bytesRead = client.read(pktData, pktLength - bytesIn);
bytesIn += bytesRead;
if (bytesIn >= pktLength) {
packetParseState = 0;
}
break;
}
dn(timer_ticks);
dn(':');
dn(BufferedFrames);
dn(':');
d(ESP.getFreeHeap());
#if OSCDEBUG
d(TotalFrames);
#endif
}
}
/*
ESPOPC -- Open Pixel Control server for ESP8266.
TODO
Support OTA
Notes
Gamma should be corrected on sender side
The MIT License (MIT)
Copyright (c) 2015-2017 by george@genericmaker.com
Based on code by bbx10node@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Esp.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <NeoPixelBus.h>
// Wifi credentials
const char *ssid = "";
const char *password = "";
// Actual name will be *.local
const char myDNSName[] = "lights";
#define MAX_PIXEL_COUNT 480 // Do I have an off by one error somewhere here? (Ya, probably)
#define COLORS 4 // RGB (3) or RGBW (4)
#define BufferCount (12)
const int PixelPin = 2; // Pin # is decided by the chosen pixel output method
#define OSCDEBUG (0)
#if OSCDEBUG
#define d(x) Serial.println(x)
#define dn(x) Serial.print(x)
#define df(x, ...) Serial.printf(x, __VA_ARGS__)
#else
#define d(x)
#define dn(x)
#define df(x, ...)
#endif
// You can also use one of these for Esp8266, each having their own restrictions
//
// NOTE: These will ignore the PIN and use GPI03 pin
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(MAX_PIXEL_COUNT, PixelPin);
//NeoPixelBus<NeoRgbFeature, NeoEsp8266Dma400KbpsMethod> strip(MAX_PIXEL_COUNT, PixelPin);
// Uart method is good for the Esp-01 or other pin restricted modules
// NOTE: These will ignore the PIN and use GPI02 pin
#if COLORS == 4
NeoPixelBus<NeoRgbwFeature, NeoEsp8266Uart800KbpsMethod> strip(MAX_PIXEL_COUNT, PixelPin);
#else
NeoPixelBus<NeoRgbFeature, NeoEsp8266Uart800KbpsMethod> strip(MAX_PIXEL_COUNT, PixelPin);
#endif
// four element pixels, RGBW
//NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> strip(MAX_PIXEL_COUNT, PixelPin);
#if COLORS == 4
RgbwColor Buffer[BufferCount][MAX_PIXEL_COUNT];
#else
RgbColor Buffer[BufferCount][MAX_PIXEL_COUNT];
#endif
volatile uint8_t FrontBuffer = 0;
volatile uint8_t BufferedFrames = 0;
uint8_t BackBuffer = 0;
static void frame_timer_interrupt() ICACHE_RAM_ATTR;
int timer_ticks = 0;
int one_ms;
volatile bool ready = false;
WiFiClient client;
WiFiUDP Udp;
MDNSResponder mdns;
#define minsize(x, y) (((x) < (y)) ? (x) : (y))
uint32_t usToTicks(uint32_t us)
{
return (clockCyclesPerMicrosecond() * us);
}
#define LED (1)
void setup()
{
#if OSCDEBUG
Serial.begin(115200);
#else
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#endif
d();
// Reset all the neopixels to an off state
strip.Begin();
strip.Show();
// Connect to WiFi network
d();
d();
dn("Connecting to ");
d(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
dn(".");
}
d("");
d("WiFi connected");
// Print the IP address
d(WiFi.localIP());
// Set up mDNS responder:
if (!mdns.begin(myDNSName, WiFi.localIP())) {
d("Error setting up MDNS responder!");
} else {
d("mDNS responder started");
df("My name is [%s]\r\n", myDNSName);
}
// Start the server listening for incoming client connections
Udp.begin(7890);
d("UDP server listening on port 7890");
// ESP.wdtDisable();
one_ms = usToTicks(1000);
// Setup 30FPS timer
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(frame_timer_interrupt);
timer_ticks = usToTicks(1000000 / 30); // FPS
timer0_write(ESP.getCycleCount() + timer_ticks);
interrupts();
}
#if OSCDEBUG
unsigned long previousFrameTime;
unsigned long TotalFrames = 0;
#endif
// TODO: Just set a flag here
static void frame_timer_interrupt()
{
// Set an interrupt for 1/30s from now
timer0_write(ESP.getCycleCount() + timer_ticks);
// No frame to show, come again soon
if (BufferedFrames == 0) {
return;
}
// We're about to overlap the (active) back buffer
if (FrontBuffer == BackBuffer) {
return;
}
// Display current frame (TODO: use memcpy)
for (int i = 0; i < MAX_PIXEL_COUNT; i++) {
strip.SetPixelColor(i, Buffer[FrontBuffer][i]);
}
strip.Show();
// Advance front buffer
BufferedFrames--;
FrontBuffer++;
if (FrontBuffer >= BufferCount)
FrontBuffer = 0;
#if OSCDEBUG
d(BufferedFrames);
// Show frame-frame time (probably shouldn't print print here)
df("%lu\r\n", micros() - previousFrameTime);
previousFrameTime = micros();
TotalFrames++;
#endif
}
#define MAX_PACKET_LEN (MAX_PIXEL_COUNT * COLORS + 4 + 100)
void loop()
{
static int packetParseState = 0;
static uint8_t pktChannel, pktCommand;
static uint16_t packet_pixel_count;
static uint8_t udp_packet_buffer[MAX_PACKET_LEN];
static uint16_t currentPixel;
uint16_t bytesRead;
int packetSize = Udp.parsePacket();
if (!packetSize) {
return;
}
d(packetSize);
d(" ");
df("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(udp_packet_buffer, MAX_PACKET_LEN);
int current_byte = 0;
// Start of a new packet
if (packetParseState == 0) {
pktChannel = udp_packet_buffer[current_byte++];
pktCommand = udp_packet_buffer[current_byte++];
packet_pixel_count = ((uint16_t)udp_packet_buffer[current_byte++] << 8 | udp_packet_buffer[current_byte++]) / COLORS;
currentPixel = 0;
if ((pktCommand == 0) && (pktChannel <= 1)) {
d("N");
packetParseState = 1;
// Don't allow buffer to overflow; don't drop frames
// int safety = 0;
// while (BufferedFrames >= BufferCount - 1) // (need 1 buffer for the front buffer)
// {
// delay(0);
// if (safety++ > 100000) {
// d("Trig.");
// break;
// }
// }
// Drop frames if we've overflowed
if (BufferedFrames >= BufferCount - 1) { // (need 1 buffer for the front buffer)
return;
}
// Advance back buffer
noInterrupts();
BackBuffer++;
if (BackBuffer >= BufferCount) {
BackBuffer = 0;
}
interrupts();
}
}
// Pixel data
if (packetParseState == 1) {
while (current_byte < len &&
currentPixel < MAX_PIXEL_COUNT &&
currentPixel < packet_pixel_count) {
uint8_t g = udp_packet_buffer[current_byte++]; //GammaLUT[*pixrgbw++];
uint8_t r = udp_packet_buffer[current_byte++];
uint8_t b = udp_packet_buffer[current_byte++];
#if COLORS == 4
uint8_t w = udp_packet_buffer[current_byte++];
Buffer[BackBuffer][currentPixel] = RgbwColor(r, g, b, w);
#else
Buffer[BackBuffer][currentPixel] = RgbColor(r, g, b);
#endif
currentPixel++;
}
if (currentPixel == packet_pixel_count || currentPixel == MAX_PIXEL_COUNT) {
packetParseState = 2;
}
}
// Done getting pixel data
if (packetParseState == 2) {
noInterrupts();
BufferedFrames++;
// Dynamically adjust frame timer
// if(BufferedFrames > (High + Low) * (3/4)) {
// timer_ticks -= one_ms/100; // Overrun, play faster
// } else if(BufferedFrames < (High + Low) * (1/4)) {
// timer_ticks += one_ms/100; // Running low, play slower
// }
interrupts();
packetParseState = 0;
d("D");
}
d('.');
#if OSCDEBUG
dn(TotalFrames);
dn(':');
#endif
dn(timer_ticks);
dn(':');
dn(BufferedFrames);
dn(':');
d(ESP.getFreeHeap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment