Add delay to avoid resets

Per discussion in #6, this adds a `delay(0)` call to avoid watchdog timer resets in the ESP8266 when not using the SPI hardware pins.
This commit is contained in:
André David 2018-06-19 23:58:22 +02:00 committed by GitHub
parent 47c83e929a
commit 521daecd91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,321 +1,324 @@
/*************************************************** /***************************************************
This is a example sketch demonstrating the graphics This is a example sketch demonstrating the graphics
capabilities of the SSD1331 library for the 0.96" capabilities of the SSD1331 library for the 0.96"
16-bit Color OLED with SSD1331 driver chip 16-bit Color OLED with SSD1331 driver chip
Pick one up today in the adafruit shop! Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/684 ------> http://www.adafruit.com/products/684
These displays use SPI to communicate, 4 or 5 pins are required to These displays use SPI to communicate, 4 or 5 pins are required to
interface interface
Adafruit invests time and resources providing this open source code, Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing please support Adafruit and open-source hardware by purchasing
products from Adafruit! products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries. Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution BSD license, all text above must be included in any redistribution
****************************************************/ ****************************************************/
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h> #include <Adafruit_SSD1331.h>
#include <SPI.h> #include <SPI.h>
// You can use any (4 or) 5 pins // You can use any (4 or) 5 pins
#define sclk 13 #define sclk 13
#define mosi 11 #define mosi 11
#define cs 10 #define cs 10
#define rst 9 #define rst 9
#define dc 8 #define dc 8
// Color definitions // Color definitions
#define BLACK 0x0000 #define BLACK 0x0000
#define BLUE 0x001F #define BLUE 0x001F
#define RED 0xF800 #define RED 0xF800
#define GREEN 0x07E0 #define GREEN 0x07E0
#define CYAN 0x07FF #define CYAN 0x07FF
#define MAGENTA 0xF81F #define MAGENTA 0xF81F
#define YELLOW 0xFFE0 #define YELLOW 0xFFE0
#define WHITE 0xFFFF #define WHITE 0xFFFF
// Option 1: use any pins but a little slower // Option 1: use any pins but a little slower
//Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst); //Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);
// Option 2: must use the hardware SPI pins // Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be // (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want // an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example) // to use the microSD card (see the image drawing example)
Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, rst); Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, rst);
float p = 3.1415926; float p = 3.1415926;
void setup(void) { void setup(void) {
Serial.begin(9600); Serial.begin(9600);
Serial.print("hello!"); Serial.print("hello!");
display.begin(); display.begin();
Serial.println("init"); Serial.println("init");
uint16_t time = millis(); uint16_t time = millis();
display.fillScreen(BLACK); display.fillScreen(BLACK);
time = millis() - time; time = millis() - time;
Serial.println(time, DEC); Serial.println(time, DEC);
delay(500); delay(500);
lcdTestPattern(); lcdTestPattern();
delay(1000); delay(1000);
display.fillScreen(BLACK); display.fillScreen(BLACK);
display.setCursor(0,0); display.setCursor(0,0);
display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa"); display.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
delay(1000); delay(1000);
// tft print function! // tft print function!
tftPrintTest(); tftPrintTest();
delay(2000); delay(2000);
//a single pixel //a single pixel
display.drawPixel(display.width()/2, display.height()/2, GREEN); display.drawPixel(display.width()/2, display.height()/2, GREEN);
delay(500); delay(500);
// line draw test // line draw test
testlines(YELLOW); testlines(YELLOW);
delay(500); delay(500);
// optimized lines // optimized lines
testfastlines(RED, BLUE); testfastlines(RED, BLUE);
delay(500); delay(500);
testdrawrects(GREEN); testdrawrects(GREEN);
delay(1000); delay(1000);
testfillrects(YELLOW, MAGENTA); testfillrects(YELLOW, MAGENTA);
delay(1000); delay(1000);
display.fillScreen(BLACK); display.fillScreen(BLACK);
testfillcircles(10, BLUE); testfillcircles(10, BLUE);
testdrawcircles(10, WHITE); testdrawcircles(10, WHITE);
delay(1000); delay(1000);
testroundrects(); testroundrects();
delay(500); delay(500);
testtriangles(); testtriangles();
delay(500); delay(500);
Serial.println("done"); Serial.println("done");
delay(1000); delay(1000);
} }
void loop() { void loop() {
} }
void testlines(uint16_t color) { void testlines(uint16_t color) {
display.fillScreen(BLACK); display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) { for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(0, 0, x, display.height()-1, color); display.drawLine(0, 0, x, display.height()-1, color);
} }
for (int16_t y=0; y < display.height()-1; y+=6) { for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(0, 0, display.width()-1, y, color); display.drawLine(0, 0, display.width()-1, y, color);
} }
display.fillScreen(BLACK); display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=6) { for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(display.width()-1, 0, x, display.height()-1, color); display.drawLine(display.width()-1, 0, x, display.height()-1, color);
} }
for (int16_t y=0; y < display.height()-1; y+=6) { for (int16_t y=0; y < display.height()-1; y+=6) {
display.drawLine(display.width()-1, 0, 0, y, color); display.drawLine(display.width()-1, 0, 0, y, color);
} }
display.fillScreen(BLACK); // To avoid ESP8266 watchdog timer resets when not using the hardware SPI pins
for (int16_t x=0; x < display.width()-1; x+=6) { delay(0);
display.drawLine(0, display.height()-1, x, 0, color);
} display.fillScreen(BLACK);
for (int16_t y=0; y < display.height()-1; y+=6) { for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(0, display.height()-1, display.width()-1, y, color); display.drawLine(0, display.height()-1, x, 0, color);
} }
for (int16_t y=0; y < display.height()-1; y+=6) {
display.fillScreen(BLACK); display.drawLine(0, display.height()-1, display.width()-1, y, color);
for (int16_t x=0; x < display.width()-1; x+=6) { }
display.drawLine(display.width()-1, display.height()-1, x, 0, color);
} display.fillScreen(BLACK);
for (int16_t y=0; y < display.height()-1; y+=6) { for (int16_t x=0; x < display.width()-1; x+=6) {
display.drawLine(display.width()-1, display.height()-1, 0, y, color); display.drawLine(display.width()-1, display.height()-1, x, 0, color);
} }
for (int16_t y=0; y < display.height()-1; y+=6) {
} display.drawLine(display.width()-1, display.height()-1, 0, y, color);
}
void testdrawtext(char *text, uint16_t color) {
display.setTextSize(1); }
display.setTextColor(WHITE);
display.setCursor(0,0); void testdrawtext(char *text, uint16_t color) {
display.setTextSize(1);
for (uint8_t i=0; i < 168; i++) { display.setTextColor(WHITE);
if (i == '\n') continue; display.setCursor(0,0);
display.write(i);
if ((i > 0) && (i % 21 == 0)) for (uint8_t i=0; i < 168; i++) {
display.println(); if (i == '\n') continue;
} display.write(i);
} if ((i > 0) && (i % 21 == 0))
display.println();
void testfastlines(uint16_t color1, uint16_t color2) { }
display.fillScreen(BLACK); }
for (int16_t y=0; y < display.height()-1; y+=5) {
display.drawFastHLine(0, y, display.width()-1, color1); void testfastlines(uint16_t color1, uint16_t color2) {
} display.fillScreen(BLACK);
for (int16_t x=0; x < display.width()-1; x+=5) { for (int16_t y=0; y < display.height()-1; y+=5) {
display.drawFastVLine(x, 0, display.height()-1, color2); display.drawFastHLine(0, y, display.width()-1, color1);
} }
} for (int16_t x=0; x < display.width()-1; x+=5) {
display.drawFastVLine(x, 0, display.height()-1, color2);
void testdrawrects(uint16_t color) { }
display.fillScreen(BLACK); }
for (int16_t x=0; x < display.height()-1; x+=6) {
display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color); void testdrawrects(uint16_t color) {
} display.fillScreen(BLACK);
} for (int16_t x=0; x < display.height()-1; x+=6) {
display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color);
void testfillrects(uint16_t color1, uint16_t color2) { }
display.fillScreen(BLACK); }
for (int16_t x=display.height()-1; x > 6; x-=6) {
display.fillRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color1); void testfillrects(uint16_t color1, uint16_t color2) {
display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color2); display.fillScreen(BLACK);
} for (int16_t x=display.height()-1; x > 6; x-=6) {
} display.fillRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color1);
display.drawRect((display.width()-1)/2 -x/2, (display.height()-1)/2 -x/2 , x, x, color2);
void testfillcircles(uint8_t radius, uint16_t color) { }
for (uint8_t x=radius; x < display.width()-1; x+=radius*2) { }
for (uint8_t y=radius; y < display.height()-1; y+=radius*2) {
display.fillCircle(x, y, radius, color); void testfillcircles(uint8_t radius, uint16_t color) {
} for (uint8_t x=radius; x < display.width()-1; x+=radius*2) {
} for (uint8_t y=radius; y < display.height()-1; y+=radius*2) {
} display.fillCircle(x, y, radius, color);
}
void testdrawcircles(uint8_t radius, uint16_t color) { }
for (int16_t x=0; x < display.width()-1+radius; x+=radius*2) { }
for (int16_t y=0; y < display.height()-1+radius; y+=radius*2) {
display.drawCircle(x, y, radius, color); void testdrawcircles(uint8_t radius, uint16_t color) {
} for (int16_t x=0; x < display.width()-1+radius; x+=radius*2) {
} for (int16_t y=0; y < display.height()-1+radius; y+=radius*2) {
} display.drawCircle(x, y, radius, color);
}
void testtriangles() { }
display.fillScreen(BLACK); }
int color = 0xF800;
int t; void testtriangles() {
int w = display.width()/2; display.fillScreen(BLACK);
int x = display.height(); int color = 0xF800;
int y = 0; int t;
int z = display.width(); int w = display.width()/2;
for(t = 0 ; t <= 15; t+=1) { int x = display.height();
display.drawTriangle(w, y, y, x, z, x, color); int y = 0;
x-=4; int z = display.width();
y+=4; for(t = 0 ; t <= 15; t+=1) {
z-=4; display.drawTriangle(w, y, y, x, z, x, color);
color+=100; x-=4;
} y+=4;
} z-=4;
color+=100;
void testroundrects() { }
display.fillScreen(BLACK); }
int color = 100;
int i; void testroundrects() {
int t; display.fillScreen(BLACK);
for(t = 0 ; t <= 4; t+=1) { int color = 100;
int x = 0; int i;
int y = 0; int t;
int w = display.width(); for(t = 0 ; t <= 4; t+=1) {
int h = display.height(); int x = 0;
for(i = 0 ; i <= 8; i+=1) { int y = 0;
display.drawRoundRect(x, y, w, h, 5, color); int w = display.width();
x+=2; int h = display.height();
y+=3; for(i = 0 ; i <= 8; i+=1) {
w-=4; display.drawRoundRect(x, y, w, h, 5, color);
h-=6; x+=2;
color+=1100; y+=3;
} w-=4;
color+=100; h-=6;
} color+=1100;
} }
color+=100;
void tftPrintTest() { }
display.fillScreen(BLACK); }
display.setCursor(0, 5);
display.setTextColor(RED); void tftPrintTest() {
display.setTextSize(1); display.fillScreen(BLACK);
display.println("Hello World!"); display.setCursor(0, 5);
display.setTextColor(YELLOW, GREEN); display.setTextColor(RED);
display.setTextSize(2); display.setTextSize(1);
display.print("Hello Wo"); display.println("Hello World!");
display.setTextColor(BLUE); display.setTextColor(YELLOW, GREEN);
display.setTextSize(3); display.setTextSize(2);
display.print(1234.567); display.print("Hello Wo");
delay(1500); display.setTextColor(BLUE);
display.setCursor(0, 5); display.setTextSize(3);
display.fillScreen(BLACK); display.print(1234.567);
display.setTextColor(WHITE); delay(1500);
display.setTextSize(0); display.setCursor(0, 5);
display.println("Hello World!"); display.fillScreen(BLACK);
display.setTextSize(1); display.setTextColor(WHITE);
display.setTextColor(GREEN); display.setTextSize(0);
display.print(p, 5); display.println("Hello World!");
display.println(" Want pi?"); display.setTextSize(1);
display.print(8675309, HEX); // print 8,675,309 out in HEX! display.setTextColor(GREEN);
display.print(" Print HEX"); display.print(p, 5);
display.setTextColor(WHITE); display.println(" Want pi?");
display.println("Sketch has been"); display.print(8675309, HEX); // print 8,675,309 out in HEX!
display.println("running for: "); display.print(" Print HEX");
display.setTextColor(MAGENTA); display.setTextColor(WHITE);
display.print(millis() / 1000); display.println("Sketch has been");
display.setTextColor(WHITE); display.println("running for: ");
display.print(" seconds."); display.setTextColor(MAGENTA);
} display.print(millis() / 1000);
display.setTextColor(WHITE);
void mediabuttons() { display.print(" seconds.");
// play }
display.fillScreen(BLACK);
display.fillRoundRect(25, 10, 78, 60, 8, WHITE); void mediabuttons() {
display.fillTriangle(42, 20, 42, 60, 90, 40, RED); // play
delay(500); display.fillScreen(BLACK);
// pause display.fillRoundRect(25, 10, 78, 60, 8, WHITE);
display.fillRoundRect(25, 90, 78, 60, 8, WHITE); display.fillTriangle(42, 20, 42, 60, 90, 40, RED);
display.fillRoundRect(39, 98, 20, 45, 5, GREEN); delay(500);
display.fillRoundRect(69, 98, 20, 45, 5, GREEN); // pause
delay(500); display.fillRoundRect(25, 90, 78, 60, 8, WHITE);
// play color display.fillRoundRect(39, 98, 20, 45, 5, GREEN);
display.fillTriangle(42, 20, 42, 60, 90, 40, BLUE); display.fillRoundRect(69, 98, 20, 45, 5, GREEN);
delay(50); delay(500);
// pause color // play color
display.fillRoundRect(39, 98, 20, 45, 5, RED); display.fillTriangle(42, 20, 42, 60, 90, 40, BLUE);
display.fillRoundRect(69, 98, 20, 45, 5, RED); delay(50);
// play color // pause color
display.fillTriangle(42, 20, 42, 60, 90, 40, GREEN); display.fillRoundRect(39, 98, 20, 45, 5, RED);
} display.fillRoundRect(69, 98, 20, 45, 5, RED);
// play color
/**************************************************************************/ display.fillTriangle(42, 20, 42, 60, 90, 40, GREEN);
/*! }
@brief Renders a simple test pattern on the LCD
*/ /**************************************************************************/
/**************************************************************************/ /*!
void lcdTestPattern(void) @brief Renders a simple test pattern on the LCD
{ */
uint8_t w,h; /**************************************************************************/
display.setAddrWindow(0, 0, 96, 64); void lcdTestPattern(void)
{
for(h=0; h<64; h++) uint8_t w,h;
{ display.setAddrWindow(0, 0, 96, 64);
for(w=0; w<96; w++)
{ for(h=0; h<64; h++)
if(w>83){display.writePixel(WHITE);} {
else if(w>71){display.writePixel(BLUE);} for(w=0; w<96; w++)
else if(w>59){display.writePixel(GREEN);} {
else if(w>47){display.writePixel(CYAN);} if(w>83){display.writePixel(WHITE);}
else if(w>35){display.writePixel(RED);} else if(w>71){display.writePixel(BLUE);}
else if(w>23){display.writePixel(MAGENTA);} else if(w>59){display.writePixel(GREEN);}
else if(w>11){display.writePixel(YELLOW);} else if(w>47){display.writePixel(CYAN);}
else {display.writePixel(BLACK);} else if(w>35){display.writePixel(RED);}
} else if(w>23){display.writePixel(MAGENTA);}
} else if(w>11){display.writePixel(YELLOW);}
display.endWrite(); else {display.writePixel(BLACK);}
}
}
display.endWrite();
} }