Skip to content

Instantly share code, notes, and snippets.

@ivarprudnikov
Created February 10, 2015 21:50
Show Gist options
  • Save ivarprudnikov/76d2fbd6b1015720fba8 to your computer and use it in GitHub Desktop.
Save ivarprudnikov/76d2fbd6b1015720fba8 to your computer and use it in GitHub Desktop.
Arduino Uno + Adafruit SSD1306 - etch a sketch
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// If using software SPI (the default case):
#define OLED_MOSI 9 // data
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 lcd(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define BUTTON_LEFT 4
#define BUTTON_RIGHT 5
#define SERIAL_UPDATE_INTERVAL_MILLIS 1000
void setup(void)
{
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
lcd.begin(SSD1306_SWITCHCAPVCC);
// init done
lcd.display();
delay(2000);
lcd.clearDisplay();
testdrawcircle();
lcd.display();
delay(2000);
lcd.clearDisplay();
drawClear();
pinMode(BUTTON_LEFT, INPUT);
digitalWrite(BUTTON_LEFT, HIGH);
pinMode(BUTTON_RIGHT, INPUT);
digitalWrite(BUTTON_RIGHT, HIGH);
}
void drawCursor(int h, int v)
{
// TODO(pim): Overlay a crosshair on the cursor so we know where we are if
// the pen is up.
}
void drawClear(void)
{
for (int y = 0; y < SSD1306_LCDHEIGHT; y++) {
if (y>0) {
for(int x = 0; x < SSD1306_LCDWIDTH; x++) {
lcd.drawPixel(x, y-1, 0);
}
}
lcd.drawFastVLine(0,y,SSD1306_LCDWIDTH-1,y);
lcd.display();
delay(8);
}
lcd.clearDisplay();
}
void testdrawcircle(void) {
for (int16_t i=0; i<lcd.height(); i+=2) {
lcd.drawCircle(lcd.width()/2, lcd.height()/2, i, WHITE);
lcd.display();
}
}
void loop(void)
{
static bool penDown = false;
static int phase = 0;
static unsigned long next_serial_timestamp = 0;
int H = map(analogRead(A0), 0, 1023, 0, SSD1306_LCDWIDTH - 1);
int V = map(analogRead(A1), 0, 1023, 0, SSD1306_LCDHEIGHT - 1);
if (digitalRead(BUTTON_LEFT) == LOW) {
Serial.println("BUTTON_LEFT pushed");
penDown = false;
drawClear();
while (digitalRead(BUTTON_LEFT) == LOW) ;
}
if (digitalRead(BUTTON_RIGHT) == LOW) {
Serial.println("BUTTON_RIGHT pushed");
penDown = !penDown;
while (digitalRead(BUTTON_RIGHT) == LOW) ;
}
if (next_serial_timestamp < millis()) {
Serial.print("Pen is ");
Serial.println(penDown ? "down" : "up");
Serial.print("H=");
Serial.println(H);
Serial.print("V=");
Serial.println(V);
next_serial_timestamp = millis() + SERIAL_UPDATE_INTERVAL_MILLIS;
}
drawCursor(H, V);
if (penDown) {
lcd.drawPixel(H, V, WHITE);
}
lcd.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment