-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (43 loc) · 1.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def on_bluetooth_connected():
basic.show_icon(IconNames.DIAMOND)
bluetooth.on_bluetooth_connected(on_bluetooth_connected)
def on_bluetooth_disconnected():
basic.show_icon(IconNames.NO)
bluetooth.on_bluetooth_disconnected(on_bluetooth_disconnected)
class Dir:
FORWARD = 0x0
BACKWARD = 0x1
class Motor:
LEFT = 0
RIGHT = 1
ALL = 2
def motor_stop(motor):
motor_run(motor, 0, 0)
addressToWrite = 0x10;
def motor_run(motor, direction, speed):
addressToWrite = 0x10;
buf = bytearray(3)
buf[1] = direction
buf[2] = speed
if(motor == Motor.LEFT or motor == Motor.ALL):
buf[0] = 0x00;
pins.i2c_write_buffer(addressToWrite, buf, False)
if(motor == Motor.RIGHT or motor == Motor.ALL):
buf[0] = 0x02;
pins.i2c_write_buffer(addressToWrite, buf, False)
def on_uart_data_received():
uartCmd = bluetooth.uart_read_until(serial.delimiters(Delimiters.NEW_LINE))
basic.show_string(uartCmd)
if uartCmd == "f":
motor_run(Motor.ALL, Dir.FORWARD, 84)
elif uartCmd == "s":
motor_stop(Motor.ALL)
elif uartCmd == "b":
motor_run(Motor.ALL, Dir.BACKWARD, 84)
elif uartCmd == "r":
motor_run(Motor.RIGHT, Dir.FORWARD, 84)
elif uartCmd == "l":
motor_run(Motor.LEFT, Dir.FORWARD, 84)
bluetooth.on_uart_data_received(serial.delimiters(Delimiters.NEW_LINE), on_uart_data_received)
bluetooth.start_uart_service()
basic.show_icon(IconNames.YES)