mirror of
https://github.com/lowobservable/coax.git
synced 2026-04-27 20:48:54 +00:00
Add open_serial_interface function
This commit is contained in:
@@ -1,53 +1,43 @@
|
||||
import sys
|
||||
import time
|
||||
import os
|
||||
from serial import Serial
|
||||
from contextlib import contextmanager
|
||||
|
||||
sys.path.append('..')
|
||||
|
||||
from coax import SerialInterface, poll, poll_ack
|
||||
from coax import open_serial_interface, poll, poll_ack
|
||||
|
||||
DEFAULT_SERIAL_PORT = '/dev/ttyACM0'
|
||||
|
||||
def create_serial():
|
||||
port = os.environ.get('COAX_PORT', DEFAULT_SERIAL_PORT)
|
||||
@contextmanager
|
||||
def open_example_serial_interface(reset=True, poll_flush=True):
|
||||
serial_port = os.environ.get('COAX_PORT', DEFAULT_SERIAL_PORT)
|
||||
|
||||
print(f'Opening {port}...')
|
||||
print(f'Opening {serial_port}...')
|
||||
|
||||
serial = Serial(port, 115200)
|
||||
with open_serial_interface(serial_port, reset=False) as interface:
|
||||
if reset:
|
||||
print('Resetting interface...')
|
||||
|
||||
return serial
|
||||
interface.reset()
|
||||
|
||||
def create_interface(serial, reset=True, poll_flush=True):
|
||||
if 'COAX_FAST_START' not in os.environ:
|
||||
print('Sleeping to allow interface time to wake up...')
|
||||
if interface.legacy_firmware_detected:
|
||||
print(f'Firmware version is {interface.legacy_firmware_version}')
|
||||
|
||||
time.sleep(3)
|
||||
if poll_flush:
|
||||
print('POLLing...')
|
||||
|
||||
interface = SerialInterface(serial)
|
||||
|
||||
if reset:
|
||||
print('Resetting interface...')
|
||||
|
||||
interface.reset()
|
||||
|
||||
if interface.legacy_firmware_detected:
|
||||
print(f'Firmware version is {interface.legacy_firmware_version}')
|
||||
|
||||
if poll_flush:
|
||||
print('POLLing...')
|
||||
|
||||
count = 0
|
||||
|
||||
poll_response = poll(interface, receive_timeout=1)
|
||||
|
||||
while poll_response:
|
||||
poll_ack(interface)
|
||||
|
||||
count += 1
|
||||
count = 0
|
||||
|
||||
poll_response = poll(interface, receive_timeout=1)
|
||||
|
||||
print(f'ACK\'d {count} POLL responses')
|
||||
while poll_response:
|
||||
poll_ack(interface)
|
||||
|
||||
return interface
|
||||
count += 1
|
||||
|
||||
poll_response = poll(interface, receive_timeout=1)
|
||||
|
||||
print(f'ACK\'d {count} POLL responses')
|
||||
|
||||
yield interface
|
||||
|
||||
Reference in New Issue
Block a user