-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteArduino.py
More file actions
208 lines (161 loc) · 7.15 KB
/
RemoteArduino.py
File metadata and controls
208 lines (161 loc) · 7.15 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import serial
import time
from decimal import *
EOR = ''.join([chr(13), chr(10)])
class RemoteArduino:
""" Class to control an Arduino remotely. A light Firmata-like thing. """
def __init__(self, port):
""" Open a connection to an arduino
port: Serial port device name, for example: '/dev/ttyACM0'
"""
self.serial = serial.Serial(port, baudrate=19200, timeout=10.0)
def sendCommand(self, command, args=None):
if (args is not None):
command += ' ' + args;
self.serial.write(command)
self.serial.write(EOR)
response = self.serial.readline()
#make sure we have a complete response
if (not response.endswith(EOR)):
raise Exception("Bad response for command '%s'. End of response not found. Timed out." % command)
#clean up
response = response.rstrip(EOR)
tokens = response.split()
#make sure that the response starts with OK or ERR
try:
if (tokens[0] not in {'OK', 'ERR'}):
raise Exception("Bad response for command '%s'. Response starts with '%s' and not OK or ERR." % (command, tokens[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Response is empty." % command)
#check if the response is an error
try:
if (tokens[0] == 'ERR'):
raise Exception("Error response '%s' for command '%s'" % (tokens[1], command))
except IndexError:
raise Exception("Bad response for command '%s'. Missing error code." % command)
tokens.pop(0)
return tokens
def version(self):
""" Read the arduino sketch version. """
response = self.sendCommand('VERSION')
try:
value = Decimal(response[0])
value = float(value)
except ValueError:
raise Exception("Bad response for command '%s'. Version '%s' is not a number." % ('VERSION', response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'VERSION')
return value
def setBoard(self, board):
""" Configure the Arduino to test a specific type of board. """
response = self.sendCommand('SET_BOARD', board)
try:
if (response[0] != board):
raise Exception("Bad response for command '%s'. Failed to set board %s. Response value is '%s'." % ('SET_BOARD', board, response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'SET_BOARD')
return True
def getBoard(self):
""" Get the type of board the Arduino is configured to test. """
response = self.sendCommand('GET_BOARD')
try:
board = response[0]
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'GET_BOARD')
return board
def led(self, color, status):
""" Enable or disable a relay on the specified pin. """
self.sendCommand('LED', ' '.join([color, status]))
return True
def relay(self, pin, status):
""" Enable or disable a relay on the specified pin. """
self.sendCommand('RELAY', ' '.join([str(pin), status]))
return True
def current(self, voltage='5V'):
""" Read the current from the shield's current sensor. """
response = self.sendCommand('READ_CURRENT', voltage)
try:
value = Decimal(response[0])
value = float(value)
except ValueError:
raise Exception("Bad response for command '%s'. Value '%s' is not a float." % ('READ_CURRENT', response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'READ_CURRENT')
return value
def pinMode(self, pin, mode):
""" Change the mode of a digital pin. """
self.sendCommand('PIN_MODE', ' '.join(['DIGITAL', str(pin), mode]))
def analogWrite(self, pin, value):
""" Change the state of a digital pin configured as output. """
self.sendCommand('WRITE_ANALOG', ' '.join([str(pin), str(value)]))
def digitalWrite(self, pin, value):
""" Change the state of a digital pin configured as output. """
self.sendCommand('WRITE_DIGITAL', ' '.join([str(pin), value]))
def analogRead(self, pin):
""" Read the value of an analog pin. """
response = self.sendCommand('READ_ANALOG', str(pin))
try:
value = int(response[0])
except ValueError:
raise Exception("Bad response for command '%s %s'. Value '%s' is not an integer." % ('READ_ANALOG', str(pin), response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'READ_ANALOG')
return value
def digitalRead(self, pin):
""" Read the value of a digital pin. """
response = self.sendCommand('READ_DIGITAL', str(pin))
try:
if (response[0] in {'0', '1'}):
value = int(response[0])
else:
raise Exception("Bad response for command '%s %s'. Value '%s' is not 0 or 1." % ('READ_DIGITAL', str(pin), response[0]))
except ValueError:
raise Exception("Bad response for command '%s %s'. Value '%s' is not an integer." % ('READ_DIGITAL', str(pin), response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'READ_ANALOG')
return value
def debug(self, status):
""" Enable/disable debug mode. """
self.sendCommand('DEBUG', status)
return True
def initMPR121(self, address):
""" Initializes an MPR121 at the given address.
Returns OK on success, FAILED otherwise. """
self.sendCommand('INIT_MPR121', str(address))
return True
def waitForTouch(self, address, pin, timeout):
""" Get the touch status of the MPR121 at the given address.
Waits for a touch until timeout.
Returns empty touch status if timeout is reached. """
response = self.sendCommand('WAIT_TOUCH', ' '.join([str(address), str(pin), str(timeout)]))
try:
value = int(response[0])
except ValueError:
raise Exception("Bad response for command '%s %s %s %s'. Value '%s' is not an integer." % ('WAIT_TOUCH', str(addr), str(pin), str(timeout), response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'WAIT_TOUCH')
return value
def resetEEPROM(self):
self.sendCommand('RESET_EEPROM')
return True
def writeEEPROM(self, addr, value):
self.sendCommand('WRITE_EEPROM', ' '.join([str(addr), str(value)]))
return True
def readEEPROM(self, addr):
response = self.sendCommand('READ_EEPROM', str(addr))
try:
value = int(response[0])
except ValueError:
raise Exception("Bad response for command '%s %s'. Value '%s' is not an integer." % ('WAIT_TOUCH', str(addr), response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'WAIT_TOUCH')
return value
def idModule(self, dpin, apin):
response = self.sendCommand('ID_MODULE', ' '.join([str(dpin), str(apin)]))
try:
value = int(response[0])
except ValueError:
raise Exception("Bad response for command '%s %s %s'. Value '%s' is not an integer." % ('ID_MODULE', str(dpin), str(apin), response[0]))
except IndexError:
raise Exception("Bad response for command '%s'. Missing value." % 'ID_MODULE')
return value