#!/usr/bin/env python2.7
#
# knobtest - a test program for the knobterm box
#

from __future__ import print_function

import knobterm
import knobui
import time
import sys
import random

if len(sys.argv) > 1:
  serial_port = sys.argv[1]
else:
  serial_port = None
kt = knobterm.KnobTerm(serial_port)
if not kt.open():
  print("error opening knobterm!")
  sys.exit(1)

(w,h) = kt.get_size()

l = knobui.Label(0,0,w,"pifon",align=knobui.Label.ALIGN_CENTER, scale=knobui.Label.SCALE_XY)
l.draw(kt)

g = knobui.Gauge(0,2,w,3)
g.draw(kt, True)

e = (
  "Enable",
  "Disable"
)
m = knobui.Menu(5,10,w-10,"Menu",e)
i = m.pick(kt)

# simulate a main loop
while True:
  g.set_value(random.randint(0,100))
  g.draw(kt)
  ev = kt.get_next_event(0.1)
  if ev != None:
    if ev['type'] in ('b', 'q'):
      break
    else:
      print(ev)
