import badge,ugfx,wifi,wsclient

badge.init()
ugfx.init()
ugfx.clear(ugfx.WHITE)
ugfx.string(20,10,"Init!","Roboto_BlackItalic24",ugfx.BLACK)
ugfx.flush()
ugfx.input_init()

# Buttons
#ugfx.JOY_UP
#ugfx.JOY_DOWN
#ugfx.JOY_LEFT
#ugfx.JOY_RIGHT
#ugfx.BTN_A
#ugfx.BTN_B
#ugfx.BTN_SELECT
#ugfx.BTN_START
#ugfx.BTN_FLASH


wifi.connect()
wifi.wait()
mywsclient=wsclient.connect('ws://192.168.1.66:80')
ugfx.clear(ugfx.WHITE)
#ugfx.string(20,10,"Done!","Roboto_BlackItalic24",ugfx.BLACK)
ip_addr, netmask, gateway, dns_server = wifi._STA_IF.ifconfig()
ugfx.string(10,100,ip_addr,"Roboto_Regular12",ugfx.BLACK)
ugfx.string(100,10,"Push a button!","PermanentMarker22",ugfx.BLACK)
ugfx.flush()
def render(text, pushed):
	ugfx.clear(ugfx.WHITE)
	ugfx.string(10,100,ip_addr,"Roboto_Regular12",ugfx.BLACK)
	if(pushed):
	  	mywsclient.send(text)
		ugfx.string(100,10,text,"PermanentMarker22",ugfx.WHITE)
	else:
		ugfx.string(100,10,text,"PermanentMarker22",ugfx.BLACK)
	ugfx.flush()

ugfx.input_init()
ugfx.input_attach(ugfx.JOY_UP, lambda pressed: render('UP', pressed))
ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: render('DOWN', pressed))
ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: render('LEFT', pressed))
ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: render('RIGHT', pressed))
ugfx.input_attach(ugfx.BTN_A, lambda pressed: render('A', pressed))
ugfx.input_attach(ugfx.BTN_B, lambda pressed: render('B', pressed))
ugfx.input_attach(ugfx.BTN_START, lambda pressed: render('Start', pressed))
ugfx.input_attach(ugfx.BTN_SELECT, lambda pressed: render('Select', pressed))

while True:
    pass