|
| 1 | +#! /usr/bin/python |
| 2 | +#Sierpenski Triangle Example for ssd1306 OLED board (and Omega2 of course!) |
| 3 | +#by Casten Riepling |
| 4 | +#Thanks to greenbreakfast for the nice i2c-exp-driver library + examples! |
| 5 | + |
| 6 | +from OmegaExpansion import oledExp as oled |
| 7 | +import random |
| 8 | +import time |
| 9 | + |
| 10 | +#since we can only write a byte at a time, we need to keep |
| 11 | +#the previous state in memory to OR it with |
| 12 | +state = [[0 for x in range(16)] for y in range(128)] |
| 13 | + |
| 14 | + |
| 15 | +#init all the 1306 driver stuff |
| 16 | +oled.driverInit() |
| 17 | +oled.setImageColumns() |
| 18 | +oled.setDisplayMode(0) |
| 19 | + |
| 20 | +#init our positional state and boundary vertices |
| 21 | +currX = currY = 0 |
| 22 | +vertices = [{'x':0,'y':0}, {'x':63,'y':63}, {'x':0,'y':127}] |
| 23 | + |
| 24 | +#To make it a little more fun, we'll use a diminishing wait so one can |
| 25 | +#get an idea of the algorithm at work |
| 26 | +sleepTime = 1.0 |
| 27 | + |
| 28 | +#The algorithm works as follows. Start with 3 vertices of a triangle and begin at |
| 29 | +#one of them. Randomly choose a vertex. Go half way to that vertex and draw a point. |
| 30 | +#Repeat until you are happy. |
| 31 | + |
| 32 | +while True: |
| 33 | + #pick a vertex |
| 34 | + i = random.randint(0,2) |
| 35 | + vert = vertices[i] |
| 36 | + |
| 37 | + #find the point halfway between |
| 38 | + currX = int((vert['x']+currX) / 2) |
| 39 | + currY = int((vert['y']+currY) / 2) |
| 40 | + |
| 41 | + #convert to columns and bit patterns |
| 42 | + col = int(currX/8) |
| 43 | + bitX = 1<<(currX%8) |
| 44 | + |
| 45 | + #OR the info into the existing data |
| 46 | + state[currY][col] |= int(state[currY][col] | bitX) |
| 47 | + |
| 48 | + #write the data to the display |
| 49 | + oled.setCursorByPixel(col,currY) |
| 50 | + oled.writeByte(state[currY][col]) |
| 51 | + |
| 52 | + #and do some sleeping for dramatic effect |
| 53 | + time.sleep(sleepTime) |
| 54 | + sleepTime = sleepTime - (sleepTime/20) |
0 commit comments