-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathExample-02_Shapes.ino
262 lines (210 loc) · 6.76 KB
/
Example-02_Shapes.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Example-01_Hello.ino
This demo shows the various methods available for drawing shapes using the OLED library
This library configures and draws graphics to OLED boards that use the
SSD1306 display hardware. The library only supports I2C.
SparkFun sells these at its website: www.sparkfun.com
Do you like this library? Help support SparkFun. Buy a board!
Micro OLED https://www.sparkfun.com/products/14532
Transparent OLED https://www.sparkfun.com/products/15173
"Narrow" OLED https://www.sparkfun.com/products/17153
Qwiic OLED 1.3in https://www.sparkfun.com/products/23453
Written by Kirk Benell @ SparkFun Electronics, March 2022
Repository:
https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
Documentation:
https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
*/
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_OLED
// The Library supports three different types of SparkFun boards. The demo uses the following
// defines to determine which device is being used. Uncomment the device being used for this demo.
QwiicMicroOLED myOLED;
// QwiicTransparentOLED myOLED;
// QwiicNarrowOLED myOLED;
// Qwiic1in3OLED myOLED;
// Global variables - used to stash our screen size
int width;
int height;
void setup()
{
Serial.begin(115200);
Serial.println("Running OLED example");
Wire.begin();
// Initalize the OLED device and related graphics system
if (myOLED.begin() == false)
{
Serial.println("Device begin failed. Freezing...");
while (true)
;
}
Serial.println("Begin success");
// save device dims for the test routines
width = myOLED.getWidth();
height = myOLED.getHeight();
}
void loop()
{
// Loop over our test functions
for (int i = 0; i < 7; i++)
{
myOLED.erase();
if (i == 0)
lineTest1();
else if (i == 1)
lineTest2();
else if (i == 2)
lineTestVertical();
else if (i == 3)
rectangleTest();
else if (i == 4)
rectangleTestMove();
else if (i == 5)
rectangleFillTest();
else if (i == 6)
circleTest();
myOLED.display();
delay(1000);
}
}
////////////////////////////////////////////////////////////////////////////////
// lineTest1()
//
// This test draws a horizontal line on the screen, moving it from the
// top of the screen to the bottom.
//
// This sequence repeats multiple times, starting with a small line and
// growing to the width of the screen.
//
// Since the library uses a "dirty range" methodology to minimize the amount of
// data sent to the device, as the lenght of the line increases, the rate
// of the display updates also decreases. A longer line, results in more data being
// sent to the display.
void lineTest1(void)
{
int x, y, i;
int mid = width / 2;
int delta = mid / 8;
for (int j = 1; j < 8; j++)
{
x = delta * j;
for (i = 0; i < height * 2; i++)
{
y = i % height;
myOLED.erase();
myOLED.line(mid - x, y, mid + x, y);
myOLED.display();
}
}
}
////////////////////////////////////////////////////////////////////////////////
// lineTest2()
//
// This test draws a series of lines bursting out from the corner of the display.
//
// It demostrates the abilty of the library to draw arbitray straight lines.
void lineTest2(void)
{
for (int i = 0; i < width; i += 6)
{
myOLED.line(0, 0, i, height - 1);
myOLED.display();
}
delay(200);
myOLED.erase();
for (int i = width - 1; i >= 0; i -= 6)
{
myOLED.line(width - 1, 0, i, height - 1);
myOLED.display();
}
}
////////////////////////////////////////////////////////////////////////////////
// lineTestVertIterative()
//
// Draws a series of vertical lines, some horz lines and two diag lines.
//
// Internally, this uses the standard line algorithm (diag), fast horz and fast
// vertical lines functions.
//
// Iterator function - called to animate graphic
void lineTestVerticalIterative(uint8_t y0, uint8_t y1)
{
for (int i = 0; i < width; i += 8)
myOLED.line(i, y0, i, y1);
// end off the vertical lines
myOLED.line(width - 1, y0, width - 1, y1);
// End lines and cross lines
myOLED.line(0, y0, width - 1, y0);
myOLED.line(0, y1, width - 1, y1);
myOLED.line(0, y0, width - 1, y1);
myOLED.line(0, y1, width - 1, y0);
}
// Entry point for test
void lineTestVertical(void)
{
int mid = height / 2;
for (int i = 0; i < height; i += 4)
{
myOLED.erase();
lineTestVerticalIterative(mid - i / 2, mid + i / 2);
myOLED.display();
delay(10);
}
}
////////////////////////////////////////////////////////////////////////////////
// rect_test()
//
// Simple - draw a rectangle test
void rectangleTest(void)
{
myOLED.rectangle(4, 4, width - 8, height - 8);
}
////////////////////////////////////////////////////////////////////////////////
// rectangleTestMove()
//
// Draws a rectangle, then moves it across the screen
//
void rectangleTestMove(void)
{
float steps = height;
float xinc = width / steps;
float yinc = height / steps;
int side = 10;
float x = 0;
float y = 0;
for (int i = 0; i < steps; i++)
{
// Draw the rectangle and send it to device
myOLED.rectangle(x, y, side, side);
myOLED.display(); // sends erased rect and new rect pixels to device
// Erase the that rect, increment and loop
myOLED.rectangle(x, y, side, side, 0);
x += xinc;
y += yinc;
}
}
////////////////////////////////////////////////////////////////////////////////
// rectangleFillTest()
//
// Draws two filled rectangles, switches to XOR draw mode and overwrites them
// with another rectangle
void rectangleFillTest(void)
{
myOLED.rectangleFill(4, 4, width / 2 - 8, height - 8);
myOLED.rectangleFill(width / 2 + 4, 4, width / 2 - 8, height - 8);
myOLED.setDrawMode(grROPXOR); // xor
myOLED.rectangleFill(width / 4, 8, width / 2, height - 16);
myOLED.setDrawMode(grROPCopy); // back to copy op (default)
}
////////////////////////////////////////////////////////////////////////////////
// circleTest()
//
// Draw a series of circles - filled and not filled
void circleTest(void)
{
// Let's draw some circles that fit on the device
myOLED.circle(width / 4, height / 2, height / 3);
myOLED.circleFill(width - width / 4, height / 2, height / 3);
myOLED.circle(4, height / 2, height / 3);
myOLED.circleFill(width - width / 2, height / 2, height / 4);
}