4141PIN_MODES = collections .namedtuple (
4242 'PIN_MODES' , ['INPUT' , 'OUTPUT' ], verbose = False )(INPUT = 0 , OUTPUT = 1 )
4343
44+ class ProtocolException (Exception ):
45+ pass
46+
47+ class ProtocolIOException (ProtocolException ):
48+ def __init__ (self ,what ):
49+ ProtocolException .__init__ (self , "Protocol IO error: %s" % (what ))
50+
51+ class ProtocolSetupException (ProtocolException ):
52+ def __init__ (self , what ):
53+ ProtocolException .__init__ (self , "Setup error: %s" % (what ))
4454
4555class ArduinoInterfaceSingleton ():
4656 _instance = None
@@ -53,16 +63,16 @@ def get_instance(protocol_config=None, conn_setup=None):
5363 serial_conn = None
5464 try :
5565 serial_conn = SerialConnection (** conn_setup )
56- except Exception , err :
66+ except ConnectionException , err :
5767 lg .logger_ .info ("[PROTOCOL] Error while loading SerialConnection. "
58- "Err msg : {0}" .format (err ))
68+ "Err info : {0}" .format (err ))
5969 raise
6070
6171 protocol_config = protocol_config ._replace (connection = serial_conn )
6272 ArduinoInterfaceSingleton ._instance = BuildSerial (protocol_config )
6373
6474 if ArduinoInterfaceSingleton ._instance is None :
65- raise Exception ("ArduinoInterface was not configured." )
75+ raise ValueError ("ArduinoInterface was not configured." )
6676
6777 return ArduinoInterfaceSingleton ._instance
6878
@@ -108,31 +118,31 @@ def get_version(self):
108118
109119 def set_pwm (self , pin , duty_cicle ):
110120 if port in self ._anlg_inputs or port in self ._digital_inputs :
111- raise Exception ("Port %s is configured as input!" % port )
121+ raise ProtocolSetupException ("Port %s is configured as input!" % port )
112122
113123 self ._connection .send (_PROTOCOL_CMDS ["ANALAOG_WRITE" ] % (
114124 chr (pin ), chr ((duty_cicle & 0xFF00 ) >> 8 ), chr (duty_cicle & 0x00FF )))
115125
116126 def set_precision (self , bits ):
117127 if bits > 32 or bits < 1 :
118- raise Exception ("Precision bits must be between 1 and 32!" )
128+ raise ValueError ("Precision bits must be between 1 and 32!" )
119129 self ._connection .send (_PROTOCOL_CMDS ["ANALOG_PRECISION" ] % chr (bits ))
120130 self ._anlg_precition = bits
121131
122132 def __set_pin_mode (self , port , mode ):
123133 if mode not in PIN_MODES ._asdict ().values ():
124- raise Exception ("Pind mode error. Unknown mode: %s. Modes availables: %s " %
134+ raise ValueError ("Pind mode error. Unknown mode: %s. Modes availables: %s " %
125135 (mode , str (PIN_MODES_asdict ().keys ())))
126136
127137 self ._connection .send (
128138 _PROTOCOL_CMDS ["PIN_MODE" ] % (chr (port ), chr (mode )))
129139
130140 def set_report_mode (self , mode , read_count = 1 , read_delay = 0 ):
131141 if mode not in REPORT_MODES ._asdict ().values ():
132- raise Exception ("Report mode error. Unknown value: %s" % mode )
142+ raise ValueError ("Report mode error. Unknown value: %s" % mode )
133143
134144 if read_count <= 0 :
135- raise Exception ("Read count value must be > 0" )
145+ raise ValueError ("Read count value must be > 0" )
136146
137147 self ._report_mode = mode
138148 self ._read_count = read_count - 1
@@ -143,7 +153,7 @@ def set_report_mode(self, mode, read_count=1, read_delay=0):
143153
144154 def add_input (self , port ):
145155 if port in self ._anlg_outputs or port in self ._digital_outputs :
146- raise Exception ("Pin %s is configured as output!" % port )
156+ raise ProtocolSetupException ("Pin %s is configured as output!" % port )
147157
148158 self .__validate_pin (port )
149159
@@ -161,7 +171,7 @@ def add_input(self, port):
161171
162172 def add_output (self , port ):
163173 if port in self ._anlg_inputs or port in self ._digital_inputs :
164- raise Exception ("Port %s is configured as input!" % port )
174+ raise ProtocolSetupException ("Port %s is configured as input!" % port )
165175
166176 self .__validate_pin (port )
167177
@@ -178,7 +188,7 @@ def add_output(self, port):
178188
179189 def __validate_pin (self , pin ):
180190 if pin not in self ._board ["DIGITAL_PINS" ] and pin not in self ._board ["ANALOG_PINS" ]:
181- raise Exception ("Invalid pin %s for board %s" %
191+ raise ValueError ("Invalid pin %s for board %s" %
182192 (pin , self ._board ["NAME" ]))
183193
184194 def reset (self ):
@@ -200,7 +210,7 @@ def actuate(self, data):
200210 # Sets as payload every digital or analog port
201211 for i in data :
202212 if i [0 ] not in self ._anlg_outputs and i [0 ] not in self ._digital_outputs :
203- raise Exception ("Port %s not configured as output!" % i [0 ])
213+ raise ProtocolSetupException ("Port %s not configured as output!" % i [0 ])
204214 if i [0 ] in self ._anlg_outputs :
205215 payload = "" .join (
206216 [payload , chr (i [0 ]), chr ((i [1 ] & 0xFF00 ) >> 8 ), chr (i [1 ] & 0x00FF )])
@@ -212,20 +222,21 @@ def actuate(self, data):
212222 self ._connection .send (
213223 "" .join ([_PROTOCOL_CMDS ["ACTUATE" ], chr ((size & 0xFF000000 ) >> 24 ), chr ((size & 0x00FF0000 ) >> 16 ),
214224 chr ((size & 0x0000FF00 ) >> 8 ), chr (size & 0x000000FF ), payload ]))
215-
225+ #FIXME catch connection exceptions
216226 response = self ._connection .recv (1 )
217227
218228 if response == _PROTOCOL_CMDS ["ACK" ]:
219229 response = self ._connection .recv (4 ) # Clears buffer
220- raise Exception ("Actuate error. Code: %s" % response )
230+ raise ProtocolIOException ("Actuate error. Code: %s" % response )
221231
222232 if response != _PROTOCOL_CMDS ["ACTUATE_REPORT" ]:
223233 response = self ._connection .recv (4 )
224- raise Exception (
234+ raise ProtocolIOException (
225235 "Actuate error. Unknown response %s after actuate operation" % ord (response ))
226-
236+ # FIXME catch connection exceptions
227237 raw_len = self ._connection .recv (4 )
228238 length = (ord (raw_len [0 ]) << 24 ) + (ord (raw_len [1 ]) << 16 ) + (ord (raw_len [2 ]) << 8 ) + ord (raw_len [3 ])
239+ # FIXME catch connection exceptions
229240 data = self ._connection .recv (length )
230241
231242 pos = 0
@@ -261,8 +272,8 @@ def actuate(self, data):
261272 (sum (results ["D%d" % (pin )]) * 2 ) > (self ._read_count + 1 )]
262273
263274 else :
264- raise Exception (
265- "Unknown port \" %d\" in response. Restart Arduino board, your software and pray " % pin )
275+ raise ProtocolIOException (
276+ "Unknown port \" %d\" in response. Please, restart the Arduino board and the experiment. " % pin )
266277
267278 return results
268279
@@ -287,11 +298,9 @@ def __new__(cls, connection, report_mode=REPORT_MODES.AVERAGE, read_count=2, rea
287298
288299
289300def BuildSerial (protocol_config ):
290- interface = ArduinoInterface (
291- protocol_config .connection , protocol_config .board_type )
301+ interface = ArduinoInterface (protocol_config .connection , protocol_config .board_type )
292302 interface .reset ()
293- interface .set_report_mode (
294- protocol_config .report_mode , protocol_config .read_count , protocol_config .read_delay )
303+ interface .set_report_mode (protocol_config .report_mode , protocol_config .read_count , protocol_config .read_delay )
295304
296305 interface .set_precision (protocol_config .analog_resolution )
297306
@@ -307,7 +316,6 @@ def BuildSerial(protocol_config):
307316 for port in protocol_config .analog_output_pins :
308317 interface .add_output (port )
309318
310- # FIXME: Make this variable configurable
311- interface .set_precision (12 )
319+ interface .set_precision (protocol_config .analog_resolution )
312320
313321 return interface
0 commit comments