Skip to content

Commit 9ec808a

Browse files
author
Kirk
committed
port of the multi demo to the new library, cleanup
1 parent c15d086 commit 9ec808a

File tree

3 files changed

+279
-659
lines changed

3 files changed

+279
-659
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
// Example-08_Multi.ino
2+
//
3+
// This is a library written for SparkFun Qwiic OLED boards that use the SSD1306.
4+
//
5+
// SparkFun sells these at its website: www.sparkfun.com
6+
//
7+
// Do you like this library? Help support SparkFun. Buy a board!
8+
//
9+
// Micro OLED https://www.sparkfun.com/products/14532
10+
// Transparent OLED https://www.sparkfun.com/products/15173
11+
// "Narrow" OLED https://www.sparkfun.com/products/17153
12+
//
13+
//
14+
// Updated from example writtin by Paul Clark @ SparkFun Electronics
15+
// Original Creation Date: December 11th, 2020
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 8 for the SparkFun Qwiic OLED Arduino Library
49+
//
50+
// >> Overview <<
51+
//
52+
// This demo performs multiple examples
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+
//////////////////////////////////////////////////////////////////////////////////////////
68+
69+
#include <stdint.h>
70+
71+
// Include the SparkFun qwiic OLED Library
72+
#include <SparkFun_Qwiic_OLED.h>
73+
74+
// What device is being used in this demo
75+
76+
#if defined(TRANSPARENT)
77+
QwiicTransparentOLED myOLED;
78+
const char * deviceName = "Transparent OLED";
79+
80+
#elif defined(NARROW)
81+
QwiicNarrowOLED myOLED;
82+
const char * deviceName = "Narrow OLED";
83+
84+
#else
85+
QwiicMicroOLED myOLED;
86+
const char * deviceName = "Micro OLED";
87+
88+
#endif
89+
90+
91+
int width;
92+
int height;
93+
94+
95+
///////////////////////////////////////////////////////////////////////////////////////////////
96+
// setup()
97+
//
98+
// Standard Arduino setup routine
99+
100+
void setup()
101+
{
102+
103+
delay(500); //Give display time to power on
104+
Serial.begin(115200);
105+
106+
Serial.println("\n\r-----------------------------------");
107+
108+
Serial.print("Running Test #2 on: ");
109+
Serial.println(String(deviceName));
110+
111+
if(!myOLED.begin()){
112+
113+
Serial.println("- Device Begin Failed");
114+
while(1);
115+
}
116+
117+
Serial.println("- Begin Successful");
118+
119+
width = myOLED.getWidth();
120+
height = myOLED.getHeight();
121+
122+
123+
randomSeed(analogRead(A0) + analogRead(A1));
124+
}
125+
126+
///////////////////////////////////////////////////////////////////////////////////////////////
127+
//
128+
void pixelExample()
129+
{
130+
//printTitle("Pixels", 1);
131+
myOLED.erase();
132+
for(int i = 0; i < 512; i++){
133+
myOLED.pixel(random(width), random(height));
134+
myOLED.display();
135+
delay(10);
136+
}
137+
}
138+
///////////////////////////////////////////////////////////////////////////////////////////////
139+
140+
void lineExample()
141+
{
142+
int middleX = width / 2;
143+
int middleY = height/ 2;
144+
int xEnd, yEnd;
145+
int lineWidth = min(middleX, middleY);
146+
147+
//printTitle("Lines!", 1);
148+
myOLED.erase();
149+
int deg;
150+
151+
for (int i = 0; i < 3; i++){
152+
153+
for (deg = 0; deg < 360; deg += 15){
154+
155+
xEnd = lineWidth * cos(deg * PI / 180.0);
156+
yEnd = lineWidth * sin(deg * PI / 180.0);
157+
158+
myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd);
159+
myOLED.display();
160+
delay(10);
161+
}
162+
163+
for(deg = 0; deg < 360; deg += 15){
164+
165+
xEnd = lineWidth * cos(deg * PI / 180.0);
166+
yEnd = lineWidth * sin(deg * PI / 180.0);
167+
168+
myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd, 0);
169+
myOLED.display();
170+
delay(10);
171+
}
172+
}
173+
}
174+
///////////////////////////////////////////////////////////////////////////////////////////////
175+
void shapeExample()
176+
{
177+
//printTitle("Shapes!", 0);
178+
179+
// Silly pong demo. It takes a lot of work to fake pong...
180+
int paddleW = 3; // Paddle width
181+
int paddleH = 15; // Paddle height
182+
183+
// Paddle 0 (left) position coordinates
184+
int paddle0_Y = (height / 2) - (paddleH / 2);
185+
int paddle0_X = 2;
186+
187+
// Paddle 1 (right) position coordinates
188+
int paddle1_Y = ( height / 2) - (paddleH / 2);
189+
int paddle1_X = width - 3 - paddleW;
190+
int ball_rad = 2; // Ball radius
191+
192+
// Ball position coordinates
193+
int ball_X = paddle0_X + paddleW + ball_rad;
194+
int ball_Y = random(1 + ball_rad, height - ball_rad); //paddle0_Y + ball_rad;
195+
int ballVelocityX = 1; // Ball left/right velocity
196+
int ballVelocityY = 1; // Ball up/down velocity
197+
int paddle0Velocity = -1; // Paddle 0 velocity
198+
int paddle1Velocity = 1; // Paddle 1 velocity
199+
200+
//while(ball_X >= paddle0_X + paddleW - 1)
201+
while((ball_X - ball_rad > 1) && (ball_X + ball_rad < width - 2)){
202+
203+
// Increment ball's position
204+
ball_X += ballVelocityX;
205+
ball_Y += ballVelocityY;
206+
207+
// Check if the ball is colliding with the left paddle
208+
if (ball_X - ball_rad < paddle0_X + paddleW){
209+
210+
// Check if ball is within paddle's height
211+
if((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y + paddleH)){
212+
213+
ball_X++; // Move ball over one to the right
214+
ballVelocityX = -ballVelocityX; // Change velocity
215+
}
216+
}
217+
218+
// Check if the ball hit the right paddle
219+
if(ball_X + ball_rad > paddle1_X){
220+
221+
// Check if ball is within paddle's height
222+
if((ball_Y > paddle1_Y) && (ball_Y < paddle1_Y + paddleH)){
223+
224+
ball_X--; // Move ball over one to the left
225+
ballVelocityX = -ballVelocityX; // change velocity
226+
}
227+
}
228+
229+
// Check if the ball hit the top or bottom
230+
if((ball_Y <= ball_rad) || (ball_Y >= (height - ball_rad - 1))){
231+
232+
// Change up/down velocity direction
233+
ballVelocityY = -ballVelocityY;
234+
}
235+
236+
// Move the paddles up and down
237+
paddle0_Y += paddle0Velocity;
238+
paddle1_Y += paddle1Velocity;
239+
240+
// Change paddle 0's direction if it hit top/bottom
241+
if((paddle0_Y <= 1) || (paddle0_Y > height - 2 - paddleH))
242+
paddle0Velocity = -paddle0Velocity;
243+
244+
// Change paddle 1's direction if it hit top/bottom
245+
if((paddle1_Y <= 1) || (paddle1_Y > height - 2 - paddleH))
246+
paddle1Velocity = -paddle1Velocity;
247+
248+
// Draw the Pong Field
249+
myOLED.erase();
250+
251+
// Draw an outline of the screen:
252+
myOLED.rectangle(0, 0, width-1, height-1);
253+
254+
// Draw the center line
255+
myOLED.rectangleFill( width/ 2 - 1, 0, width/2 + 1, height-1);
256+
257+
// Draw the Paddles:
258+
myOLED.rectangleFill(paddle0_X, paddle0_Y, paddle0_X+paddleW, paddle0_Y+paddleH);
259+
myOLED.rectangleFill(paddle1_X, paddle1_Y, paddle1_X+paddleW, paddle1_Y+paddleH);
260+
261+
// Draw the ball:
262+
myOLED.circle(ball_X, ball_Y, ball_rad);
263+
264+
// Actually draw everything on the screen:
265+
myOLED.display();
266+
267+
delay(25); // Delay for visibility
268+
}
269+
delay(1000);
270+
}
271+
///////////////////////////////////////////////////////////////////////////////////////////////
272+
273+
void loop()
274+
{
275+
pixelExample(); // Run the pixel example function
276+
lineExample(); // Then the line example function
277+
shapeExample(); // Then the shape example
278+
}
279+

0 commit comments

Comments
 (0)