#!/usr/bin/env python
#version    :       2023.08.18
#language   :       en
#hardware   :       pi
from maix import display
from maix import image
from maix import camera
import smbus2
import time
import sys
sys.path.append("/root/")
from CocoPi import stm8s
iic_slaver=stm8s()
iic_slaver.clear()
del iic_slaver
from CocoPi import dcMotor
from CocoPi import BUTTON

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

D1=dcMotor(1)
D2=dcMotor(2)

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

canvas = image.new(size = (320, 240))
while True:
    if (key_B.is_pressed()):
        while key_B.is_pressed():
            time.sleep(0.001)
        rotation=(-1)*rotation
    if(key_C.is_pressed()):
        if speed<255:
            addValue=1
        else:
            addValue=0
    elif key_D.is_pressed():
        if speed>0:
            addValue=-1
        else:
            addValue=0
    else:
        addValue=0
    canvas = image.new(color=(0,0,0),size = (320, 240))
    
    speed=speed+addValue
    canvas.draw_string(80, 8, "speed:"+str(speed), 1, color=(0, 255, 0))
    if rotation==1:
        canvas.draw_image((image.open("/root/preset/img/clockwise_bfbfbf_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
        #canvas.draw_string(40, 24, "rotation:clockwise", 1, color=(0, 255, 0))
    else:
        canvas.draw_image((image.open("/root/preset/img/anticlockwise_bfbfbf_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
        #canvas.draw_string(40, 24, "rotation:anticlockwise", 1, color=(0, 255, 0))
    angle=angle-int(rotation*speed/3)
    if angle>3600:
        angle=angle-3600
    elif angle<-3600:
        angle=angle+3600
    else:
        angle=angle
    #canvas.draw_string(4, 180, "C:change fan's direction of rotation", 1, color=(0, 255, 0))
    #canvas.draw_string(4, 200, "D:increase fan's speed of rotation", 1, color=(0, 255, 0))
    #canvas.draw_string(4, 220, "B:decrease fan's speed of rotation", 1, color=(0, 255, 0))
    canvas.draw_image((image.open("/root/preset/img/ttMotor_200_64.png")).rotate(0, adjust=1),80,90,alpha=1)
    canvas.draw_image((image.open("/root/preset/img/fan_140_140.png")).rotate(angle, adjust=0),60,52,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)
    if(speed<80):
        D1.dcMotorCtrl(rotation,speed)
        D2.dcMotorCtrl(rotation,speed)
    else:
        if rotation==1:
            D1.dcMotorCtrl(1,speed)
            D2.dcMotorCtrl(1,speed)
        else:
            D1.dcMotorCtrl(0,speed)
            D2.dcMotorCtrl(0,speed)
    if ScreenOrientation:
        canvasVER = canvas.crop(0,0,240,320)
        canvasVER = canvasVER.rotate(-90, adjust=1)
        display.show(canvasVER)
    else:
        display.show(canvas)