code.py
  1  # SPDX-FileCopyrightText: 2020 Jeff Epler for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # This example implements a rainbow colored scroller, in which each letter
  6  # has a different color. This is not possible with
  7  # Adafruit_Circuitpython_Display_Text, where each letter in a label has the
  8  # same color
  9  #
 10  # This demo also supports only ASCII characters and the built-in font.
 11  # See the simple_scroller example for one that supports alternative fonts
 12  # and characters, but only has a single color per label.
 13  
 14  import array
 15  
 16  from rainbowio import colorwheel
 17  import board
 18  import displayio
 19  import framebufferio
 20  import rgbmatrix
 21  import terminalio
 22  displayio.release_displays()
 23  
 24  matrix = rgbmatrix.RGBMatrix(
 25      width=64, height=32, bit_depth=3,
 26      rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
 27      addr_pins=[board.A5, board.A4, board.A3, board.A2],
 28      clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
 29  display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
 30  
 31  # Create a tilegrid with a bunch of common settings
 32  def tilegrid(palette):
 33      return displayio.TileGrid(
 34          bitmap=terminalio.FONT.bitmap, pixel_shader=palette,
 35          width=1, height=1, tile_width=6, tile_height=14, default_tile=32)
 36  
 37  g = displayio.Group()
 38  
 39  # We only use the built in font which we treat as being 7x14 pixels
 40  linelen = (64//7)+2
 41  
 42  # prepare the main groups
 43  l1 = displayio.Group()
 44  l2 = displayio.Group()
 45  g.append(l1)
 46  g.append(l2)
 47  display.show(g)
 48  
 49  l1.y = 1
 50  l2.y = 16
 51  
 52  # Prepare the palettes and the individual characters' tiles
 53  sh = [displayio.Palette(2) for _ in range(linelen)]
 54  tg1 = [tilegrid(shi) for shi in sh]
 55  tg2 = [tilegrid(shi) for shi in sh]
 56  
 57  # Prepare a fast map from byte values to
 58  charmap = array.array('b', [terminalio.FONT.get_glyph(32).tile_index]) * 256
 59  for ch in range(256):
 60      glyph = terminalio.FONT.get_glyph(ch)
 61      if glyph is not None:
 62          charmap[ch] = glyph.tile_index
 63  
 64  # Set the X coordinates of each character in label 1, and add it to its group
 65  for idx, gi in enumerate(tg1):
 66      gi.x = 7 * idx
 67      l1.append(gi)
 68  
 69  # Set the X coordinates of each character in label 2, and add it to its group
 70  for idx, gi in enumerate(tg2):
 71      gi.x = 7 * idx
 72      l2.append(gi)
 73  
 74  #  These pairs of lines should be the same length
 75  lines = [
 76      b"This scroller is brought to you by    CircuitPython & PROTOMATTER",
 77      b"        .... . .-.. .-.. --- / .--. .-. --- - --- -- .- - - . .-.",
 78      b"Greetz to ...          @PaintYourDragon      @v923z  @adafruit         ",
 79      b"  @danh        @ladyada  @kattni      @tannewt    all showers & tellers",
 80      b"New York Strong                       Wash Your Hands                  ",
 81      b"                  Flatten the curve                   Stronger Together",
 82  ]
 83  
 84  even_lines = lines[0::2]
 85  odd_lines = lines[1::2]
 86  
 87  # Scroll a top text and a bottom text
 88  def scroll(t, b):
 89      # Add spaces to the start and end of each label so that it goes from
 90      # the far right all the way off the left
 91      sp = b' ' * linelen
 92      t = sp + t + sp
 93      b = sp + b + sp
 94      maxlen = max(len(t), len(b))
 95      # For each whole character position...
 96      for i in range(maxlen-linelen):
 97          # Set the letter displayed at each position, and its color
 98          for j in range(linelen):
 99              sh[j][1] = colorwheel(3 * (2*i+j))
100              tg1[j][0] = charmap[t[i+j]]
101              tg2[j][0] = charmap[b[i+j]]
102          # And then for each pixel position, move the two labels
103          # and then refresh the display.
104          for j in range(7):
105              l1.x = -j
106              l2.x = -j
107              display.refresh(minimum_frames_per_second=0)
108  
109  # Repeatedly scroll all the pairs of lines
110  while True:
111      for e, o in zip(even_lines, odd_lines):
112          scroll(e, o)