Simple test

Ensure your device works with this simple test.

examples/displayio_flipclock_simpletest.py
  1# SPDX-FileCopyrightText: Copyright (c) 2022 Tim Cocks for Adafruit Industries
  2#
  3# SPDX-License-Identifier: MIT
  4"""
  5An example that shows how to initialize and use the FlipClock displayio object.
  6
  7This example is the most basic usage of FlipClock and is not intended to count
  8time or any other specific units or values.
  9"""
 10import time
 11import board
 12from displayio import Group
 13import adafruit_imageload
 14from adafruit_displayio_flipclock.flip_clock import FlipClock
 15
 16#  == Configuration Variables ==
 17
 18# seconds per animation frame
 19ANIMATION_DELAY = 0.01
 20
 21# number of frames in the animation
 22ANIMATION_FRAME_COUNT = 10
 23
 24# color indexes that will be made transparent in the palette
 25TRANSPARENT_INDEXES = range(11)
 26
 27# Brightness modifier for top half during animation
 28BRIGHTER_LEVEL = 0.99
 29
 30# Brightness modifier for bottom half in the shadow during animation
 31DARKER_LEVEL = 0.5
 32
 33# Brightness modifier to use by default for static sprites
 34MEDIUM_LEVEL = 0.9
 35
 36# == END configuration variables ==
 37
 38# access built-in display
 39display = board.DISPLAY
 40
 41# load the static sprite sheet
 42static_spritesheet, static_palette = adafruit_imageload.load("static_sheet.bmp")
 43static_palette.make_transparent(0)
 44
 45# load the animation sprite sheets
 46top_animation_spritesheet, top_animation_palette = adafruit_imageload.load(
 47    "grey_top_animation_sheet.bmp"
 48)
 49bottom_animation_spritesheet, bottom_animation_palette = adafruit_imageload.load(
 50    "grey_bottom_animation_sheet.bmp"
 51)
 52
 53# set the transparent color indexes in respective palettes
 54for i in TRANSPARENT_INDEXES:
 55    top_animation_palette.make_transparent(i)
 56    bottom_animation_palette.make_transparent(i)
 57
 58# calculate sprite size by dividing total sheet
 59SPRITE_WIDTH = static_spritesheet.width // 3
 60SPRITE_HEIGHT = (static_spritesheet.height // 4) // 2
 61
 62# initialize FlipClock widget object
 63clock = FlipClock(
 64    static_spritesheet,
 65    static_palette,
 66    top_animation_spritesheet,
 67    top_animation_palette,
 68    bottom_animation_spritesheet,
 69    bottom_animation_palette,
 70    SPRITE_WIDTH,
 71    SPRITE_HEIGHT,
 72    anim_delay=ANIMATION_DELAY,
 73    brighter_level=BRIGHTER_LEVEL,
 74    darker_level=DARKER_LEVEL,
 75    medium_level=MEDIUM_LEVEL,
 76)
 77
 78# position it in the center of the display
 79clock.anchor_point = (0.5, 0.5)
 80clock.anchored_position = (display.width // 2, display.height // 2)
 81
 82# group to hold our flip clock
 83main_group = Group()
 84
 85# append the clock to the group
 86main_group.append(clock)
 87
 88# show the group on the display
 89board.DISPLAY.root_group = main_group
 90
 91# set a value to start with in the first pair
 92clock.first_pair = "13"
 93
 94while True:
 95    # increment first pair number
 96    cur_val = clock.first_pair
 97    next_val = int(cur_val) + 1
 98
 99    # if it's time to reset
100    if next_val > 20:
101        # reset value back to 0
102        next_val = 0
103
104    # set the first pair value on the FlipClock
105    clock.first_pair = str(next_val)
106
107    # increment second pair value
108    cur_val = clock.second_pair
109    next_val = int(cur_val) + 1
110
111    # if it's time to reset
112    if next_val > 99:
113        # reset value back to 0
114        next_val = 0
115
116    # set the second pair value on the FlipClock
117    clock.second_pair = str(next_val)
118
119    time.sleep(0.5)