Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Commit 9e0049b

Browse files
committed
Add support for generic commands
This includes passing them through to the arduino as well as wrapping the responses in common boilerplate for interpretation by robot-api.
1 parent 97e4676 commit 9e0049b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

robotd/devices.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def start(self):
301301
self.make_safe()
302302
print('Finished initialising servo assembly on {}'.format(device))
303303

304-
def _command(self, *args) -> List[str]:
304+
def _command(self, *args, generic_command=False) -> List[str]:
305305
command_id = random.randint(1, 65535)
306306

307307
while True:
@@ -344,7 +344,7 @@ def _command(self, *args) -> List[str]:
344344
return results
345345

346346
elif line.startswith(b'- '):
347-
if b'unknown command' in line:
347+
if b'unknown command' in line and not generic_command:
348348
break # try again
349349
else:
350350
raise CommandError(
@@ -362,6 +362,12 @@ def _command(self, *args) -> List[str]:
362362
else:
363363
raise InvalidResponse(args, line)
364364

365+
except InvalidResponse:
366+
if generic_command:
367+
raise
368+
else:
369+
break
370+
365371
except ValueError:
366372
break
367373

@@ -407,6 +413,19 @@ def _read_ultrasound(self, trigger_pin, echo_pin):
407413

408414
self._ultrasound_value = list(sorted(found_values))[1] / 1000.0
409415

416+
def _generic_command(self, command):
417+
try:
418+
return {
419+
'status': 'ok',
420+
'data': self._command(*command),
421+
}
422+
except (CommandError, InvalidResponse) as e:
423+
return {
424+
'status': 'error',
425+
'type': type(e).__name__,
426+
'description': str(e),
427+
}
428+
410429
def status(self):
411430
return {
412431
'servos': self._servo_status,
@@ -449,6 +468,11 @@ def command(self, cmd):
449468
if len(read_ultrasound) == 2:
450469
self._read_ultrasound(read_ultrasound[0], read_ultrasound[1])
451470

471+
# handle direct command access
472+
command = cmd.get('command', [])
473+
if command:
474+
return self._generic_command(command)
475+
452476

453477
# Grab the full list of boards from the workings of the metaclass
454478
BOARDS = BoardMeta.BOARDS

0 commit comments

Comments
 (0)