11
22_PROTOCOL_CMDS = { "ANALOG_PRECISION" : '\x01 \x01 %s' ,
3- "ANALOG_INPUT " : '\x02 \x01 %s' ,
4- "ANALOG_OUTPUT " : '\x03 \x01 %s' ,
3+ "SET_INPUT " : '\x02 \x01 %s' ,
4+ "PIN_OUTPUT " : '\x03 \x01 %s' ,
55 "PIN_MODE" : '\x04 \x02 %s%s' ,
66 "REPORT_MODE" : '\x05 \x03 %s%s%s' ,
77 "ACK" : '\xFF \x00 ' ,
@@ -12,11 +12,14 @@ class ArduinoInterface:
1212 PIN_MOD = {"INPUT" :0 , "OUTPUT" :1 } # 0=input 1=output -- wiring_constants.h
1313 REPORT_MOD = {"AVERAGE" :0 , "BULK" :1 , "RT" :2 }
1414
15- def __init__ (self , connection ):
15+ def __init__ (self , connection , board ):
1616 self ._connection = connection
1717 self ._anlg_inputs = []
18+ self ._digital_inputs = []
1819 self ._anlg_outputs = []
19- self ._anlg_precition = 10 #Default Arduino analog precition
20+ self ._digital_outputs = []
21+ self ._anlg_precition = 10 #Default Arduino analog precision
22+ self ._board = board
2023
2124 def set_precition (self , bits ):
2225 if bits > 32 or bits < 1 :
@@ -35,27 +38,49 @@ def set_report_mode(self, mode, read_count=1, read_delay=0):
3538 raise Exception ("Report mode error. Unknown value: %s" % mode )
3639
3740 self ._connection .send (_PROTOCOL_CMDS ["REPORT_MODE" ] % (chr (self .REPORT_MOD [mode ]), chr (read_count ), chr (read_delay )))
38-
41+
3942
40- def add_analog_input (self , port ):
41- if port in self ._anlg_outputs :
42- raise Exception ("Port %s is configured as output!" % port )
43-
44- if port not in self ._anlg_inputs :
45- self ._connection .send (_PROTOCOL_CMDS ["ANALOG_INPUT" ] % chr (port ))
43+ def add_input (self , port ):
44+ if port in self ._anlg_outputs or port in self ._digital_outputs :
45+ raise Exception ("Pin %s is configured as output!" % port )
46+
47+ self ._validate_pin (port )
48+
49+ # Determines if we are setting as input an analog port
50+ if port not in self ._anlg_inputs and port in self ._board ["ANALOG_PINS" ]:
51+ # The "analogRead" arduino function maps the ADC pins not by their base pin number but from their relative ADC pin number
52+ self ._connection .send (_PROTOCOL_CMDS ["SET_INPUT" ] % chr (port - min (self ._board ["ANALOG_PINS" ])))
4653 self ._anlg_inputs .append (port )
4754
48- def add_analog_output (self , port ):
49- if port in self ._anlg_inputs :
55+ # Determines if we are setting as input a Digital port
56+ if port not in self ._digital_inputs and port in self ._board ["DIGITAL_PINS" ]:
57+ self ._connection .send (_PROTOCOL_CMDS ["SET_INPUT" ] % chr (port ))
58+ self ._digital_inputs .append (port )
59+
60+
61+ def add_output (self , port ):
62+ if port in self ._anlg_inputs or port in self ._digital_inputs :
5063 raise Exception ("Port %s is configured as input!" % port )
5164
65+ self ._validate_pin (port )
66+
5267 if port not in self ._anlg_outputs :
53- self ._connection .send (_PROTOCOL_CMDS ["ANALOG_OUTPUT " ] % chr (port ))
68+ self ._connection .send (_PROTOCOL_CMDS ["PIN_OUTPUT " ] % chr (port ))
5469 self ._anlg_outputs .append (port )
5570
71+ # Determines if we are setting as input a Digital port
72+ if port not in self ._digital_outputs and port in self ._board ["DIGITAL_PINS" ]:
73+ self ._connection .send (_PROTOCOL_CMDS ["PIN_OUTPUT" ] % chr (port ))
74+ self ._digital_outputs .append (port )
75+
76+ def _validate_pin (self , pin ):
77+ if pin not in self ._board ["DIGITAL_PINS" ] and pin not in self ._board ["ANALOG_PINS" ]:
78+ raise Exception ("Invalid pin %s for board %s" % (pin , self ._board ["NAME" ]))
79+ return
80+
5681 def actuate (self , data ):
5782 """
58- Actuate over the input port sent as parameters
83+ Actuate over the parametrized output pins
5984 All the ports must has been configured as output!
6085
6186 arguments:
@@ -65,10 +90,14 @@ def actuate(self, data):
6590 size = 0
6691 #TODO: Ver como validar puertos digitales
6792 for i in data :
68- if i [0 ] not in self ._anlg_outputs :
93+ if i [0 ] not in self ._anlg_outputs and i [ 0 ] not in self . _digital_outputs :
6994 raise Exception ("Port %s not configured as output!" % i [0 ])
70- payload = "" .join ([payload , chr (i [0 ]), chr ((i [1 ] & 0xFF00 ) >> 8 ), chr (i [1 ] & 0x00FF )])
71- size += 3
95+ if i [0 ] in self ._anlg_outputs :
96+ payload = "" .join ([payload , chr (i [0 ]), chr ((i [1 ] & 0xFF00 ) >> 8 ), chr (i [1 ] & 0x00FF )])
97+ size += 3
98+ if i [0 ] in self ._digital_outputs :
99+ payload = "" .join ([payload , chr (i [0 ]), chr (i [1 ])])
100+ size += 2
72101
73102 self ._connection .send ("" .join ([_PROTOCOL_CMDS ["ACTUATE" ], chr (size ), payload ]))
74103 response = self ._connection .recv (1 )
0 commit comments