10000 Optimization in display function to speed up drawing, added esp-idf e… · kebot-embedded/esp32-snippets@0acf6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0acf6a3

Browse files
committed
Optimization in display function to speed up drawing, added esp-idf example
1 parent f9a876e commit 0acf6a3

File tree

4 files changed

+371
-1
lines changed

4 files changed

+371
-1
lines changed

hardware/displays/Adafruit_SSD1306-Library/Adafruit_SSD1306.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX {
123123

124124
void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS, bool reset=true);
125125
void ssd1306_command(uint8_t c);
126-
void ssd1306_command(uint8_t *c, uint8_t l);
126+
127127
#ifndef ARDUINO
128128
void print(char*);
129129
void println(char*);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Main component makefile.
3+
#
4+
# This Makefile can be left empty. By default, it will take the sources in the
5+
# src/ directory, compile them and link them into lib(subdirectory_name).a
6+
# in the build directory. This behaviour is entirely configurable,
7+
# please read the ESP-IDF documents if you need to do this.
8+
#
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "esp_event.h"
3+
4+
extern "C"{
5+
void app_main(void);
6+
}
7+
void test_task(void*);
8+
9+
void app_main(void)
10+
{
11+
xTaskCreate(&test_task, "test", 2048, NULL, 5, NULL);
12+
}
13+
Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
/*
2+
* tests.cpp
3+
*
4+
* Created on: Oct 9, 2017
5+
* Author: chegewara
6+
*/
7+
#define enablePartialUpdate
8+
9+
#include "freertos/FreeRTOS.h"
10+
#include "freertos/task.h"
11+
#include "driver/gpio.h"
12+
//#include "math.h"
13+
#include "sdkconfig.h"
14+
15+
#include "Adafruit_SSD1306.h"
16+
#define SCLK_PIN GPIO_NUM_18
17+
#define DIN_PIN GPIO_NUM_23
18+
#define DC_PIN GPIO_NUM_16
19+
#define CS_PIN GPIO_NUM_5
20+
#define RST_PIN GPIO_NUM_14
21+
22+
#ifndef min
23+
#define min(a,b) (((a) < (b)) ? (a) : (b))
24+
#endif
25+
26+
#define NUMFLAKES 10
27+
#define XPOS 0
28+
#define YPOS 1
29+
#define DELTAY 2
30+
#define delay(x) vTaskDelay(x/portTICK_PERIOD_MS)
31+
32+
#define LOGO16_GLCD_HEIGHT 16
33+
#define LOGO16_GLCD_WIDTH 16
34+
35+
static const unsigned char logo16_glcd_bmp[] =
36+
{ 0b00000000, 0b11000000,
37+
0b00000001, 0b11000000,
38+
0b00000001, 0b11000000,
39+
0b00000011, 0b11100000,
40+
0b11110011, 0b11100000,
41+
0b11111110, 0b11111000,
42+
0b01111110, 0b11111111,
43+
0b00110011, 0b10011111,
44+
0b00011111, 0b11111100,
45+
0b00001101, 0b01110000,
46+
0b00011011, 0b10100000,
47+
0b00111111, 0b11100000,
48+
0b00111111, 0b11110000,
49+
0b01111100, 0b11110000,
50+
0b01110000, 0b01110000,
51+
0b00000000, 0b00110000 };
52+
53+
Adafruit_SSD1306 display = Adafruit_SSD1306(DIN_PIN, SCLK_PIN, DC_PIN, RST_PIN, CS_PIN);
54+
55+
void testdrawchar(void) {
56+
display.setTextSize(1);
57+
display.setTextColor(WHITE);
58+
display.setCursor(0,0);
59+
60+
for (uint8_t i=0; i < 168; i++) {
61+
if (i == '\n') continue;
62+
display.write(i);
63+
//if ((i > 0) && (i % 14 == 0))
64+
//display.println();
65+
}
66+
display.display();
67+
}
68+
69+
void testdrawcircle(void) {
70+
for (int16_t i=0; i<display.height(); i+=2) {
71+
display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
72+
display.display();
73+
}
74+
}
75+
76+
void testfillrect(void) {
77+
uint8_t color = 1;
78+
for (int16_t i=0; i<display.height()/2; i+=3) {
79+
// alternate colors
80+
display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
81+
display.display();
82+
color++;
83+
}
84+
}
85+
86+
void testdrawtriangle(void) {
87+
for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
88+
display.drawTriangle(display.width()/2, display.height()/2-i,
89+
display.width()/2-i, display.height()/2+i,
90+
display.width()/2+i, display.height()/2+i, WHITE);
91+
display.display();
92+
}
93+
}
94+
95+
void testfilltriangle(void) {
96+
uint8_t color = BLACK;
97+
for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
98+
display.fillTriangle(display.width()/2, display.height()/2-i,
99+
display.width()/2-i, display.height()/2+i,
100+
display.width()/2+i, display.height()/2+i, color);
101+
if (color == WHITE) color = BLACK;
102+
else color = WHITE;
103+
display.display();
104+
}
105+
}
106+
107+
void testdrawroundrect(void) {
108+
for (int16_t i=0; i<display.height()/2-2; i+=2) {
109+
display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
110+
display.display();
111+
}
112+
}
113+
114+
void testfillroundrect(void) {
115+
uint8_t color = BLACK;
116+
for (int16_t i=0; i<display.height()/2-2; i+=2) {
117+
display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
118+
if (color == WHITE) color = BLACK;
119+
else color = WHITE;
120+
display.display();
121+
}
122+
}
123+
124+
void testdrawrect(void) {
125+
for (int16_t i=0; i<display.height()/2; i+=2) {
126+
display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
127+
display.display();
128+
}
129+
}
130+
131+
void testdrawline() {
132+
for (int16_t i=0; i<display.width(); i+=4) {
133+
display.drawLine(0, 0, i, display.height()-1, WHITE);
134+
display.display();
135+
}
136+
for (int16_t i=0; i<display.height(); i+=4) {
137+
display.drawLine(0, 0, display.width()-1, i, WHITE);
138+
display.display();
139+
}
140+
delay(25);
141+
142+
display.clearDisplay();
143+
for (int16_t i=0; i<display.width(); i+=4) {
144+
display.drawLine(0, display.height()-1, i, 0, WHITE);
145+
display.display();
146+
}
147+
for (int8_t i=display.height()-1; i>=0; i-=4) {
148+
display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
149+
display.display();
150+
}
151+
delay(25);
152+
153+
display.clearDisplay();
154+
for (int16_t i=display.width()-1; i>=0; i-=4) {
155+
display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
156+
display.display();
157+
}
158+
for (int16_t i=display.height()-1; i>=0; i-=4) {
159+
display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
160+
display.display();
161+
}
162+
delay(25);
163+
164+
display.clearDisplay();
165+
for (int16_t i=0; i<display.height(); i+=4) {
166+
display.drawLine(display.width()-1, 0, 0, i, WHITE);
167+
display.display();
168+
}
169+
for (int16_t i=0; i<display.width(); i+=4) {
170+
display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);
171+
display.display();
172+
}
173+
delay(25);
174+
}
175+
176+
void testscrolltext(void) {
177+
display.setTextSize(2);
178+
display.setTextColor(WHITE);
179+
display.setCursor(10,0);
180+
display.clearDisplay();
181+
display.println((char*)"scroll");
182+
display.display();
183+
184+
display.startscrollright(0x00, 0x0F);
185+
delay(2000);
186+
display.stopscroll();
187+
delay(1000);
188+
display.startscrollleft(0x00, 0x0F);
189+
delay(2000);
190+
display.stopscroll();
191+
delay(1000);
192+
display.startscrolldiagright(0x00, 0x07);
193+
delay(2000);
194+
display.startscrolldiagleft(0x00, 0x07);
195+
delay(2000);
196+
display.stopscroll();
197+
}
198+
199+
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
200+
uint8_t icons[NUMFLAKES][3];
201+
202+
// initialize
203+
for (uint8_t f=0; f< NUMFLAKES; f++) {
204+
icons[f][XPOS] = rand()%display.width();
205+
icons[f][YPOS] = 0;
206+
icons[f][DELTAY] = rand()%(5) + 1;
207+
}
208+
209+
while (1) {
210+
// draw each icon
211+
for (uint8_t f=0; f< NUMFLAKES; f++) {
212+
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, WHITE);
213+
}
214+
display.display();
215+
delay(20);
216+
217+
// then erase it + move it
218+
for (uint8_t f=0; f< NUMFLAKES; f++) {
219+
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, BLACK);
220+
// move it
221+
icons[f][YPOS] += icons[f][DELTAY];
222+
// if its gone, reinit
223+
if (icons[f][YPOS] > display.height()) {
224+
icons[f][XPOS] = rand()%(display.width());
225+
icons[f][YPOS] = 0;
226+
icons[f][DELTAY] = rand()%(5) + 1;
227+
}
228+
}
229+
}
230+
}
231+
232+
void test_task(void*) {
233+
while(1){
234+
display.begin();
235+
//display.setContrast(50);
236+
display.display();
237+
238+
delay(2000);
239+
display.clearDisplay(); // clears the screen and buffer
240+
241+
// draw a single pixel
242+
display.drawPixel(10, 10, WHITE);
243+
display.display();
244+
delay(2000);
245+
display.clearDisplay();
246+
247+
// draw many lines
248+
testdrawline();
249+
display.display();
250+
delay(2000);
251+
display.clearDisplay();
252+
253+
// draw rectangles
254+
testdrawrect();
255+
display.display();
256+
delay(2000);
257+
display.clearDisplay();
258+
259+
// draw multiple rectangles
260+
testfillrect();
261+
display.display();
262+
delay(2000);
263+
display.clearDisplay();
264+
265+
// draw mulitple circles
266+
testdrawcircle();
267+
display.display();
268+
delay(2000);
269+
display.clearDisplay();
270+
271+
// draw a circle, 10 pixel radius
272+
display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
273+
display.display();
274+
delay(2000);
275+
display.clearDisplay();
276+
277+
testdrawroundrect();
278+
delay(2000);
279+
display.clearDisplay();
280+
281+
testfillroundrect();
282+
delay(2000);
283+
display.clearDisplay();
284+
285+
testdrawtriangle();
286+
delay(2000);
287+
display.clearDisplay();
288+
289+
testfilltriangle();
290+
delay(2000);
291+
display.clearDisplay();
292+
293+
// draw the first ~12 characters in the font
294+
testdrawchar();
295+
display.display();
296+
delay(2000);
297+
display.clearDisplay();
298+
299+
// draw scrolling text
300+
testscrolltext();
301+
delay(2000);
302+
display.clearDisplay();
303+
304+
// text display tests
305+
display.setTextSize(2);
306+
display.setTextColor(WHITE);
307+
display.setCursor(0,0);
308+
display.println((char*)"Hello, world!");
309+
display.setTextColor(WHITE, BLACK); // 'inverted' text
310+
display.println((char*)"3.141592");
311+
display.setTextSize(1);
312+
display.setTextColor(WHITE);
313+
display.print((char*)"0x");
314+
display.println((char*)"DEADBEEF");
315+
display.display();
316+
delay(2000);
317+
318+
// rotation example
319+
display.clearDisplay();
320+
display.setRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further.
321+
display.setTextSize(1);
322+
display.setTextColor(WHITE);
323+
display.setCursor(0,0);
324+
display.println((char*)"Rotation");
325+
display.setTextSize(1);
326+
display.println((char*)"Example!");
327+
display.display();
328+
delay(2000);
329+
330+
// revert back to no rotation
331+
display.setRotation(0);
332+
333+
// miniature bitmap display
334+
display.clearDisplay();
335+
display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, WHITE);
336+
display.display();
337+
338+
// invert the display
339+
display.invertDisplay(true);
340+
delay(1000);
341+
display.invertDisplay(false);
342+
delay(1000);
343+
344+
// draw a bitmap icon and 'animate' movement
345+
testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT);
346+
}
347+
}
348+
349+

0 commit comments

Comments
 (0)
0