Skip to content

Commit e3e772e

Browse files
author
Kirk
committed
finished impl/port for example 2
1 parent 08be44e commit e3e772e

File tree

2 files changed

+159
-74
lines changed

2 files changed

+159
-74
lines changed

examples/Example-01_Hello/Example-01_Hello.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ void setup()
118118
// Do a simple test - fill a rectangle on the screen and then print hello!
119119

120120
// fill a rectangle on the screen that has a 4 pixel board
121-
122121
myOLED.rectangleFill(4, 4, myOLED.getWidth()-4, myOLED.getHeight()-4);
123122

124123
String hello = "hello"; // our message
125-
124+
126125
// Lets center our message on the screen. We need to current font.
127126

128127
QwiicFont * pFont = myOLED.getFont();
Lines changed: 158 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,110 @@
1-
/*
2-
MicroOLED_Hello.ino
3-
SFE_MicroOLED Hello World Demo
4-
Jim Lindblom @ SparkFun Electronics
5-
Original Creation Date: October 26, 2014
6-
7-
This sketch lights up a familiar pattern on the MicroOLED
8-
Breakout. It's a great way to prove you've connected everything
9-
correctly and that your display is in working order.
10-
11-
Hardware Connections:
12-
This example assumes you are using Qwiic. See the SPI examples for
13-
a detailed breakdown of connection info.
14-
15-
Note: The display on the MicroOLED is a 1.8V-3.3V device only.
16-
Don't try to connect a 5V Arduino directly to it! Use level
17-
shifters in between the data signals if you have to resort to
18-
a 5V board.
19-
20-
This code is beerware; if you see me (or any other SparkFun
21-
employee) at the local, and you've found our code helpful,
22-
please buy us a round!
23-
24-
Distributed as-is; no warranty is given.
25-
*/
1+
2+
// Example-02_Shapes.ino
3+
//
4+
// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306.
5+
//
6+
// SparkFun sells these at its website: www.sparkfun.com
7+
//
8+
// Do you like this library? Help support SparkFun. Buy a board!
9+
//
10+
// Micro OLED https://www.sparkfun.com/products/14532
11+
// Transparent OLED https://www.sparkfun.com/products/15173
12+
// "Narrow" OLED https://www.sparkfun.com/products/17153
13+
//
14+
//
15+
// Written by Kirk Benell @ SparkFun Electronics, March 2022
16+
//
17+
// This library configures and draws graphics to OLED boards that use the
18+
// SSD1306 display hardware. The library only supports I2C.
19+
//
20+
// Repository:
21+
// https://github.com/sparkfun/SparkFun_Qwiic_OLED_Arduino_Library
22+
//
23+
// Documentation:
24+
// https://sparkfun.github.io/SparkFun_Qwiic_OLED_Arduino_Library/
25+
//
26+
//
27+
// SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
28+
//
29+
// SPDX-License-Identifier: MIT
30+
//
31+
// The MIT License (MIT)
32+
//
33+
// Copyright (c) 2022 SparkFun Electronics
34+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
35+
// associated documentation files (the "Software"), to deal in the Software without restriction,
36+
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
37+
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to
38+
// do so, subject to the following conditions:
39+
// The above copyright notice and this permission notice shall be included in all copies or substantial
40+
// portions of the Software.
41+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
42+
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
44+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46+
//
47+
//////////////////////////////////////////////////////////////////////////////////////////
48+
// Example 2 for the SparkFun Qwiic OLED Arduino Library
49+
//
50+
// >> Overview <<
51+
//
52+
// This demo shows the various methods available for drawing shapes using the OLED library
53+
//
54+
//////////////////////////////////////////////////////////////////////////////////////////
55+
//
56+
// >>> SELECT THE CONNECTED DEVICE FOR THIS EXAMPLE <<<
57+
//
58+
// The Library supports three different types of SparkFun boards. The demo uses the following
59+
// defines to determine which device is being used. Uncomment the device being used for this demo.
60+
//
61+
// The default is Micro OLED
62+
63+
#define MICRO
64+
//#define NARROW
65+
//#define TRANSPARENT
66+
67+
//////////////////////////////////////////////////////////////////////////////////////////
2668

2769
#include <stdint.h>
2870

2971
// Include the SparkFun qwiic OLED Library
3072
#include <SparkFun_Qwiic_OLED.h>
3173

32-
#define MICRO
33-
//#define NARROW
74+
// What device is being used in this demo
3475

35-
#if defined(MICRO)
36-
QwiicMicroOLED myOLED;
37-
const char * deviceName = "Micro OLED";
76+
#if defined(TRANSPARENT)
77+
QwiicTransparentOLED myOLED;
78+
const char * deviceName = "Transparent OLED";
3879

3980
#elif defined(NARROW)
40-
QwiccNarrowOLED myOLED;
81+
QwiicNarrowOLED myOLED;
4182
const char * deviceName = "Narrow OLED";
4283

4384
#else
44-
QwiicTransparentOLED myOLED;
45-
const char * deviceName = "Transparent OLED";
46-
#endif
47-
85+
QwiicMicroOLED myOLED;
86+
const char * deviceName = "Micro OLED";
4887

49-
//#include "res/qw_fnt_5x7.h"
50-
#include <res/qw_fnt_8x16.h>
51-
//#include "res/qw_fnt_7segment.h"
52-
//#include "res/qw_fnt_31x48.h"
53-
//#include "res/qw_fnt_largenum.h"
88+
#endif
5489

90+
// Global variables - used to stash our screen size
5591

5692
int width;
5793
int height;
5894

95+
////////////////////////////////////////////////////////////////////////////////
96+
// line_test_1()
97+
//
98+
// This test draws a horizontal line on the screen, moving it from the
99+
// top of the screen to the bottom.
100+
//
101+
// This sequence repeats multiple times, starting with a small line and
102+
// growing to the width of the screen.
103+
//
104+
// Since the library uses a "dirty range" methodology to minimize the amount of
105+
// data sent to the device, as the lenght of the line increases, the rate
106+
// of the display updates also decreases. A longer line, results in more data being
107+
// sent to the display.
59108

60109
void line_test_1(void){
61110

@@ -80,6 +129,13 @@ void line_test_1(void){
80129

81130
}
82131

132+
////////////////////////////////////////////////////////////////////////////////
133+
// line_test_2()
134+
//
135+
// This test draws a series of lines bursting out from the corner of the display.
136+
//
137+
// It demostrates the abilty of the library to draw arbitray straight lines.
138+
83139
void line_test_2(void){
84140

85141
for(int i=0; i < width; i +=6){
@@ -94,18 +150,31 @@ void line_test_2(void){
94150
}
95151
}
96152

153+
////////////////////////////////////////////////////////////////////////////////
154+
// line_test_vert()
155+
//
156+
// Draws a series of vertical lines, some horz lines and two diag lines.
157+
//
158+
// Internally, this uses the standard line algorithm (diag), fast horz and fast
159+
// vertical lines functions.
160+
//
161+
// Iterator function - called to animate graphic
97162
void line_test_vert_iter(uint8_t y0, uint8_t y1){
98163

99164
for(int i=0; i < width; i += 8)
100165
myOLED.line( i, y0, i, y1);
101166

167+
// end off the vertical lines
102168
myOLED.line( width-1, y0, width-1, y1);
103169

170+
// End lines and cross lines
104171
myOLED.line(0, y0, width-1, y0);
105172
myOLED.line(0, y1, width-1, y1);
106173
myOLED.line(0, y0, width-1, y1);
107174
myOLED.line(0, y1, width-1, y0);
108175
}
176+
177+
// Entry point for test
109178
void line_test_vert(void){
110179

111180
int mid = height/2;
@@ -119,12 +188,21 @@ void line_test_vert(void){
119188
}
120189
}
121190

191+
////////////////////////////////////////////////////////////////////////////////
192+
// rect_test()
193+
//
194+
// Simple - draw a rectangle test
122195
void rect_test(void){
123196

124197
myOLED.rectangle(4, 4, width-4, height-4);
125198

126199
}
127200

201+
////////////////////////////////////////////////////////////////////////////////
202+
// rect_test_move()
203+
//
204+
// Draws a rectangle, then moves it across the screen
205+
//
128206
void rect_test_move(void){
129207

130208
float steps = height;
@@ -133,13 +211,14 @@ void rect_test_move(void){
133211
int side = 10;
134212
float x = 0;
135213
float y = 0;
214+
136215
for(int i = 0; i < steps; i++){
137216

217+
// Draw the rectangle and send it to device
138218
myOLED.rectangle(x, y, x+side, y+side);
139-
myOLED.display();
140-
//delay(100);
219+
myOLED.display(); // sends erased rect and new rect pixels to device
141220

142-
// Erase the that rect
221+
// Erase the that rect, increment and loop
143222
myOLED.rectangle(x, y, x+side, y+side, 0);
144223

145224
x += xinc;
@@ -148,17 +227,27 @@ void rect_test_move(void){
148227

149228
}
150229

230+
////////////////////////////////////////////////////////////////////////////////
231+
// rect_fill_test()
232+
//
233+
// Draws two filled rectangles, switches to XOR draw mode and overwrites them
234+
// with another rectangle
235+
151236
void rect_fill_test(void){
152237

153238
myOLED.rectangleFill(4, 4, width/2-4, height-4);
154239

155240
myOLED.rectangleFill(width/2+4, 4, width-4, height-4);
156241

157-
myOLED.setDrawMode(grROPXOR);
242+
myOLED.setDrawMode(grROPXOR); // xor
158243
myOLED.rectangleFill(width/4, 8, width - width/4, height-8);
159-
myOLED.setDrawMode(grROPCopy);
244+
myOLED.setDrawMode(grROPCopy); // back to copy op (default)
160245
}
161246

247+
////////////////////////////////////////////////////////////////////////////////
248+
// circle_test()
249+
//
250+
// Draw a series of circles - filled and not filled
162251
void circle_test(void){
163252

164253
myOLED.circle(width/4, height/2, height/3);
@@ -167,21 +256,15 @@ void circle_test(void){
167256

168257
myOLED.circle(4, height/2, height/3);
169258

170-
myOLED.circleFill(width - width/4 , height/2, height/3);
259+
myOLED.circleFill(width - width/2 , height/2, height/4);
171260

172-
myOLED.setDrawMode(grROPXOR);
173-
myOLED.circleFill(width - width/4 - height/4, height/2, height/3); // bug in here
174-
myOLED.setDrawMode(grROPCopy);
175261
}
176262

177-
void text_hello(void){
178-
179-
myOLED.text(10, 10 , "Hello World");
180263

181-
myOLED.setDrawMode(grROPXOR);
182-
myOLED.rectangleFill(8, 8, width/2, height/2);
183-
myOLED.setDrawMode(grROPCopy);
184-
}
264+
// Make an array of function pointers - used to hold the list of tests to run.
265+
//
266+
// Makes the loop call to each test function simple.
267+
//
185268
// testing function table
186269
typedef void (*testFn)(void);
187270

@@ -192,53 +275,56 @@ static const testFn testFunctions[] = {
192275
rect_test,
193276
rect_test_move,
194277
rect_fill_test,
195-
circle_test,
196-
text_hello
278+
circle_test
197279
};
198280

199281
////////////////////////////////////////////////////////////////////////////////////////////////
282+
// setup()
283+
//
284+
// Standard Arduino setup routine
285+
200286
void setup()
201287
{
202288
delay(100); //Give display time to power on
203289
Serial.begin(115200);
204290

205291
Serial.println("\n\r-----------------------------------");
206292

207-
Serial.print("Running Test #1 on: ");
293+
Serial.print("Running Test #2 on: ");
208294
Serial.println(String(deviceName));
209295

210296
if(!myOLED.begin()){
211297

212-
Serial.println("Device Begin Failed");
298+
Serial.println("- Device Begin Failed");
213299
while(1);
214300
}
215301

216-
Serial.println("- Begin Success");
302+
Serial.println("- Begin Successful");
217303

304+
// save device dims for the test routines
218305
width = myOLED.getWidth();
219306
height = myOLED.getHeight();
220-
221-
// myOLED.set_font(QW_FONT_5X7); // works
222-
myOLED.setFont(QW_FONT_8X16); // works
223-
// myOLED.set_font(QW_FONT_31X48); // works
224-
// myOLED.set_font(QW_FONT_LARGENUM); // works
225-
226-
// myOLED.set_font(QW_FONT_7SEGMENT); // works
227-
228307

229308
}
230309

310+
////////////////////////////////////////////////////////////////////////////////////////////////
311+
// loop()
312+
//
313+
// Standard Arduino loop function.
231314

232315
void loop(){
233316

317+
// Just loop over our test functions
234318
for(uint8_t i=0; i < sizeof(testFunctions)/sizeof(testFunctions[0]); i++){
235319

236320
myOLED.erase();
237-
testFunctions[i]();
321+
322+
testFunctions[i](); // next function
323+
238324
myOLED.display();
325+
239326
delay(1000);
240327

241328
}
242-
Serial.println(">>>>>> End Test Loop <<<<<<");
243-
delay(1000);
329+
244330
}

0 commit comments

Comments
 (0)