def set_background(self, filename): print("Set background to ", filename) try: self._bg_group.pop() except IndexError: pass # s'ok, we'll fix to test once we can if not filename: return # we're done, no background desired if self._bg_file: self._bg_file.close() self._bg_file = open(filename, "rb") background = displayio.OnDiskBitmap(self._bg_file) try: self._bg_sprite = displayio.TileGrid( background, pixel_shader=displayio.ColorConverter(), position=(0, 0)) except: self._bg_sprite = displayio.Sprite( background, pixel_shader=displayio.ColorConverter(), position=(0, 0)) self._bg_group.append(self._bg_sprite) board.DISPLAY.refresh_soon() gc.collect() board.DISPLAY.wait_for_frame()
def show_image(filename): image_file = open(filename, "rb") odb = displayio.OnDiskBitmap(image_file) face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) time.sleep(1) screen.append(face) board.DISPLAY.wait_for_frame() backlight.duty_cycle = max_brightness
def show_image(filename): image_file = open(filename, "rb") odb = displayio.OnDiskBitmap(image_file) face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) backlight.value = False splash.append(face) board.DISPLAY.wait_for_frame() backlight.value = True
def show_image(filename): image_file = open(filename, "rb") odb = displayio.OnDiskBitmap(image_file) face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) backlight.value = False splash.append(face) try: board.DISPLAY.refresh(target_frames_per_second=60) except AttributeError: board.DISPLAY.wait_for_frame() backlight.value = True
def show_image(filename): image_file = open(filename, "rb") odb = displayio.OnDiskBitmap(image_file) face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) backlight.duty_cycle = 0 splash.append(face) # Wait for the image to load. board.DISPLAY.wait_for_frame() backlight.duty_cycle = max_brightness
def show_image(filename): image_file = open(filename, "rb") odb = displayio.OnDiskBitmap(image_file) face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) backlight.duty_cycle = 0 splash.append(face) # Wait for the image to load. try: board.DISPLAY.refresh(target_frames_per_second=60) except AttributeError: board.DISPLAY.wait_for_frame() backlight.duty_cycle = max_brightness
def advance(self): """Displays the next image. Returns True when a new image was displayed, False otherwise. """ if self._image_file: self._fade_down() self._group.pop() self._image_file.close() self._image_file = None self._current_image += self.direction # Try and load an OnDiskBitmap until a valid file is found or we run out of options. This # loop stops because we either set odb or reduce the length of _file_list. odb = None while not odb and self._file_list: if 0 <= self._current_image < len(self._file_list): pass elif not self.loop: return False else: image_count = len(self._file_list) if self._current_image < 0: self._current_image += image_count elif self._current_image >= image_count: self._current_image -= image_count self._reorder_images() image_name = self._file_list[self._current_image] self._image_file = open(image_name, "rb") try: odb = displayio.OnDiskBitmap(self._image_file) except ValueError: self._image_file.close() self._image_file = None del self._file_list[self._current_image] if not odb: raise RuntimeError("No valid images") sprite = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) self._group.append(sprite) self._display.wait_for_frame() self._fade_up() self._img_start = time.monotonic() return True
def draw_QR(matrix): # how big each pixel is, add 2 blocks on either side BLOCK_SIZE = DISPLAY_W // (matrix.width + 4) # Center the QR code in the middle of the screen X_OFFSET = (DISPLAY_W - BLOCK_SIZE * matrix.width) // 2 Y_OFFSET = (DISPLAY_H - BLOCK_SIZE * matrix.height) // 2 # monochome (2 color) palette palette = displayio.Palette(2) palette[0] = 0xFFFFFF palette[1] = 0x000000 # bitmap the size of the screen, monochrome (2 colors) bitmap = displayio.Bitmap(DISPLAY_H, DISPLAY_W, 2) # raster the QR code line = bytearray(DISPLAY_W // 8) # monochrome means 8 pixels per byte for y in range(matrix.height): # each scanline in the height for i, _ in enumerate(line): # initialize it to be empty line[i] = 0 for x in range(matrix.width): if matrix[x, y]: for b in range(BLOCK_SIZE): _x = X_OFFSET + x * BLOCK_SIZE + b line[_x // 8] |= 1 << (7 - (_x % 8)) for b in range(BLOCK_SIZE): # load this line of data in, as many time as block size #pylint: disable=protected-access bitmap._load_row(Y_OFFSET + y * BLOCK_SIZE + b, line) # display the bitmap using our palette splash = displayio.Group() face = displayio.Sprite(bitmap, pixel_shader=palette, position=(0, 0)) splash.append(face) board.DISPLAY.show(splash) board.DISPLAY.wait_for_frame()
def _update_text(self, new_text): x = 0 y = 0 i = 0 first_different = self._text is not None for c in new_text: if chr(ord(c)) == '\n': y += int(self.height * 1.25) x = 0 continue glyph = self.font.get_glyph(ord(c)) if not glyph: continue # Remove any characters that are different if first_different and c != self._text[i]: # TODO(tannewt): Make this smarter when we can remove and add things into the middle # of a group. for _ in range(len(self.sprites) - i): try: self.group.pop() except IndexError: break first_different = False if not first_different: position = (self._x + x, self._y + y + self.height - glyph["bounds"][1] - glyph["bounds"][3]) try: face = displayio.TileGrid(glyph["bitmap"], pixel_shader=self.p, position=position) except: face = displayio.Sprite(glyph["bitmap"], pixel_shader=self.p, position=position) self.group.append(face) self.sprites[i] = face x += glyph["shift"][0] # TODO skip this for control sequences or non-printables. i += 1 # TODO: support multiple lines by adjusting y self._text = new_text
def show_QR(self, qr_data, qr_size=128, position=None): # pylint: disable=invalid-name """Display a QR code on the TFT :param qr_data: The data for the QR code. :param int qr_size: The size of the QR code in pixels. :param position: The (x, y) tuple position of the QR code on the display. """ import adafruit_miniqr if not qr_data: # delete it if self._qr_group: try: self._qr_group.pop() except IndexError: pass board.DISPLAY.refresh_soon() board.DISPLAY.wait_for_frame() return if not position: position = (0, 0) if qr_size % 32 != 0: raise RuntimeError("QR size must be divisible by 32") qrcode = adafruit_miniqr.QRCode() qrcode.add_data(qr_data) qrcode.make() # pylint: disable=invalid-name # how big each pixel is, add 2 blocks on either side BLOCK_SIZE = qr_size // (qrcode.matrix.width + 4) # Center the QR code in the middle X_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.width) // 2 Y_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.height) // 2 # monochome (2 color) palette palette = displayio.Palette(2) palette[0] = 0xFFFFFF palette[1] = 0x000000 # bitmap the size of the matrix + borders, monochrome (2 colors) qr_bitmap = displayio.Bitmap(qr_size, qr_size, 2) # raster the QR code line = bytearray(qr_size // 8) # monochrome means 8 pixels per byte for y in range(qrcode.matrix.height): # each scanline in the height for i, _ in enumerate(line): # initialize it to be empty line[i] = 0 for x in range(qrcode.matrix.width): if qrcode.matrix[x, y]: for b in range(BLOCK_SIZE): _x = X_OFFSET + x * BLOCK_SIZE + b line[_x // 8] |= 1 << (7 - (_x % 8)) for b in range(BLOCK_SIZE): # load this line of data in, as many time as block size for i, byte in enumerate(line): qr_bitmap[Y_OFFSET + y * BLOCK_SIZE + b + i] = byte # pylint: enable=invalid-name # display the bitmap using our palette qr_sprite = displayio.Sprite(qr_bitmap, pixel_shader=palette, position=position) if self._qr_group: try: self._qr_group.pop() except IndexError: # later test if empty pass else: self._qr_group = displayio.Group() self.splash.append(self._qr_group) self._qr_group.append(qr_sprite) board.DISPLAY.refresh_soon() board.DISPLAY.wait_for_frame()
# Set up accelerometer on I2C bus, 4G range: I2C = busio.I2C(board.SCL, board.SDA) try: ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18) # Production board except ValueError: ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x19) # Beta hardware ACCEL.range = adafruit_lis3dh.RANGE_4_G try: import displayio SCREEN = displayio.Group() board.DISPLAY.show(SCREEN) BITMAP = displayio.OnDiskBitmap(open(IMAGEFILE, 'rb')) SCREEN.append( displayio.Sprite(BITMAP, pixel_shader=displayio.ColorConverter(), position=(0, 0))) board.DISPLAY.wait_for_frame() # Wait for the image to load. BACKLIGHT.duty_cycle = 65535 # Turn on display backlight except (ImportError, NameError, AttributeError) as err: pass # Probably earlier CircuitPython; no displayio support # If everything has initialized correctly, turn off the onboard NeoPixel: PIXEL = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0) PIXEL.show() while True: # No freefall detect in LIS3DH library, but it's easily done manually... # poll the accelerometer and look for near-zero readings on all axes. X, Y, Z = ACCEL.acceleration A2 = X * X + Y * Y + Z * Z # Acceleration^2 in 3space (no need for sqrt)
audiofilename = file_name wavefile = audioio.WaveFile(audiofile) audio.play(wavefile) # draw an image as a background splash = displayio.Group() background = displayio.OnDiskBitmap(open(IMAGE_FILE, "rb")) try: bg_sprite = displayio.TileGrid(background, pixel_shader=displayio.ColorConverter(), position=(0, 0)) except: bg_sprite = displayio.Sprite(background, pixel_shader=displayio.ColorConverter(), position=(0, 0)) splash.append(bg_sprite) board.DISPLAY.show(splash) board.DISPLAY.wait_for_frame() while True: p = ts.touch_point if p: print(p) x, y, z = p for button in buttons: box = button[0] if (box[0][0] <= x <= box[1][0]) and (box[0][1] <= y <= box[1][1]): play_file(button[1])
ACCEL = adafruit_lis3dh.LIS3DH_I2C(I2C, address=0x18) ACCEL.range = adafruit_lis3dh.RANGE_4_G while True: shaken = False with open(images[i], "rb") as f: print("Image load {}".format(images[i])) try: odb = displayio.OnDiskBitmap(f) except ValueError: print("Image unsupported {}".format(images[i])) del images[i] continue face = displayio.Sprite(odb, pixel_shader=displayio.ColorConverter(), position=(0, 0)) splash.append(face) # Wait for the image to load. board.DISPLAY.wait_for_frame() # Fade up the backlight for b in range(100): backlight.duty_cycle = b * max_brightness // 100 time.sleep(0.01) # default (0.01) # Wait forever while not shaken: try: ACCEL_Z = ACCEL.acceleration[2] # Read Z axis acceleration except IOError: