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


Python PyMata.close方法代码示例

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


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

示例1: ArduinoBoard

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [as 别名]
class ArduinoBoard(object):
    """ Represents an Arduino board. """

    def __init__(self, port):
        from PyMata.pymata import PyMata
        self._port = port
        self._board = PyMata(self._port, verbose=False)

    def set_mode(self, pin, direction, mode):
        """ Sets the mode and the direction of a given pin. """
        if mode == 'analog' and direction == 'in':
            self._board.set_pin_mode(pin,
                                     self._board.INPUT,
                                     self._board.ANALOG)
        elif mode == 'analog' and direction == 'out':
            self._board.set_pin_mode(pin,
                                     self._board.OUTPUT,
                                     self._board.ANALOG)
        elif mode == 'digital' and direction == 'in':
            self._board.set_pin_mode(pin,
                                     self._board.OUTPUT,
                                     self._board.DIGITAL)
        elif mode == 'digital' and direction == 'out':
            self._board.set_pin_mode(pin,
                                     self._board.OUTPUT,
                                     self._board.DIGITAL)
        elif mode == 'pwm':
            self._board.set_pin_mode(pin,
                                     self._board.OUTPUT,
                                     self._board.PWM)

    def get_analog_inputs(self):
        """ Get the values from the pins. """
        self._board.capability_query()
        return self._board.get_analog_response_table()

    def set_digital_out_high(self, pin):
        """ Sets a given digital pin to high. """
        self._board.digital_write(pin, 1)

    def set_digital_out_low(self, pin):
        """ Sets a given digital pin to low. """
        self._board.digital_write(pin, 0)

    def get_digital_in(self, pin):
        """ Gets the value from a given digital pin. """
        self._board.digital_read(pin)

    def get_analog_in(self, pin):
        """ Gets the value from a given analog pin. """
        self._board.analog_read(pin)

    def get_firmata(self):
        """ Return the version of the Firmata firmware. """
        return self._board.get_firmata_version()

    def disconnect(self):
        """ Disconnects the board and closes the serial connection. """
        self._board.reset()
        self._board.close()
开发者ID:FanaHOVA,项目名称:home-assistant,代码行数:62,代码来源:arduino.py

示例2: print

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

signal.signal(signal.SIGINT, signal_handler)

# send the arduino a firmata reset
firmata.reset()

# configure the stepper to use pins 9.10,11,12 and specify 512 steps per revolution
firmata.stepper_config(512, [12, 11, 10, 9])

# allow time for config to complete
time.sleep(.5)

# ask Arduino to return the stepper library version number to PyMata
firmata.stepper_request_library_version()

# allow time for command and reply to go across the serial link
time.sleep(.5)

print("Stepper Library Version",)
print(firmata.get_stepper_version())

# move motor #0 500 steps forward at a speed of 20
firmata.stepper_step(20, 500)

# move motor #0 500 steps reverse at a speed of 20
firmata.stepper_step(20, -500)

# close firmata
firmata.close()
开发者ID:MrYsLab,项目名称:PyMata,代码行数:31,代码来源:stepper_demo.py

示例3: s2a_fm

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [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

示例4: pair

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

#.........这里部分代码省略.........
        # A row entry consists of 2 bytes, the first always being 0 and the second
        # being the state of the pixel (high or low)
        """

        @param row: pixel row number
        @param column: pix column number
        @param color: pixel color (yellow is both red and green both on)
        @param suppress_write: if true, just sets the internal data structure, else writes out the pixel to the display
        """
        if (row < 0) or (row >= 8):
            print "set_pixel(): ROW out of range"
            return
        if (column < 0) or (column >= 8):
            print "set_pixel(): COLUMN out of range"
            return

        self.display_buffer[row][column] = color

        # output changes to row on display

        green = 0
        red = 0

        # calculate row for green rows and then adjust it for red

        for col in range(0, 8):
            # assemble green data for the row and output
            if self.display_buffer[row][col] == self.LED_GREEN:
                green |= 1 << col
            elif self.display_buffer[row][col] == self.LED_RED:
                red |= 1 << col
            elif self.display_buffer[row][col] == self.LED_YELLOW:
                green |= 1 << col
                red |= 1 << col
            elif self.display_buffer[row][col] == self.LED_OFF:
                green &= ~(1 << col)
                red &= ~(1 << col)

        if suppress_write == False:
            self.firmata.i2c_write(0x70, row * 2, 0, green)
            self.firmata.i2c_write(0x70, row * 2 + 1, 0, red)

    def set_bit_map(self, shape, color):
        """
        Populate the bit map with the supplied "shape" and color
        and then write the entire bitmap to the display
        @param shape: pattern to display
        @param color: color for the pattern
        """
        for row in xrange(0, 8):
            data = shape[row]
            # shift data into buffer
            bit_mask = 0x80
            for column in xrange(0, 8):
                if data & bit_mask:
                    self.set_pixel(row, column, color, True)
                bit_mask >>= 1
        self.output_entire_buffer()

    def output_entire_buffer(self):
        """
        Write the entire buffer to the display
        """
        green = 0
        red = 0

        for row in xrange(0, 8):
            for col in xrange(0, 8):
                if self.display_buffer[row][col] == self.LED_GREEN:
                    green |= 1 << col
                elif self.display_buffer[row][col] == self.LED_RED:
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_YELLOW:
                    green |= 1 << col
                    red |= 1 << col
                elif self.display_buffer[row][col] == self.LED_OFF:
                    green &= ~(1 << col)
                    red &= ~(1 << col)

            self.firmata.i2c_write(0x70, row * 2, 0, green)
            self.firmata.i2c_write(0x70, row * 2 + 1, 0, red)


    def clear_display_buffer(self):
        """
        Set all led's to off.
        """
        for row in range(0, 8):
            self.firmata.i2c_write(0x70,  row * 2, 0, 0)
            self.firmata.i2c_write(0x70,  (row * 2) + 1, 0, 0)

            for column in range(0, 8):
                self.display_buffer[row][column] = 0

    def close(self):
        """
        close the interface down cleanly

        """
        self.firmata.close()
开发者ID:Haobot,项目名称:PyMata,代码行数:104,代码来源:bicolor_display_controller.py

示例5: PyMata

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [as 别名]
# create a PyMata instance
board = PyMata("/dev/ttyACM0")


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)
# control the servo - note that you don't need to set pin mode
# configure the servo
board.servo_config(SERVO_MOTOR)

while (1):
    # move the servo to 20 degrees
    board.analog_write(SERVO_MOTOR, 20)
    time.sleep(1)

    # move the servo to 100 degrees
    board.analog_write(SERVO_MOTOR, 100)
    time.sleep(1)

# move the servo to 20 degrees
board.analog_write(SERVO_MOTOR, 20)

# close the interface down cleanly
board.close()
开发者ID:solishi,项目名称:woodbot,代码行数:31,代码来源:servo_demo.py

示例6: PyMata

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [as 别名]
# This code is supporting material for the book
# Python Programming for Arduino
# by Pratik Desai
# published by PACKT Publishing

import time
from PyMata.pymata import PyMata

#Initializing Arduino using PyFirmata constructor
port = PyMata("COM5")

#Configure I2C pin
port.i2c_config(0, port.ANALOG, 4, 5)

# One shot read asking peripheral to send 2 bytes
port.i2c_read(0x23, 0, 2, port.I2C_READ)
# Wait for peripheral to send the data
time.sleep(3)

# Read from the peripheral
data = port.i2c_get_read_data(0x23)

# Obtain lux values from received data
LuxSum = (data[1] << 8 | data[2]) >> 4

lux = LuxSum/1.2
print str(lux) + ' lux'

port.close()
开发者ID:HanBinTsao,项目名称:python-x-arduino-book,代码行数:31,代码来源:PyMata_Example_BH1750.py

示例7: __init__

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [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

示例8: input

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [as 别名]
@description: Blink Arduino mediante Firmata
@url: http://www.aprendiendoarduino.com/
'''


import time
import sys
from PyMata.pymata import PyMata

BOARD_LED = 13
port = input("Puerto Serie Arduino: ")

try:
    arduino = PyMata(port, verbose=True)

    for x in range(10):
        print(x + 1)
        # Set the output to 1 = High
        arduino.digital_write(BOARD_LED, 1)
        # Wait a half second between toggles.
        time.sleep(.5)
        # Set the output to 0 = Low
        arduino.digital_write(BOARD_LED, 0)
        time.sleep(.5)

    # Close PyMata when we are done
    arduino.close()
except: # catch *all* exceptions
    e = sys.exc_info()[0]
    print("Puerto no válido. %s" % e)
开发者ID:jecrespo,项目名称:Aprendiendo-Arduino,代码行数:32,代码来源:blink.py

示例9: Firmata

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [as 别名]
class Firmata(Adaptor):

    def __init__(self, options):
        super(Firmata, self).__init__(options)

        if 'port' not in options:
            raise self.ParameterRequired(
                'A port must be specified for Firmata connection.'
            )

        self.port = options.get('port')
        self.board = PyMata('/dev/ttyACM0', verbose=True)

        signal.signal(signal.SIGINT, self.signal_handler)

        self.pins = {
            'digital': [],
            'analog': [],
            'pwm': [],
            'servo': [],
            'i2c': [],
        }

    def analog_write(self, pin_number, value):
        if pin_number not in self.pins['analog']:
            self.pins['analog'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.OUTPUT,
                self.board.ANALOG
            )

        self.board.analog_write(pin_number, value)

    def analog_read(self, pin_number):
        if pin_number not in self.pins['analog']:
            self.pins['analog'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.INPUT,
                self.board.ANALOG
            )

        return self.board.analog_read(pin_number)

    def digital_write(self, pin_number, value):
        if pin_number not in self.pins['digital']:
            self.pins['digital'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.OUTPUT,
                self.board.DIGITAL
            )

        self.board.digital_write(pin_number, value)

    def digital_read(self, pin_number):
        if pin_number not in self.pins['digital']:
            self.pins['digital'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.INPUT,
                self.board.DIGITAL
            )

        return self.board.analog_write(pin_number)

    def pwm_write(self, pin_number, value):
        if pin_number not in self.pins['pwm']:
            self.pins['pwm'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.PWM,
                self.board.DIGITAL
            )

        return self.board.analog_write(pin_number, value)

    def pwm_read(self, pin_number):
        if pin_number not in self.pins['pwm']:
            self.pins['pwm'].append(pin_number)
            self.board.set_pin_mode(
                pin_number,
                self.board.PWM,
                self.board.DIGITAL
            )

        return self.board.analog_read(pin_number)

    def servo_write(self, pin_number, value):
        if pin_number not in self.pins['servo']:
            self.pins['servo'].append(pin_number)
            self.board.servo_config(pin_number)

        self.board.analog_write(pin_number, value)

    def disconnect(self):
        # Close the firmata interface down cleanly
        self.board.close()

#.........这里部分代码省略.........
开发者ID:zorg,项目名称:zorg-firmata,代码行数:103,代码来源:adaptor.py

示例10: s2a_fm

# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import close [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.0    Copyright(C) 2013 Alan Yorinks    All Rights Reserved ')
    print 's2a_fm version 1.0    Copyright(C) 2013 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()

    # 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)

    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()
        return

    except KeyboardInterrupt:
        # give control back to the shell that started us
        logging.info('s2a_fm.py: keyboard interrupt exception')
        firmata.close()
        return
开发者ID:rsmont,项目名称:s2a_fm,代码行数:81,代码来源:s2a_fm.py


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