1
+ """
2
+ RGB Matrix Ocean Scroller
3
+ Adafruit invests time and resources providing this open source code.
4
+ Please support Adafruit and open source hardware by purchasing
5
+ products from Adafruit!
6
+ Written by Jeff Epler & Limor Fried for Adafruit Industries
7
+ Copyright (c) 2019-2020 Adafruit Industries
8
+ Licensed under the MIT license.
9
+ All text above must be included in any redistribution.
10
+ """
11
+
12
+ import math
13
+ import time
14
+ import random
15
+
16
+ import adafruit_imageload .bmp
17
+ import board
18
+ import displayio
19
+ import framebufferio
20
+ import rgbmatrix
21
+ import ulab
22
+
23
+ displayio .release_displays ()
24
+
25
+ class Reshader :
26
+ '''reshader fades the image to mimic brightness control'''
27
+ def __init__ (self , palette ):
28
+ self .palette = palette
29
+ ulab_palette = ulab .zeros ((len (palette ), 3 ))
30
+ for i in range (len (palette )):
31
+ rgb = int (palette [i ])
32
+ ulab_palette [i , 2 ] = rgb & 0xff
33
+ ulab_palette [i , 1 ] = (rgb >> 8 ) & 0xff
34
+ ulab_palette [i , 0 ] = rgb >> 16
35
+ self .ulab_palette = ulab_palette
36
+
37
+ def reshade (self , brightness ):
38
+ '''reshader'''
39
+ palette = self .palette
40
+ shaded = ulab .array (self .ulab_palette * brightness , dtype = ulab .uint8 )
41
+ for i in range (len (palette )):
42
+ palette [i ] = tuple (shaded [i ])
43
+
44
+ def do_crawl_down (image_file , * ,
45
+ speed = 12 , weave = 4 , pulse = .5 ,
46
+ weave_speed = 1 / 6 , pulse_speed = 1 / 7 ):
47
+ # pylint:disable=too-many-locals
48
+ '''function to scroll the image'''
49
+ the_bitmap , the_palette = adafruit_imageload .load (
50
+ image_file ,
51
+ bitmap = displayio .Bitmap ,
52
+ palette = displayio .Palette )
53
+
54
+ shader = Reshader (the_palette )
55
+
56
+ group = displayio .Group ()
57
+ tile_grid = displayio .TileGrid (bitmap = the_bitmap , pixel_shader = the_palette )
58
+ group .append (tile_grid )
59
+ display .show (group )
60
+
61
+ start_time = time .monotonic_ns ()
62
+ start_y = display .height # High enough to be "off the top"
63
+ end_y = - the_bitmap .height # Low enough to be "off the bottom"
64
+
65
+ # Mix up how the bobs and brightness change on each run
66
+ r1 = random .random () * math .pi
67
+ r2 = random .random () * math .pi
68
+
69
+ y = start_y
70
+ while y > end_y :
71
+ now = time .monotonic_ns ()
72
+ y = start_y - speed * ((now - start_time ) / 1e9 )
73
+ group .y = round (y )
74
+
75
+ # wave from side to side
76
+ group .x = round (weave * math .cos (y * weave_speed + r1 ))
77
+
78
+ # Change the brightness
79
+ if pulse > 0 :
80
+ shader .reshade ((1 - pulse ) + pulse * math .sin (y * pulse_speed + r2 ))
81
+
82
+ display .refresh (minimum_frames_per_second = 0 , target_frames_per_second = 60 )
83
+
84
+ def do_pulse (image_file , * , duration = 4 , pulse_speed = 1 / 8 , pulse = .5 ):
85
+ '''pulse animation'''
86
+ the_bitmap , the_palette = adafruit_imageload .load (
87
+ image_file ,
88
+ bitmap = displayio .Bitmap ,
89
+ palette = displayio .Palette )
90
+
91
+ shader = Reshader (the_palette )
92
+
93
+ group = displayio .Group ()
94
+ tile_grid = displayio .TileGrid (bitmap = the_bitmap , pixel_shader = the_palette )
95
+ group .append (tile_grid )
96
+ group .x = (display .width - the_bitmap .width ) // 2
97
+ group .y = (display .height - the_bitmap .height ) // 2
98
+ display .show (group )
99
+
100
+ start_time = time .monotonic_ns ()
101
+ end_time = start_time + int (duration * 1e9 )
102
+
103
+ now_ns = time .monotonic_ns ()
104
+ while now_ns < end_time :
105
+ now_ns = time .monotonic_ns ()
106
+ current_time = (now_ns - start_time ) / 1e9
107
+
108
+ shader .reshade ((1 - pulse ) - pulse
109
+ * math .cos (2 * math .pi * current_time * pulse_speed )** 2 )
110
+
111
+ display .refresh (minimum_frames_per_second = 0 , target_frames_per_second = 60 )
112
+
113
+ matrix = rgbmatrix .RGBMatrix (
114
+ width = 64 , height = 32 , bit_depth = 5 ,
115
+ rgb_pins = [board .D6 , board .D5 , board .D9 , board .D11 , board .D10 , board .D12 ],
116
+ addr_pins = [board .A5 , board .A4 , board .A3 , board .A2 ],
117
+ clock_pin = board .D13 , latch_pin = board .D0 , output_enable_pin = board .D1 )
118
+ display = framebufferio .FramebufferDisplay (matrix , auto_refresh = False )
119
+
120
+ # Image playlist - set to run randomly. Set pulse from 0 to .5
121
+ while True :
122
+ show_next = random .randint (1 , 5 ) #change to reflect how many images you add
123
+ if show_next == 1 :
124
+ do_crawl_down ("/ray.bmp" )
125
+ elif show_next == 2 :
126
+ do_crawl_down ("/waves1.bmp" , speed = 7 , weave = 0 , pulse = .35 )
127
+ elif show_next == 3 :
128
+ do_crawl_down ("/waves2.bmp" , speed = 9 , weave = 0 , pulse = .35 )
129
+ elif show_next == 4 :
130
+ do_pulse ("/heart.bmp" , duration = 4 , pulse = .45 )
131
+ elif show_next == 5 :
132
+ do_crawl_down ("/dark.bmp" )
0 commit comments