コード例 #1
0
    def __init__(self, port_id='/dev/ttyACM0', bluetooth=True, verbose=True):
        """
        The "constructor" instantiates the entire interface. It starts the operational threads for the serial
        interface as well as for the command handler.
        @param port_id: Communications port specifier (COM3, /dev/ttyACM0, etc)
        @param bluetooth: Sets start up delays for bluetooth connectivity. Set to False for faster start up.
        @param verbose: If set to False, the status print statements are suppressed.
        """
        # Currently only serial communication over USB is supported, but in the future
        # wifi and other transport mechanism support is anticipated

        try:
            # save the user's request if specified
            self.verbose = verbose

            if self.verbose:
                print("\nPython Version %s" % sys.version)
                print('\nPyMata version 2.07  Copyright(C) 2013-15 Alan Yorinks    All rights reserved.')

            # Instantiate the serial support class
            self.transport = PyMataSerial(port_id, self.command_deque)

            # wait for HC-06 Bluetooth slave to initialize in case it is being used.
            if bluetooth:
                time.sleep(5)

            # Attempt opening communications with the Arduino micro-controller
            self.transport.open(self.verbose)

            # additional wait for HC-06 if it is being used
            if bluetooth:
                time.sleep(2)
            else:
                # necessary to support Arduino Mega
                time.sleep(1)

            # Start the data receive thread
            self.transport.start()

            # Instantiate the command handler
            self._command_handler = PyMataCommandHandler(self)
            self._command_handler.system_reset()

            ########################################################################
            # constants defined locally from values contained in the command handler
            ########################################################################

            # Data latch state constants to be used when accessing data returned from get_latch_data methods.
            # The get_latch data methods return [pin_number, latch_state, latched_data, time_stamp]
            # These three constants define possible values for the second item in the list, latch_state

            # this pin will be ignored for latching - table initialized with this value
            self.LATCH_IGNORE = self._command_handler.LATCH_IGNORE
            # When the next pin value change is received for this pin, if it matches the latch criteria
            # the data will be latched.
            self.LATCH_ARMED = self._command_handler.LATCH_ARMED
            # Data has been latched. Read the data to re-arm the latch.
            self.LATCH_LATCHED = self._command_handler.LATCH_LATCHED

            #
            # These constants are used when setting a data latch.
            # Latch threshold types
            #
            self.DIGITAL_LATCH_HIGH = self._command_handler.DIGITAL_LATCH_HIGH
            self.DIGITAL_LATCH_LOW = self._command_handler.DIGITAL_LATCH_LOW

            self.ANALOG_LATCH_GT = self._command_handler.ANALOG_LATCH_GT
            self.ANALOG_LATCH_LT = self._command_handler.ANALOG_LATCH_LT
            self.ANALOG_LATCH_GTE = self._command_handler.ANALOG_LATCH_GTE
            self.ANALOG_LATCH_LTE = self._command_handler.ANALOG_LATCH_LTE

            # constants to be used to parse the data returned from calling
            # get_X_latch_data()

            self.LATCH_PIN = 0
            self.LATCH_STATE = 1
            self.LATCHED_DATA = 2
            self.LATCHED_TIME_STAMP = 3

            # Start the command processing thread
            self._command_handler.start()

            # Command handler should now be prepared to receive replies from the Arduino, so go ahead
            # detect the Arduino board

            if self.verbose:
                print('\nPlease wait while Arduino is being detected. This can take up to 30 seconds ...')

            # perform board auto discovery
            if not self._command_handler.auto_discover_board(self.verbose):
                # board was not found so shutdown
                if self.verbose:
                    print("Board Auto Discovery Failed!, Shutting Down")
                self._command_handler.stop()
                self.transport.stop()
                self._command_handler.join()
                self.transport.join()
                time.sleep(2)

        except KeyboardInterrupt:
            if self.verbose:
                print("Program Aborted Before PyMata Instantiated")
            sys.exit()
コード例 #2
0
    def __init__(self, port_id='/dev/ttyACM0'):
        """
        The "constructor" instantiates the entire interface. It starts the operational threads for the serial
        interface as well as for the command handler.
        @param port_id: Communications port specifier (COM3, /dev/ttyACM0, etc)
        """
        # Currently only serial communication over USB is supported, but in the future
        # wifi and other transport mechanism support is anticipated

        print 'PyMata version 1.58  Copyright(C) 2013-14 Alan Yorinks    Tous droits reserves'
        print 'Traductions francaises par Sebastien Canet'

        # Instantiate the serial support class
        self.transport = PyMataSerial(port_id, self.command_deque)

        # wait for HC-06 Bluetooth slave to initialize in case it is being used.
        time.sleep(5)

        # Attempt opening communications with the Arduino micro-controller
        self.transport.open()

        # additional wait for HC-06 if it is being used
        time.sleep(2)

        # Start the data receive thread
        self.transport.start()

        # Instantiate the command handler
        self._command_handler = PyMataCommandHandler(self)

        ########################################################################
        # constants defined locally from values contained in the command handler
        ########################################################################

        # Data latch state constants to be used when accessing data returned from get_latch_data methods.
        # The get_latch data methods return [pin_number, latch_state, latched_data, time_stamp]
        # These three constants define possible values for the second item in the list, latch_state

        # this pin will be ignored for latching - table initialized with this value
        self.LATCH_IGNORE = self._command_handler.LATCH_IGNORE
        # When the next pin value change is received for this pin, if it matches the latch criteria
        # the data will be latched.
        self.LATCH_ARMED = self._command_handler.LATCH_ARMED
        # Data has been latched. Read the data to re-arm the latch.
        self.LATCH_LATCHED = self._command_handler.LATCH_LATCHED

        #
        # These constants are used when setting a data latch.
        # Latch threshold types
        #
        self.DIGITAL_LATCH_HIGH = self._command_handler.DIGITAL_LATCH_HIGH
        self.DIGITAL_LATCH_LOW = self._command_handler.DIGITAL_LATCH_LOW

        self.ANALOG_LATCH_GT = self._command_handler.ANALOG_LATCH_GT
        self.ANALOG_LATCH_LT = self._command_handler.ANALOG_LATCH_LT
        self.ANALOG_LATCH_GTE = self._command_handler.ANALOG_LATCH_GTE
        self.ANALOG_LATCH_LTE = self._command_handler.ANALOG_LATCH_LTE

        # constants to be used to parse the data returned from calling
        # get_X_latch_data()

        self.LATCH_PIN = 0
        self.LATCH_STATE = 1
        self.LATCHED_DATA = 2
        self.LATCHED_TIME_STAMP = 3

        # Start the command processing thread
        self._command_handler.start()

        # Command handler should now be prepared to receive replies from the Arduino, so go ahead
        # detect the Arduino board

        print 'Merci de patienter pendant la detection de carte Arduino...'

        # perform board auto discovery

        if not self._command_handler.auto_discover_board():
            # board was not found so shutdown
            print "Auto-detection de carte impossible ! Sortie du script."
            self._command_handler.stop()
            self.transport.stop()
            self._command_handler.join()
            self.transport.join()
            time.sleep(2)
コード例 #3
0
ファイル: pymata.py プロジェクト: ChrisNolan/PyMata
    def __init__(self, port_id='/dev/ttyACM0'):
        """
        The constructor instantiates the entire interface. It starts the operational threads for the serial
        interface as well as for the command handler.
        @param port_id: Communications port specifier (COM3, /dev/ttyACM0, etc)
        """
        # Currently only serial communication over USB is supported, but in the future
        # wifi and other transport mechanism support is anticipated

        print 'PyMata version 1.56  Copyright(C) 2013-14 Alan Yorinks    All rights reserved.'

        # Instantiate the serial support class
        self._arduino = PyMataSerial(port_id, self._command_deque)

        # Attempt opening communications with the Arduino micro-controller
        self._arduino.open()
        time.sleep(1)

        # Start the data receive thread
        self._arduino.start()

        # Instantiate the command handler
        self._command_handler = PyMataCommandHandler(self._arduino,
                                                     self._command_deque,
                                                     self._data_lock)

        ########################################################################
        # constants defined locally from values contained in the command handler
        ########################################################################

        # pin modes
        self.INPUT = self._command_handler.INPUT
        self.OUTPUT = self._command_handler.OUTPUT
        self.PWM = self._command_handler.PWM
        self.SERVO = self._command_handler.SERVO
        self.I2C = self._command_handler.I2C
        self.TONE = self._command_handler.TONE
        self.IGNORE = self._command_handler.IGNORE
        self.ENCODER = self._command_handler.ENCODER
        self.DIGITAL = self._command_handler.DIGITAL
        self.SONAR = self._command_handler.SONAR

        # Data latch state constants to be used when accessing data returned from get_latch_data methods.
        # The get_latch data methods return [pin_number, latch_state, latched_data, time_stamp]
        # These three constants define possible values for the second item in the list, latch_state

        # this pin will be ignored for latching - table initialized with this value
        self.LATCH_IGNORE = self._command_handler.LATCH_IGNORE
        # When the next pin value change is received for this pin, if it matches the latch criteria
        # the data will be latched.
        self.LATCH_ARMED = self._command_handler.LATCH_ARMED
        # Data has been latched. Read the data to re-arm the latch.
        self.LATCH_LATCHED = self._command_handler.LATCH_LATCHED

        #
        # These constants are used when setting a data latch.
        # Latch threshold types
        #
        self.DIGITAL_LATCH_HIGH = self._command_handler.DIGITAL_LATCH_HIGH
        self.DIGITAL_LATCH_LOW = self._command_handler.DIGITAL_LATCH_LOW

        self.ANALOG_LATCH_GT = self._command_handler.ANALOG_LATCH_GT
        self.ANALOG_LATCH_LT = self._command_handler.ANALOG_LATCH_LT
        self.ANALOG_LATCH_GTE = self._command_handler.ANALOG_LATCH_GTE
        self.ANALOG_LATCH_LTE = self._command_handler.ANALOG_LATCH_LTE

        # constants to be used to parse the data returned from calling
        # get_X_latch_data()

        self.LATCH_PIN = 0
        self.LATCH_STATE = 1
        self.LATCHED_DATA = 2
        self.LATCHED_TIME_STAMP = 3

        # Start the command processing thread
        self._command_handler.start()

        # Command handler should now be prepared to receive replies from the Arduino, so go ahead
        # detect the Arduino board

        print 'Please wait while Arduino is being detected. This can take up to 30 seconds ...'

        # perform board auto discovery

        if not self._command_handler.auto_discover_board():
            # board was not found so shutdown
            print "Board Auto Discovery Failed!, Shutting Down"
            self._arduino.close()
            time.sleep(2)
            sys.exit(0)