1#!/usr/bin/env python
2#-----------------------------------------------------------------------------
3# ex3_demo.py
4#
5# Demo Example for the Qwiic OLED Display
6#------------------------------------------------------------------------
7#
8# Written by SparkFun Electronics, May 2021
9#
10# This python library supports the SparkFun Electroncis qwiic
11# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12# board computers.
13#
14# More information on qwiic is at https:# www.sparkfun.com/qwiic
15#
16# Do you like this library? Help support SparkFun. Buy a board!
17#
18#==================================================================================
19# Copyright (c) 2021 SparkFun Electronics
20#
21# Permission is hereby granted, free of charge, to any person obtaining a copy
22# of this software and associated documentation files (the "Software"), to deal
23# in the Software without restriction, including without limitation the rights
24# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25# copies of the Software, and to permit persons to whom the Software is
26# furnished to do so, subject to the following conditions:
27#
28# The above copyright notice and this permission notice shall be included in all
29# copies or substantial portions of the Software.
30#
31# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37# SOFTWARE.
38#==================================================================================
39# Example 3 - An example showing various features of the display driver on the Qwiic OLED Display.
40#
41
42from __future__ import print_function, division
43import qwiic_oled_display
44import time
45import sys
46import math
47from random import randint
48
49#-------------------------------------------------------------------
50def pixelExample(myOLED):
51
52 print("Pixels!")
53
54 lWidth = myOLED.get_lcd_width()
55 lHeight = myOLED.get_lcd_height()
56 for i in range(128):
57 myOLED.pixel(randint(0, lWidth), randint(0, lHeight))
58 myOLED.display()
59
60 myOLED.clear(myOLED.PAGE)
61#-------------------------------------------------------------------
62def lineExample(myOLED):
63
64 middleX = myOLED.get_lcd_width() // 2
65 middleY = myOLED.get_lcd_height() // 2
66
67 lineWidth = min(middleX, middleY)
68
69 print("Lines!")
70
71 for i in range(2):
72
73 for deg in range(0, 360, 15):
74
75 xEnd = lineWidth * math.cos(deg * math.pi / 180.0)
76 yEnd = lineWidth * math.sin(deg * math.pi / 180.0)
77
78 myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd)
79 myOLED.display()
80 time.sleep(.05)
81
82 for deg in range(0, 360, 15):
83
84 xEnd = lineWidth * math.cos(deg * math.pi / 180.0)
85 yEnd = lineWidth * math.sin(deg * math.pi / 180.0)
86
87 myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd, myOLED.BLACK, myOLED.NORM)
88 myOLED.display()
89 time.sleep(.05)
90#-------------------------------------------------------------------
91def shapeExample(myOLED):
92
93 print("Shapes!")
94
95 # Silly pong demo. It takes a lot of work to fake pong...
96 paddleW = 3 # Paddle width
97 paddleH = 15 # Paddle height
98
99 lWidth = myOLED.get_lcd_width()
100 lHeight = myOLED.get_lcd_height()
101
102 # Paddle 0 (left) position coordinates
103 paddle0_Y = (lHeight // 2) - (paddleH // 2)
104 paddle0_X = 2
105
106 # Paddle 1 (right) position coordinates
107 paddle1_Y = (lHeight // 2) - (paddleH // 2)
108 paddle1_X = lWidth - 3 - paddleW
109
110 ball_rad = 2 #Ball radius
111 # // Ball position coordinates
112 ball_X = paddle0_X + paddleW + ball_rad
113 ball_Y = randint(1 + ball_rad, lHeight - ball_rad) #paddle0_Y + ball_rad
114 ballVelocityX = 1 # Ball left/right velocity
115 ballVelocityY = 1 # Ball up/down velocity
116 paddle0Velocity = -1 # Paddle 0 velocity
117 paddle1Velocity = 1 # Paddle 1 velocity
118
119
120 while (ball_X - ball_rad > 1) and (ball_X + ball_rad < lWidth - 2):
121
122 # // Increment ball's position
123 ball_X += ballVelocityX
124 ball_Y += ballVelocityY
125 # // Check if the ball is colliding with the left paddle
126 if ball_X - ball_rad < paddle0_X + paddleW:
127
128 # // Check if ball is within paddle's height
129 if (ball_Y > paddle0_Y) and (ball_Y < paddle0_Y + paddleH):
130
131 ball_X +=1 # Move ball over one to the right
132 ballVelocityX = -ballVelocityX # Change velocity
133
134 # Check if the ball hit the right paddle
135 if ball_X + ball_rad > paddle1_X:
136
137 # Check if ball is within paddle's height
138 if (ball_Y > paddle1_Y) and (ball_Y < paddle1_Y + paddleH):
139
140 ball_X -= 1 # Move ball over one to the left
141 ballVelocityX = -ballVelocityX # change velocity
142
143 # // Check if the ball hit the top or bottom
144 if (ball_Y <= ball_rad) or (ball_Y >= (lHeight - ball_rad - 1)):
145
146 # Change up/down velocity direction
147 ballVelocityY = -ballVelocityY
148
149 # // Move the paddles up and down
150 paddle0_Y += paddle0Velocity
151 paddle1_Y += paddle1Velocity
152
153 # // Change paddle 0's direction if it hit top/bottom
154 if (paddle0_Y <= 1) or (paddle0_Y > lHeight - 2 - paddleH):
155
156 paddle0Velocity = -paddle0Velocity
157
158 # // Change paddle 1's direction if it hit top/bottom
159 if (paddle1_Y <= 1) or (paddle1_Y > lHeight - 2 - paddleH):
160
161 paddle1Velocity = -paddle1Velocity
162
163 # Draw the Pong Field
164 myOLED.clear(myOLED.PAGE) # Clear the page
165
166 # Draw an outline of the screen:
167 myOLED.rect(0, 0, lWidth - 1, lHeight)
168
169 # Draw the center line
170 myOLED.rect_fill(lWidth//2 - 1, 0, 2, lHeight)
171
172 # Draw the Paddles:
173 myOLED.rect_fill(paddle0_X, paddle0_Y, paddleW, paddleH)
174 myOLED.rect_fill(paddle1_X, paddle1_Y, paddleW, paddleH)
175
176 # # Draw the ball:
177 myOLED.circle(ball_X, ball_Y, ball_rad)
178
179 # Actually draw everything on the screen:
180 myOLED.display()
181 time.sleep(.01) # Delay for visibility
182
183 time.sleep(.2)
184
185#-------------------------------------------------------------------
186def textExamples(myOLED):
187
188 print("Text!")
189
190 # Demonstrate font 0. 5x8 font
191 myOLED.clear(myOLED.PAGE) # Clear the screen
192 myOLED.set_font_type(0) # Set font to type 0
193 myOLED.set_cursor(0, 0) # Set cursor to top-left
194 # There are 255 possible characters in the font 0 type.
195 # Lets run through all of them and print them out!
196 for i in range(256):
197
198 # You can write byte values and they'll be mapped to
199 # their ASCII equivalent character.
200 myOLED.write(i) # Write a byte out as a character
201 myOLED.display() # Draw on the screen
202 # time.sleep(.05)
203
204 # We can only display 60 font 0 characters at a time.
205 # Every 60 characters, pause for a moment. Then clear
206 # the page and start over.
207 if (i%60 == 0) and (i != 0):
208
209 time.sleep(.1)
210 myOLED.clear(myOLED.PAGE) # Clear the page
211 myOLED.set_cursor(0, 0) # Set cursor to top-left
212
213 time.sleep(.5) # Wait 500ms before next example
214
215 # Demonstrate font 1. 8x16. Let's use the print function
216 # to display every character defined in this font.
217 myOLED.set_font_type(1) # Set font to type 1
218 myOLED.clear(myOLED.PAGE) # Clear the page
219 myOLED.set_cursor(0, 0) # Set cursor to top-left
220 # Print can be used to print a string to the screen:
221 myOLED.print(" !\"#$%&'()*+,-./01234")
222 myOLED.display() # Refresh the display
223 time.sleep(1)
224
225 myOLED.clear(myOLED.PAGE)
226 myOLED.set_cursor(0, 0)
227 myOLED.print("56789:<=>?@ABCDEFGHI")
228 myOLED.display()
229 time.sleep(1)
230
231 myOLED.clear(myOLED.PAGE)
232 myOLED.set_cursor(0, 0)
233 myOLED.print("JKLMNOPQRSTUVWXYZ[\\]^")
234 myOLED.display()
235 time.sleep(1)
236
237 myOLED.clear(myOLED.PAGE)
238 myOLED.set_cursor(0, 0)
239 myOLED.print("_`abcdefghijklmnopqrs")
240 myOLED.display()
241 time.sleep(1)
242
243 myOLED.clear(myOLED.PAGE)
244 myOLED.set_cursor(0, 0)
245 myOLED.print("tuvwxyz{|}~")
246 myOLED.display()
247 time.sleep(1)
248
249 # Demonstrate font 2. 10x16. Only numbers and '.' are defined.
250 # This font looks like 7-segment displays.
251 # Lets use this big-ish font to display readings from the
252 # analog pins.
253 for i in range(25):
254
255 myOLED.clear(myOLED.PAGE) # Clear the display
256 myOLED.set_cursor(0, 0) # Set cursor to top-left
257 myOLED.set_font_type(0) # Smallest font
258 myOLED.print("A0: ") # Print "A0"
259 myOLED.set_font_type(2) # 7-segment font
260 myOLED.print("%.3d" % randint(0,255))
261
262 myOLED.set_cursor(0, 16) # Set cursor to top-middle-left
263 myOLED.set_font_type(0) # Repeat
264 myOLED.print("A1: ")
265 myOLED.set_font_type(2)
266
267 myOLED.print("%.3d" % randint(0,255))
268 myOLED.set_cursor(0, 32)
269 myOLED.set_font_type(0)
270 myOLED.print("A2: ")
271 myOLED.set_font_type(2)
272 myOLED.print("%.3d" % randint(0,255))
273
274 myOLED.display()
275 time.sleep(.1)
276
277 # Demonstrate font 3. 12x48. Stopwatch demo.
278 myOLED.set_font_type(3) # Use the biggest font
279 ms = 0
280 s = 0
281
282 while s <= 5:
283
284 myOLED.clear(myOLED.PAGE) # Clear the display
285 myOLED.set_cursor(0, 0) # Set cursor to top-left
286 if s < 10:
287 myOLED.print("00") # Print "00" if s is 1 digit
288 elif s < 100:
289 myOLED.print("0") # Print "0" if s is 2 digits
290
291 myOLED.print(s) # Print s's value
292 myOLED.print(":") # Print ":"
293 myOLED.print(ms) # Print ms value
294 myOLED.display() # Draw on the screen
295 ms +=1 # Increment ms
296 if ms >= 10 : #If ms is >= 10
297 ms = 0 # Set ms back to 0
298 s +=1 # and increment s
299
300 # Demonstrate font 4. 31x48. Let's use the print function
301 # to display some characters defined in this font.
302 myOLED.set_font_type(4) # Set font to type 4
303 myOLED.clear(myOLED.PAGE) #Clear the page
304 myOLED.set_cursor(0, 0) #Set cursor to top-left
305
306 # Print can be used to print a string to the screen:
307 myOLED.print("OL")
308 myOLED.display() # Refresh the display
309 time.sleep(1)
310
311 myOLED.clear(myOLED.PAGE)
312 myOLED.set_cursor(0, 0)
313 myOLED.print("ED")
314 myOLED.display()
315 time.sleep(1)
316
317 myOLED.set_font_type(1)
318 myOLED.clear(myOLED.PAGE)
319 myOLED.set_cursor(0, 0)
320 myOLED.print("DONE!")
321 myOLED.display()
322 time.sleep(1)
323
324
325#-------------------------------------------------------------------
326
327def runExample():
328
329 # These three lines of code are all you need to initialize the
330 # OLED and print the splash screen.
331
332 # Before you can start using the OLED, call begin() to init
333 # all of the pins and configure the OLED.
334
335
336 print("\nSparkFun OLED Display Everything Example\n")
337 myOLED = qwiic_oled_display.QwiicOledDisplay()
338
339 if not myOLED.connected:
340 print("The Qwiic OLED Display isn't connected to the system. Please check your connection", \
341 file=sys.stderr)
342 return
343
344 myOLED.begin()
345 # clear(ALL) will clear out the OLED's graphic memory.
346 # clear(PAGE) will clear the Arduino's display buffer.
347 myOLED.clear(myOLED.ALL) # Clear the display's memory (gets rid of artifacts)
348 # To actually draw anything on the display, you must call the
349 # display() function.
350 myOLED.display()
351 time.sleep(1)
352
353 myOLED.clear(myOLED.PAGE)
354
355 print("-"*30)
356 pixelExample(myOLED)
357 print("-"*30)
358 lineExample(myOLED)
359 print("-"*30)
360 shapeExample(myOLED)
361 print("-"*30)
362 textExamples(myOLED)
363 print("-"*30)
364 print("DONE")
365
366#-------------------------------------------------------------------
367
368if __name__ == '__main__':
369 try:
370 runExample()
371 except (KeyboardInterrupt, SystemExit) as exErr:
372 print("\nEnding OLED Everything Example")
373 sys.exit(0)