├── LICENSE ├── README.md ├── ect.py └── example.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ethan Tamasar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-sense-hat-animations 2 | Raspberry Pi Sense Hat animation library 3 | 4 | Functions for animating; drawing lines, circles, triangles, and squares 5 | 6 | 7 | 8 | # Animate a ball moving on the screen 9 | 10 | 11 | ```python 12 | for x in range(0,7): 13 | ect.cell(image,[0,x],[randint(0,255), randint(0,255), randint(0,255)],0.1) 14 | ect.cell(image,[0,x],e,0.1) 15 | 16 | for x in range(7,0, -1): 17 | ect.cell(image,[0,x],[randint(0,255), randint(0,255), randint(0,255)],0.1) 18 | ect.cell(image,[0,x],e,0.1) 19 | ``` 20 | 21 | 22 | # Colorful squares 23 | ```python 24 | for x in range(5): 25 | ect.square(image, [0,0], [7,0], [7,7],[0,7],[randint(0,255),randint(0,255),randint(0,255)],.01) 26 | ect.square(image, [1,1], [6,1], [6,6],[1,6],[randint(0,255),randint(0,255),randint(0,255)],.01) 27 | ect.square(image, [2,2], [5,2], [5,5],[2,5],[randint(0,255),randint(0,255),randint(0,255)],.01) 28 | ``` 29 | 30 | # Checker board 31 | ```python 32 | for y in range(0,7): 33 | if (y%2 == 0): 34 | ect.line(image,[0,y], [7,y], b, 1) 35 | 36 | 37 | for x in range(0,7): 38 | if (x%2 == 0): 39 | ect.line(image,[x,0], [x,7], b, 1) 40 | 41 | ``` 42 | 43 | 44 | 45 | 46 | 47 | 48 | # It's easy and there's much more! 49 | -------------------------------------------------------------------------------- /ect.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Sense HAT graphic animations: circle, triangle, line, and square functions. 3 | 4 | By Ethan Tamasar, 5/15/2017 5 | ''' 6 | 7 | from sense_hat import SenseHat 8 | import time 9 | import numpy as np 10 | import time 11 | from random import randint 12 | 13 | 14 | def circle(image, position, radius, color, timer): 15 | 16 | sense = SenseHat() 17 | 18 | width, height = 8, 8 19 | a, b = position[0], position[1] 20 | r = radius 21 | EPSILON = 1.2 22 | 23 | image2 = image.reshape(8,8,3) 24 | 25 | # draw the circle 26 | for y in range(height): 27 | for x in range(width): 28 | if abs((x-a)**2 + (y-b)**2 - r**2) < EPSILON**2: 29 | image2[y][x] = color 30 | 31 | image3 = image2.reshape(64,3) 32 | sense.set_pixels(image3) 33 | time.sleep(timer) 34 | 35 | return image3 36 | 37 | 38 | def cell(image, position, color, timer): 39 | 40 | sense = SenseHat() 41 | 42 | image2 = image.reshape(8,8,3) 43 | image2[position[0],position[1]] = color 44 | image3 = image2.reshape(64,3) 45 | 46 | sense.set_pixels(image3) 47 | 48 | time.sleep(timer) 49 | 50 | 51 | def line(image, point1, point2, color, timer): 52 | 53 | sense = SenseHat() 54 | image2 = image.reshape(8,8,3) 55 | 56 | x1 = point1[0] 57 | y1 = point1[1] 58 | x2 = point2[0] 59 | y2 = point2[1] 60 | dx = (x2 - x1) 61 | dy = (y2 - y1) 62 | 63 | if abs(dx) > abs(dy) : 64 | steps = abs(dx) 65 | else : 66 | steps = abs(dy) 67 | 68 | Xincrement = dx / steps 69 | Yincrement = dy / steps 70 | x = x1 71 | y = y1 72 | 73 | for v in range(steps + 1): 74 | image2[y,x] = color 75 | x = x + Xincrement; 76 | y = y + Yincrement; 77 | 78 | image3 = image2.reshape(64,3) 79 | sense.set_pixels(image3) 80 | time.sleep(timer) 81 | return image3 82 | 83 | 84 | 85 | def triangle(image, point1, point2, point3, color, timer): 86 | 87 | sense = SenseHat() 88 | image2 = image.reshape(8,8,3) 89 | 90 | line(image2, point2, point1, color, timer) 91 | line(image2, point3, point2, color, timer) 92 | line(image2, point1, point3, color, timer) 93 | 94 | image3 = image2.reshape(64,3) 95 | sense.set_pixels(image3) 96 | time.sleep(timer) 97 | 98 | return image3 99 | 100 | 101 | 102 | def square(image, point1, point2, point3, point4, color, timer): 103 | 104 | sense = SenseHat() 105 | image2 = image.reshape(8,8,3) 106 | 107 | line(image2, point1, point2, color, 0) 108 | line(image2, point2, point3, color, 0) 109 | line(image2, point3, point4, color, 0) 110 | line(image2, point4, point1, color, 0) 111 | 112 | image3 = image2.reshape(64,3) 113 | sense.set_pixels(image3) 114 | time.sleep(timer) 115 | 116 | return image3 117 | 118 | 119 | 120 | def clear(image): 121 | 122 | sense = SenseHat() 123 | 124 | e = [0, 0, 0] 125 | 126 | image2 = np.array([ 127 | e,e,e,e,e,e,e,e, 128 | e,e,e,e,e,e,e,e, 129 | e,e,e,e,e,e,e,e, 130 | e,e,e,e,e,e,e,e, 131 | e,e,e,e,e,e,e,e, 132 | e,e,e,e,e,e,e,e, 133 | e,e,e,e,e,e,e,e, 134 | e,e,e,e,e,e,e,e 135 | ]) 136 | 137 | image = image2 138 | 139 | sense.set_pixels(image) 140 | 141 | return image 142 | 143 | 144 | 145 | 146 | def blinking_circle(image,position): 147 | 148 | sense = SenseHat() 149 | 150 | for x in range(0, 10): 151 | r = randint(0,255) 152 | g = randint(0,255) 153 | b = randint(0,255) 154 | image1 = circle(image,(position[0],position[1]), 3, [r, g, b], 0.1) 155 | image2 = circle(image1,(position[0],position[1]), 2, [r, g, b], 0.1) 156 | image3 = circle(image2,(position[0],position[1]), 1, [r, g, b], 0.1) 157 | 158 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Examples using Sense HAT animations: circle, triangle, line, and square functions. 3 | 4 | By Ethan Tamasar, 5/15/2017 5 | ''' 6 | 7 | 8 | 9 | from sense_hat import SenseHat 10 | import time 11 | import numpy as np 12 | import time 13 | import ect 14 | from random import randint 15 | import sys 16 | 17 | sense = SenseHat() 18 | 19 | w = [150, 150, 150] 20 | b = [0, 0, 255] 21 | e = [0, 0, 0] 22 | 23 | 24 | # create empty screen 25 | image = np.array([ 26 | e,e,e,e,e,e,e,e, 27 | e,e,e,e,e,e,e,e, 28 | e,e,e,e,e,e,e,e, 29 | e,e,e,e,e,e,e,e, 30 | e,e,e,e,e,e,e,e, 31 | e,e,e,e,e,e,e,e, 32 | e,e,e,e,e,e,e,e, 33 | e,e,e,e,e,e,e,e 34 | ]) 35 | 36 | 37 | 38 | ''' 39 | Demonstrate a bouncing ball 40 | It will draw the ball, erase it, and then draw another ball in a different position 41 | ''' 42 | 43 | 44 | for x in range(0,7): 45 | ect.cell(image,[0,x],[randint(0,255), randint(0,255), randint(0,255)],0.1) 46 | ect.cell(image,[0,x],e,0.1) 47 | for x in range(7,0, -1): 48 | ect.cell(image,[0,x],[randint(0,255), randint(0,255), randint(0,255)],0.1) 49 | ect.cell(image,[0,x],e,0.1) 50 | 51 | 52 | # triangles 53 | ect.triangle(image,[0,0],[3,3],[0,6],[0,0,255], 1) 54 | ect.triangle(image,[6,0],[3,3],[6,6],[0,0,255], 1) 55 | image = ect.clear(image) 56 | 57 | 58 | # moving circles 59 | for y in range(5): 60 | ect.circle(image,(3,4), 2, w, .1) 61 | ect.circle(image,(3,4), 2, e, 0) 62 | ect.circle(image,(4,5), 3, w, .1) 63 | ect.circle(image,(4,5), 3, e, 0) 64 | ect.circle(image,(5,6), 3, w, .1) 65 | ect.circle(image,(5,6), 3, e, 0) 66 | ect.circle(image,(6,7), 3, w, .1) 67 | ect.circle(image,(6,7), 3, e, 0) 68 | ect.circle(image,(7,8), 3, w, .1) 69 | ect.circle(image,(7,8), 3, e, 0) 70 | ect.circle(image,(8,9), 3, w, .1) 71 | ect.circle(image,(8,9), 3, e, 0) 72 | ect.circle(image,(1,2), 2, w, .1) 73 | ect.circle(image,(1,2), 2, e, 0) 74 | 75 | 76 | # colorful squares 77 | for x in range(5): 78 | ect.square(image, [0,0], [7,0], [7,7],[0,7],[randint(0,255),randint(0,255),randint(0,255)],.01) 79 | ect.square(image, [1,1], [6,1], [6,6],[1,6],[randint(0,255),randint(0,255),randint(0,255)],.01) 80 | ect.square(image, [2,2], [5,2], [5,5],[2,5],[randint(0,255),randint(0,255),randint(0,255)],.01) 81 | 82 | 83 | # clear screen with squares 84 | ect.square(image, [0,0], [7,0], [7,7],[0,7],e,.01) 85 | ect.square(image, [1,1], [6,1], [6,6],[1,6],e,.01) 86 | ect.square(image, [2,2], [5,2], [5,5],[2,5],e,.01) 87 | 88 | # more moving circles 89 | ect.circle(image,(3,4), 3, w, 1) 90 | ect.circle(image,(3,4), 3, e, 0) 91 | ect.circle(image,(4,4), 3, w, 1) 92 | ect.circle(image,(4,4), 3, e, 0) 93 | ect.circle(image,(3,4), 3, w, 1) 94 | ect.circle(image,(3,4), 3, e, 0) 95 | 96 | 97 | ''' 98 | This subroutine first draw a circle with an epicenter of (4,4). It then has a radius of 3 followed by 99 | random colors done with [randint(0,255), randint(0,255), randint(0,255)] this can generate a random number 100 | from 0 to 255. For instance, it might generate [10,233,100]. The last value of 0.1 is the number of second 101 | it will keep the image displayed. 102 | ''' 103 | 104 | for x in range(0, 10): 105 | ect.circle(image,(4,4), 3, [randint(0,255), randint(0,255), randint(0,255)], 0.1) 106 | ect.circle(image,(4,4), 2, [randint(0,255), randint(0,255), randint(0,255)], 0.1) 107 | ect.circle(image,(4,4), 1, [randint(0,255), randint(0,255), randint(0,255)], 0.1) 108 | 109 | 110 | 111 | ect.circle(image,(4,4), 3, w, 1) 112 | ect.circle(image,(4,4), 3, b, 1) 113 | ect.circle(image,(4,4), 3, w, 1) 114 | ect.circle(image,(4,4), 3, b, 1) 115 | ect.circle(image,(4,4), 2, b, 1) 116 | ect.circle(image,(4,4), 1, b, 1) 117 | ect.circle(image,(4,4), 1, e, 1) 118 | ect.circle(image,(4,4), 2, e, 1) 119 | ect.circle(image,(4,4), 3, e, 1) 120 | 121 | 122 | 123 | # stack images ontop of each other 124 | 125 | for x in range(0, 10): 126 | r = randint(0,255) 127 | g = randint(0,255) 128 | b = randint(0,255) 129 | image1 = ect.circle(image,(4,4), 3, [r, g, b], 0.1) 130 | image2 = ect.circle(image1,(4,4), 2, [r, g, b], 0.1) 131 | image3 = ect.circle(image2,(4,4), 1, [r, g, b], 0.1) 132 | 133 | image3 = ect.clear(image3) 134 | 135 | --------------------------------------------------------------------------------