-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuartPractice.py
More file actions
105 lines (89 loc) · 2.29 KB
/
Copy pathuartPractice.py
File metadata and controls
105 lines (89 loc) · 2.29 KB
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
99
100
101
102
103
104
105
import serial
import RPi.GPIO as IO
import time
commands = []
duty = 0
def PrintDuty():
print('Duty Cycle is : ' + str(duty))
def IncreaseDuty10():
global duty
duty = duty + 10
pwm.ChangeDutyCycle(duty)
def DecreaseDuty10():
global duty
duty = duty - 10
pwm.ChangeDutyCycle(duty)
def SetDuty():
global duty
port.write('New Duty : ')
duty = int(readLine())
pwm.ChangeDutyCycle(duty)
def convert(l) : # convert list of chars to string
s = ""
for c in l :
s += c
return s
def readLine():
global line
line = []
while True :
for c in port.read() :
port.write(c)
if c == '\n' or c == '\r':
port.write('\r\n')
return convert(line) #returns a string, not a list
if c == '\x08' : #Backspace
port.write(' ') #erase last letter from screen
port.write(c)
line.pop() # erase last letter from 'list'
else :
line.append(c)
def InterpretCommand(cmd):
a = 0 #index of command
for i in range(len(commands)) :
if str(commands[i]) == cmd :
a = i + 1
break
print(a)
switcher = {
1 : PrintDuty,
2 : IncreaseDuty10,
3 : DecreaseDuty10,
4 : SetDuty
}
func = switcher.get(a, lambda: "Invalid Command")
func()
def readBT():
global port
global line
cmd = readLine() # updates 'line'
InterpretCommand(cmd) # calls its corresponding function
def commandsInit():
commands.append('PrintDuty')
commands.append('IncreaseDuty10')
commands.append('DecreaseDuty10')
commands.append('SetDuty')
def PWMsetup(pin, freq):
IO.setwarnings(False)
IO.setmode(IO.BCM)
IO.setup(pin, IO.OUT)
p = IO.PWM(pin, freq)
p.start(0)
return p
def UARTsetup():
port = serial.Serial("/dev/ttyS0", baudrate = 9600, timeout = 3.0)
port.close()
port.open()
port.write('Starting UART connection...\r\n')
return port
port = UARTsetup()
pwm = PWMsetup(19,100)
line = []
def main(args):
commandsInit()
while True:
readBT()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))