本文整理汇总了Python中PyMata.pymata.PyMata.set_pin_mode方法的典型用法代码示例。如果您正苦于以下问题:Python PyMata.set_pin_mode方法的具体用法?Python PyMata.set_pin_mode怎么用?Python PyMata.set_pin_mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyMata.pymata.PyMata
的用法示例。
在下文中一共展示了PyMata.set_pin_mode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ArduinoBoard
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [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()
示例2: setupLED
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
def setupLED():
global PWM_pin
global board
PWM_pin = 5
# use pin D5 as pwm to adjest LED.
board = PyMata('/dev/ttyS0', verbose = True)
board.set_pin_mode(PWM_pin, board.PWM, board.DIGITAL)
print "Start PWM test by pin D5"
示例3: smartkitdata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
class smartkitdata(object):
def __init__(self):
self.board = PyMata(ARDUINO_ADDRESS)
self.board.set_pin_mode(1, self.board.INPUT, self.board.ANALOG)
self._data = self.gen_data()
def get_data(self):
return pd.DataFrame(self._data)[-60:]
def update_data(self):
self._data = np.vstack([self._data, self.gen_data()])
def gen_data(self):
return np.array(self.board.analog_read(1))
示例4: cb_sensor
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
# Indices for data passed to callback function
PIN_MODE = 0 # This is the PyMata Pin MODE = ANALOG = 2 and DIGITAL = 0x20:
PIN_NUMBER = 1
DATA_VALUE = 2
def cb_sensor(data):
print("Analog Data: ", " Pin: ", data[PIN_NUMBER], " Pin Mode: ", data[PIN_MODE], " Data Value: ", data[DATA_VALUE])
# data = data[2]
# pub.publish(data)
arduino = PyMata("/dev/ttyACM0", verbose=True)
arduino.servo_config(SERVO_MOTOR)
arduino.set_pin_mode(SENSOR, arduino.INPUT, arduino.ANALOG, cb_sensor)
# arduino = serial.Serial('/dev/ttyACM0', 9600);
def callback(data):
servo_pose = data.finger_pose
arduino.analog_write(SERVO_MOTOR, servo_pose)
rospy.loginfo(rospy.get_caller_id() + " I heard %d", servo_pose)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# node are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'talker' node so that multiple talkers can
# run simultaneously.
示例5: ArduinoFirmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
class ArduinoFirmata(Board, AnalogInputCapable):
def __init__(self, port=None):
try:
from PyMata.pymata import PyMata
except ImportError:
msg = 'pingo.arduino.Arduino requires PyMata installed'
raise ImportError(msg)
super(ArduinoFirmata, self).__init__()
self.port = port
self.PyMata = PyMata(self.port)
detected_digital_pins = len(self.PyMata._command_handler.digital_response_table)
detected_analog_pins = len(self.PyMata._command_handler.analog_response_table)
self._add_pins(
[DigitalPin(self, location)
for location in range(detected_digital_pins)] +
[AnalogPin(self, 'A%s' % location, resolution=10)
for location in range(detected_analog_pins)]
)
def cleanup(self):
#self.PyMata.close() has sys.exit(0)
if hasattr(self, 'PyMata'):
try:
self.PyMata.transport.close()
except AttributeError:
pass
def __repr__(self):
cls_name = self.__class__.__name__
return '<{cls_name} {self.port!r}>'.format(**locals())
def _set_pin_mode(self, pin, mode):
self.PyMata.set_pin_mode(
pin.location,
PIN_MODES[mode],
self.PyMata.DIGITAL
)
def _get_pin_state(self, pin):
_state = self.PyMata.digital_read(pin.location)
if _state == self.PyMata.HIGH:
return pingo.HIGH
return pingo.LOW
def _set_pin_state(self, pin, state):
self.PyMata.digital_write(
pin.location,
PIN_STATES[state]
)
def _set_analog_mode(self, pin, mode):
pin_id = int(pin.location[1:])
self.PyMata.set_pin_mode(
pin_id,
self.PyMata.INPUT,
self.PyMata.ANALOG
)
def _get_pin_value(self, pin):
pin_id = int(pin.location[1:])
return self.PyMata.analog_read(pin_id)
示例6: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
import textwrap
import subprocess
import struct
from PyMata.pymata import PyMata
# Give the pins names:
REDPIN = 5
GREENPIN = 3
BLUEPIN = 6
# Create an instance of PyMata.
SERIAL_PORT = "/dev/ttyS0"
firmata = PyMata( SERIAL_PORT, max_wait_time=5 )
# initialize the digital pin as an output.
firmata.set_pin_mode( REDPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( GREENPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( BLUEPIN, firmata.PWM, firmata.DIGITAL)
api_key=''
api_secret=''
access_token_key=''
access_token_secret=''
class DisplayLoop(StreamListener):
"""
This class is a listener for tweet stream data.
It's also callable so it can run the main display
thread loop to update the display.
"""
示例7: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
# Create a PyMata instance
board = PyMata("/dev/ttyUSB0", verbose=True)
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)
# Set digital pin 13 to be an output port
board.set_pin_mode(BOARD_LED, board.OUTPUT, board.DIGITAL)
time.sleep(2)
print("Blinking LED on pin 13 for 10 times ...")
# Blink for 10 times
for x in range(10):
print(x + 1)
# Set the output to 1 = High
board.digital_write(BOARD_LED, 1)
# Wait a half second between toggles.
time.sleep(.5)
# Set the output to 0 = Low
board.digital_write(BOARD_LED, 0)
time.sleep(.5)
示例8: __init__
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
class blech_board:
def __init__(self, port = '/dev/ttyACM0'):
#Warn experimenter that ports are going to switch on, before they are turned off again after initialization
raw_input('WARNING: Switch off your instruments, all ports are going to turn on. Press ANY KEY to continue')
self.board = PyMata(port)
#Look at pymata.py and pymata_command_handler.py if you want to follow the logic. PyMata instances have a _command_handler instance in them - this _command_handler instance has two variables - total_pins_discovered and number_of_analog_pins_discovered. This sets digital pins 0 to (total_pins_discovered - number_of analog_pins_discovered - 1) to 0
dio = self.board._command_handler.total_pins_discovered - self.board._command_handler.number_of_analog_pins_discovered
for i in range(dio):
self.board.digital_write(i, 0)
print 'It is safe to turn your instruments on now. All digital ports have been switched off'
def calibrate(self, port, on_time, repeats, iti):
for i in range(repeats):
sleep(iti)
start = time()*1000.0
self.board.digital_write(port, 1)
while True:
if (time()*1000.0 - start) >= on_time*1000.0:
self.board.digital_write(port, 0)
break
def passive_deliveries_constant(self, ports = [0, 1, 2, 3], iti = 15.0, deliveries_per_channel = 30, on_time = [0.02,0.02,0.02,0.02]):
num_tastes = len(ports)
total_deliveries = num_tastes*deliveries_per_channel
self.delivery_arr = []
for i in range(total_deliveries):
self.delivery_arr.append(i%num_tastes)
shuffle(self.delivery_arr)
for i in self.delivery_arr:
sleep(iti)
start = time()*1000.0
self.board.digital_write(ports[i], 1)
while True:
if (time()*1000.0 - start) >= on_time[i]*1000.0:
self.board.digital_write(ports[i], 0)
break
def clear_tastes(self, ports = [2,3,4,5], open_time = 200.0):
for i in ports:
self.board.digital_write(i, 1)
sleep(open_time)
for i in ports:
self.board.digital_write(port, 0)
def np_no_switch(self, poke_ports = [8, 9], taste_ports = [2, 3], deliveries_per_channel = 50, iti = 5.0, on_time = [0.02, 0.02]):
for i in poke_ports:
self.board.set_pin_mode(i, self.board.INPUT, self.board.DIGITAL)
sleep(2.0)
total_deliveries = len(poke_ports)*deliveries_per_channel
delivery_counter = 0
while (delivery_counter<total_deliveries):
sleep(iti)
for i in range(len(poke_ports)):
if self.board.digital_read(poke_ports[i]) == 0:
start = time()*1000.0
self.board.digital_write(taste_ports[i], 1)
while True:
if (time()*1000.0 - start) >= on_time[i]*1000.0:
self.board.digital_write(taste_ports[i], 0)
break
print 'Trial ', delivery_counter+1,' of ', total_deliveries, ' done'
delivery_counter += 1
print 'All done'
def np_switching(self, poke_ports = [8, 9], taste_ports = [2, 3], deliveries_per_channel = 30, iti = 15.0, switching_every = 1, on_time = [0.02, 0.02]):
for i in poke_ports:
self.board.set_pin_mode(i, self.board.INPUT, self.board.DIGITAL)
sleep(2.0)
total_deliveries = len(poke_ports)*deliveries_per_channel
self.poke_array = []
for i in range(total_deliveries):
self.poke_array.append((i/switching_every)%len(poke_ports))
delivery_counter = 0
for i in self.poke_array:
sleep(iti)
while True:
if self.board.digital_read(poke_ports[i]) == 0:
start = time()*1000.0
self.board.digital_write(taste_ports[i], 1)
while True:
#.........这里部分代码省略.........
示例9: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [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)
# set pin modes
board.set_pin_mode(GREEN_LED, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(RED_LED, board.PWM, board.DIGITAL)
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL)
board.set_pin_mode(POTENTIOMETER, board.INPUT, board.ANALOG)
board.set_analog_latch(POTENTIOMETER, board.ANALOG_LATCH_GTE, 1000)
# do nothing loop - program exits when latch data event occurs for potentiometer
while 1:
count += 1
if count == 300:
print('bye bye')
board.close()
analog = board.analog_read(POTENTIOMETER)
board.analog_write(RED_LED, analog // 4)
示例10: Firmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [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()
#.........这里部分代码省略.........
示例11: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
# Create an instance of PyMata.
# The PyMata constructor will print status to the console and will return
# when PyMata is ready to accept commands or will exit if unsuccessful
firmata = PyMata("/dev/ttyACM0")
# Retrieve and print Arduino Firmware version information
firmata.refresh_report_firmware()
print firmata.get_firmata_firmware_version()
# Set the pin mode for a digital output pin
firmata.set_pin_mode(RED_LED, firmata.OUTPUT, firmata.DIGITAL)
# Turn on the RED LED
firmata.digital_write(RED_LED, 1)
# Wait 3 seconds and turn it off
time.sleep(3)
firmata.digital_write(RED_LED, 0)
# Set the white led for pwm operation
firmata.set_pin_mode(WHITE_LED, firmata.PWM, firmata.DIGITAL)
# Set the white led to full brightness (255) for 1 second
firmata.analog_write(WHITE_LED, 255)
time.sleep(1)
示例12: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
# create a PyMata instance
firmata = PyMata("/dev/ttyACM0")
def signal_handler(sig, frame):
print("You pressed Ctrl+C!!!!")
if firmata is not None:
firmata.reset()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# set digital pin 13 to be an output port
firmata.set_pin_mode(BOARD_LED, firmata.OUTPUT, firmata.DIGITAL)
firmata.set_pin_mode(BOARD_BAT, firmata.INPUT, firmata.ANALOG)
firmata.set_analog_latch(BOARD_BAT, firmata.ANALOG_LATCH_GTE, 1000)
time.sleep(5)
print("Blinking LED on pin 13")
# blink for 10 times
for x in range(10):
print(x + 1)
firmata.digital_write(BOARD_LED, 1)
# wait a half second between toggles.
time.sleep(0.2)
firmata.digital_write(BOARD_LED, 0)
time.sleep(0.2)
示例13: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
This
See the README.md file for instructions on compiling
"""
# import the API class
import time
from PyMata.pymata import PyMata
# instantiate PyMata
firmata = PyMata("/dev/ttyACM0")
time.sleep(2)
# configure a group of pins to control 8 LEDs
firmata.set_pin_mode(46,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(47,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(48,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(49,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(50,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(51,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(52,firmata.OUTPUT,firmata.DIGITAL)
firmata.set_pin_mode(53,firmata.OUTPUT,firmata.DIGITAL)
# configure 4 pins for 4 SONAR modules
firmata.sonar_config(6,6)
time.sleep(.1)
firmata.sonar_config(7,7)
time.sleep(.1)
firmata.sonar_config(37,37)
time.sleep(.1)
示例14: cb_push_button
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
"Analog Data: Pin Mode - ",
data[PIN_MODE],
" Pin Number -",
data[PIN_NUMBER],
" Data Value - ",
data[DATA_VALUE],
)
def cb_push_button(data):
print(
"Digital Data: Pin Mode - ",
data[PIN_MODE],
" Pin Number -",
data[PIN_NUMBER],
" Data Value - ",
data[DATA_VALUE],
)
signal.signal(signal.SIGINT, signal_handler) # Instantiate PyMata
board = PyMata("/dev/ttyACM1")
# set up pin modes for both pins with callbacks for each
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL, cb_push_button)
board.set_pin_mode(
POTENTIOMETER, board.INPUT, board.ANALOG, cb_potentiometer
) # A forever loop until user presses Control=c
while 1:
pass
示例15: signal_handler
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import set_pin_mode [as 别名]
PIN_NUMBER = 10
DATA_VALUE = 2
# Control-C signal handler to suppress exceptions if user presses Crtl+C
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)
def cb_push_button(data):
print("Digital Data:",
" Pin: ", data[PIN_NUMBER],
" Pin Mode: ", data[PIN_MODE],
" Data Value: ", data[DATA_VALUE])
# Instantiate PyMata
board = PyMata("/dev/ttyACM0", verbose=True)
# Set up pin modes for both pins with callbacks for each
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL, cb_push_button)
# A forever loop until user presses Ctrl+C
while 1:
pass