def __init__(self, analog_pin=2, poll_time=5, differential=5): """ :param analog_pin: arduino analog input pin number :param poll_time: polling interval in seconds :param differential: difference between current value and previous value to consider the change in value a change event """ self.analog_pin = analog_pin self.poll_time = poll_time self.differential = differential self.loop = asyncio.get_event_loop() # Callback data indices self.CB_PIN_MODE = 0 # pin mode (see pin modes in private_constants.py) self.CB_PIN = 1 # pin number self.CB_VALUE = 2 # reported value self.CB_TIME = 3 # raw time stamp # instantiate pymata_express self.board = pymata_express.PymataExpress() # set the pin mode for analog input self.loop.run_until_complete( self.board.set_pin_mode_analog_input(self.analog_pin, self.the_callback, self.differential)) # start polling try: self.loop.run_until_complete(self.keep_polling()) except KeyboardInterrupt: self.loop.run_until_complete(self.board.shutdown()) sys.exit(0)
""" await my_board.set_pin_mode_analog_input(pin, callback=the_callback, differential=5) # run forever waiting for input changes try: while True: await asyncio.sleep(POLL_TIME) # retrieve both the value and time stamp with each poll value, time_stamp = await board.analog_read(pin) # format the time stamp formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_stamp)) print( f'Reading latest analog input data for pin {pin} = {value} change received on {formatted_time} ' f'(raw_time: {time_stamp})') except KeyboardInterrupt: await my_board.shutdown() sys.exit(0) # get the event loop loop = asyncio.get_event_loop() # instantiate pymata_express board = pymata_express.PymataExpress() try: # start the main function loop.run_until_complete(analog_in(board, 2)) except (KeyboardInterrupt, RuntimeError) as e: loop.run_until_complete(board.shutdown()) sys.exit(0)
:param my_board: an PymataExpress instance :param pin: pin to be controlled """ # set the pin mode await my_board.set_pin_mode_digital_output(pin) # toggle the pin 4 times and exit for x in range(4): print('ON') await my_board.digital_write(pin, 1) await asyncio.sleep(1) print('OFF') await my_board.digital_write(pin, 0) await asyncio.sleep(1) # get the event loop loop = asyncio.get_event_loop() # instantiate pymata_express board = pymata_express.PymataExpress(ip_address=IP_ADDRESS, ip_port=IP_PORT, loop=loop) try: # start the main function loop.run_until_complete(blink(board, DIGITAL_PIN)) except KeyboardInterrupt: loop.run_until_complete(board.shutdown()) sys.exit(0)
def arduino_init(delay=2): if pymata_ex: return pymata_express.PymataExpress(arduino_wait=delay) else: return pymata4.Pymata4(arduino_wait=delay)