#!/usr/bin/env python
#version    :       2023.06.0`
#language   :       en
#hardware   :       pi

from maix import display
from maix import image
from maix import camera
import time
import sys
sys.path.append('/root/')
from CocoPi import BUTTON
from CocoPi import multiFuncGpio
S1=multiFuncGpio(0,1)
S2=multiFuncGpio(1,1)

key_D = BUTTON(7)
key_C = BUTTON(13)

camera.camera.config(size=(320,240))
ScreenOrientation = False
angle=0
addValue=0
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")

canvas = image.new(size = (320, 240))
while True:
    if(key_C.is_pressed()):
        if angle<180:
            addValue=1
        else:
            addValue=0
    elif key_D.is_pressed():
        if angle>0:
            addValue=-1
        else:
            addValue=0
    else:
        addValue=0
    canvas = image.new(color=(0,0,0),size = (320, 240))
    
    angle=angle+addValue
    #canvas.draw_string(4, 180, "C:increase servo's angle", 1, color=(0, 255, 0))
    #canvas.draw_string(4, 200, "D:decrease servo's angle", 1, color=(0, 255, 0))
    canvas.draw_string(80, 10, "servo angle:"+str(angle), 1, color=(0, 0, 255))
    canvas.draw_image((image.open("/root/preset/img/sg90.png")).rotate(0, adjust=1),132,70,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/sg90_double_arm.png")).rotate(angle, adjust=0),103,89,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/increase_bfbfbf_24x24.png")).rotate(0, adjust=0),288,8,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/decrease_bfbfbf_24x24.png")).rotate(0, adjust=0),8,8,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
    S1.servoCtrl(angle)
    S2.servoCtrl(angle)
    if ScreenOrientation:
        canvasVER = canvas.crop(0,0,240,320)
        canvasVER = canvasVER.rotate(-90, adjust=1)
        display.show(canvasVER)
    else:
        display.show(canvas)
