Skip to content

Display Face

We can use the OLED display to put a fun face on our robot. We can do this by using the ellipse() function to draw eyes and a mouth.

We have two functions. One that is a basic face with no parameters. The other version allows you to pass a parameter of the eye gaze direction as a number between -25 (left) to 25 (right).

 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Display face with eye with gaze movement

from machine import Pin
from utime import sleep
import ssd1306
import VL53L0X
from neopixel import NeoPixel

WIDTH = 128
HEIGHT = 64
SCK=Pin(2)
SDL=Pin(3)
# servo pins
RES = machine.Pin(13)
DC = machine.Pin(14)
CS = machine.Pin(15)

spi=machine.SPI(0,baudrate=100000,sck=SCK, mosi=SDL)
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

HALF_HEIGHT = HEIGHT >> 1
QUARTER_HEIGHT = HALF_HEIGHT >> 1
HALF_WIDTH = WIDTH >> 1
QUARTER_WIDTH = HALF_WIDTH >> 1
ONE_THIRD_HEIGHT = int(HEIGHT/3)

# draw readability
ON = 1
OFF = 0
NO_FILL = 0
FILL = 1
phm = 18 # pupal horizontal movement range 
eye_dist_from_top = 21
eyeWidth = 27
eyeHeight = 10
mouth_vpos = 45
mouth_width = 40

def display_face_eye(i):
    oled.fill(0)

    # left eye
    oled.ellipse(32, eye_dist_from_top, eyeWidth, eyeHeight, ON, FILL)
    oled.ellipse(32+i, eye_dist_from_top, 5, 5, OFF, FILL)

    # right eye
    oled.ellipse(94, eye_dist_from_top, eyeWidth, eyeHeight, ON, FILL)
    oled.ellipse(94+i, eye_dist_from_top, 5, 5, OFF, FILL)

    # draw mouth
    # draw bottom half by doing a bitwise and of 8 and 4
    oled.ellipse(HALF_WIDTH, mouth_vpos, mouth_width, 10, ON, NO_FILL, 12)
    oled.show()

while True:
    display_face_eye(0)
    sleep(1)
    # look forward
    for i in range(0, 23):
        display_face_eye(i)
        sleep(0.05)
    # gaze to the right
    for i in range(23, -22, -1):
        display_face_eye(i)
        sleep(0.05)
    # gaze to the left
    for i in range(-22, 0):
        display_face_eye(i)
        sleep(0.05)