def __init__(self, timezone="Pacific", hour_24=False, auto_dst=True, sound=False, brightness=1.0, debug=False): # Input parameters self._timezone = timezone self._hour_24_12 = hour_24 self._dst = False self._auto_dst = auto_dst self._sound = sound self._brightness = brightness # Other parameters self._message = "PyBadge Clock" self._colon = True self._batt_level = 0 # Default battery level self._label_edits = [] # label edit attributes self._label_restore_color = [] # for restoring text color values self._weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] self._month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] # Load the text font from the fonts folder self._font_0 = bitmap_font.load_font("/fonts/OpenSans-9.bdf") self._font_1 = bitmap_font.load_font("/fonts/Helvetica-Bold-36.bdf") # Instantiate PyBadger instance self.panel = pybadger self.panel._neopixels.brightness = 0.1 # Set default NeoPixel brightness self.panel.pixels.fill(0x000000) # Clear all NeoPixels self.panel.play_tone(440, 0.1) # A4 welcome tone # The board's integral display size and element size WIDTH = board.DISPLAY.width # 160 for PyGamer and PyBadge HEIGHT = board.DISPLAY.height # 128 for PyGamer and PyBadge ELEMENT_SIZE = WIDTH // 4 # Size of element_grid blocks in pixels board.DISPLAY.brightness = self._brightness # Default colors self.BLACK = 0x000000 self.RED = 0xFF0000 self.ORANGE = 0xFF8811 self.YELLOW = 0xFFFF00 self.GREEN = 0x00FF00 self.CYAN = 0x00FFFF self.BLUE = 0x0000FF self.LT_BLUE = 0x000044 self.VIOLET = 0x9900FF self.DK_VIO = 0x110022 self.WHITE = 0xFFFFFF self.GRAY = 0x444455 ### Define the display group ### self._image_group = displayio.Group(max_size=15) # Create a background color fill layer; image_group[0] self._color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1) self._color_palette = displayio.Palette(1) self._color_palette[0] = self.DK_VIO self._background = displayio.TileGrid(self._color_bitmap, pixel_shader=self._color_palette, x=0, y=0) self._image_group.append(self._background) self._label_restore_color.append(self.DK_VIO) self._label_edits.append(None) # Battery indicator tile grid; image_group[1] self._sprite_sheet, self._palette = adafruit_imageload.load("/cedargrove_clock_builder/batt_sprite_sheet.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette) self._batt_icon = displayio.TileGrid(self._sprite_sheet, pixel_shader=self._palette, width = 1, height = 1, tile_width = 16, tile_height = 16) self._batt_icon.x = WIDTH - 16 self._batt_icon.y = 1 self._image_group.append(self._batt_icon) self._label_restore_color.append(self.BLACK) self._label_edits.append(None) ### Define labels and values using element grid coordinates # Colon; image_group[2] self._clock_digits_colon = Label(self._font_1, text=":", color=self.WHITE, max_glyphs=1) self._clock_digits_colon.x = 62 self._clock_digits_colon.y = (HEIGHT // 2) + 10 - 3 self._image_group.append(self._clock_digits_colon) self._label_restore_color.append(self._clock_digits_colon.color) self._label_edits.append(None) # Weekday; image_group[3] self._clock_wday = Label(self._font_0, text="---", color=self.YELLOW, max_glyphs=3) self._clock_wday.x = 21 self._clock_wday.y = 40 self._image_group.append(self._clock_wday) self._label_restore_color.append(self._clock_wday.color) self._label_edits.append(None) # Comma for Month Day (date); image_group[4] self._clock_comma = Label(self._font_0, text=",", color=self.YELLOW, max_glyphs=1) self._clock_comma.x = 99 self._clock_comma.y = 40 self._image_group.append(self._clock_comma) self._label_restore_color.append(self._clock_comma.color) self._label_edits.append(None) # AM/PM indicator; image_group[5] self._clock_ampm = Label(self._font_0, text="--", color=self.WHITE, max_glyphs=2) self._clock_ampm.x = 120 self._clock_ampm.y = (HEIGHT // 2) + 10 - 8 self._image_group.append(self._clock_ampm) self._label_restore_color.append(self._clock_ampm.color) self._label_edits.append(None) # Time Zone indicator; image_group[6] self._clock_dst = Label(self._font_0, text="---", color=self.VIOLET, max_glyphs=3) self._clock_dst.x = 120 self._clock_dst.y = (HEIGHT // 2) + 10 + 8 self._image_group.append(self._clock_dst) self._label_restore_color.append(self._clock_dst.color) self._label_edits.append(None) # Sound indicator; image_group[7] self._clock_sound = Label(self._font_0, text="-----", color=self.VIOLET, max_glyphs=5) self._clock_sound.x = 5 self._clock_sound.y = HEIGHT - 8 self._image_group.append(self._clock_sound) self._label_restore_color.append(self._clock_sound.color) self._label_edits.append(("boolean", 0, 1)) # Automatic DST indicator; image_group[8] self._clock_auto_dst = Label(self._font_0, text="-------", color=self.VIOLET, max_glyphs=7) self._clock_auto_dst.x = 104 self._clock_auto_dst.y = HEIGHT - 8 self._image_group.append(self._clock_auto_dst) self._label_restore_color.append(self._clock_auto_dst.color) self._label_edits.append(("boolean", 0 , 1)) # Month; image_group[9] self._clock_month = Label(self._font_0, text="---", color=self.YELLOW, max_glyphs=3) self._clock_month.x = 55 self._clock_month.y = 40 self._image_group.append(self._clock_month) self._label_restore_color.append(self._clock_month.color) self._label_edits.append(("month", 1, 12)) # Month Day (date); image_group[10] self._clock_mday = Label(self._font_0, text="--", color=self.YELLOW, max_glyphs=2) self._clock_mday.x = 83 self._clock_mday.y = 40 self._image_group.append(self._clock_mday) self._label_restore_color.append(self._clock_mday.color) self._label_edits.append(("int2", 1, 31)) # Year; image_group[11] self._clock_year = Label(self._font_0, text="----", color=self.YELLOW, max_glyphs=4) self._clock_year.x = 105 self._clock_year.y = 40 self._image_group.append(self._clock_year) self._label_restore_color.append(self._clock_year.color) self._label_edits.append(("int4", 2000, 2037)) # Hour; image_group[12] self._clock_digits_hour = Label(self._font_1, text="--", color=self.WHITE, max_glyphs=2) self._clock_digits_hour.x = 20 self._clock_digits_hour.y = (HEIGHT // 2) + 10 self._image_group.append(self._clock_digits_hour) self._label_restore_color.append(self._clock_digits_hour.color) self._label_edits.append(("int2", 0, 23)) # Minutes; image_group[13] self._clock_digits_min = Label(self._font_1, text="--", color=self.WHITE, max_glyphs=2) self._clock_digits_min.x = 74 self._clock_digits_min.y = (HEIGHT // 2) + 10 self._image_group.append(self._clock_digits_min) self._label_restore_color.append(self._clock_digits_min.color) self._label_edits.append(("int2", 0, 59)) # Clock Message area; image_group[14] self._clock_message = Label(self._font_0, text="", color=self.VIOLET, max_glyphs=20) self._clock_message.x = 5 self._clock_message.y = 5 self._image_group.append(self._clock_message) self._label_restore_color.append(self._clock_message.color) self._label_edits.append(None) # debug parameters self._debug = debug if self._debug: print("*Init:", self.__class__) print("*Init: ", self.__dict__)
fonts[i] = cwd + "/fonts/" + filename print(fonts) ########################################################################## THE_FONT = fonts[0] DISPLAY_STRING = "A multi-line-\nexample of\n font bounding!" WRAP_CHARS = 40 ########################################################################## # Make the display context splash = displayio.Group() board.DISPLAY.show(splash) # Make a background color fill color_bitmap = displayio.Bitmap(320, 240, 1) color_palette = displayio.Palette(1) color_palette[0] = 0xFFFFFF bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Load the font font = bitmap_font.load_font(THE_FONT) font.load_glyphs(DISPLAY_STRING.encode("utf-8")) print(DISPLAY_STRING) text = Label(font, text=DISPLAY_STRING) text.x = 20 text.y = 100 text.color = 0x0
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF, background_text_color=0xFFFFFF, foreground_text_color=0x000000, hello_font=terminalio.FONT, hello_scale=1, hello_string="HELLO", my_name_is_font=terminalio.FONT, my_name_is_scale=1, my_name_is_string="MY NAME IS", name_font=terminalio.FONT, name_scale=1, name_string="Blinka"): """Create a "Hello My Name is"-style badge. :param background_color: The color of the background. Defaults to 0xFF0000. :param foreground_color: The color of the foreground rectangle. Defaults to 0xFFFFFF. :param background_text_color: The color of the "HELLO MY NAME IS" text. Defaults to 0xFFFFFF. :param foreground_text_color: The color of the name text. Defaults to 0x000000. :param hello_font: The font for the "HELLO" string. Defaults to ``terminalio.FONT``. :param hello_scale: The size scale of the "HELLO" string. Defaults to 1. :param hello_string: The first string of the badge. Defaults to "HELLO". :param my_name_is_font: The font for the "MY NAME IS" string. Defaults to ``terminalio.FONT``. :param my_name_is_scale: The size scale of the "MY NAME IS" string. Defaults to 1. :param my_name_is_string: The second string of the badge. Defaults to "MY NAME IS". :param name_font: The font for the name string. Defaults to ``terminalio.FONT``. :param name_scale: The size scale of the name string. Defaults to 1. :param name_string: The third string of the badge - change to be your name. Defaults to "Blinka". """ splash = displayio.Group(max_size=20) color_bitmap = displayio.Bitmap(self.display.width, self.display.height, 1) color_palette = displayio.Palette(1) color_palette[0] = background_color bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) rect = Rect(0, (int(self.display.height * 0.4)), self.display.width, (int(self.display.height * 0.5)), fill=foreground_color) splash.append(rect) hello_scale = hello_scale hello_group = displayio.Group(scale=hello_scale) hello_label = Label(font=hello_font, text=hello_string) (_, _, width, height) = hello_label.bounding_box hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2) hello_label.y = int(height // (1.2 * hello_scale)) hello_label.color = background_text_color hello_group.append(hello_label) my_name_is_scale = my_name_is_scale my_name_is_group = displayio.Group(scale=my_name_is_scale) my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string) (_, _, width, height) = my_name_is_label.bounding_box my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2) my_name_is_label.y = int(height // (0.42 * my_name_is_scale)) my_name_is_label.color = background_text_color my_name_is_group.append(my_name_is_label) name_scale = name_scale name_group = displayio.Group(scale=name_scale) name_label = Label(font=name_font, text=name_string) (_, _, width, height) = name_label.bounding_box name_label.x = ((self.display.width // (2 * name_scale)) - width // 2) name_label.y = int(height // (0.17 * name_scale)) name_label.color = foreground_text_color name_group.append(name_label) group = displayio.Group() group.append(splash) group.append(hello_group) group.append(my_name_is_group) group.append(name_group) self.display.show(group)
#----------------------------- Environment Variables #-- EPaper Handling last_refresh_time = 0 min_refresh_rate = 10 display_loop_index = 1 #How often has been the display been updated bitmap_has_update = True #Should the display be updating display_updates_enabled = True # Create a bitmap with two colors # 4 colors are (0,0,0), (82,82,82), (163,163,163), (255,255,255) #number_of_colors = 2 bitmap = displayio.Bitmap(display.width, display.height, 4) # Create a two color palette palette = displayio.Palette(4) # palette[0] = 0x000000 # palette[1] = 0x666666 # palette[2] = 0x999999 # palette[3] = 0xFFFFFF palette[0] = 0x000000 palette[1] = 0x525252 palette[2] = 0xA3A3A3 palette[3] = 0xFFFFFF # Create a TileGrid using the Bitmap and Palette tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) # Create a Group group = displayio.Group() # Add the TileGrid to the Group
def createPalette(self, colour): palette = displayio.Palette(1) palette[0] = colour return palette
width=WIDTH, height=HEIGHT, rowstart=80, backlight_pin=tft_backlight, rotation=180) # Load background image try: bg_bitmap, bg_palette = adafruit_imageload.load(BACKGROUND, bitmap=displayio.Bitmap, palette=displayio.Palette) # Or just use solid color except (OSError, TypeError): BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x000000 bg_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1) bg_palette = displayio.Palette(1) bg_palette[0] = BACKGROUND background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette) # Snowflake setup flake_bitmap, flake_palette = adafruit_imageload.load( FLAKE_SHEET, bitmap=displayio.Bitmap, palette=displayio.Palette) if FLAKE_TRAN_COLOR is not None: for i, color in enumerate(flake_palette): if color == FLAKE_TRAN_COLOR: flake_palette.make_transparent(i) break NUM_SPRITES = flake_bitmap.width // FLAKE_WIDTH * flake_bitmap.height // FLAKE_HEIGHT flake_pos = [0.0] * NUM_FLAKES flakes = displayio.Group(max_size=NUM_FLAKES) for _ in range(NUM_FLAKES):
def __init__(self, width=100, height=18, output=False, value=None, value_range=65536, vref=3.3, labels=False, font=None, line_width=2, tick_width=1, line_color=None, scale_color=None, bg_color=None): ### pylint: disable=too-many-locals self._palette = displayio.Palette(3) if bg_color is None: self._palette.make_transparent(0) else: self._palette[0] = bg_color if line_color is None: self._palette[ self._LINE_COL_IDX] = WRITE_COLOR if output else READ_COLOR else: self._palette[self._LINE_COL_IDX] = line_color if scale_color is None: self._palette[ self. _SCALE_COL_IDX] = WRITE_COLOR_DIM if output else READ_COLOR_DIM else: self._palette[self._SCALE_COL_IDX] = scale_color self._line_scfactor = width - line_width self._scale_scfactor = width - tick_width self._value_range = value_range self._labels = labels self._font = font label_height = font.get_bounding_box()[1] if labels and font else 0 scale_height = 5 self._bargraph_line_dob = self._makeLine( line_width, height - scale_height - label_height - 1, self._LINE_COL_IDX) self._bargraph_scale_dob = self._makeScale(vref, tick_width, width, scale_height + label_height, self._SCALE_COL_IDX, label_height=label_height, font=font) self._bargraph_scale_dob.y = height - scale_height - label_height self._group = displayio.Group(max_size=2) self._group.append(self._bargraph_line_dob) self._group.append(self._bargraph_scale_dob) self._vref = vref self._output = output self._value = None if value is not None: self.value = value
# Create the ST7789 display: display = adafruit_st7789.ST7789( display_bus, width=240, height=240, rowstart=80, colstart=0, rotation=180, ) group = displayio.Group(max_size=10) display.show(group) bitmap = displayio.Bitmap(240, 240, 240) palette = displayio.Palette(240) for p in range(240): palette[p] = (0x010000 * p) + (0x0100 * p) + p for y in range(240): for x in range(240): bitmap[x, y] = y tileGrid = displayio.TileGrid(bitmap, pixel_shader=palette, x=0, y=0) group.append(tileGrid) # Draw a label text_group = displayio.Group(max_size=10, scale=16, x=10, y=120) text = "??" text_area = label.Label(terminalio.FONT, text=text, color=0xFF0000) text_group.append(text_area) # Subgroup for text scaling
def mandelbrot(WIDTH, HEIGHT, N=256, group=None, CENTER=(-0.5, 0), DIAMX=2.3): """ mandelbrot() return a group with drawing of mandelbrot fractal in it. Parameters : WIDTH : width of the bitmap HEIGHT : height of the bitmap N : number of iterations, and number of colors (default 256) group : reuse a group instead of creating a new one CENTER : tuple containing the center coordinates DIAMX : "zoom" level, from 2.3 to 0.000027 """ DIAMY = DIAMX*HEIGHT/WIDTH XMIN=CENTER[0]-DIAMX/2 XMAX=CENTER[0]+DIAMX/2 YMIN=CENTER[1]-DIAMY/2 YMAX=CENTER[1]+DIAMY/2 dx=(XMAX-XMIN)/WIDTH dy=(YMAX-YMIN)/HEIGHT if group is None: group = displayio.Group() elif group: group.pop() bitmap = displayio.Bitmap(WIDTH, HEIGHT, N) def ColorMap(p): sr = sg = sb = 0 if (p < 64): sr=0 ; sg=p*4 ; sb=255 elif (p < 128): sr=(p-64)*4 ; sg=255 ; sb=(255-(p-64)*4) elif (p < 192): sr=255 ; sg=255 ;sb = (p-128)*4 elif (p < 256): sr=255 ; sg=(255-(p-191)*4) ; sb= (255-(p-191)*4) return (sr,sg,sb) palette = displayio.Palette(N) for i in range(N): r=255*i//(N-1) s=ColorMap(r) palette[i]=s[0]*2**16+s[1]*2**8+s[2] palette[0]=0 palette[1]=0xffffff tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) group.append(tile_grid) ############################# # Mandelbrot Drawing # ############################# def mandelbrot_core(creal,cimag,maxiter): real = creal imag = cimag for n in range(maxiter): real2 = real*real imag2 = imag*imag if real2 + imag2 > 4.0: return n imag = 2* real*imag + cimag real = real2 - imag2 + creal return -1 x=XMIN print("Start drawing mandelbrot fractal :", CENTER, DIAMX, N) for xp in range(WIDTH): y=YMIN for yp in range(HEIGHT): bitmap[xp,yp]=mandelbrot_core(x,y,N-1)+1 y += dy x += dx #display2.refresh_soon() return group
def quasicrystal_opt(width, height, N=256, group=None, f=None, p=None, n=None): """ # Quasicrystal Pattern Generator # https://code.activestate.com/recipes/579094-quasicrystal-pattern-generator/ # https://en.wikipedia.org/wiki/Quasicrystal # http://mainisusuallyafunction.blogspot.com/2011/10/quasicrystals-as-sums-of-waves-in-plane.html # N : number of colors (default : 256) group : displayio.Group object. f : frequency, int [5-30] (default : random) p : phase, float [0-pi] (default : random) n : rotations, int [5-20] (default: random) (range indicate random range choice) """ init = 0 init_calc = 0 time_row = 0 inner = 0 draw = 0 start = time.monotonic() if group is None: group = displayio.Group() elif group: group.pop() gc.collect() pixels = displayio.Bitmap(width, height, N) palette = displayio.Palette(N) # corral colors def ColorMap(p): sr = sg = sb = 0 if (p < 64): sr=0 ; sg=p*4 ; sb=255 elif (p < 128): sr=(p-64)*4 ; sg=255 ; sb=(255-(p-64)*4) elif (p < 192): sr=255 ; sg=255 ;sb = (p-128)*4 elif (p < 256): sr=255 ; sg=(255-(p-191)*4) ; sb= (255-(p-191)*4) return (sr,sg,sb) for i in range(N): r=255*i//(N-1) s=ColorMap(r) palette[i]=s[0]*2**16+s[1]*2**8+s[2] # blue to yellow #for i in range(N): # j = int((i/N)*255) # palette[i]=j*2**16+j*2**8+128-j//2 #palette[0] = 0x000088 # grayscale #for i in range(N): #j = 128+int(i*(128/N)) # j = int((i/N)*255) # palette[i]=j*2**16+j*2**8+j #palette[0] = 0 tile_grid = displayio.TileGrid(pixels, pixel_shader=palette) group.append(tile_grid) if f is None: f = random.random() * 27 + 8 # frequency if p is None: p = random.random() * math.pi # phase if n is None: n = random.randint(7, 15) # of rotations init += time.monotonic() - start # intermediary calculus start = time.monotonic() fp = f + p rot = 3.14 * 2.0 / n Nn = (N - 1) / n #row = ulab.array([i for i in range(width)]) row = ulab.linspace(-6.28, 0, num=width//2) col = ulab.linspace(-6.28, 0, num=height//2) #x2 = row * row #y2 = col * col ky = 0 init_calc += time.monotonic() - start for y in col: s_row = time.monotonic() #y = ky * h4pi - 6.28 #y2 = y**2 atan = ulab.vector.arctan2( col[ky], row ) #atan = ulab.vector.atan( row / y ) dist = ulab.vector.sqrt((row * row) + (y**2)) * fp #dist = dist * fp z = ulab.zeros(width//2) s_inner = time.monotonic() for i in range(n): z = z + ulab.vector.cos(ulab.vector.sin(atan + (i * rot)) * dist) inner += time.monotonic() - s_inner z = abs(z * Nn) time_row += time.monotonic() - s_row s_draw = time.monotonic() for i, c in enumerate(z): pixels[ky*width+i] = int(c) pixels[ky*width+(width-1 -i)] = int(c) pixels[(height-1-ky)*width+i] = int(c) pixels[(height-1-ky)*width+(width-1 -i)] = int(c) draw += time.monotonic() - s_draw ky += 1 #gc.collect() #gc.collect() print("time report :") print(" init :", init) print(" init calc :", init_calc) print(" rows :", time_row-inner) print(" inner loop:", inner) print(" draw :", draw) return group
def quasicrystal(width, height, N=256, group=None, f=None, p=None, n=None): """ # Quasicrystal Pattern Generator # https://code.activestate.com/recipes/579094-quasicrystal-pattern-generator/ # https://en.wikipedia.org/wiki/Quasicrystal # http://mainisusuallyafunction.blogspot.com/2011/10/quasicrystals-as-sums-of-waves-in-plane.html # N : number of colors (default : 256) group : displayio.Group object. f : frequency, int [5-30] (default : random) p : phase, float [0-pi] (default : random) n : rotations, int [5-20] (default: random) (range indicate random range choice) """ if group is None: group = displayio.Group() elif group: group.pop() gc.collect() pixels = displayio.Bitmap(width, height, N) palette = displayio.Palette(N) def ColorMap(p): sr = sg = sb = 0 if (p < 64): sr=0 ; sg=p*4 ; sb=255 elif (p < 128): sr=(p-64)*4 ; sg=255 ; sb=(255-(p-64)*4) elif (p < 192): sr=255 ; sg=255 ;sb = (p-128)*4 elif (p < 256): sr=255 ; sg=(255-(p-191)*4) ; sb= (255-(p-191)*4) return (sr,sg,sb) for i in range(N): r=255*i//(N-1) s=ColorMap(r) palette[i]=s[0]*2**16+s[1]*2**8+s[2] # blue to yellow #for i in range(N): # j = int((i/N)*255) # palette[i]=j*2**16+j*2**8+128-j//2 #palette[0] = 0x000088 # grayscale #for i in range(N): #j = 128+int(i*(128/N)) # j = int((i/N)*255) # palette[i]=j*2**16+j*2**8+j #palette[0] = 0 tile_grid = displayio.TileGrid(pixels, pixel_shader=palette) group.append(tile_grid) if f is None: f = random.random() * 27 + 8 # frequency if p is None: p = random.random() * math.pi # phase if n is None: n = random.randint(7, 15) # of rotations # intermediary calculus fp = f + p rot = 3.14 * 2.0 / n Nn = (N - 1) / n h4pi = 12.56 / (height - 1) w4pi = 12.56 / (width - 1) row = ulab.array([i for i in range(width)]) row = (row * w4pi) - 6.28 for ky in range(height): y = ky * h4pi - 6.28 y2 = y**2 atan = ulab.vector.atan( row / y ) dist = ulab.vector.sqrt((row * row) + y2) * fp #dist = dist * fp z = ulab.zeros(width) for i in range(n): z = z + ulab.vector.cos(ulab.vector.sin(atan + (i * rot)) * dist) z = abs(z * Nn) for i, c in enumerate(z): pixels[ky*width+i] = int(c) gc.collect() return group
def mandelbrot_ulab(WIDTH, HEIGHT, N=256, group=None, CENTER=(-0.5, 0), DIAMX=2.3): """ mandelbrot() return a group with drawing of mandelbrot fractal in it. Parameters : WIDTH : width of the bitmap HEIGHT : height of the bitmap N : number of iterations, and number of colors (default 256) group : reuse a group instead of creating a new one CENTER : tuple containing the center coordinates DIAMX : "zoom" level, from 2.3 to 0.000027 """ DIAMY = DIAMX*HEIGHT/WIDTH XMIN=CENTER[0]-DIAMX/2 XMAX=CENTER[0]+DIAMX/2 YMIN=CENTER[1]-DIAMY/2 YMAX=CENTER[1]+DIAMY/2 r1 = ulab.linspace(XMIN, XMAX, num=WIDTH) r2 = ulab.linspace(YMIN, YMAX, num=HEIGHT) if group is None: group = displayio.Group() elif group: group.pop() bitmap = displayio.Bitmap(WIDTH, HEIGHT, N) def ColorMap(p): sr = sg = sb = 0 if (p < 64): sr=0 ; sg=p*4 ; sb=255 elif (p < 128): sr=(p-64)*4 ; sg=255 ; sb=(255-(p-64)*4) elif (p < 192): sr=255 ; sg=255 ;sb = (p-128)*4 elif (p < 256): sr=255 ; sg=(255-(p-191)*4) ; sb= (255-(p-191)*4) return (sr,sg,sb) palette = displayio.Palette(N) for i in range(N): r=255*i//(N-1) s=ColorMap(r) palette[i]=s[0]*2**16+s[1]*2**8+s[2] palette[0]=0 palette[1]=0xffffff tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) group.append(tile_grid) ############################# # Mandelbrot Drawing # ############################# before = 0 loop = 0 after = 0 print("Start drawing mandelbrot fractal :", CENTER, DIAMX, N) start = time.monotonic() for xp in range(WIDTH): s_before = time.monotonic() col_z_imag = ulab.array(r2) x = ulab.ones(HEIGHT)*r1[xp] x2 = x * x z2 = col_z_imag * col_z_imag output = ulab.zeros(HEIGHT, dtype=ulab.int16) notdone = [True]*HEIGHT before += time.monotonic() - s_before s_loop = time.monotonic() for i in range(N): z2 = col_z_imag * col_z_imag x2 = x * x try: output[(x2 + z2 ) < 4] = i except IndexError: break #col_z_imag[notdone] = ( ( x * col_z_imag )[notdone] * 2 ) + r2[notdone] col_z_imag = ( ( x * col_z_imag ) * 2 ) + r2 #x[notdone] = (x2 - z2)[notdone] + r1[xp] x = (x2 - z2) + r1[xp] loop += time.monotonic() - s_loop s_after = time.monotonic() limit = output >= N-1 if limit.count(True) > 0: output[limit] = -1 output = output + 1 for yp in range(HEIGHT): bitmap[xp,yp]= output[yp] after += time.monotonic() - s_after print("before :", before) print("loop :", loop) print("after :", after) print("total :", time.monotonic() - start) return group
def __init__( self, font, x=0, y=0, text="", max_glyphs=None, # This input parameter is ignored, only present for compatibility # with label.py color=0xFFFFFF, background_color=None, line_spacing=1.25, background_tight=False, padding_top=0, padding_bottom=0, padding_left=0, padding_right=0, anchor_point=None, anchored_position=None, save_text=True, # can reduce memory use if save_text = False scale=1, **kwargs, ): # instance the Group # self Group will contain a single local_group which contains a Group (self.local_group) # which contains a TileGrid (self.tilegrid) which contains the text bitmap (self.bitmap) super().__init__( max_size=1, x=x, y=y, scale=1, **kwargs, ) # the self group scale should always remain at 1, the self.local_group will # be used to set the scale # **kwargs will pass any additional arguments provided to the Label self.local_group = displayio.Group( max_size=1, scale=scale ) # local_group holds the tileGrid and sets the scaling self.append( self.local_group ) # the local_group will always stay in the self Group self._font = font self._text = text # Create the two-color palette self.palette = displayio.Palette(2) self.color = color self.background_color = background_color self._anchor_point = anchor_point self._anchored_position = anchored_position # call the text updater with all the arguments. self._reset_text( font=font, x=x, y=y, text=text, line_spacing=line_spacing, background_tight=background_tight, padding_top=padding_top, padding_bottom=padding_bottom, padding_left=padding_left, padding_right=padding_right, anchor_point=anchor_point, anchored_position=anchored_position, save_text=save_text, scale=scale, )
clock=board.CAMERA_PCLK, vsync=board.CAMERA_VSYNC, href=board.CAMERA_HREF, mclk=board.CAMERA_XCLK, mclk_frequency=20_000_000, size=adafruit_ov2640.OV2640_SIZE_QQVGA, ) cam.flip_x = False cam.flip_y = False cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV qrdecoder = qrio.QRDecoder(cam.width, cam.height) bitmap = displayio.Bitmap(cam.width, cam.height, 65536) # Create a greyscale palette pal = displayio.Palette(256) for i in range(256): pal[i] = 0x10101 * i label = Label( font=FONT, text="Scan QR Code...", color=0xFFFFFF, background_color=0x0, padding_top=2, padding_left=2, padding_right=2, padding_bottom=2, anchor_point=(0.5, 1.0), anchored_position=(160, 230), )
def __init__( self, font, *, x=0, y=0, text="", max_glyphs=None, color=0xFFFFFF, background_color=None, line_spacing=1.25, background_tight=False, padding_top=0, padding_bottom=0, padding_left=0, padding_right=0, anchor_point=None, anchored_position=None, scale=1, **kwargs ): if not max_glyphs and not text: raise RuntimeError("Please provide a max size, or initial text") if not max_glyphs: max_glyphs = len(text) # add one to max_size for the background bitmap tileGrid # instance the Group # self Group will contain a single local_group which contains a Group (self.local_group) # which contains a TileGrid super().__init__( max_size=1, scale=1, **kwargs ) # The self scale should always be 1 self.local_group = displayio.Group( max_size=max_glyphs + 1, scale=scale ) # local_group will set the scale self.append(self.local_group) self.width = max_glyphs self._font = font self._text = None self._anchor_point = anchor_point self.x = x self.y = y self.height = self._font.get_bounding_box()[1] self._line_spacing = line_spacing self._boundingbox = None self._background_tight = ( background_tight # sets padding status for text background box ) # Create the two-color text palette self.palette = displayio.Palette(2) self.palette[0] = 0 self.palette.make_transparent(0) self.color = color self._background_color = background_color self._background_palette = displayio.Palette(1) self._added_background_tilegrid = False self._padding_top = padding_top self._padding_bottom = padding_bottom self._padding_left = padding_left self._padding_right = padding_right self._scale = scale if text is not None: self._update_text(str(text)) if (anchored_position is not None) and (anchor_point is not None): self.anchored_position = anchored_position
def __init__( self, background_color: int = 0x000000, xrange: Tuple[int, int] = (0, 100), yrange: Tuple[int, int] = (0, 100), axes_color: int = 0xFFFFFF, axes_stroke: int = 1, tick_color: int = 0xFFFFFF, major_tick_stroke: int = 1, major_tick_length: int = 5, tick_label_font=terminalio.FONT, font_color: int = 0xFFFFFF, pointer_radius: int = 1, pointer_color: int = 0xFFFFFF, subticks: bool = False, nudge_x: int = 0, nudge_y: int = 0, **kwargs, ) -> None: super().__init__(**kwargs, max_size=3) self._background_color = background_color self._axes_line_color = axes_color self._axes_line_thickness = axes_stroke self._tick_color = tick_color if major_tick_stroke not in range(1, 5): print("tick thickness must be 1-4 pixels. Defaulting to 1") self._tick_line_thickness = 1 else: self._tick_line_thickness = major_tick_stroke if major_tick_length not in range(1, 9): print("tick length must be 1-10 pixels. Defaulting to 5") self._tick_line_height = 5 else: self._tick_line_height = major_tick_length self._pointer_radius = pointer_radius self._pointer_color = pointer_color self._font = tick_label_font self._font_color = font_color self._font_width = self._get_font_height(self._font, 1)[0] self._font_height = self._get_font_height(self._font, 1)[1] self._xrange = xrange self._normx = (self._xrange[1] - self._xrange[0]) / 100 self._valuex = self.width / 100 self._factorx = 100 / (self._xrange[1] - self._xrange[0]) self._yrange = yrange self._normy = (self._yrange[1] - self._yrange[0]) / 100 self._valuey = self.height / 100 self._factory = 100 / (self._yrange[1] - self._yrange[0]) self._tick_bitmap = displayio.Bitmap( self._tick_line_thickness, self._tick_line_height, 3 ) self._tick_bitmap.fill(1) self._subticks = subticks axesx_height = ( 2 + self._axes_line_thickness + self._font_height + self._tick_line_height // 2 ) self._axesx_bitmap = displayio.Bitmap(self.width, axesx_height, 4) self._axesx_bitmap.fill(0) self._axesy_width = ( 2 + self._axes_line_thickness + self._font_width + self._tick_line_height // 2 ) self._axesy_bitmap = displayio.Bitmap(self._axesy_width, self.height, 4) self._axesy_bitmap.fill(0) self._screen_bitmap = displayio.Bitmap(self.width, self.height, 5) self._screen_bitmap.fill(5) self._screen_palette = displayio.Palette(6) self._screen_palette.make_transparent(0) self._screen_palette[1] = self._tick_color self._screen_palette[2] = self._axes_line_color self._screen_palette[3] = 0x990099 self._screen_palette[4] = 0xFFFFFF self._screen_palette[5] = self._background_color self._corner_bitmap = displayio.Bitmap(10, 10, 5) rectangle_helper( 0, 0, self._axes_line_thickness, self._axes_line_thickness, self._corner_bitmap, 2, self._screen_palette, ) self._corner_tilegrid = displayio.TileGrid( self._corner_bitmap, pixel_shader=self._screen_palette, x=-self._axes_line_thickness, y=self.height, ) self._axesx_tilegrid = displayio.TileGrid( self._axesx_bitmap, pixel_shader=self._screen_palette, x=0, y=self.height, ) self._axesy_tilegrid = displayio.TileGrid( self._axesy_bitmap, pixel_shader=self._screen_palette, x=-self._axesy_width, y=0, ) self._screen_tilegrid = displayio.TileGrid( self._screen_bitmap, pixel_shader=self._screen_palette, x=0, y=0, ) self._nudge_x = nudge_x self._nudge_y = nudge_y self._draw_axes() self._draw_ticks() self.append(self._axesx_tilegrid) self.append(self._axesy_tilegrid) self.append(self._screen_tilegrid) self.append(self._corner_tilegrid) self._update_line = True self._pointer = None self._circle_palette = None self._pointer_vector_shape = None self.plot_line_point = None
import time import board import busio import adafruit_mlx90640 import displayio import terminalio from adafruit_display_text.label import Label from simpleio import map_range number_of_colors = 64 # Number of color in the gradian last_color = number_of_colors - 1 # Last color in palette palette = displayio.Palette(number_of_colors) # Palette with all our colors ## Heatmap code inspired from: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients color_A = [ [0, 0, 0], [0, 0, 255], [0, 255, 255], [0, 255, 0], [255, 255, 0], [255, 0, 0], [255, 255, 255], ] color_B = [[0, 0, 255], [0, 255, 255], [0, 255, 0], [255, 255, 0], [255, 0, 0]] color_C = [[0, 0, 0], [255, 255, 255]] color_D = [[0, 0, 255], [255, 0, 0]] color = color_B NUM_COLORS = len(color)
import busio from cytron_edubit import Edubit from meters import PeakMeter ### Need to create i2c bus at 100kHz until CircuitPython lowers the default ### https://github.com/adafruit/Adafruit_CircuitPython_CLUE/issues/36 edubit = Edubit(i2c=busio.I2C(board.SCL, board.SDA, frequency=100 * 1000)) peak_meter = PeakMeter(edubit.pixels) display = board.DISPLAY # Create a heatmap color palette PALETTE_LEN = 52 palette = displayio.Palette(PALETTE_LEN) for idx, color in enumerate( (0xff0000, 0xff0a00, 0xff1400, 0xff1e00, 0xff2800, 0xff3200, 0xff3c00, 0xff4600, 0xff5000, 0xff5a00, 0xff6400, 0xff6e00, 0xff7800, 0xff8200, 0xff8c00, 0xff9600, 0xffa000, 0xffaa00, 0xffb400, 0xffbe00, 0xffc800, 0xffd200, 0xffdc00, 0xffe600, 0xfff000, 0xfffa00, 0xfdff00, 0xd7ff00, 0xb0ff00, 0x8aff00, 0x65ff00, 0x3eff00, 0x17ff00, 0x00ff10, 0x00ff36, 0x00ff5c, 0x00ff83, 0x00ffa8, 0x00ffd0, 0x00fff4, 0x00a4ff, 0x0094ff, 0x0084ff, 0x0074ff, 0x0064ff, 0x0054ff, 0x0044ff, 0x0032ff, 0x0022ff, 0x0012ff, 0x0002ff, 0x0000ff)): palette[PALETTE_LEN - 1 - idx] = color class RollingGraph(displayio.TileGrid): def __init__(self, scale=2): # Create a bitmap with heatmap colors
) # set the text anchored position to the upper right of the graph text_label1b = label.Label(font=font, text=str(sparkline1.y_bottom), color=0xFFFFFF) # y_bottom label text_label1b.anchor_point = (0, 0.5) # set the anchorpoint text_label1b.anchored_position = ( 10 + chart_width, 10 + chart_height, ) # set the text anchored position to the upper right of the graph # Setup the second bitmap and sparkline # sparkline2 uses a vertical y range between 0 to 1, and will contain a # maximum of 10 items # palette2 = displayio.Palette(1) # color palette used for bitmap2 (one color) palette2[0] = 0x0000FF bitmap2 = displayio.Bitmap(chart_width * 2, chart_height * 2, 1) # create bitmap2 tilegrid2 = displayio.TileGrid(bitmap2, pixel_shader=palette2, x=150, y=10) # Add bitmap2 to tilegrid2 sparkline2 = Sparkline( width=chart_width * 2, height=chart_height * 2, max_items=10, y_min=0, y_max=1, x=150, y=10, color=0xFF00FF,
def __init__(self, display=board.DISPLAY): self._logger = logging.getLogger("Paint") self._logger.setLevel(logging.DEBUG) self._display = display self._w = self._display.width self._h = self._display.height self._x = self._w // 2 self._y = self._h // 2 self._splash = displayio.Group(max_size=5) self._bg_bitmap = displayio.Bitmap(self._w, self._h, 1) self._bg_palette = displayio.Palette(1) self._bg_palette[0] = Color.BLACK self._bg_sprite = displayio.TileGrid(self._bg_bitmap, pixel_shader=self._bg_palette, x=0, y=0) self._splash.append(self._bg_sprite) self._palette_bitmap = displayio.Bitmap(self._w, self._h, 5) self._palette_palette = displayio.Palette(len(Color.colors)) for i, c in enumerate(Color.colors): self._palette_palette[i] = c self._palette_sprite = displayio.TileGrid( self._palette_bitmap, pixel_shader=self._palette_palette, x=0, y=0) self._splash.append(self._palette_sprite) self._fg_bitmap = displayio.Bitmap(self._w, self._h, 5) self._fg_palette = displayio.Palette(len(Color.colors)) for i, c in enumerate(Color.colors): self._fg_palette[i] = c self._fg_sprite = displayio.TileGrid(self._fg_bitmap, pixel_shader=self._fg_palette, x=0, y=0) self._splash.append(self._fg_sprite) self._number_of_palette_options = len(Color.colors) + 2 self._swatch_height = self._h // self._number_of_palette_options self._swatch_width = self._w // 10 self._logger.debug('Height: %d', self._h) self._logger.debug('Swatch height: %d', self._swatch_height) self._palette = self._make_palette() self._splash.append(self._palette) self._display.show(self._splash) self._display.refresh_soon() gc.collect() self._display.wait_for_frame() self._brush = 0 self._cursor_bitmaps = [ self._cursor_bitmap_1(), self._cursor_bitmap_3() ] if hasattr(board, 'TOUCH_XL'): self._poller = TouchscreenPoller(self._splash, self._cursor_bitmaps[0]) elif hasattr(board, 'BUTTON_CLOCK'): self._poller = CursorPoller(self._splash, self._cursor_bitmaps[0]) else: raise AttributeError('PyPaint requires a touchscreen or cursor.') self._a_pressed = False self._last_a_pressed = False self._location = None self._last_location = None self._pencolor = 7
except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise print(" Metro Minimal Clock") print("Time will be set for {}".format(secrets["timezone"])) # --- Display setup --- matrix = Matrix() display = matrix.display network = Network(status_neopixel=board.NEOPIXEL, debug=False) # --- Drawing setup --- group = displayio.Group() # Create a Group bitmap = displayio.Bitmap(64, 32, 2) # Create a bitmap object,width, height, bit depth color = displayio.Palette(4) # Create a color palette color[0] = 0x000000 # black background color[1] = 0xFF0000 # red color[2] = 0xCC4000 # amber color[3] = 0x85FF00 # greenish # Create a TileGrid using the Bitmap and Palette tile_grid = displayio.TileGrid(bitmap, pixel_shader=color) group.append(tile_grid) # Add the TileGrid to the Group display.show(group) if not DEBUG: font = bitmap_font.load_font("/IBMPlexMono-Medium-24_jep.bdf") else: font = terminalio.FONT
def set_background(self, file_or_color, position=None): """The background image to a bitmap file. :param file_or_color: The filename of the chosen background image, or a hex color. """ print("Set background to ", file_or_color) while self._bg_group: self._bg_group.pop() if not position: position = (0, 0) # default in top corner if not file_or_color: return # we're done, no background desired if self._bg_file: self._bg_file.close() if isinstance(file_or_color, str): # its a filenme: self._bg_file = open(file_or_color, "rb") background = displayio.OnDiskBitmap(self._bg_file) try: self._bg_sprite = displayio.TileGrid( background, pixel_shader=displayio.ColorConverter(), position=position, ) except TypeError: self._bg_sprite = displayio.TileGrid( background, pixel_shader=displayio.ColorConverter(), x=position[0], y=position[1], ) elif isinstance(file_or_color, int): # Make a background color fill color_bitmap = displayio.Bitmap( board.DISPLAY.width, board.DISPLAY.height, 1 ) color_palette = displayio.Palette(1) color_palette[0] = file_or_color try: self._bg_sprite = displayio.TileGrid( color_bitmap, pixel_shader=color_palette, position=(0, 0) ) except TypeError: self._bg_sprite = displayio.TileGrid( color_bitmap, pixel_shader=color_palette, x=position[0], y=position[1], ) else: raise RuntimeError("Unknown type of background") self._bg_group.append(self._bg_sprite) try: board.DISPLAY.refresh(target_frames_per_second=60) gc.collect() except AttributeError: board.DISPLAY.refresh_soon() gc.collect() board.DISPLAY.wait_for_frame()
text="calib: ((x_min, x_max), (y_min, y_max))", color=WHITE, ) coordinates.anchor_point = (0.5, 0.5) coordinates.anchored_position = (display.width // 2, display.height // 4) display_rotation = Label( font=font_0, text="rotation: " + str(display.rotation), color=WHITE, ) display_rotation.anchor_point = (0.5, 0.5) display_rotation.anchored_position = (display.width // 2, display.height // 4 - 30) # Define graphic objects for the screen fill, boundary, and touch pen. target_palette = displayio.Palette(1) target_palette[0] = BLUE_DK screen_fill = vectorio.Rectangle( pixel_shader=target_palette, x=2, y=2, width=display.width - 4, height=display.height - 4, ) target_palette = displayio.Palette(1) target_palette[0] = RED boundary = vectorio.Rectangle( pixel_shader=target_palette, x=0, y=0,
print("===========================================") # GFX Font font = terminalio.FONT # Initialize new PyPortal object pyportal = PyPortal(esp=esp, external_spi=spi) # Root DisplayIO root_group = displayio.Group(max_size=100) display.show(root_group) BACKGROUND = BACKGROUND if isinstance(BACKGROUND, int) else 0x0 bg_bitmap = displayio.Bitmap(display.width, display.height, 1) bg_palette = displayio.Palette(1) bg_palette[0] = BACKGROUND background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette) # Create a new DisplayIO group splash = displayio.Group(max_size=15) splash.append(background) key_group = displayio.Group(scale=5) # We'll use a default text placeholder for this label label_secret = Label(font, text="000 000") label_secret.x = (display.width // 2) // 13 label_secret.y = 17 key_group.append(label_secret)
def __init__(self, display, layout_json): self.json = layout_json if "view_type" not in layout_json: raise MissingTypeError if layout_json["view_type"] != "Image": raise IncorrectTypeError( "view_type '{}' does not match Layout Class 'Image'".format( layout_json["view_type"])) self._display = display if "attributes" in layout_json: _missing_attrs = [] for attribute in REQUIRED_ATTRIBUTES: if attribute not in layout_json: _missing_attrs.append(attribute) if len(_missing_attrs) > 0: raise MissingRequiredAttributesError( "Missing required attributes: {}".format(_missing_attrs)) _image_filepath = None if "image_file" in layout_json["attributes"]: _image_filepath = layout_json["attributes"]["image_file"] _background_color = None if "background_color" in layout_json["attributes"]: _background_color = int( layout_json["attributes"]["background_color"], 16) _padding_top = 0 if "padding_top" in layout_json["attributes"]: _padding_top = int(layout_json["attributes"]["padding_top"]) _padding_right = 0 if "padding_right" in layout_json["attributes"]: _padding_right = int( layout_json["attributes"]["padding_right"]) _padding_left = 0 if "padding_left" in layout_json["attributes"]: _padding_left = int(layout_json["attributes"]["padding_left"]) _padding_bottom = 0 if "padding_bottom" in layout_json["attributes"]: _padding_bottom = int( layout_json["attributes"]["padding_bottom"]) _padding = 0 if "padding" in layout_json["attributes"]: _padding = int(layout_json["attributes"]["padding"]) image, palette = adafruit_imageload.load(_image_filepath, bitmap=displayio.Bitmap, palette=displayio.Palette) img_tile_grid = displayio.TileGrid(image, pixel_shader=palette) group = displayio.Group() img_tile_grid.x = _padding // 2 img_tile_grid.y = _padding // 2 _width = image.width _height = image.height self.width = _width self.height = _height if _padding and _background_color: # Draw a green background bg_bitmap = displayio.Bitmap(image.width + _padding, image.height + _padding, 1) bg_palette = displayio.Palette(1) bg_palette[0] = _background_color _width = bg_bitmap.width _height = bg_bitmap.height bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0) group.append(bg_sprite) _x = 0 if "x" in layout_json["attributes"]: _x = self.keyword_compiler(layout_json["attributes"]["x"], { "WIDTH": _width, "HEIGHT": _height }) _y = 0 if "y" in layout_json["attributes"]: _y = self.keyword_compiler(layout_json["attributes"]["y"], { "WIDTH": _width, "HEIGHT": _height }) group.x = _x group.y = _y group.append(img_tile_grid) self.image = group if "anchor_point" in layout_json["attributes"]: point = layout_json["attributes"]["anchor_point"] self.image.anchor_point = (point[0], point[1]) if "anchored_position" in layout_json["attributes"]: pos = layout_json["attributes"]["anchored_position"] self.image.anchored_position = (self.keyword_compiler(pos[0]), self.keyword_compiler(pos[1])) self.view = self.image else: #default attributes pass
displayio.release_displays() spi = board.SPI() tft_cs = board.D9 tft_dc = board.D10 display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) display = HX8357(display_bus, width=480, height=320) # Make the display context splash = displayio.Group() display.show(splash) color_bitmap = displayio.Bitmap(480, 320, 1) color_palette = displayio.Palette(1) color_palette[0] = 0x00FF00 # Bright Green bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) # Draw a smaller inner rectangle inner_bitmap = displayio.Bitmap(440, 280, 1) inner_palette = displayio.Palette(1) inner_palette[0] = 0xAA0088 # Purple inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=20,
def __init__(self, display=None): if display: self._display = display else: try: self._display = board.DISPLAY except AttributeError: raise RuntimeError( "No display available. One must be provided.") self._logger = logging.getLogger("Turtle") self._logger.setLevel(logging.INFO) self._w = self._display.width self._h = self._display.height self._x = self._w // 2 self._y = self._h // 2 self._speed = 6 self._heading = 90 self._logomode = False self._fullcircle = 360.0 self._degreesPerAU = 1.0 self._mode = "standard" self._angleOffset = 0 self._splash = displayio.Group(max_size=3) self._bg_bitmap = displayio.Bitmap(self._w, self._h, 1) self._bg_palette = displayio.Palette(1) self._bg_palette[0] = Color.BLACK self._bg_sprite = displayio.TileGrid(self._bg_bitmap, pixel_shader=self._bg_palette, x=0, y=0) self._splash.append(self._bg_sprite) self._fg_bitmap = displayio.Bitmap(self._w, self._h, 5) self._fg_palette = displayio.Palette(len(Color.colors) + 1) self._fg_palette.make_transparent(0) for i, c in enumerate(Color.colors): self._fg_palette[i + 1] = c self._fg_sprite = displayio.TileGrid(self._fg_bitmap, pixel_shader=self._fg_palette, x=0, y=0) self._splash.append(self._fg_sprite) self._turtle_bitmap = displayio.Bitmap(9, 9, 2) self._turtle_palette = displayio.Palette(2) self._turtle_palette.make_transparent(0) self._turtle_palette[1] = Color.WHITE for i in range(4): self._turtle_bitmap[4 - i, i] = 1 self._turtle_bitmap[i, 4 + i] = 1 self._turtle_bitmap[4 + i, 7 - i] = 1 self._turtle_bitmap[4 + i, i] = 1 self._turtle_sprite = displayio.TileGrid( self._turtle_bitmap, pixel_shader=self._turtle_palette, x=-100, y=-100) self._drawturtle() self._splash.append(self._turtle_sprite) self._penstate = False self._pencolor = None self._pensize = 1 self.pencolor(Color.WHITE) self._display.show(self._splash) gc.collect()