from mpython import *
from horse import *
import network
import time
from umqtt.simple import MQTTClient
from machine import Timer
import ubinascii
import audio
import neopixel


audio.player_init()
audio.set_volume(100)
my_rgb = neopixel.NeoPixel(Pin(Pin.P7), n=12,bpp=3,timing=1)

myHorse = Car()
speed = 60
my_wifi = wifi()
my_wifi.connectWiFi("STEAM", "12345687")

mqtt = MQTTClient("0805e3d04f3b34e7", "192.168.100.6", 1883, "siot", "dfrobot", keepalive=30)

try:
    mqtt.connect()
    print('Connected')
except:
    print('Disconnected')

def mqtt_topic_4446526f626f742f686f727365(_msg):
    global speed
    if _msg == "jiasu":
        speed = speed+10
    if _msg == "jiansu":
        speed = speed-10
    if _msg == "forward":
        myHorse.forward(speed)
    if _msg == "backward":
        myHorse.backward(-speed)
    if _msg == "turn_l":
        myHorse.turn_l(speed)
    if _msg == "turn_r":
        myHorse.turn_r(speed)
    if _msg == "stop":
        myHorse.stop()
    if _msg == "getRGB":
        oled.fill(0)
        oled.DispChar("RGB", 0, 0, 1)
        oled.DispChar(str(myHorse.getRGB()), 0, 24, 1)
        oled.show()
    if _msg == "getHSV":
        oled.fill(0)
        oled.DispChar("HSV", 0, 0, 1)
        oled.DispChar(str(myHorse.getHSV()), 0, 24, 1)
        oled.show()
    if _msg == "discern":
        oled.fill(0)
        oled.DispChar("颜色识别", 0, 0, 1)
        oled.DispChar(str(myHorse.discern()), 0, 24, 1)
        oled.show()
    if _msg == "distance":
        oled.fill(0)
        oled.DispChar("超声波", 0, 0, 1)
        oled.DispChar(str(myHorse.distance()), 0, 24, 1)
        oled.show()
    if _msg == "cemabenteng":
        audio.play("http://192.168.100.6/music/cemabenteng.mp3")
    if _msg == "paomadeng":
        paomadeng()
    if _msg == "jijiudeng":
        pass
    if _msg == "pinshandeng":
        pinshandeng()
        
def paomadeng():
    for count in range(3):
        for i in range(10):
            my_rgb.fill((200,0,0))
            my_rgb[i] = (50,200,50)
            my_rgb[(i+1)] = (153,153,0)
            my_rgb[(i+2)] = (0,0,153)
            my_rgb.write()
            time.sleep_ms(400)
        for i in range(9,-1,-1):
            my_rgb.fill((200,0,0))
            my_rgb[i] = (50,200,50)
            my_rgb[i+1] = (153,153,0)
            my_rgb[i+2] = (0,0,153)
            my_rgb.write()
            time.sleep_ms(400)
        my_rgb.fill((0,0,255))
        my_rgb.write()
def pinshandeng():
    for count in range(3):
        my_rgb.fill((255,0,0))
        my_rgb.write()
        time.sleep_ms(500)
        my_rgb.fill((0,255,0))
        my_rgb.write()
        time.sleep_ms(500)
        my_rgb.fill((0,0,255))
        my_rgb.write()
        time.sleep_ms(500)
        
def mqtt_callback(topic, msg):
    try:
        topic = topic.decode('utf-8', 'ignore')
        _msg = msg.decode('utf-8', 'ignore')
        eval('mqtt_topic_' + bytes.decode(ubinascii.hexlify(topic)) + '("' + _msg + '")')
    except: print((topic, msg))

mqtt.set_callback(mqtt_callback)

mqtt.subscribe("DFRobot/horse")

def timer14_tick(_):
    mqtt.ping()

tim14 = Timer(14)
tim14.init(period=20000, mode=Timer.PERIODIC, callback=timer14_tick)


oled.fill(0)
oled.DispChar("千里马智能机器人", 18, 24, 1)
oled.show()
time.sleep(1)
mqtt.publish("DFRobot/horse", "hello")
while True:
    mqtt.wait_msg()
