Example 4 - Demo for 128 x 43 pixel OLED Display

examples/ex4_large_oled_demo.py
  1#!/usr/bin/env python
  2#-----------------------------------------------------------------------------
  3# ex4_large_oled_demo.py
  4#
  5# Simple Example for a 128 x 64 pixel 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 a 128 x 64 pixel OLED display.
 40# *Illustrates the same features as exmple 3, but illustrates the difference in speed to display the contents of the entire buffer
 41# 
 42
 43from __future__ import print_function, division
 44import qwiic_oled_base
 45import time
 46import sys
 47import math
 48from random import randint
 49
 50#-------------------------------------------------------------------
 51def pixelExample(myOLED):
 52
 53    print("Pixels!")
 54
 55    lWidth = myOLED.get_lcd_width()
 56    lHeight = myOLED.get_lcd_height()
 57    for i in range(128):
 58        myOLED.pixel(randint(0, lWidth), randint(0, lHeight))
 59        myOLED.display()
 60
 61    myOLED.clear(myOLED.PAGE)
 62#-------------------------------------------------------------------
 63def lineExample(myOLED):
 64
 65    middleX = myOLED.get_lcd_width() // 2
 66    middleY = myOLED.get_lcd_height() // 2
 67
 68    lineWidth = min(middleX, middleY)
 69
 70    print("Lines!")
 71
 72    for i in range(2):
 73
 74        for deg in range(0, 360, 15):
 75
 76            xEnd = lineWidth * math.cos(deg * math.pi / 180.0)
 77            yEnd = lineWidth * math.sin(deg * math.pi / 180.0)
 78
 79            myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd)
 80            myOLED.display()
 81            time.sleep(.05)
 82
 83        for deg in range(0, 360, 15):
 84
 85            xEnd = lineWidth * math.cos(deg * math.pi / 180.0)
 86            yEnd = lineWidth * math.sin(deg * math.pi / 180.0)
 87
 88            myOLED.line(middleX, middleY, middleX + xEnd, middleY + yEnd, myOLED.BLACK, myOLED.NORM)
 89            myOLED.display()
 90            time.sleep(.05)
 91#-------------------------------------------------------------------
 92def shapeExample(myOLED):
 93
 94    print("Shapes!")
 95
 96    # Silly pong demo. It takes a lot of work to fake pong...
 97    paddleW = 3  # Paddle width
 98    paddleH = 15  # Paddle height
 99
100    lWidth = myOLED.get_lcd_width()
101    lHeight = myOLED.get_lcd_height()
102
103    # Paddle 0 (left) position coordinates
104    paddle0_Y = (lHeight // 2) - (paddleH // 2)
105    paddle0_X = 2
106
107    # Paddle 1 (right) position coordinates
108    paddle1_Y = (lHeight // 2) - (paddleH // 2)
109    paddle1_X = lWidth - 3 - paddleW
110
111    ball_rad = 2  #Ball radius
112    # // Ball position coordinates
113    ball_X = paddle0_X + paddleW + ball_rad
114    ball_Y = randint(1 + ball_rad, lHeight - ball_rad) #paddle0_Y + ball_rad
115    ballVelocityX = 1  # Ball left/right velocity
116    ballVelocityY = 1  # Ball up/down velocity
117    paddle0Velocity = -1  # Paddle 0 velocity
118    paddle1Velocity = 1  # Paddle 1 velocity
119
120
121    while (ball_X - ball_rad > 1) and (ball_X + ball_rad < lWidth - 2):
122
123        # // Increment ball's position
124        ball_X += ballVelocityX
125        ball_Y += ballVelocityY
126        # // Check if the ball is colliding with the left paddle
127        if ball_X - ball_rad < paddle0_X + paddleW:
128
129            # // Check if ball is within paddle's height
130            if (ball_Y > paddle0_Y)  and (ball_Y < paddle0_Y + paddleH):
131
132                ball_X +=1  # Move ball over one to the right
133                ballVelocityX = -ballVelocityX # Change velocity
134
135        # Check if the ball hit the right paddle
136        if ball_X + ball_rad > paddle1_X:
137
138            # Check if ball is within paddle's height
139            if (ball_Y > paddle1_Y) and (ball_Y < paddle1_Y + paddleH):
140
141                ball_X -= 1  # Move ball over one to the left
142                ballVelocityX = -ballVelocityX # change velocity
143
144        # // Check if the ball hit the top or bottom
145        if (ball_Y <= ball_rad) or (ball_Y >= (lHeight - ball_rad - 1)):
146
147            # Change up/down velocity direction
148            ballVelocityY = -ballVelocityY
149
150        # // Move the paddles up and down
151        paddle0_Y += paddle0Velocity
152        paddle1_Y += paddle1Velocity
153
154        # // Change paddle 0's direction if it hit top/bottom
155        if (paddle0_Y <= 1) or (paddle0_Y > lHeight - 2 - paddleH):
156
157            paddle0Velocity = -paddle0Velocity
158
159        # // Change paddle 1's direction if it hit top/bottom
160        if (paddle1_Y <= 1) or (paddle1_Y > lHeight - 2 - paddleH):
161
162            paddle1Velocity = -paddle1Velocity
163
164        # Draw the Pong Field
165        myOLED.clear(myOLED.PAGE)  # Clear the page
166
167        # Draw an outline of the screen:
168        myOLED.rect(0, 0, lWidth - 1, lHeight)
169
170        # Draw the center line
171        myOLED.rect_fill(lWidth//2 - 1, 0, 2, lHeight)
172
173        # Draw the Paddles:
174        myOLED.rect_fill(paddle0_X, paddle0_Y, paddleW, paddleH)
175        myOLED.rect_fill(paddle1_X, paddle1_Y, paddleW, paddleH)
176
177        # # Draw the ball:
178        myOLED.circle(ball_X, ball_Y, ball_rad)
179
180        # Actually draw everything on the screen:
181        myOLED.display()
182        time.sleep(.01)  # Delay for visibility
183
184    time.sleep(.2)
185
186#-------------------------------------------------------------------
187def textExamples(myOLED):
188
189    print("Text!")
190
191    # Demonstrate font 0. 5x8 font
192    myOLED.clear(myOLED.PAGE)     # Clear the screen
193    myOLED.set_font_type(0)  # Set font to type 0
194    myOLED.set_cursor(0, 0) # Set cursor to top-left
195    # There are 255 possible characters in the font 0 type.
196    # Lets run through all of them and print them out!
197    for i in range(256):
198
199        # You can write byte values and they'll be mapped to
200        # their ASCII equivalent character.
201        myOLED.write(i)  # Write a byte out as a character
202        myOLED.display() # Draw on the screen
203        # time.sleep(.05)
204
205        # We can only display 60 font 0 characters at a time.
206        # Every 60 characters, pause for a moment. Then clear
207        # the page and start over.
208        if (i%60 == 0) and (i != 0):
209
210            time.sleep(.1)
211            myOLED.clear(myOLED.PAGE)     # Clear the page
212            myOLED.set_cursor(0, 0) # Set cursor to top-left
213
214    time.sleep(.5) # Wait 500ms before next example
215
216    # Demonstrate font 1. 8x16. Let's use the print function
217    # to display every character defined in this font.
218    myOLED.set_font_type(1)  # Set font to type 1
219    myOLED.clear(myOLED.PAGE)     # Clear the page
220    myOLED.set_cursor(0, 0) # Set cursor to top-left
221    # Print can be used to print a string to the screen:
222    myOLED.print(" !\"#$%&'()*+,-./01234")
223    myOLED.display()       # Refresh the display
224    time.sleep(1)
225
226    myOLED.clear(myOLED.PAGE)
227    myOLED.set_cursor(0, 0)
228    myOLED.print("56789:<=>?@ABCDEFGHI")
229    myOLED.display()
230    time.sleep(1)
231
232    myOLED.clear(myOLED.PAGE)
233    myOLED.set_cursor(0, 0)
234    myOLED.print("JKLMNOPQRSTUVWXYZ[\\]^")
235    myOLED.display()
236    time.sleep(1)
237
238    myOLED.clear(myOLED.PAGE)
239    myOLED.set_cursor(0, 0)
240    myOLED.print("_`abcdefghijklmnopqrs")
241    myOLED.display()
242    time.sleep(1)
243
244    myOLED.clear(myOLED.PAGE)
245    myOLED.set_cursor(0, 0)
246    myOLED.print("tuvwxyz{|}~")
247    myOLED.display()
248    time.sleep(1)
249
250    # Demonstrate font 2. 10x16. Only numbers and '.' are defined.
251    # This font looks like 7-segment displays.
252    # Lets use this big-ish font to display readings from the
253    # analog pins.
254    for i in range(25):
255
256        myOLED.clear(myOLED.PAGE)            # Clear the display
257        myOLED.set_cursor(0, 0)        # Set cursor to top-left
258        myOLED.set_font_type(0)         # Smallest font
259        myOLED.print("A0: ")          # Print "A0"
260        myOLED.set_font_type(2)         # 7-segment font
261        myOLED.print("%.3d" % randint(0,255))
262
263        myOLED.set_cursor(0, 16)       # Set cursor to top-middle-left
264        myOLED.set_font_type(0)         # Repeat
265        myOLED.print("A1: ")
266        myOLED.set_font_type(2)
267
268        myOLED.print("%.3d" % randint(0,255))
269        myOLED.set_cursor(0, 32)
270        myOLED.set_font_type(0)
271        myOLED.print("A2: ")
272        myOLED.set_font_type(2)
273        myOLED.print("%.3d" % randint(0,255))
274
275        myOLED.display()
276        time.sleep(.1)
277
278    # Demonstrate font 3. 12x48. Stopwatch demo.
279    myOLED.set_font_type(3)  # Use the biggest font
280    ms = 0
281    s = 0
282
283    while s <= 5:
284
285        myOLED.clear(myOLED.PAGE)     # Clear the display
286        myOLED.set_cursor(0, 0) # Set cursor to top-left
287        if s < 10:
288            myOLED.print("00")   # Print "00" if s is 1 digit
289        elif s < 100:
290            myOLED.print("0")    # Print "0" if s is 2 digits
291
292        myOLED.print(s)        # Print s's value
293        myOLED.print(":")      # Print ":"
294        myOLED.print(ms)       # Print ms value
295        myOLED.display()       # Draw on the screen
296        ms +=1         # Increment ms
297        if ms >= 10 : #If ms is >= 10
298            ms = 0     # Set ms back to 0
299            s +=1        # and increment s
300
301    # Demonstrate font 4. 31x48. Let's use the print function
302    # to display some characters defined in this font.
303    myOLED.set_font_type(4)  # Set font to type 4
304    myOLED.clear(myOLED.PAGE)     #Clear the page
305    myOLED.set_cursor(0, 0) #Set cursor to top-left
306
307    # Print can be used to print a string to the screen:
308    myOLED.print("OL")
309    myOLED.display()       # Refresh the display
310    time.sleep(1)
311
312    myOLED.clear(myOLED.PAGE)
313    myOLED.set_cursor(0, 0)
314    myOLED.print("ED")
315    myOLED.display()
316    time.sleep(1)
317
318    myOLED.set_font_type(1)
319    myOLED.clear(myOLED.PAGE)
320    myOLED.set_cursor(0, 0)
321    myOLED.print("DONE!")
322    myOLED.display()
323    time.sleep(1)
324
325
326#-------------------------------------------------------------------
327
328def runExample():
329
330    #  These three lines of code are all you need to initialize the
331    #  OLED and print the splash screen.
332
333    #  Before you can start using the OLED, call begin() to init
334    #  all of the pins and configure the OLED.
335
336
337    print("\n128 x 64 OLED Display - Everything Example\n")
338    myOLED = qwiic_oled_base.QwiicOledBase(None, 128, 64)
339
340    if not myOLED.connected:
341        print("An OLED device isn't connected to the system. Please check your connection", \
342            file=sys.stderr)
343        return
344
345    myOLED.begin()
346    #  clear(ALL) will clear out the OLED's graphic memory.
347    #  clear(PAGE) will clear the Arduino's display buffer.
348    myOLED.clear(myOLED.ALL)  #  Clear the display's memory (gets rid of artifacts)
349    #  To actually draw anything on the display, you must call the
350    #  display() function.
351    myOLED.display()
352    time.sleep(1)
353
354    myOLED.clear(myOLED.PAGE)
355
356    print("-"*30)
357    pixelExample(myOLED)
358    print("-"*30)
359    lineExample(myOLED)
360    print("-"*30)
361    shapeExample(myOLED)
362    print("-"*30)
363    textExamples(myOLED)
364    print("-"*30)
365    print("DONE")
366
367#-------------------------------------------------------------------
368
369if __name__ == '__main__':
370    try:
371        runExample()
372    except (KeyboardInterrupt, SystemExit) as exErr:
373        print("\nEnding OLED Everything Example")
374        sys.exit(0)