Skip to content

Instantly share code, notes, and snippets.

@AdySan
Last active July 19, 2018 23:54
Show Gist options
  • Save AdySan/02ccd7b11cdb60a29d4e to your computer and use it in GitHub Desktop.
Save AdySan/02ccd7b11cdb60a29d4e to your computer and use it in GitHub Desktop.
ESP8266/Arduino Plasma effect demo using esp8266-oled-ssd1306 library.
#include <Wire.h>
#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;
}
// Initialize the oled display for address 0x3c
SSD1306 display(0x3c, D2, D3);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
display.resetDisplay();
display.displayOn();
display.init();
display.flipScreenVertically();
}
void loop() {
display.clear();
doplasma( display.buffer );
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment