|
| 1 | +# Copyright (c) 2017 Adafruit Industries |
| 2 | +# Author: Carter Nelson |
| 3 | +# Modified from Matrix8x8 by Tony DiCola |
| 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 |
| 13 | +# all 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 |
| 21 | +# THE SOFTWARE. |
| 22 | +from . import HT16K33 |
| 23 | +from PIL import Image |
| 24 | +import time |
| 25 | + |
| 26 | +class Matrix8x16(HT16K33.HT16K33): |
| 27 | + """Single color 8x16 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(Matrix8x16, 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 7 and 0 to 15, resp. Value should be 0 for off and non-zero for on. |
| 38 | + """ |
| 39 | + if x < 0 or x > 7 or y < 0 or y > 15: |
| 40 | + # Ignore out of bounds pixels. |
| 41 | + return |
| 42 | + self.set_led((7 - x) * 16 + y, 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 != 16: |
| 50 | + raise ValueError('Image must be an 8x16 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 xrange(8): |
| 55 | + for y in xrange(16): |
| 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, 16)) |
| 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 8x16. If the input image is |
| 72 | + larger than this, then all columns will be scrolled through but only |
| 73 | + the top 16 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, 16)) |
| 92 | + display_section = self.create_blank_image() |
| 93 | + display_section.paste(section, (8 - x, 0, 8, 16)) |
| 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, 16)) |
| 99 | + display_section = self.create_blank_image() |
| 100 | + display_section.paste(section, (0, 0, 8, 16)) |
| 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, 16)) |
| 107 | + display_section = self.create_blank_image() |
| 108 | + display_section.paste(section, (0, 0, 7 - (x - (width - 7)), 16)) |
| 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 8x16. 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(16): |
| 137 | + section = image.crop((0, 0, 8, y)) |
| 138 | + display_section = self.create_blank_image() |
| 139 | + display_section.paste(section, (0, 8 - y, 8, 16)) |
| 140 | + image_list.append(display_section) |
| 141 | + |
| 142 | + #Scroll across the input image. |
| 143 | + for y in range(16, height + 1): |
| 144 | + section = image.crop((0, y - 16, 8, y)) |
| 145 | + display_section = self.create_blank_image() |
| 146 | + display_section.paste(section, (0, 0, 8, 16)) |
| 147 | + image_list.append(display_section) |
| 148 | + |
| 149 | + #Scroll out, leaving the blank image. |
| 150 | + if padding: |
| 151 | + for y in range(height - 15, 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 - 15)))) |
| 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