-
Notifications
You must be signed in to change notification settings - Fork 0
/
picon.py
98 lines (83 loc) · 2.59 KB
/
picon.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import piconzero as pz
import threading
light_status = 0
motor_direction = 'Stop'
motor_speed = 100
class lights(object):
""" A class to control the lights """
def __init__(self):
pz.init()
# 1= 2 place, 1 = PWM output channel
pz.setOutputConfig(1, 1)
# 2= 3 place, 1 = PWM output channel
pz.setOutputConfig(2, 1)
# Set initial state as off
pz.setOutput(1, 0)
pz.setOutput(2, 0)
# Create a lock to syncronize access to hardware from multiple threads.
self._lock = threading.Lock()
def read_led(self):
global light_status
""" Read the state of the lights """
with self._lock:
return light_status
def set_led(self, value):
global light_status
if value is True:
pz.setOutput(1, 100)
pz.setOutput(2, 100)
light_status = 1
else:
pz.setOutput(1, 0)
pz.setOutput(2, 0)
light_status = 0
class motors():
""" A class to control the motors """
def __init__(self):
pz.init()
# Create a lock to syncronize access to hardware from multiple threads.
self._lock = threading.Lock()
def read_direction(self):
""" read the global variable motor_direction """
global motor_direction
with self._lock:
return motor_direction
def forward(self):
""" Move both motors forward """
global motor_speed
global motor_direction
with self._lock:
# pz.forward(speed)
pz.forward(motor_speed)
motor_direction = 'Forward'
return motor_direction
def reverse(self):
""" Move both motors backwards """
global motor_speed
global motor_direction
with self._lock:
pz.reverse(motor_speed)
motor_direction = 'Reverse'
return motor_direction
def spin_right(self):
""" Rotate to the right """
global motor_speed
global motor_direction
with self._lock:
pz.spinLeft(motor_speed)
motor_direction = 'Spin Right'
return motor_direction
def spin_left(self):
""" Rotate to the left """
global motor_speed
global motor_direction
with self._lock:
pz.spinRight(motor_speed)
motor_direction = 'Spin Left'
return motor_direction
def stop(self):
global motor_direction
with self._lock:
pz.stop()
motor_direction = 'Stop'
return motor_direction