Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Last active November 19, 2017 18:58
Show Gist options
  • Save jdriscoll/8882f4b6c1583a94a10cf4fe68446d9b to your computer and use it in GitHub Desktop.
Save jdriscoll/8882f4b6c1583a94a10cf4fe68446d9b to your computer and use it in GitHub Desktop.
Adafruit Feather + OLED Wing demo
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306();
#if defined(ESP8266)
#define BUTTON_A 0
#define BUTTON_B 16
#define BUTTON_C 2
#define LED 0
#elif defined(ESP32)
#define BUTTON_A 15
#define BUTTON_B 32
#define BUTTON_C 14
#define LED 13
#elif defined(ARDUINO_STM32F2_FEATHER)
#define BUTTON_A PA15
#define BUTTON_B PC7
#define BUTTON_C PC5
#define LED PB5
#elif defined(TEENSYDUINO)
#define BUTTON_A 4
#define BUTTON_B 3
#define BUTTON_C 8
#define LED 13
#elif defined(ARDUINO_FEATHER52)
#define BUTTON_A 31
#define BUTTON_B 30
#define BUTTON_C 27
#define LED 17
#else // 32u4, M0, and 328p
#define BUTTON_A 9
#define BUTTON_B 6
#define BUTTON_C 5
#define LED 13
#endif
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define NUM_POINTS 512
#define SPAWN_DELAY 100
#define MAX_VELOCITY 6
struct point_t {
int16_t x;
int16_t y;
float velocity;
};
enum Direction {
down,
left,
right
} dir;
point_t points[NUM_POINTS];
bool point_is_offscreen(point_t point) {
if (point.x < 0 || point.x > SSD1306_LCDWIDTH || point.y < 0 || point.y > SSD1306_LCDHEIGHT) { return true; }
return false;
}
void setup() {
randomSeed(analogRead(2));
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.display();
dir = down;
for (int i = 0; i < NUM_POINTS; i++) {
point_t point;
point.x = -1;
point.y = -1;
point.velocity = 0;
points[i] = point;
}
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
}
void loop() {
if (! digitalRead(BUTTON_A)) dir = down;
if (! digitalRead(BUTTON_B)) dir = left;
if (! digitalRead(BUTTON_C)) dir = right;
display.clearDisplay();
for (int i = 0; i < NUM_POINTS; i++) {
point_t point = points[i];
if (point_is_offscreen(point) && random(SPAWN_DELAY) == 1) {
switch (dir) {
case down:
point.x = random(SSD1306_LCDWIDTH);
point.y = 0;
break;
case left:
point.x = SSD1306_LCDWIDTH;
point.y = random(SSD1306_LCDHEIGHT);
break;
case right:
point.x = 0;
point.y = random(SSD1306_LCDHEIGHT);
break;
}
point.velocity = random(MAX_VELOCITY) + 1;
}
else {
switch (dir) {
case down:
point.y = point.y + point.velocity;
break;
case left:
point.x = point.x - point.velocity;
break;
case right:
point.x = point.x + point.velocity;
break;
}
}
if (!point_is_offscreen(point)) {
display.drawPixel(point.x, point.y, 1);
}
points[i] = point;
}
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment