当前位置: 首页>>代码示例>>Python>>正文


Python PyMata.get_capability_query_results方法代码示例

本文整理汇总了Python中PyMata.pymata.PyMata.get_capability_query_results方法的典型用法代码示例。如果您正苦于以下问题:Python PyMata.get_capability_query_results方法的具体用法?Python PyMata.get_capability_query_results怎么用?Python PyMata.get_capability_query_results使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyMata.pymata.PyMata的用法示例。


在下文中一共展示了PyMata.get_capability_query_results方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: s2a_fm

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import get_capability_query_results [as 别名]
def s2a_fm():

    """
    This is the "main" function of the program.
    It will instantiate PyMata for communication with an Arduino micro-controller
    and the command handlers class.
    It will the start the HTTP server to communicate with Scratch 2.0
    @return : This is the main loop and should never return
    """
    # total number of pins on arduino board
    total_pins_discovered = 0
    # number of pins that are analog
    number_of_analog_pins_discovered = 0

    # make sure we have a log directory and if not, create it.
    if not os.path.exists('log'):
        os.makedirs('log')

    # turn on logging
    logging.basicConfig(filename='./log/s2a_fm_debugging.log', filemode='w', level=logging.DEBUG)
    logging.info('s2a_fm version 1.5    Copyright(C) 2013-14 Alan Yorinks    All Rights Reserved ')
    print 's2a_fm version 1.5   Copyright(C) 2013-14 Alan Yorinks    All Rights Reserved '

    # get the com_port from the command line or default if none given
    # if user specified the com port on the command line, use that when invoking PyMata,
    # else use '/dev/ttyACM0'
    if len(sys.argv) == 2:
        com_port = str(sys.argv[1])
    else:
        com_port = '/dev/ttyACM0'
    logging.info('com port = %s' % com_port)

    try:
        # instantiate PyMata
        firmata = PyMata(com_port)  # pragma: no cover
    except Exception:
        print 'Could not instantiate PyMata - is your Arduino plugged in?'
        logging.exception('Could not instantiate PyMata - is your Arduino plugged in?')
        logging.debug("Exiting s2a_fm")
        return

    # determine the total number of pins and the number of analog pins for the Arduino
    # get the arduino analog pin map
    # it will contain an entry for all the pins with non-analog set to firmata.IGNORE
    firmata.analog_mapping_query()

    capability_map = firmata.get_analog_mapping_request_results()

    firmata.capability_query()
    print "Please wait for Total Arduino Pin Discovery to complete. This can take up to 30 additional seconds."

    # count the pins
    for pin in capability_map:
            total_pins_discovered += 1
            # non analog pins will be marked as IGNORE
            if pin != firmata.IGNORE:
                number_of_analog_pins_discovered += 1

    # log the number of pins found
    logging.info('%d Total Pins and %d Analog Pins Found' % (total_pins_discovered, number_of_analog_pins_discovered))

    # instantiate the command handler
    scratch_command_handler = ScratchCommandHandlers(firmata, com_port, total_pins_discovered,
                                                     number_of_analog_pins_discovered)

    # wait for a maximum of 30 seconds to retrieve the Arduino capability query
    start_time = time.time()

    pin_capability = firmata.get_capability_query_results()
    while not pin_capability:
        if time.time() - start_time > 30:
            print ''
            print "Could not determine pin capability - exiting."
            firmata.close()
            # keep sending out a capability query until there is a response
        pin_capability = firmata.get_capability_query_results()
        time.sleep(.1)

    # we've got the capability, now build a dictionary with pin as the key and a list of all the capabilities
    # for the pin as the key's value
    pin_list = []
    total_pins_discovered = 0
    for entry in pin_capability:
        # bump up pin counter each time IGNORE is found
        if entry == firmata.IGNORE:
            scratch_command_handler.pin_map[total_pins_discovered] = pin_list
            total_pins_discovered += 1
            pin_list = []
        else:
            pin_list.append(entry)

    print "Arduino Total Pin Discovery completed in %d seconds" % (int(time.time() - start_time))

    try:
        # start the server passing it the handle to PyMata and the command handler.
        scratch_http_server.start_server(firmata, scratch_command_handler)

    except Exception:
        logging.debug('Exception in s2a_fm.py %s' % str(Exception))
        firmata.close()
#.........这里部分代码省略.........
开发者ID:2thetop,项目名称:s2a_fm,代码行数:103,代码来源:s2a_fm.py

示例2: signal_handler

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import get_capability_query_results [as 别名]
def signal_handler(sig, frame):
    print('You pressed Ctrl+C')
    if board is not None:
        board.reset()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Create a PyMata instance
board = PyMata("/dev/ttyACM0")

# Set the pin mode
board.servo_config(5)
board.set_pin_mode(12, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(0, board.INPUT, board.ANALOG)

# Send query request to Arduino
board.capability_query()

# Some boards take a long time to respond - adjust as needed
time.sleep(5)
print("Pin Capability Report")
print(board.get_capability_query_results())

print("PyMata Digital Response Table")
print(board.get_digital_response_table())

print("PyMata Analog Response Table")
print(board.get_analog_response_table())

开发者ID:cero2k6,项目名称:PyMata,代码行数:31,代码来源:capability_test.py

示例3: ArduinoFirmata

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import get_capability_query_results [as 别名]
class ArduinoFirmata(Board, AnalogInputCapable, PwmOutputCapable):

    def __init__(self, port=None):
        try:
            from PyMata.pymata import PyMata as PyMata  # noqa
        except ImportError:
            msg = 'pingo.arduino.Arduino requires PyMata installed'
            raise ImportError(msg)

        super(ArduinoFirmata, self).__init__()
        self.port = port
        self.firmata_client = PyMata(self.port, verbose=VERBOSE)

        self.firmata_client.capability_query()
        time.sleep(10)  # TODO: Find a small and safe value
        capability_query_results = self.firmata_client.get_capability_query_results()
        capability_dict = pin_list_to_board_dict(capability_query_results)

        self._add_pins(
            [DigitalPin(self, location)
                for location in capability_dict['digital']] +
            [PwmPin(self, location)
                for location in capability_dict['pwm']] +
            [AnalogPin(self, 'A%s' % location, resolution=10)
                for location in capability_dict['analog']]
        )

    def cleanup(self):
        # self.firmata_client.close() has sys.exit(0)
        if hasattr(self, 'PyMata'):
            try:
                self.firmata_client.transport.close()
            except AttributeError:
                pass

    def __repr__(self):
        cls_name = self.__class__.__name__
        return '<{cls_name} {self.port!r}>'.format(**locals())

    def _set_digital_mode(self, pin, mode):
        self.firmata_client.set_pin_mode(
            pin.location,
            PIN_MODES[mode],
            self.firmata_client.DIGITAL
        )

    def _set_analog_mode(self, pin, mode):
        pin_id = int(pin.location[1:])
        self.firmata_client.set_pin_mode(
            pin_id,
            self.firmata_client.INPUT,
            self.firmata_client.ANALOG
        )

    def _set_pwm_mode(self, pin, mode):
        pin_id = int(pin.location)
        self.firmata_client.set_pin_mode(
            pin_id,
            self.firmata_client.PWM,
            self.firmata_client.DIGITAL
        )

    def _get_pin_state(self, pin):
        _state = self.firmata_client.digital_read(pin.location)
        if _state == self.firmata_client.HIGH:
            return pingo.HIGH
        return pingo.LOW

    def _set_pin_state(self, pin, state):
        self.firmata_client.digital_write(
            pin.location,
            PIN_STATES[state]
        )

    def _get_pin_value(self, pin):
        pin_id = int(pin.location[1:])
        return self.firmata_client.analog_read(pin_id)

    def _set_pwm_duty_cycle(self, pin, value):
        pin_id = int(pin.location)
        firmata_value = int(value * 255)
        return self.firmata_client.analog_write(pin_id, firmata_value)

    def _set_pwm_frequency(self, pin, value):
        raise NotImplementedError
开发者ID:scls19fr,项目名称:pingo-py,代码行数:87,代码来源:firmata.py

示例4: __init__

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import get_capability_query_results [as 别名]
class s2a_fm:

    """
    This is the "main" function of the program.
    It will instantiate PyMata for communication with an Arduino micro-controller
    and the command handlers class.
    It will the start the HTTP server to communicate with Scratch 2.0
    @return : This is the main loop and should never return
    """
    def __init__(self):
        subprocess.Popen("scratch2")
        # total number of pins on arduino board
        self.total_pins_discovered = 0
        # number of pins that are analog
        self.number_of_analog_pins_discovered = 0
        # COM-Port
        self.com_port = "COM1"
        #firmata
        #self.firmata ="-

        # make sure we have a log directory and if not, create it.
        if not os.path.exists('log'):
            os.makedirs('log')

        # turn on logging
        logging.basicConfig(filename='./log/s2a_fm_debugging.log', filemode='w', level=logging.DEBUG)
        logging.info('s2a_fm version 1.5    Copyright(C) 2013-14 Alan Yorinks    All Rights Reserved ')
        print 's2a_fm version 1.5   Copyright(C) 2013-14 Alan Yorinks    All Rights Reserved '

        # get the com_port from the command line or default if none given
        # if user specified the com port on the command line, use that when invoking PyMata,
        # else use '/dev/ttyACM0'
    

    def search_port(self):
        #returns all serial COM-Ports
        possible_ports = serial_ports() 
        print 
        for com_port in possible_ports:
            logging.info('com port = %s' % com_port)
            try:
                # instantiate PyMata
                self.firmata = PyMata(com_port)  # pragma: no cover
                self.com_port = com_port
                return 1
            except Exception:
                print('Could not instantiate PyMata - is your Arduino plugged in?')
                logging.exception('Could not instantiate PyMata on Port %s' % com_port)
        return 0
        #TODO: Ask if User Wants to flash Arduino on this Port
            
        
    def discover_arduino(self):
        # determine the total number of pins and the number of analog pins for the Arduino
        # get the arduino analog pin map
        # it will contain an entry for all the pins with non-analog set to self.firmata.IGNORE
        self.firmata.analog_mapping_query()

        self.capability_map = self.firmata.get_analog_mapping_request_results()

        self.firmata.capability_query()
        print("Please wait for Total Arduino Pin Discovery to complete. This can take up to 30 additional seconds.")

        # count the pins
        for pin in self.capability_map:
                self.total_pins_discovered += 1
                # non analog pins will be marked as IGNORE
                if pin != self.firmata.IGNORE:
                    self.number_of_analog_pins_discovered += 1

        # log the number of pins found
        logging.info('%d Total Pins and %d Analog Pins Found' % (self.total_pins_discovered, self.number_of_analog_pins_discovered))

        # instantiate the command handler
        self.scratch_command_handler = ScratchCommandHandlers(self.firmata, self.com_port, self.total_pins_discovered,
                                                         self.number_of_analog_pins_discovered)

        # wait for a maximum of 30 seconds to retrieve the Arduino capability query
        start_time = time.time()

        pin_capability = self.firmata.get_capability_query_results()
        while not pin_capability:
            if time.time() - start_time > 30:
                print "Could not determine pin capability - exiting."
                self.firmata.close()
                # keep sending out a capability query until there is a response
            pin_capability = self.firmata.get_capability_query_results()
            time.sleep(.1)

        # we've got the capability, now build a dictionary with pin as the key and a list of all the capabilities
        # for the pin as the key's value
        pin_list = []
        total_pins_discovered = 0
        for entry in pin_capability:
            # bump up pin counter each time IGNORE is found
            if entry == self.firmata.IGNORE:
                self.scratch_command_handler.pin_map[total_pins_discovered] = pin_list
                total_pins_discovered += 1
                pin_list = []
            else:
#.........这里部分代码省略.........
开发者ID:mirko314,项目名称:s2a_fm,代码行数:103,代码来源:s2a_fm.py


注:本文中的PyMata.pymata.PyMata.get_capability_query_results方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。