def __init__( self, event_loop=None, com_port=None, arduino_instance_id=None, keep_alive=False, log=True, bucket_token=20, bucket_fill_rate=10, **kwargs # 暂未处理启动提醒 ): # set the event loop to be used. accept user's if provided self.event_loop = event_loop if not self.event_loop: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self.event_loop = loop self.log = log # instantiate pymata express to control the arduino # if user want to pass in a com port, then pass it in try: if com_port: self.arduino = PymataExpress(loop=self.event_loop, com_port=com_port) # if user wants to set an instance id, then pass it in elif arduino_instance_id: self.arduino = PymataExpress( loop=self.event_loop, arduino_instance_id=arduino_instance_id) # default settings else: self.arduino = PymataExpress(loop=self.event_loop) except RuntimeError as e: if self.log: logging.exception("Exception occurred", exc_info=True) raise # 正常 self.connect_status = "connected" # extract pin info from self.arduino self.number_of_digital_pins = len(self.arduino.digital_pins) self.number_of_analog_pins = len(self.arduino.analog_pins) self.first_analog_pin = self.arduino.first_analog_pin # Initialize the parent super().__init__(bucket_token=bucket_token, bucket_fill_rate=bucket_fill_rate, **kwargs) self.first_analog_pin = self.arduino.first_analog_pin self.keep_alive = keep_alive
def main(event_loop, cb=None): """ Processes the joystick analog and digital inputs :param event_loop: the event loop to use for execution :param cb: an optional async callback to use """ asyncio.set_event_loop(event_loop) board = PymataExpress() try: pin_setup_task = asyncio.gather( board.set_pin_mode_analog_input(X_PIN, callback=x_handler, differential=DIFFERENTIAL), board.set_pin_mode_analog_input(Y_PIN, callback=y_handler, differential=DIFFERENTIAL), board.set_pin_mode_digital_input_pullup(SW_PIN, callback=switch_handler)) event_loop.run_until_complete(pin_setup_task) reporting_task = asyncio.ensure_future(print_vals(cb)) # Now loop forever printing the recorded values # from the handlers (callbacks) event_loop.run_until_complete(reporting_task) except KeyboardInterrupt: # hide the keyboard interrupt pass finally: for task in asyncio.Task.all_tasks(): task.cancel() event_loop.run_until_complete(board.shutdown()) event_loop.stop()
def __init__(self, *subscriber_list, back_plane_ip_address=None, subscriber_port='43125', publisher_port='43124', process_name='ArduinoGateway', event_loop=None, ): """ Set up the gateway for operation :param subscriber_list: a tuple or list of subscription topics. :param back_plane_ip_address: ip address of backplane or none if local :param subscriber_port: backplane subscriber port :param publisher_port: backplane publisher port :param process_name: name to display on the console :param event_loop: optional parameter to pass in an asyncio event loop """ # set the event loop to be used. accept user's if provided if event_loop: self.event_loop = event_loop else: self.event_loop = asyncio.get_event_loop() # instantiate pymata express to control the arduino self.arduino = PymataExpress(loop=self.event_loop) # extract pin info from self.arduino self.number_of_digital_pins = len(self.arduino.digital_pins) self.number_of_analog_pins = len(self.arduino.analog_pins) self.first_analog_pin = self.arduino.first_analog_pin # Initialize the parent super(ArduinoGateway, self).__init__(subscriber_list=subscriber_list, event_loop=self.event_loop, back_plane_ip_address=back_plane_ip_address, subscriber_port=subscriber_port, publisher_port=publisher_port, process_name=process_name, )
async def get_board(data: Mapping) -> PymataExpress: """Create a Pymata board object.""" board_data = {} if CONF_SERIAL_PORT in data: board_data["com_port"] = data[CONF_SERIAL_PORT] if CONF_SERIAL_BAUD_RATE in data: board_data["baud_rate"] = data[CONF_SERIAL_BAUD_RATE] if CONF_ARDUINO_INSTANCE_ID in data: board_data["arduino_instance_id"] = data[CONF_ARDUINO_INSTANCE_ID] if CONF_ARDUINO_WAIT in data: board_data["arduino_wait"] = data[CONF_ARDUINO_WAIT] if CONF_SLEEP_TUNE in data: board_data["sleep_tune"] = data[CONF_SLEEP_TUNE] board_data["autostart"] = False board_data["shutdown_on_exception"] = True board_data["close_loop_on_shutdown"] = False board = PymataExpress(**board_data) await board.start_aio() return board
async def digital_in(my_board, pin): """ This function establishes the pin as a digital input. Any changes on this pin will be reported through the call back function. :param my_board: a pymata_express instance :param pin: Arduino pin number """ # set the pin mode await my_board.set_pin_mode_digital_input_pullup(pin, callback=the_callback) while True: await my_board.digital_read(pin) while True: await asyncio.sleep(1) loop = asyncio.get_event_loop() board = PymataExpress() try: loop.run_until_complete(digital_in(board, 2)) loop.run_until_complete(board.shutdown()) except KeyboardInterrupt: loop.run_until_complete(board.shutdown()) sys.exit(0)
def __init__(self, *subscriber_list, back_plane_ip_address=None, subscriber_port='43125', publisher_port='43124', process_name='ArduinoGateway', event_loop=None, keep_alive=False, com_port=None, arduino_instance_id=None, log=False): """ Set up the gateway for operation :param subscriber_list: a tuple or list of subscription topics. :param back_plane_ip_address: ip address of backplane or none if local :param subscriber_port: backplane subscriber port :param publisher_port: backplane publisher port :param process_name: name to display on the console :param event_loop: optional parameter to pass in an asyncio event loop :param keep_alive: if True, enable FirmataExpress keep-alives :param com_port: force pymata-express to use this comport :param arduino_instance: set an arduino instance id that must be programmed into the FirmataExpress sketch. :param log: enable logging """ # set up logging if requested self.log = log if self.log: fn = str(pathlib.Path.home()) + "/ardgw.log" self.logger = logging.getLogger(__name__) logging.basicConfig(filename=fn, filemode='w', level=logging.DEBUG) sys.excepthook = self.my_handler # set the event loop to be used. accept user's if provided if event_loop: self.event_loop = event_loop else: self.event_loop = asyncio.get_event_loop() the_loop = self.event_loop # instantiate pymata express to control the arduino # if user want to pass in a com port, then pass it in try: if com_port: self.arduino = PymataExpress(loop=the_loop, com_port=com_port) # if user wants to set an instance id, then pass it in elif arduino_instance_id: self.arduino = PymataExpress( loop=the_loop, arduino_instance_id=arduino_instance_id) # default settings else: self.arduino = PymataExpress(loop=the_loop) except RuntimeError: if self.log: logging.exception("Exception occurred", exc_info=True) raise # extract pin info from self.arduino self.number_of_digital_pins = len(self.arduino.digital_pins) self.number_of_analog_pins = len(self.arduino.analog_pins) self.first_analog_pin = self.arduino.first_analog_pin # Initialize the parent super(ArduinoGateway, self).__init__( subscriber_list=subscriber_list, event_loop=self.event_loop, back_plane_ip_address=back_plane_ip_address, subscriber_port=subscriber_port, publisher_port=publisher_port, process_name=process_name, ) self.first_analog_pin = self.arduino.first_analog_pin self.keep_alive = keep_alive