import display
import buttons
import virtualtimers
import random
import mch22

display.drawFill(0xFFFFFF)

x = int(display.width() / 2)
y = int(display.height() / 2)

width = 30
height = 30

xspeed = 1
yspeed = -1

text = "DVD"
text_width = display.getTextWidth(text, "roboto_regular18")
text_height = display.getTextHeight(text, "roboto_regular18")

colors = [0xFF0000, 0x00FF00, 0x0000FF]
colornr = 0
def colorpicker():
  global colornr
  colornr = colornr + 1
  if colornr >= 3:
    colornr = 0

def screensaver():
  global x, y, xspeed, yspeed, width, height, colornr, colors
  display.drawCircle(x, y, width, 0, 360, True, colors[colornr])
  display.drawText(x-20, y-10, "DVD", 0x000000, "roboto_regular18")
  display.flush(display.FLAG_LUT_FASTEST)
  display.drawCircle(x, y, width, 0, 360, True, 0x000000)
  x += xspeed
  y += yspeed
  if x > display.width() - width:
    xspeed = -xspeed
    colorpicker()
  if y > display.height() - height:
    yspeed = -yspeed
    colorpicker()
  if x < width:
    xspeed = -xspeed
    colorpicker()
  if y < height:
    yspeed = -yspeed
    colorpicker()

  return 1000 // 30

display.drawFill(0x0)
virtualtimers.begin(50)
virtualtimers.new(1000, screensaver)

def reboot(pressed):
  if pressed:
    mch22.exit_python()

buttons.attach(buttons.BTN_A,reboot)
buttons.attach(buttons.BTN_B,reboot)
buttons.attach(buttons.BTN_HOME,reboot)
buttons.attach(buttons.BTN_MENU,reboot)
buttons.attach(buttons.BTN_SELECT,reboot)
buttons.attach(buttons.BTN_START,reboot)