Raspberry Pi Pico Examples
These examples run on the Raspberry Pi Pico with Waveshare displays
rp_2040/waveshare_13/feathers.py
1"""
2feathers.py
3
4 Smoothly scroll mirrored rainbow colored random curves across the display.
5
6"""
7
8import random
9import math
10import utime
11from machine import Pin, SoftSPI
12import st7789py as st7789
13
14
15def between(left, right, along):
16 """returns a point along the curve from left to right"""
17 dist = (1 - math.cos(along * math.pi)) / 2
18 return left * (1 - dist) + right * dist
19
20
21def color_wheel(position):
22 """returns a 565 color from the given position of the color wheel"""
23 position = (255 - position) % 255
24
25 if position < 85:
26 return st7789.color565(255 - position * 3, 0, position * 3)
27
28 if position < 170:
29 position -= 85
30 return st7789.color565(0, position * 3, 255 - position * 3)
31
32 position -= 170
33 return st7789.color565(position * 3, 255 - position * 3, 0)
34
35
36def main():
37 '''
38 The big show!
39 '''
40 #enable display and clear screen
41
42 tft = st7789.ST7789(
43 SoftSPI(baudrate=30000000, polarity=1, sck=Pin(10), mosi=Pin(11), miso=Pin(16)),
44 240,
45 240,
46 reset=Pin(12, Pin.OUT),
47 cs=Pin(9, Pin.OUT),
48 dc=Pin(8, Pin.OUT),
49 backlight=Pin(13, Pin.OUT),
50 rotation=1)
51
52 tft.fill(st7789.BLACK) # clear screen
53
54 height = tft.height # height of display in pixels
55 width = tft.width # width if display in pixels
56
57 tfa = 0 # top free area when scrolling
58 bfa = 80 # bottom free area when scrolling
59
60 scroll = 0 # scroll position
61 wheel = 0 # color wheel position
62
63 tft.vscrdef(tfa, width, bfa) # set scroll area
64 tft.vscsad(scroll + tfa) # set scroll position
65 tft.fill(st7789.BLACK) # clear screen
66
67 half = (height >> 1) - 1 # half the height of the dislay
68 interval = 0 # steps between new points
69 increment = 0 # increment per step
70 counter = 1 # step counter, overflow to start
71 current_y = 0 # current_y value (right point)
72 last_y = 0 # last_y value (left point)
73
74 # segment offsets
75 x_offsets = [x * (width // 8) -1 for x in range(2,9)]
76
77 while True:
78 # when the counter exceeds the interval, save current_y to last_y,
79 # choose a new random value for current_y between 0 and 1/2 the
80 # height of the display, choose a new random interval then reset
81 # the counter to 0
82
83 if counter > interval:
84 last_y = current_y
85 current_y = random.randint(0, half)
86 counter = 0
87 interval = random.randint(10, 100)
88 increment = 1/interval # increment per step
89
90 # clear the first column of the display and scroll it
91 tft.vline(scroll, 0, height, st7789.BLACK)
92 tft.vscsad(scroll + tfa)
93
94 # get the next point between last_y and current_y
95 tween = int(between(last_y, current_y, counter * increment))
96
97 # draw mirrored pixels across the display at the offsets using the color_wheel effect
98 for i, x_offset in enumerate(x_offsets):
99 tft.pixel((scroll + x_offset) % width, half + tween, color_wheel(wheel+(i<<2)))
100 tft.pixel((scroll + x_offset) % width, half - tween, color_wheel(wheel+(i<<2)))
101
102 # increment scroll, counter, and wheel
103 scroll = (scroll + 1) % width
104 wheel = (wheel + 1) % 256
105 counter += 1
106
107main()
rp_2040/waveshare_114/feathers.py
1"""
2feathers.py
3
4 Smoothly scroll mirrored rainbow colored random curves across the display.
5
6"""
7
8import random
9import math
10import utime
11from machine import Pin, SoftSPI
12import st7789py as st7789
13
14
15def between(left, right, along):
16 """returns a point along the curve from left to right"""
17 dist = (1 - math.cos(along * math.pi)) / 2
18 return left * (1 - dist) + right * dist
19
20
21def color_wheel(position):
22 """returns a 565 color from the given position of the color wheel"""
23 position = (255 - position) % 255
24
25 if position < 85:
26 return st7789.color565(255 - position * 3, 0, position * 3)
27
28 if position < 170:
29 position -= 85
30 return st7789.color565(0, position * 3, 255 - position * 3)
31
32 position -= 170
33 return st7789.color565(position * 3, 255 - position * 3, 0)
34
35
36def main():
37 '''
38 The big show!
39 '''
40 #enable display and clear screen
41
42 tft = st7789.ST7789(
43 SoftSPI(baudrate=30000000, polarity=1, sck=Pin(10), mosi=Pin(11), miso=Pin(16)),
44 135,
45 240,
46 reset=Pin(12, Pin.OUT),
47 cs=Pin(9, Pin.OUT),
48 dc=Pin(8, Pin.OUT),
49 backlight=Pin(13, Pin.OUT),
50 rotation=1)
51
52 tft.fill(st7789.BLACK) # clear screen
53
54 height = tft.height # height of display in pixels
55 width = tft.width # width if display in pixels
56
57 tfa = 40 # top free area when scrolling
58 bfa = 40 # bottom free area when scrolling
59
60 scroll = 0 # scroll position
61 wheel = 0 # color wheel position
62
63 tft.vscrdef(tfa, width, bfa) # set scroll area
64 tft.vscsad(scroll + tfa) # set scroll position
65 tft.fill(st7789.BLACK) # clear screen
66
67 half = (height >> 1) - 1 # half the height of the dislay
68 interval = 0 # steps between new points
69 increment = 0 # increment per step
70 counter = 1 # step counter, overflow to start
71 current_y = 0 # current_y value (right point)
72 last_y = 0 # last_y value (left point)
73
74 # segment offsets
75 x_offsets = [x * (width // 8) -1 for x in range(2,9)]
76
77 while True:
78 # when the counter exceeds the interval, save current_y to last_y,
79 # choose a new random value for current_y between 0 and 1/2 the
80 # height of the display, choose a new random interval then reset
81 # the counter to 0
82
83 if counter > interval:
84 last_y = current_y
85 current_y = random.randint(0, half)
86 counter = 0
87 interval = random.randint(10, 100)
88 increment = 1/interval # increment per step
89
90 # clear the first column of the display and scroll it
91 tft.vline(scroll, 0, height, st7789.BLACK)
92 tft.vscsad(scroll + tfa)
93
94 # get the next point between last_y and current_y
95 tween = int(between(last_y, current_y, counter * increment))
96
97 # draw mirrored pixels across the display at the offsets using the color_wheel effect
98 for i, x_offset in enumerate(x_offsets):
99 tft.pixel((scroll + x_offset) % width, half + tween, color_wheel(wheel+(i<<2)))
100 tft.pixel((scroll + x_offset) % width, half - tween, color_wheel(wheel+(i<<2)))
101
102 # increment scroll, counter, and wheel
103 scroll = (scroll + 1) % width
104 wheel = (wheel + 1) % 256
105 counter += 1
106
107main()