152def parse_args(raw_args):
153 """Parses the args into ready to write register_data."""
154
155 parser = argparse.ArgumentParser()
156 parser.add_argument("--mode", type=str, default="analog",
157 help="Mode: analog, calibrationFSC, calibrationOFC, digitalIn, digitalOut default analog")
158 parser.add_argument("--port_p", type=str, default="0",
159 help="Positive Analog Input Port: 0 thru 8 default 0")
160 parser.add_argument("--port_n", type=str, default="8",
161 help="Negative Analog Input Port: 0 thru 8 default 8 (AINCOM)")
162 parser.add_argument("--gain", type=str, default="1",
163 help="Input Amp Gain: 1, 2, 4, 8, 16, 32, 64 default 1")
164 parser.add_argument("--rate", type=str, default="2.5",
165 help="Data Rate in samples per second: 2.5, 5, 10, 15, 25, 30, 50, 60, 100, 500, 1000, 2000, 3750, 7500, 15000, 30000 default 2.5")
166 parser.add_argument("--buffer", type=str, default="off",
167 help="Input Buffer: off, on default off")
168 parser.add_argument("--sdcs", type=str, default="off",
169 help="SDCS sensor direct current source microamps: off, 0.5, 2, 10 default off")
170 parser.add_argument("--pin", type=str, default="D0",
171 help="Specify pin for modes digitalIn and digitalOut: D0, D1, D2, D3 default D0")
172 parser.add_argument("--output", type=str, default="-1",
173 help="Absence means input mode, or specify output for modes digitalIn and digitalOut: 0, 1 default input mode")
174 parser.add_argument("-t", "--test", action="store_true",
175 help="Print JSON of register_data")
176 args = parser.parse_args(raw_args)
177
178
179 global mode
180 mode = args.mode
181 global output
182 output = args.output
183
184
185 register_data['status'] = 0x00
186 if (args.buffer == 'on'):
187 register_data['status'] = 0x02
188 if (args.buffer == 'off'):
189 register_data['status'] = 0x00
190
191
192 register_data['adcon'] = 0x20
193 if (args.gain == '1'):
194 register_data['adcon'] += 0x00
195 if (args.gain == '2'):
196 register_data['adcon'] += 0x01
197 if (args.gain == '4'):
198 register_data['adcon'] += 0x02
199 if (args.gain == '8'):
200 register_data['adcon'] += 0x03
201 if (args.gain == '16'):
202 register_data['adcon'] += 0x04
203 if (args.gain == '32'):
204 register_data['adcon'] += 0x05
205 if (args.gain == '64'):
206 register_data['adcon'] += 0x06
207 if (args.sdcs == 'off'):
208 register_data['adcon'] += 0x00
209 if (args.sdcs == '0.5'):
210 register_data['adcon'] += 0x08
211 if (args.sdcs == '2'):
212 register_data['adcon'] += 0x10
213 if (args.sdcs == '10'):
214 register_data['adcon'] += 0x18
215
216
217 register_data['drate'] = 0x03
218 if (args.rate == '30000'):
219 register_data['drate'] = 0xF0
220 if (args.rate == '15000'):
221 register_data['drate'] = 0xE0
222 if (args.rate == '7500'):
223 register_data['drate'] = 0xD0
224 if (args.rate == '3750'):
225 register_data['drate'] = 0xC0
226 if (args.rate == '2000'):
227 register_data['drate'] = 0xB0
228 if (args.rate == '1000'):
229 register_data['drate'] = 0xA1
230 if (args.rate == '500'):
231 register_data['drate'] = 0x92
232 if (args.rate == '100'):
233 register_data['drate'] = 0x82
234 if (args.rate == '60'):
235 register_data['drate'] = 0x72
236 if (args.rate == '50'):
237 register_data['drate'] = 0x63
238 if (args.rate == '30'):
239 register_data['drate'] = 0x53
240 if (args.rate == '25'):
241 register_data['drate'] = 0x43
242 if (args.rate == '15'):
243 register_data['drate'] = 0x33
244 if (args.rate == '10'):
245 register_data['drate'] = 0x23
246 if (args.rate == '5'):
247 register_data['drate'] = 0x13
248 if (args.rate == '2.5'):
249 register_data['drate'] = 0x03
250
251
252
253 register_data['mux'] = (int(args.port_p) << 4) | int(args.port_n)
254
255
256 register_data['gpio_direction_bitmask'] = 0x10
257 register_data['gpio_status_bitmask'] = 0x01
258 if (args.pin == 'D0'):
259 register_data['gpio_direction_bitmask'] = 0x10
260 register_data['gpio_status_bitmask'] = 0x01
261 if (args.pin == 'D1'):
262 register_data['gpio_direction_bitmask'] = 0x20
263 register_data['gpio_status_bitmask'] = 0x02
264 if (args.pin == 'D2'):
265 register_data['gpio_direction_bitmask'] = 0x40
266 register_data['gpio_status_bitmask'] = 0x04
267 if (args.pin == 'D3'):
268 register_data['gpio_direction_bitmask'] = 0x80
269 register_data['gpio_status_bitmask'] = 0x08
270
271
272 if args.test:
273 output_json()
274 exit()
275
276
277
278
279
280
281
282