|
19 | 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
20 | 20 | # THE SOFTWARE. |
21 | 21 | import HT16K33 |
| 22 | +import Image |
| 23 | +import time |
22 | 24 |
|
23 | 25 |
|
24 | 26 | class Matrix8x8(HT16K33.HT16K33): |
25 | | - """Single color 8x8 matrix LED backpack display.""" |
26 | | - |
27 | | - def __init__(self, **kwargs): |
28 | | - """Initialize display. All arguments will be passed to the HT16K33 class |
29 | | - initializer, including optional I2C address and bus number parameters. |
30 | | - """ |
31 | | - super(Matrix8x8, self).__init__(**kwargs) |
32 | | - |
33 | | - def set_pixel(self, x, y, value): |
34 | | - """Set pixel at position x, y to the given value. X and Y should be values |
35 | | - of 0 to 8. Value should be 0 for off and non-zero for on. |
36 | | - """ |
37 | | - if x < 0 or x > 7 or y < 0 or y > 7: |
38 | | - # Ignore out of bounds pixels. |
39 | | - return |
40 | | - self.set_led(y*16+((x+7)%8), value) |
41 | | - |
42 | | - def set_image(self, image): |
43 | | - """Set display buffer to Python Image Library image. Image will be converted |
44 | | - to 1 bit color and non-zero color values will light the LEDs. |
45 | | - """ |
46 | | - imwidth, imheight = image.size |
47 | | - if imwidth != 8 or imheight != 8: |
48 | | - raise ValueError('Image must be an 8x8 pixels in size.') |
49 | | - # Convert image to 1 bit color and grab all the pixels. |
50 | | - pix = image.convert('1').load() |
51 | | - # Loop through each pixel and write the display buffer pixel. |
52 | | - for x in [0, 1, 2, 3, 4, 5, 6, 7]: |
53 | | - for y in [0, 1, 2, 3, 4, 5, 6, 7]: |
54 | | - color = pix[(x, y)] |
55 | | - # Handle the color of the pixel, off or on. |
56 | | - if color == 0: |
57 | | - self.set_pixel(x, y, 0) |
58 | | - else: |
59 | | - self.set_pixel(x, y, 1) |
| 27 | + """Single color 8x8 matrix LED backpack display.""" |
| 28 | + |
| 29 | + def __init__(self, **kwargs): |
| 30 | + """Initialize display. All arguments will be passed to the HT16K33 class |
| 31 | + initializer, including optional I2C address and bus number parameters. |
| 32 | + """ |
| 33 | + super(Matrix8x8, self).__init__(**kwargs) |
| 34 | + |
| 35 | + def set_pixel(self, x, y, value): |
| 36 | + """Set pixel at position x, y to the given value. X and Y should be values |
| 37 | + of 0 to 8. Value should be 0 for off and non-zero for on. |
| 38 | + """ |
| 39 | + if x < 0 or x > 7 or y < 0 or y > 7: |
| 40 | + # Ignore out of bounds pixels. |
| 41 | + return |
| 42 | + self.set_led(y * 16 + ((x + 7) % 8), value) |
| 43 | + |
| 44 | + def set_image(self, image): |
| 45 | + """Set display buffer to Python Image Library image. Image will be converted |
| 46 | + to 1 bit color and non-zero color values will light the LEDs. |
| 47 | + """ |
| 48 | + imwidth, imheight = image.size |
| 49 | + if imwidth != 8 or imheight != 8: |
| 50 | + raise ValueError('Image must be an 8x8 pixels in size.') |
| 51 | + # Convert image to 1 bit color and grab all the pixels. |
| 52 | + pix = image.convert('1').load() |
| 53 | + # Loop through each pixel and write the display buffer pixel. |
| 54 | + for x in [0, 1, 2, 3, 4, 5, 6, 7]: |
| 55 | + for y in [0, 1, 2, 3, 4, 5, 6, 7]: |
| 56 | + color = pix[(x, y)] |
| 57 | + # Handle the color of the pixel, off or on. |
| 58 | + if color == 0: |
| 59 | + self.set_pixel(x, y, 0) |
| 60 | + else: |
| 61 | + self.set_pixel(x, y, 1) |
| 62 | + |
| 63 | + def create_blank_image(self): |
| 64 | + return Image.new("RGB", (8, 8)) |
| 65 | + |
| 66 | + |
| 67 | + def horizontal_scroll(self, image, padding=True): |
| 68 | + """Returns a list of images which appear to scroll from left to right |
| 69 | + across the input image when displayed on the LED matrix in order. |
| 70 | +
|
| 71 | + The input image is not limited to being 8x8. If the input image is |
| 72 | + larger than this, then all columns will be scrolled through but only |
| 73 | + the top 8 rows of pixels will be displayed. |
| 74 | +
|
| 75 | + Keyword arguments: |
| 76 | + image -- The image to scroll across. |
| 77 | + padding -- If True, the animation will begin with a blank screen and the |
| 78 | + input image will scroll into the blank screen one pixel column at a |
| 79 | + time. Similarly, after scrolling across the whole input image, the |
| 80 | + end of the image will scroll out of a blank screen one column at a |
| 81 | + time. If this is not True, then only the input image will be scroll |
| 82 | + across without beginning or ending with "whitespace." |
| 83 | + (Default = True) |
| 84 | + """ |
| 85 | + |
| 86 | + image_list = list() |
| 87 | + width = image.size[0] |
| 88 | + # Scroll into the blank image. |
| 89 | + if padding: |
| 90 | + for x in range(8): |
| 91 | + section = image.crop((0, 0, x, 8)) |
| 92 | + display_section = self.create_blank_image() |
| 93 | + display_section.paste(section, (8 - x, 0, 8, 8)) |
| 94 | + image_list.append(display_section) |
| 95 | + |
| 96 | + #Scroll across the input image. |
| 97 | + for x in range(8, width + 1): |
| 98 | + section = image.crop((x - 8, 0, x, 8)) |
| 99 | + display_section = self.create_blank_image() |
| 100 | + display_section.paste(section, (0, 0, 8, 8)) |
| 101 | + image_list.append(display_section) |
| 102 | + |
| 103 | + #Scroll out, leaving the blank image. |
| 104 | + if padding: |
| 105 | + for x in range(width - 7, width + 1): |
| 106 | + section = image.crop((x, 0, width, 8)) |
| 107 | + display_section = self.create_blank_image() |
| 108 | + display_section.paste(section, (0, 0, 7 - (x - (width - 7)), 8)) |
| 109 | + image_list.append(display_section) |
| 110 | + |
| 111 | + #Return the list of images created |
| 112 | + return image_list |
| 113 | + |
| 114 | + def vertical_scroll(self, image, padding=True): |
| 115 | + """Returns a list of images which appear to scroll from top to bottom |
| 116 | + down the input image when displayed on the LED matrix in order. |
| 117 | +
|
| 118 | + The input image is not limited to being 8x8. If the input image is |
| 119 | + largerthan this, then all rows will be scrolled through but only the |
| 120 | + left-most 8 columns of pixels will be displayed. |
| 121 | +
|
| 122 | + Keyword arguments: |
| 123 | + image -- The image to scroll down. |
| 124 | + padding -- If True, the animation will begin with a blank screen and the |
| 125 | + input image will scroll into the blank screen one pixel row at a |
| 126 | + time. Similarly, after scrolling down the whole input image, the end |
| 127 | + of the image will scroll out of a blank screen one row at a time. |
| 128 | + If this is not True, then only the input image will be scroll down |
| 129 | + without beginning or ending with "whitespace." (Default = True) |
| 130 | + """ |
| 131 | + |
| 132 | + image_list = list() |
| 133 | + height = image.size[1] |
| 134 | + # Scroll into the blank image. |
| 135 | + if padding: |
| 136 | + for y in range(8): |
| 137 | + section = image.crop((0, 0, 8, y)) |
| 138 | + display_section = self.create_blank_image() |
| 139 | + display_section.paste(section, (0, 8 - y, 8, 8)) |
| 140 | + image_list.append(display_section) |
| 141 | + |
| 142 | + #Scroll across the input image. |
| 143 | + for y in range(8, height + 1): |
| 144 | + section = image.crop((0, y - 8, 8, y)) |
| 145 | + display_section = self.create_blank_image() |
| 146 | + display_section.paste(section, (0, 0, 8, 8)) |
| 147 | + image_list.append(display_section) |
| 148 | + |
| 149 | + #Scroll out, leaving the blank image. |
| 150 | + if padding: |
| 151 | + for y in range(height - 7, height + 1): |
| 152 | + section = image.crop((0, y, 8, height)) |
| 153 | + display_section = self.create_blank_image() |
| 154 | + display_section.paste(section, (0, 0, 8, 7 - (y - (height - 7)))) |
| 155 | + image_list.append(display_section) |
| 156 | + |
| 157 | + #Return the list of images created |
| 158 | + return image_list |
| 159 | + |
| 160 | + def animate(self, images, delay=.25): |
| 161 | + """Displays each of the input images in order, pausing for "delay" |
| 162 | + seconds after each image. |
| 163 | +
|
| 164 | + Keyword arguments: |
| 165 | + image -- An iterable collection of Image objects. |
| 166 | + delay -- How many seconds to wait after displaying an image before |
| 167 | + displaying the next one. (Default = .25) |
| 168 | + """ |
| 169 | + for image in images: |
| 170 | + # Draw the image on the display buffer. |
| 171 | + self.set_image(image) |
| 172 | + |
| 173 | + # Draw the buffer to the display hardware. |
| 174 | + self.write_display() |
| 175 | + time.sleep(delay) |
0 commit comments