Modular ESP Firmware - OLED - Source

From The TinkerNet Wiki
Jump to navigation Jump to search

platformio.ini

 1 [env]
 2 platform = espressif8266
 3 ; For now, let's stick with the 8266 family MCUs
 4 
 5 framework = arduino
 6 
 7 monitor_speed = 115200
 8 ; I like my serial comms fast.  (Without this line you get the default of 9600)
 9 
10 build_flags =
11             -D DEBUG      ; This is just plain handy.  Use it to turn debugging output on/off...
12 
13 lib_deps =
14         Adafruit GFX Library
15         Adafruit SSD1306
16         Adafruit BusIO
17 
18 [env:esp07]
19 platform = espressif8266
20 board = esp07
21 framework = arduino

A nifty little test program...

main.cpp

 1 #include <Wire.h>
 2 #include <Adafruit_GFX.h>
 3 #include <Adafruit_SSD1306.h>
 4 #include <Fonts/FreeMonoBold12pt7b.h>
 5 #include <Fonts/FreeMonoBold9pt7b.h>
 6 
 7 #define SCREEN_WIDTH 128 // OLED display width, in pixels
 8 #define SCREEN_HEIGHT 64 // OLED display height, in pixels
 9 
10 // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
11 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
12 
13 void setup()
14 {
15   Serial.begin(115200);
16 
17   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
18   { // Address 0x3D for 128x64
19     Serial.println(F("SSD1306 allocation failed"));
20     for (;;)
21       ;
22   }
23   delay(2000);
24   display.clearDisplay();
25 
26   display.dim(true);
27 
28   display.setFont(&FreeMonoBold12pt7b);
29 
30   display.setTextSize(1);
31   display.setTextColor(WHITE);
32   display.setCursor(0, 15);
33   // Display static text
34   display.println("ColdStuff");
35   display.display();
36 
37 }
38 int X = 0;
39 
40 void loop()
41 {
42   display.setFont(&FreeMonoBold9pt7b);
43 
44   display.setTextSize(1);
45 
46   display.setCursor(2, 30);
47   display.println("Beer:   7.3");
48 
49   display.setCursor(2, 50);
50   display.println("Food: -13.1");
51 
52   X++;
53   if (X > 128)
54   {
55     X = 0;
56   }
57   display.drawPixel(X, 63, WHITE);
58   display.drawPixel(X-1, 63, BLACK);
59 delay(20);
60   display.display();
61 }

Bouncy Ball

main.cpp

 1 #include <Wire.h>
 2 #include <Adafruit_GFX.h>
 3 #include <Adafruit_SSD1306.h>
 4 #include <Fonts/FreeMonoBold12pt7b.h>
 5 #include <Fonts/FreeMonoBold9pt7b.h>
 6 
 7 #define SCREEN_WIDTH 128 // OLED display width, in pixels
 8 #define SCREEN_HEIGHT 64 // OLED display height, in pixels
 9 
10 // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
11 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
12 
13 void setup()
14 {
15   Serial.begin(115200);
16 
17   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
18   { // Address 0x3D for 128x64
19     Serial.println(F("SSD1306 allocation failed"));
20     for (;;)
21       ;
22   }
23   delay(2000);
24   display.clearDisplay();
25 
26   // display.dim(true);
27 }
28 
29 int X = 0, Y = 0;
30 int Xold = -1, Yold = -1;
31 int maxX = 127, maxY = 63;
32 int dirX = 1, dirY = 1;
33 
34 void loop()
35 {
36 
37   display.drawPixel(X, Y, WHITE);
38   display.drawPixel(Xold, Yold, BLACK);
39   Xold = X;
40   Yold = Y;
41 
42   X += dirX;
43   if (X == maxX || X == 0)
44     dirX = dirX * -1;
45   Y += dirY;
46   if (Y == maxY || Y == 0)
47     dirY = dirY * -1;
48   display.display();
49   delay(20);
50 }