# -*- coding: utf-8 -*-
# 键盘控制Tello

import socket
import time
import curses
stdscr = curses.initscr()

DISTANCE = 20
ANGLE = 30

curses.noecho()
curses.cbreak()
stdscr.keypad(1)

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tello_address = ("192.168.10.1" , 8889)

while True:
    key = stdscr.getch()
    
    if key == ord("c"):
        print ("起動します")
        socket.sendto("command".encode("utf-8"),tello_address)

    elif key == ord("t"):
        print ("1秒後離陸します")
        time.sleep(1)
        socket.sendto("takeoff".encode("utf-8"),tello_address)

    elif key == ord("l"):
        print ("着陸します")
        socket.sendto("land".encode("utf-8"),tello_address)

    elif key == ord("w"):
        print(f"前に{DISTANCE}cm移動します")
        socket.sendto(("foward "+str(DISTANCE)).encode("utf-8"),tello_address)
    
    elif key == ord("s"):
        print(f"後ろに{DISTANCE}cm移動します")
        socket.sendto(("back "+str(DISTANCE)).encode("utf-8"),tello_address)
        
    elif key == ord("a"):
        print(f"左に{DISTANCE}cm移動します")
        socket.sendto(("left "+str(DISTANCE)).encode("utf-8"),tello_address)
        
    elif key == ord("d"):
        print(f"右に{DISTANCE}cm移動します")
        socket.sendto(("right "+str(DISTANCE)).encode("utf-8"),tello_address)

    elif key == ord("e"):
        print(f"時計回りに{ANGLE}度回転します")
        socket.sendto(("cw "+str(ANGLE)).encode("utf-8"),tello_address)

    elif key == ord("q"):
        print(f"反時計回りに{ANGLE}度回転します")
        socket.sendto(("ccw "+str(ANGLE)).encode("utf-8"),tello_address)
     
    elif key == ord("p"):
        print ("着陸します")
        socket.sendto("land".encode("utf-8"),tello_address)
        break  # Exit the while()

curses.echo() #打开输入回显功能
curses.nocbreak()#关闭字符终端功能（只有回车时才发生终端）
stdscr.keypad(0)

curses.endwin()