OLED Patterns
In this lesson, we will show how you can display interesting repeating patterns on your OLED screen. Our program will write a pattern into the framebuffer using a simple math equation. The oled.show() will then update the pattern on the display.
This lesson was suggested by Parker Erickson.
Math Functions
We will use a few unusual functions to create repeating patterns:
- Modulo (%)
- Bitwise AND (&)
The modulo function is written %
. It returns the integer remainder after a division. So 7 % 3
is 1 and 7 % 4
is 3.
The Power function of X to the Y power is written in python as pow(x,y)
. For example pow(7, 2) is seven squared = 49.
The bitwise and is written as x & y
1 2 |
|
Function | Returns |
---|---|
13 & 0 | 0 |
13 & 1 | 1 |
13 & 2 | 0 |
13 & 3 | 1 |
13 & 4 | 4 |
13 & 5 | 5 |
13 & 6 | 4 |
13 & 7 | 5 |
13 & 8 | 8 |
13 & 9 | 9 |
13 & 10 | 8 |
13 & 11 | 9 |
13 & 12 | 12 |
Some Sample Equations
- x & y
- x % y
- (x ^ y) % 9
- (x ^ y) % 5
- (x ^ y) % 17
- (x ^ y) % 33
- (x * y) & 64
- (x * y) & 24
- (x * y) & 47
- (x * 2) % y
- (x * 64) % y
- (x * 31) % y
- ((x-128) * 64) % (y-128)
- (x % y) % 4
- (y % x) % 20
- 40 % (x % y)
Note there are other patterns that use the power pow(x,y)
or Exponentiation **
function but I can't get these to work with Micropython.
Sample Code
This program evaluates the function x % (y+1)
for each of the pixels on the screen. If the function returns a non-zero the pixel will be off. If the pixel is zero, the pixel will be on.
draw-patterns-ssd1306-spi.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Adding a List of Patterns
The Eval Function
The eval() function takes any string and passes it to the python interpreter for evaluation within the current context of variables that are in scope. We can use eval to pass an expression that should be evaluated to any function.
1 2 3 4 5 6 7 8 |
|
Output:
1 2 3 4 |
|
The Command Design Pattern
The command pattern holds a list of commands in an array. Each command is executed in the sequence it appears in the list of commands.
In the following program we have the equations in a list. The program steps through each item in the list and displays that equation on the OLED display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Sample Screen Images
X Modulo Y
x % y
(x % y) % 4
Sierpinsky Triangles (x & y)
Sierpinsky Triangles
Bitwise and of x and y