from time import sleep
from pyjuni.robodog import *
from pyjuni.deflib import *
from pyjuni.jkeyevent import *

down_cmd = "DOWN"
down_bytes = down_cmd.encode('utf-8')
up_cmd = "UP"
up_bytes = up_cmd.encode('utf-8')
go_cmd = "GO"
go_bytes = go_cmd.encode('utf-8')
stop_cmd = "STOP"
stop_bytes = stop_cmd.encode('utf-8')
ready_cmd = "READY"
ready_bytes = ready_cmd.encode('utf-8')

if __name__ == "__main__":
    robodog = RoboDog()
    jkey = JKeyEvent()  

    robodog.Open()
    robodog.headLEDDraw(bytearray(8), bytearray(8))
    runfile = robodog.rb_runfile("qrcode.py")
    print(runfile)
    sleep(0.5)

    while (not jkey.isKeyEscPressed()) and robodog.is_rb_alive():
        rb_msg = robodog.get_rb_bytearray()
        qr_data = rb_msg.decode("utf-8")
        if rb_msg[0] != 0 :
            print(qr_data)

            if up_bytes in rb_msg:
                robodog.leg_bend(60, 60, 60, 60)
            if down_bytes in rb_msg:
                robodog.leg_bend(40, 40, 40, 40)
            if go_bytes in rb_msg:
                robodog.move(50)
            if stop_bytes in rb_msg:
                robodog.move(0)
            if ready_bytes in rb_msg:
                robodog.gesture(0)
        sleep(0.1)

    robodog.Close()
