<reponame>kellechu/gemma-m0-animations
import time
import adafruit_ht16k33.matrix
import board
import busio as io
import touchio

touch = touchio.TouchIn(board.D1)

i2c = io.I2C(board.SCL, board.SDA)
matrix = adafruit_ht16k33.matrix.Matrix8x8(i2c)

x_pix = y_pix = 8
matrix.fill(0)

xMask = [
  0x80,
  0x40,
  0x20,
  0x10,
  0x08,
  0x04,
  0x02,
  0x01,
]

f = [[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x6A,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xA2,
  0xB6,
  0x62,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xB6,
  0xA2,
  0x62,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xAA,
  0xA2,
  0xA2,
  0x62,
  0x3E
],[
  0x04,
  0x0F,
  0x3F,
  0x55,
  0x51,
  0x51,
  0x31,
  0x1F
],[
  0x02,
  0x07,
  0x1F,
  0x2A,
  0x28,
  0x28,
  0x1C,
  0x0F
],[
  0x01,
  0x03,
  0x0F,
  0x15,
  0x14,
  0x15,
  0x0C,
  0x07
],[
  0x00,
  0x01,
  0x07,
  0x0A,
  0x0B,
  0x0A,
  0x06,
  0x03
],[
  0x00,
  0x00,
  0x03,
  0x05,
  0x05,
  0x05,
  0x03,
  0x01
],[
  0x00,
  0x00,
  0x01,
  0x02,
  0x02,
  0x02,
  0x01,
  0x00
],[
  0x00,
  0x00,
  0x00,
  0x01,
  0x01,
  0x01,
  0x00,
  0x00
],[
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
],[
  0xFC,
  0xC2,
  0xC3,
  0xFF,
  0xC2,
  0xC3,
  0xFE,
  0xFC
],[
  0xF8,
  0x84,
  0x86,
  0xFE,
  0x84,
  0x86,
  0xFC,
  0xF8
],[
  0xF1,
  0x09,
  0x0D,
  0xFD,
  0x09,
  0x0D,
  0xF9,
  0xF1
],[
  0xE3,
  0x13,
  0x1B,
  0xFB,
  0x13,
  0x1B,
  0xF3,
  0xE3
],[
  0xC7,
  0x27,
  0x36,
  0xF7,
  0x26,
  0x36,
  0xE7,
  0xC7
],[
  0x8F,
  0x4F,
  0x6C,
  0xEF,
  0x4C,
  0x6C,
  0xCF,
  0x8F
],[
  0x1F,
  0x9F,
  0xD8,
  0xDE,
  0x98,
  0xD8,
  0x9F,
  0x1F
],[
  0x3E,
  0x3E,
  0xB0,
  0xBC,
  0x30,
  0xB0,
  0x3E,
  0x3E
],[
  0x7D,
  0x7D,
  0x61,
  0x79,
  0x61,
  0x61,
  0x7D,
  0x7D
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xF7,
  0xF7,
  0x86,
  0xE7,
  0x86,
  0x86,
  0xF7,
  0xF7
],[
  0xEF,
  0xEF,
  0x0C,
  0xCF,
  0x0C,
  0x0C,
  0xEF,
  0xEF
],[
  0xDF,
  0xDF,
  0x18,
  0x9E,
  0x18,
  0x18,
  0xDF,
  0xDF
],[
  0xBE,
  0xBE,
  0x30,
  0x3C,
  0x30,
  0x30,
  0xBE,
  0xBE
],[
  0x7D,
  0x7D,
  0x61,
  0x79,
  0x61,
  0x61,
  0x7D,
  0x7D
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xF7,
  0xF7,
  0x86,
  0xE6,
  0x87,
  0x86,
  0xF6,
  0xF6
],[
  0xEF,
  0xEF,
  0x0C,
  0xCC,
  0x0F,
  0x0C,
  0xEC,
  0xEC
],[
  0xDF,
  0xDF,
  0x18,
  0x98,
  0x1F,
  0x19,
  0xD8,
  0xD8
],[
  0xBE,
  0xBF,
  0x31,
  0x31,
  0x3E,
  0x33,
  0xB1,
  0xB1
],[
  0x7C,
  0x7E,
  0x63,
  0x62,
  0x7C,
  0x66,
  0x62,
  0x63
],[
  0xF8,
  0xFC,
  0xC6,
  0xC4,
  0xF8,
  0xCC,
  0xC4,
  0xC6
],[
  0xF0,
  0xF8,
  0x8C,
  0x89,
  0xF1,
  0x99,
  0x88,
  0x8C
],[
  0xE0,
  0xF0,
  0x19,
  0x12,
  0xE2,
  0x32,
  0x11,
  0x18
],[
  0xC0,
  0xE0,
  0x33,
  0x25,
  0xC5,
  0x65,
  0x23,
  0x31
],[
  0x80,
  0xC1,
  0x67,
  0x4A,
  0x8A,
  0xCA,
  0x46,
  0x63
],[
  0x00,
  0x83,
  0xCF,
  0x94,
  0x14,
  0x94,
  0x8C,
  0xC7
],[
  0x00,
  0x07,
  0x9F,
  0x28,
  0x28,
  0x28,
  0x18,
  0x8F
],[
  0x00,
  0x0E,
  0x3F,
  0x51,
  0x51,
  0x51,
  0x31,
  0x1F
],[
  0x00,
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x62,
  0x3E
],[
  0x00,
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x72,
  0x3E
],[
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xAA,
  0x62,
  0x3E,
  0x00
],[
  0x7E,
  0xA2,
  0xB2,
  0xA6,
  0x62,
  0x3E,
  0x00,
  0x00
],[
  0xAA,
  0xA6,
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C
],[
  0xA6,
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE
],[
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3
],[
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0
],[
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF
],[
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3
],[
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3,
  0xFE
],[
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3,
  0xFE,
  0x7C
],[
  0xF0,
  0xF8,
  0x0C,
  0x00,
  0x3C,
  0x0C,
  0xF8,
  0xF0
],[
  0xC3,
  0xE3,
  0x33,
  0x03,
  0xF3,
  0x33,
  0xE3,
  0xC3
],[
  0x0F,
  0x8F,
  0xCC,
  0x0F,
  0xCC,
  0xCC,
  0x8F,
  0x0F
],[
  0x3E,
  0x3E,
  0x30,
  0x3C,
  0x30,
  0x30,
  0x3E,
  0x3E
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xEF,
  0xEF,
  0x0C,
  0xCF,
  0x0C,
  0x0C,
  0xEF,
  0xEF
],[
  0xBE,
  0xBE,
  0x30,
  0x3C,
  0x30,
  0x30,
  0xBE,
  0xBE
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xEC,
  0xEC,
  0x0D,
  0xCF,
  0x0F,
  0x0D,
  0xEC,
  0xEC
],[
  0xB1,
  0xB3,
  0x37,
  0x3C,
  0x3C,
  0x36,
  0xB7,
  0xB3
],[
  0xC5,
  0xCD,
  0xDD,
  0xF0,
  0xF0,
  0xD8,
  0xDD,
  0xCD
],[
  0x17,
  0x37,
  0x76,
  0xC3,
  0xC0,
  0x60,
  0x76,
  0x37
],[
  0x5F,
  0xDF,
  0xD8,
  0x0E,
  0x03,
  0x81,
  0xDB,
  0xDF
],[
  0xBE,
  0xBE,
  0xB0,
  0x1C,
  0x06,
  0x02,
  0xB6,
  0xBE
]]

smile = [[
  0x3C,
  0x42,
  0xA5,
  0x81,
  0xA5,
  0x99,
  0x42,
  0x3C
]]

alien = [
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0xA5],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0x42],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0xA5],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0x42],
]

def id(x, y, px):
  return x, y, px

def inv(x, y, px):
  return x, y, not px

def rotLt(x, y, px):
  return 7 - y, x, px

def rotRt(x, y, px):
  return y, 7 - x, px

def rot180(x, y, px):
  return 7 - x, 7 - y, px

def play(frames=f, delay=.05, tf=id):
  for f in frames:
    for y, row in enumerate(f):
      for x, mask in enumerate(xMask):
        pixel = row & mask == mask
        xPx, yPx, pixel = tf(x, y, pixel)
        if pixel:
          matrix.pixel(xPx, yPx, 1)
        else:
          matrix.pixel(xPx, yPx, 0)
  matrix.show()
  time.sleep(delay)

go = True
sleeps = 0
touches = 0
while True:
  if go:
    play()
    play(f[::-1])
    play(tf=inv)
    play(f[0:5], .1, tf=rotRt)
    play(f[0:5], .1, tf=rot180)
    play(f[0:5], .1, tf=rotLt)
    play()
    go = False
  else:
    matrix.fill(0)
    matrix.show()
    time.sleep(2)
    sleeps = sleeps + 1
    if touch.value:
      go = False
      if touches == 0:
        play(smile, delay=2)
      else:
        play(alien, delay=1)
      touches = 1 - touches
      sleeps = 0
    if sleeps >= 15:
      go = True
      sleeps = 0

import time
import adafruit_ht16k33.matrix
import board
import busio as io
import touchio

touch = touchio.TouchIn(board.D1)

i2c = io.I2C(board.SCL, board.SDA)
matrix = adafruit_ht16k33.matrix.Matrix8x8(i2c)

x_pix = y_pix = 8
matrix.fill(0)

xMask = [
  0x80,
  0x40,
  0x20,
  0x10,
  0x08,
  0x04,
  0x02,
  0x01,
]

f = [[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x6A,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xA2,
  0xB6,
  0x62,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xA2,
  0xB6,
  0xA2,
  0x62,
  0x3E
],[
  0x04,
  0x1E,
  0x7E,
  0xAA,
  0xA2,
  0xA2,
  0x62,
  0x3E
],[
  0x04,
  0x0F,
  0x3F,
  0x55,
  0x51,
  0x51,
  0x31,
  0x1F
],[
  0x02,
  0x07,
  0x1F,
  0x2A,
  0x28,
  0x28,
  0x1C,
  0x0F
],[
  0x01,
  0x03,
  0x0F,
  0x15,
  0x14,
  0x15,
  0x0C,
  0x07
],[
  0x00,
  0x01,
  0x07,
  0x0A,
  0x0B,
  0x0A,
  0x06,
  0x03
],[
  0x00,
  0x00,
  0x03,
  0x05,
  0x05,
  0x05,
  0x03,
  0x01
],[
  0x00,
  0x00,
  0x01,
  0x02,
  0x02,
  0x02,
  0x01,
  0x00
],[
  0x00,
  0x00,
  0x00,
  0x01,
  0x01,
  0x01,
  0x00,
  0x00
],[
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
],[
  0xFC,
  0xC2,
  0xC3,
  0xFF,
  0xC2,
  0xC3,
  0xFE,
  0xFC
],[
  0xF8,
  0x84,
  0x86,
  0xFE,
  0x84,
  0x86,
  0xFC,
  0xF8
],[
  0xF1,
  0x09,
  0x0D,
  0xFD,
  0x09,
  0x0D,
  0xF9,
  0xF1
],[
  0xE3,
  0x13,
  0x1B,
  0xFB,
  0x13,
  0x1B,
  0xF3,
  0xE3
],[
  0xC7,
  0x27,
  0x36,
  0xF7,
  0x26,
  0x36,
  0xE7,
  0xC7
],[
  0x8F,
  0x4F,
  0x6C,
  0xEF,
  0x4C,
  0x6C,
  0xCF,
  0x8F
],[
  0x1F,
  0x9F,
  0xD8,
  0xDE,
  0x98,
  0xD8,
  0x9F,
  0x1F
],[
  0x3E,
  0x3E,
  0xB0,
  0xBC,
  0x30,
  0xB0,
  0x3E,
  0x3E
],[
  0x7D,
  0x7D,
  0x61,
  0x79,
  0x61,
  0x61,
  0x7D,
  0x7D
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xF7,
  0xF7,
  0x86,
  0xE7,
  0x86,
  0x86,
  0xF7,
  0xF7
],[
  0xEF,
  0xEF,
  0x0C,
  0xCF,
  0x0C,
  0x0C,
  0xEF,
  0xEF
],[
  0xDF,
  0xDF,
  0x18,
  0x9E,
  0x18,
  0x18,
  0xDF,
  0xDF
],[
  0xBE,
  0xBE,
  0x30,
  0x3C,
  0x30,
  0x30,
  0xBE,
  0xBE
],[
  0x7D,
  0x7D,
  0x61,
  0x79,
  0x61,
  0x61,
  0x7D,
  0x7D
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xF7,
  0xF7,
  0x86,
  0xE6,
  0x87,
  0x86,
  0xF6,
  0xF6
],[
  0xEF,
  0xEF,
  0x0C,
  0xCC,
  0x0F,
  0x0C,
  0xEC,
  0xEC
],[
  0xDF,
  0xDF,
  0x18,
  0x98,
  0x1F,
  0x19,
  0xD8,
  0xD8
],[
  0xBE,
  0xBF,
  0x31,
  0x31,
  0x3E,
  0x33,
  0xB1,
  0xB1
],[
  0x7C,
  0x7E,
  0x63,
  0x62,
  0x7C,
  0x66,
  0x62,
  0x63
],[
  0xF8,
  0xFC,
  0xC6,
  0xC4,
  0xF8,
  0xCC,
  0xC4,
  0xC6
],[
  0xF0,
  0xF8,
  0x8C,
  0x89,
  0xF1,
  0x99,
  0x88,
  0x8C
],[
  0xE0,
  0xF0,
  0x19,
  0x12,
  0xE2,
  0x32,
  0x11,
  0x18
],[
  0xC0,
  0xE0,
  0x33,
  0x25,
  0xC5,
  0x65,
  0x23,
  0x31
],[
  0x80,
  0xC1,
  0x67,
  0x4A,
  0x8A,
  0xCA,
  0x46,
  0x63
],[
  0x00,
  0x83,
  0xCF,
  0x94,
  0x14,
  0x94,
  0x8C,
  0xC7
],[
  0x00,
  0x07,
  0x9F,
  0x28,
  0x28,
  0x28,
  0x18,
  0x8F
],[
  0x00,
  0x0E,
  0x3F,
  0x51,
  0x51,
  0x51,
  0x31,
  0x1F
],[
  0x00,
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x62,
  0x3E
],[
  0x00,
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xA2,
  0x72,
  0x3E
],[
  0x1C,
  0x7E,
  0xA2,
  0xA2,
  0xAA,
  0x62,
  0x3E,
  0x00
],[
  0x7E,
  0xA2,
  0xB2,
  0xA6,
  0x62,
  0x3E,
  0x00,
  0x00
],[
  0xAA,
  0xA6,
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C
],[
  0xA6,
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE
],[
  0xA2,
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3
],[
  0x62,
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0
],[
  0x3E,
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF
],[
  0x00,
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3
],[
  0x00,
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3,
  0xFE
],[
  0x7C,
  0xFE,
  0xC3,
  0xC0,
  0xCF,
  0xC3,
  0xFE,
  0x7C
],[
  0xF0,
  0xF8,
  0x0C,
  0x00,
  0x3C,
  0x0C,
  0xF8,
  0xF0
],[
  0xC3,
  0xE3,
  0x33,
  0x03,
  0xF3,
  0x33,
  0xE3,
  0xC3
],[
  0x0F,
  0x8F,
  0xCC,
  0x0F,
  0xCC,
  0xCC,
  0x8F,
  0x0F
],[
  0x3E,
  0x3E,
  0x30,
  0x3C,
  0x30,
  0x30,
  0x3E,
  0x3E
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xEF,
  0xEF,
  0x0C,
  0xCF,
  0x0C,
  0x0C,
  0xEF,
  0xEF
],[
  0xBE,
  0xBE,
  0x30,
  0x3C,
  0x30,
  0x30,
  0xBE,
  0xBE
],[
  0xFB,
  0xFB,
  0xC3,
  0xF3,
  0xC3,
  0xC3,
  0xFB,
  0xFB
],[
  0xEC,
  0xEC,
  0x0D,
  0xCF,
  0x0F,
  0x0D,
  0xEC,
  0xEC
],[
  0xB1,
  0xB3,
  0x37,
  0x3C,
  0x3C,
  0x36,
  0xB7,
  0xB3
],[
  0xC5,
  0xCD,
  0xDD,
  0xF0,
  0xF0,
  0xD8,
  0xDD,
  0xCD
],[
  0x17,
  0x37,
  0x76,
  0xC3,
  0xC0,
  0x60,
  0x76,
  0x37
],[
  0x5F,
  0xDF,
  0xD8,
  0x0E,
  0x03,
  0x81,
  0xDB,
  0xDF
],[
  0xBE,
  0xBE,
  0xB0,
  0x1C,
  0x06,
  0x02,
  0xB6,
  0xBE
]]

smile = [[
  0x3C,
  0x42,
  0xA5,
  0x81,
  0xA5,
  0x99,
  0x42,
  0x3C
]]

alien = [
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0xA5],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0x42],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0xA5],
  [0x18,
   0x3C,
   0x7E,
   0xDB,
   0xFF,
   0x24,
   0x5A,
   0x42],
]

def id(x, y, px):
  return x, y, px

def inv(x, y, px):
  return x, y, not px

def rotLt(x, y, px):
  return 7 - y, x, px

def rotRt(x, y, px):
  return y, 7 - x, px

def rot180(x, y, px):
  return 7 - x, 7 - y, px

def play(frames=f, delay=.05, tf=id):
  for f in frames:
    for y, row in enumerate(f):
      for x, mask in enumerate(xMask):
        pixel = row & mask == mask
        xPx, yPx, pixel = tf(x, y, pixel)
        if pixel:
          matrix.pixel(xPx, yPx, 1)
        else:
          matrix.pixel(xPx, yPx, 0)
  matrix.show()
  time.sleep(delay)

go = True
sleeps = 0
touches = 0
while True:
  if go:
    play()
    play(f[::-1])
    play(tf=inv)
    play(f[0:5], .1, tf=rotRt)
    play(f[0:5], .1, tf=rot180)
    play(f[0:5], .1, tf=rotLt)
    play()
    go = False
  else:
    matrix.fill(0)
    matrix.show()
    time.sleep(2)
    sleeps = sleeps + 1
    if touch.value:
      go = False
      if touches == 0:
        play(smile, delay=2)
      else:
        play(alien, delay=1)
      touches = 1 - touches
      sleeps = 0
    if sleeps >= 15:
      go = True
      sleeps = 0