# Glad this isn't a job interview eh...


from app import TextApp
import accelerometer
import tidal
import math
import utime
import urandom

import vga2_8x8 as default_font

COLOUR = tidal.YELLOW
COLOURS = [tidal.YELLOW,tidal.BRAND_MID_BLUE, tidal.BRAND_NAVY, tidal.RED, tidal.BRAND_CYAN, tidal.BRAND_ORANGE, tidal.BRAND_PINK, tidal.ADDITIONAL_PURPLE, tidal.ADDITIONAL_DEEP_ORANGE, tidal.ADDITIONAL_RED, tidal.GREEN]


OVERLAY = [
(0,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(1,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(2,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(3,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(4,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(5,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(6,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(7,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(8,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(9,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(10,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(11,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(12,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(13,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(14,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(15,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(16,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(17,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(18,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(19,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(20,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(21,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(22,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(23,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(24,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(25,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xff?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(26,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(27,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(28,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(29,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(30,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(31,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xff\xff\xff\xff\xff?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(32,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(33,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x01\xc0\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(34,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\x1f\x00\x00\xfc\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(35,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\xff\x07\x00\x00\xe0\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(36,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\xff\x00\x00\x00\x80\xff\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(37,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\x7f\x00\x00\x00\x00\xfe?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(38,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\x1f\x00\x00\x00\x00\xfc\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(39,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xff\x07\x00\x00\x00\x00\xf0\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(40,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xff\x03\x00\x00\x00\x00\xe0\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(41,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xff\x01\x00\x00\x00\x00\x80\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(42,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfc\xff\x00\x00\x00\x00\x00\x00\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(43,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfc\x7f\x00\x00\x00\x00\x00\x00\xfe\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(44,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00p\x00\xfe?\x00\x00\x00\x0c\x00\x00\xfc\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(45,bytearray(b'\x00\x00\x00\x00\x00 \x00\xc0\x7f\x00\xfe\x1f\x00\x00\x00\x1f\x00\x00\xfc\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(46,bytearray(b'\x00\x00\x00\x00\x00|\x00\xe0\x7f\x00\xfe\x0f\x00\x00\x80?\x00\x00\xf8\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(47,bytearray(b'\x00\x00\x00\x00\x00\xfc\x00\xf8\x7f\x00\xff\x07\x00\x00\xe0?\x00\x00\xf0\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(48,bytearray(b'\x00\x00\x00\x00\x00\xfc\xc8\xff\x0f\x00\xff\x03\x00\x00\xf0\x7f\x00\x00\xe0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(49,bytearray(b'\x00\x00\x00\x00\x00\xfe\xff\xff\x0f\x80\xff\x03\x00\x00\xf8\xff\x00\x00\xe0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(50,bytearray(b'\x00\x00\x00\x00\x80\xff\xff?\x7f\xc0\xff\x01\x00\x00\xfc\xff\x00\x00\xc0\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(51,bytearray(b'\x00\x00\x00\x00\xe0\xff\xff\x7f\xfe\xf0\xff\x01\x00\x00\xfe\xff\x01\x00\x80\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(52,bytearray(b'\x00\x00\x00\x00\xf0\xff\xff\x7f\xfc\xf8\xff\x00\x00\x00\xff\xff\x00\x00\x80\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(53,bytearray(b'\x00\x00\x00\x00\xf8\xff\xff\x7fx\xfc\xff\x00\x00\x00\xff?\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(54,bytearray(b'\x00\x00\x00\x00\xf8\xff\xff\xff \xfe\x7f\x00\x00\x80\xff\x1f\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(55,bytearray(b'\x00\x00\x00\x00\xff\xff\xff\xff\x01\xff\x7f\x00\x00\x80\xff\x0f\x00\x00\x00\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(56,bytearray(b'\x00\x00\x00\xc0\xff\xff\xff\xff\x83\xff?\x00\x00\x80\xff\x0f\x00\x00\x00\xfe\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(57,bytearray(b'\x00\x00\x00\xe0\xff\xff\xff\xff\xff\xff?\x00\x00\x80\xff\x07\x00\x00\x00\xfe\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(58,bytearray(b'\x000\x00\xf8\xff\xff\xff\xff\xff\xff?\x00\x00\x80\xff\x07\x00\x00\x00\xfe\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(59,bytearray(b'\x00\xf8\xe0\xff\xff\xff\xff\xff\xff\xff\x1f\x00\x00\x80\xff\x07\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(60,bytearray(b'\x80\xff\xfd\xff\xff\xff\xff\xff\xff\xff\x1f\x00\x00\x80\xff\x07\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(61,bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f\x00\x00\x80\xff\x07\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(62,bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1f\x00\x00\x80\xff\x07\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(63,bytearray(b'\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\x1f\x00\x00\x80\xff\x0f\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(64,bytearray(b'\xff\xff\xff\xff\xff?\xe0\xff\xff\xff\x1f\x00\x00\x00\xff\x0f\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(65,bytearray(b'\xff\xff\xff\xff\xff\x1f\xc0\xff\xff\xff\x1f\x00\x00\x00\xff\x1f\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(66,bytearray(b'\xff\xff\xff\xff\xff\x1f\x80\xff\xff\xff\x0f\x00\x00\x00\xfe\x1f\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(67,bytearray(b'\xff\xff\xff\xff\xff\x0f\x00\xff\xff\xff\x0f\x00\x00\x00\xfe?\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(68,bytearray(b'\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\x0f\x00\x00\x00\xfc?\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(69,bytearray(b'\xff\xff\xff\xff\x0f\x00\x00\xfe\xff\xff\x1f\x00\x00\x00\xfc\x7f\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(70,bytearray(b'\xff\xff\xff\xff\x07\x00\x00\xfe\xfc\xff\x1f\x00\x00\x00\xf8\x7f\x00\x00\x00\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(71,bytearray(b'\xff\xff\xff\xff\x0f\x00\x00\xfe\xf1\xff\x1f\x00\x00\x00\xf8\x7f\x00\x00\x00\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')),
(72,bytearray(b'\xff\xff\xff\x0f\x0f\x00\x00\xfe\x81\xff\x1f\x00\x00\x00\xf0\xff\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(73,bytearray(b'\xff\xff\xff\x1f\x7f\x00\x00\xfe\x01\xff\x1f\x00\x00\x00\xf0\xff\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(74,bytearray(b'\xff\xff\xff\x1f\xfe\x01\x00\xff\x03\xfe\x1f\x00\x00\x00\xf0\xff\x00\x00\x00\xfc\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(75,bytearray(b'\xe7\xff\xff\x03\xfc\x01\x00\xfe\x07\xfe\x1f\x00\x00\x00\xf0\xff\x00\x00\x00\xfc\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(76,bytearray(b'\xf2\xff\xff?\xf8\x03\x00\xfc\x0f\xfc?\x00\x00\x00\xf0\xff\x01\x00\x00\xfc\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(77,bytearray(b'\xf0\xff\xff?\xf0\x0f\x00\xf8\x0f\xfc?\x00\x00\x00\xf0\xff\x00\x00\x00\xfe\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(78,bytearray(b'\xc0\xff\xff\x0f\xf0\x1f\x00x\x0f\xfe?\x00\x00\x00\xf8\xff\x00\x00\x00\xfe\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(79,bytearray(b'\xf0\xff\xff\x1f\xf0\x0f\x00x\x07\xfe\x7f\x00\x00\x00\xfc\xff\x00\x00\x00\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(80,bytearray(b'\xf0\xff\xff\x03\xf0\x04\x00x\x00\xfc\x7f\x00\x00\x00\xfe\xff\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(81,bytearray(b'\xe0\xff\xff\x0f\xf0\x00\x00x\x00\xfc\x7f\x00\x00\x00\xff\x7f\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(82,bytearray(b'\xe0\xff\xff\x0f\xe0\x00\x00p\x00\xf8\xff\x00\x00\x80\xff\x7f\x00\x00\x80\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(83,bytearray(b'\xe0\xff\xff\x03\x00\x00\x00 \x00\xf8\xff\x00\x00\x80\xff?\x00\x00\x80\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(84,bytearray(b'\x80\xff\xff\x03\x00\x00\x00\x00\x00\xf0\xff\x01\x00\x80\xff\x1f\x00\x00\xc0\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(85,bytearray(b'\x00\xbd\xff\x00\x00\x00\x00\x00\x00\xf0\xff\x03\x00\x00\xff\x0f\x00\x00\xc0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(86,bytearray(b'\x00\x9c\xff\x00\x00\x00\x00\x00\x00\xf0\xff\x03\x00\x00\xfe\x07\x00\x00\xe0?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(87,bytearray(b'\x00\xd07\x00\x00\x00\x00\x00\x00\xe0\xff\x07\x00\x00\xfe\x03\x00\x00\xf0\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(88,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xff\x0f\x00\x00\xfc\x00\x00\x00\xf8\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(89,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\x1f\x00\x00x\x00\x00\x00\xf8\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(90,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\x1f\x00\x00\x18\x00\x00\x00\xfc\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(91,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff?\x00\x00\x00\x00\x00\x00\xfe\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(92,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\x7f\x00\x00\x00\x00\x00\x00\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(93,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xff\x00\x00\x00\x00\x00\x80\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(94,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\xff\x03\x00\x00\x00\x00\xc0\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(95,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xff\x07\x00\x00\x00\x00\xf0\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(96,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\x0f\x00\x00\x00\x00\xf8\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(97,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe?\x00\x00\x00\x00\xfe?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(98,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfc\xff\x00\x00\x00\x80\xff\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(99,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf8\xff\x03\x00\x00\xe0\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(100,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\xff\x1f\x00\x00\xf8\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(101,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\xff\x01\x80\xff\xff\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(102,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(103,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(104,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfc\xff\xff\xff\xff\x1f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(105,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\xff\xff\xff\xff\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(106,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(107,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfe\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(108,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0\xff\xff\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(109,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(110,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(111,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(112,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(113,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(114,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(115,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(116,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(117,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(118,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(119,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(120,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(121,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(122,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(123,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(124,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(125,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(126,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(127,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(128,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(129,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(130,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(131,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(132,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(133,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
(134,bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')),
]

def split(text, length):
    result = []

    while text:
        if len(text) <= length:
            result.append(text)
            break

        end = length
        for index in range(length, length - 10, -1):
            character = text[index]
            if character == " ":
                end = index
                break
            elif character == "-":
                end = index + 1
                break

        result.append(text[:end])
        text = text[index + 1:]

    return result


def show_traceback(fn):
    def _wrapped(*args,**kwargs):
        try:
            return fn(*args,**kwargs)
        except Exception as error:
            tidal.display.fill(tidal.RED)
            for n,line in enumerate(split(str(error),14)):
                tidal.display.text(default_font,line,0,10+(n*10),tidal.WHITE)
            raise
    return _wrapped




UNDER_BACKGROUND = tidal.BRAND_MID_BLUE
UNDER_FOREGROUND = tidal.BRAND_CYAN
ABOVE_BACKGROUND = tidal.BLACK
ABOVE_FOREGROUND = tidal.WHITE


class Water(TextApp):


    def __init__(self):
        super().__init__()

        self._no_generators_pls = []
        for element in enumerate(range(-68,67)):
            self._no_generators_pls.insert(urandom.randint(0,len(self._no_generators_pls)),element)

        self.accelerometer_data = (1,1,1)
        self.timers = []

        self.carry = 0
        self.heights = [120]*135
        self.shifts = [0]*135
        self.timer = None

        self._column_sequence_position = 0

        self._shift_sequence_position = 0
        self._shift_sequence = []
        for i in range(135):
            if i <= 24 or i >= 110:
                continue
            position = urandom.randint(0,len(self._shift_sequence))
            self._shift_sequence.insert(position,i)

        self._wave_offsets = {}
        for carry in range(0,360,20):
            for column in range(135):
                self._wave_offsets[carry+column] = math.sin(math.radians(carry + column)) * 10

    def _update_accelerometer(self):
        result = (0,0,0)
        while not result[1]:
            result = accelerometer.get_xyz()
        self.accelerometer_data = result

    def on_start(self):
        super().on_start()


        self.images = {'under':[],
                       'above':[]}
        for column,data in OVERLAY:
            self.images['under'].append(bytearray())
            self.images['above'].append(bytearray())
            for byte in data:
                for bit in range(8):
                    if (byte >> bit & 1):
                        self.images['under'][-1].append(UNDER_FOREGROUND >> 8)
                        self.images['under'][-1].append(UNDER_FOREGROUND)
                        self.images['above'][-1].append(ABOVE_FOREGROUND >> 8)
                        self.images['above'][-1].append(ABOVE_FOREGROUND)
                    else:
                        self.images['under'][-1].append(UNDER_BACKGROUND >> 8)
                        self.images['under'][-1].append(UNDER_BACKGROUND)
                        self.images['above'][-1].append(ABOVE_BACKGROUND >> 8)
                        self.images['above'][-1].append(ABOVE_BACKGROUND)


    def on_activate(self):
        super().on_activate()
        self.timers.append(self.periodic(50,self._update_accelerometer))
        self.timers.append(self.periodic(35,self._draw_surface))
        self.timers.append(self.periodic(60,self._draw_distortion))

        tidal.display.fill(tidal.BLACK)

        for column in range(0,135):
            tidal.display.blit_buffer(self.images['under'][column],column,0,1,120)
            tidal.display.blit_buffer(self.images['above'][column][240:],column,120,1,120)


    @show_traceback
    def on_deactivate(self):
        for timer in self.timers:
            timer.cancel()
        super().on_deactivate()

    @show_traceback
    def _draw_distortion(self):
        for i in range(20):
            column = self._shift_sequence[self._shift_sequence_position]
            self._shift_sequence_position = (self._shift_sequence_position + 1) % len(self._shift_sequence)

            image = memoryview(self.images['under'][self.shifts[column]])
            tidal.display.blit_buffer(image,column,0,1,self.heights[column])


    @show_traceback
    def _draw_surface(self):

        #st = utime.ticks_ms()

        step = self.accelerometer_data[0]/self.accelerometer_data[1]
        self.carry = (self.carry + 20) % 360

        for i in range(80):
            column,multiplier = self._no_generators_pls[self._column_sequence_position]
            self._column_sequence_position = (self._column_sequence_position + 1) % 135

        #for column,multiplier in self._no_generators_pls:
            height = 120 + int((step*multiplier) + self._wave_offsets[self.carry+column])
            height = max(0,height)
            height = min(240,height)

            # Generate the shifts here so it's more obvious close to the surface
            shifted = column + urandom.randint(-2,2)
            shifted = min(shifted,134)
            shifted = max(shifted,0)
            self.shifts[column] = shifted
            previous = self.heights[column]
            if height > self.heights[column]:

                if column > 23 and column < 111:
                    difference =  height - previous
                    image = memoryview(self.images['under'][shifted])[previous*2:]
                    tidal.display.blit_buffer(image,column,previous,1,difference)
                else:
                    # Slightly faster path for edges
                    tidal.display.line(column,previous,column,height,UNDER_BACKGROUND)

            elif height < self.heights[column]:
                if column > 24 and column < 110:
                    difference = previous - height
                    image = memoryview(self.images['above'][column])[height*2:]
                    tidal.display.blit_buffer(image,column,height,1,difference)
                else:
                    # Slightly faster path for edges
                    tidal.display.line(column,height,column,previous,ABOVE_BACKGROUND)

            elif height == self.heights[column]:
                continue

            self.heights[column] = height

        #elapsed = utime.ticks_ms() - st
        #tidal.display.text(default_font,"%sms" % elapsed,0,0,tidal.WHITE)




main = Water