Skip to content

Instantly share code, notes, and snippets.

@FWeinb
Forked from AdySan/ESP_Plasma.ino
Last active May 29, 2016 14:58
Show Gist options
  • Save FWeinb/00ba5facb8f9789669e208bd4455f457 to your computer and use it in GitHub Desktop.
Save FWeinb/00ba5facb8f9789669e208bd4455f457 to your computer and use it in GitHub Desktop.
ESP8266/Arduino Plasma effect demo using esp8266-oled-ssd1306 library.
#include <Arduino.h>
// Include the correct display lib
// SPI
// #include <SSD1306Spi.h>
// I2c using brzo_i2c
// #include <SSD1306Brzo.h>
// Standard Wire
#include <SSD1306.h>
#define SET_BIT_HIGH(__mem, __x, __y) \
__mem[x + (y >> 3)*128] |= _BV( y - ((y >> 3) << 3) )
static const uint8_t sinustable[ 0x100 ] = {
0x80, 0x7d, 0x7a, 0x77, 0x74, 0x70, 0x6d, 0x6a,
0x67, 0x64, 0x61, 0x5e, 0x5b, 0x58, 0x55, 0x52,
0x4f, 0x4d, 0x4a, 0x47, 0x44, 0x41, 0x3f, 0x3c,
0x39, 0x37, 0x34, 0x32, 0x2f, 0x2d, 0x2b, 0x28,
0x26, 0x24, 0x22, 0x20, 0x1e, 0x1c, 0x1a, 0x18,
0x16, 0x15, 0x13, 0x11, 0x10, 0x0f, 0x0d, 0x0c,
0x0b, 0x0a, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0a,
0x0b, 0x0c, 0x0d, 0x0f, 0x10, 0x11, 0x13, 0x15,
0x16, 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24,
0x26, 0x28, 0x2b, 0x2d, 0x2f, 0x32, 0x34, 0x37,
0x39, 0x3c, 0x3f, 0x41, 0x44, 0x47, 0x4a, 0x4d,
0x4f, 0x52, 0x55, 0x58, 0x5b, 0x5e, 0x61, 0x64,
0x67, 0x6a, 0x6d, 0x70, 0x74, 0x77, 0x7a, 0x7d,
0x80, 0x83, 0x86, 0x89, 0x8c, 0x90, 0x93, 0x96,
0x99, 0x9c, 0x9f, 0xa2, 0xa5, 0xa8, 0xab, 0xae,
0xb1, 0xb3, 0xb6, 0xb9, 0xbc, 0xbf, 0xc1, 0xc4,
0xc7, 0xc9, 0xcc, 0xce, 0xd1, 0xd3, 0xd5, 0xd8,
0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8,
0xea, 0xeb, 0xed, 0xef, 0xf0, 0xf1, 0xf3, 0xf4,
0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfa, 0xfb, 0xfc,
0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfd,
0xfd, 0xfc, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf6,
0xf5, 0xf4, 0xf3, 0xf1, 0xf0, 0xef, 0xed, 0xeb,
0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0, 0xde, 0xdc,
0xda, 0xd8, 0xd5, 0xd3, 0xd1, 0xce, 0xcc, 0xc9,
0xc7, 0xc4, 0xc1, 0xbf, 0xbc, 0xb9, 0xb6, 0xb3,
0xb1, 0xae, 0xab, 0xa8, 0xa5, 0xa2, 0x9f, 0x9c,
0x99, 0x96, 0x93, 0x90, 0x8c, 0x89, 0x86, 0x83
};
// 8x8 Bayer ordered dithering
// pattern. Each input pixel
// is scaled to the 0..63 range
// before looking in this table
// to determine the action.
static const uint8_t _bayer[8][8] PROGMEM = {
{ 0, 32, 8, 40, 2, 34, 10, 42},
{48, 16, 56, 24, 50, 18, 58, 26},
{12, 44, 4, 36, 14, 46, 6, 38},
{60, 28, 52, 20, 62, 30, 54, 22},
{ 3, 35, 11, 43, 1, 33, 9, 41},
{51, 19, 59, 27, 49, 17, 57, 25},
{15, 47, 7, 39, 13, 45, 5, 37},
{63, 31, 55, 23, 61, 29, 53, 21}
};
uint8_t ditherPoint(uint8_t c, uint8_t x, uint8_t y) {
return (c > pgm_read_byte(&_bayer[ x & 0x07 ][ y & 0x07 ]));
}
static void doplasma( uint8_t * fb )
{
uint8_t z0;
uint8_t z;
static uint8_t c1a,c1b;
static uint8_t c2a,c2b;
static uint8_t c1A,c1B;
static uint8_t c2A,c2B;
static uint8_t x, y;
c1a = c1A;
c1b = c1B;
for ( y = 0; y < 64; ++y ) {
c2a = c2A;
c2b = c2B;
z0 = sinustable[ c1a ] + sinustable[ c1b ];
for ( x = 0; x < 128; ++x ) {
z = z0 + sinustable[ c2a ] + sinustable[ c2b ];
z = z >> 2;
if (ditherPoint(z, x, y))
SET_BIT_HIGH(fb, x, y);
c2a += 1; //3;
c2b += 2; //7;
}
c1a += 1; //4;
c1b += 2; //9;
}
c1A += rand() % 4; //3;
c1B -= 2; //5;
c2A += 3; //2;
c2B -= 1; //3;
}
// D5 -> SCL
// D7 -> SDA
// D0 -> RES
// D2 -> DC
// D8 -> CS (Optional)
// SSD1306Spi display(D0, D2, D8);
// Initialize the oled display for address 0x3c
// D5 -> SDA
// D6 -> SCL
SD1306 display(0x3C, D5, D6);
void setup() {
display.init();
display.setColor(INVERSE);
display.flipScreenVertically();
}
int renderTime = 0;
void loop() {
unsigned long frameStart = millis();
display.clear();
doplasma( display.buffer );
display.drawString(0,0,String(renderTime));
display.display();
renderTime = millis() - frameStart;
}
/*
#include <FS.h>
#include <ArduinoOTA.h>
#include <Time.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>
#include <WebSocketsServer.h>
#include <SSD1306Spi.h>
#include <OLEDDisplayUi.h>
int drops = 0, remainingBudget = 0;
// Global vars
char* webImage = (char*)malloc(1024 * sizeof(char));
// Change as you like
const char *ssid = "Weltall";
const char *password = "09122914344893582212";
const char *otaPassword = "password";
//SSD1306Brzo display (0x3C, D5, D6);
SSD1306Spi display (D0, D2, D8);
OLEDDisplayUi ui ( &display );
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80);
#include "icons.h"
#include "DrawFunctions.h"
#include "LoadingFunctions.h"
void setup() {
memset(webImage, 0, 1024 * sizeof(uint8_t));
Serial.begin(115200);
ui.setTargetFPS(240);
ui.setIndicatorPosition(BOTTOM);
ui.setIndicatorDirection(LEFT_RIGHT);
ui.setFrameAnimation(SLIDE_LEFT);
ui.setFrames(frames, FRAME_COUNT);
ui.setOverlays(overlays, OVERLAY_COUNT);
ui.init();
display.flipScreenVertically();
display.setContrast(255);
ui.runLoadingProcess(loadingStages, LOADING_STAGES_COUNT);
}
void loop() {
unsigned long ms = millis();
int timeBudget = ui.update();
ArduinoOTA.handle();
server.handleClient();
webSocket.loop();
remainingBudget = timeBudget - (millis() - ms);
if (remainingBudget > 0) {
drops = 0;
delay(remainingBudget);
} else {
drops++;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment