本文整理汇总了Python中PyMata.pymata.PyMata.digital_read方法的典型用法代码示例。如果您正苦于以下问题:Python PyMata.digital_read方法的具体用法?Python PyMata.digital_read怎么用?Python PyMata.digital_read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyMata.pymata.PyMata
的用法示例。
在下文中一共展示了PyMata.digital_read方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ArduinoBoard
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [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: ArduinoFirmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [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)
示例3: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [as 别名]
import sys
import signal
from PyMata.pymata import PyMata
ENCODER_A = 2
ENCODER_B = 4
prev_value = 0
# 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)
# configure the pins for the encoder
board.encoder_config(ENCODER_B, ENCODER_A)
while 1:
value = board.digital_read(ENCODER_B)
if value != prev_value:
prev_value = value
print(board.digital_read(ENCODER_B))
pass
示例4: PyMata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [as 别名]
This example illustrates using callbacks for digital and analog input.
Monitor the current analog input and digital input of two pins.
"""
import signal
import sys
import time
from PyMata.pymata import PyMata
# Instantiate PyMata
board = PyMata("/dev/ttyACM0", verbose=True)
board.encoder_config(7,2)
# Set up pin modes for both pins with callbacks for each
board.set_pin_mode(5, board.PWM, board.DIGITAL)
board.set_pin_mode(6, board.PWM, board.DIGITAL)
board.analog_write(5, 255)
board.analog_write(6, 0)
time.sleep(2)
count = board.digital_read(2)
print count
time.sleep(2)
board.analog_write(5, 0)
示例5: __init__
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [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:
#.........这里部分代码省略.........
示例6: ArduinoFirmata
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [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
示例7: print
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [as 别名]
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)
digital = board.digital_read(PUSH_BUTTON)
board.digital_write(GREEN_LED, digital)
latch = board.get_analog_latch_data(POTENTIOMETER)
if latch[1] == board.LATCH_LATCHED:
board.analog_write(RED_LED, 0)
board.digital_write(GREEN_LED, 0)
print('Latching Event Occurred at: ' + time.asctime(time.gmtime(latch[3])))
board.close()
sys.exit(0)
示例8: sleep
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [as 别名]
tasteduration1 = 2 # Time valve should be open in seconds
tasteduration2 = 2
iti = 5 # Set ITI
# Set mode on input channels and clear outputs
board.set_pin_mode(pokeport1, board.INPUT, board.DIGITAL)
board.set_pin_mode(pokeport2, board.INPUT, board.DIGITAL)
#board.digital_write(tasteport1, 0)
#board.digital_write(tasteport2, 0)
trial = 1
while trial < trials:
if board.digital_read(pokeport1) == 1:
board.digital_write(tasteport1, 1)
sleep(tasteduration1)
board.digital_write(tasteport1, 0)
print 'Completed trial number', trial
sleep(iti)
trial = trial+1
elif board.digital_read(pokeport2) == 1:
board.digital_write(tasteport2, 1)
sleep(tasteduration2)
board.digital_write(tasteport2, 0)
print 'Completed trial number', trial
sleep(iti)
trial = trial+1
print('It\'s all ogre now') # Shrek is love, shrek is life
示例9:
# 需要导入模块: from PyMata.pymata import PyMata [as 别名]
# 或者: from PyMata.pymata.PyMata import digital_read [as 别名]
firmata.analog_write(WHITE_LED, 0)
# set potentiometer pin as an analog input
firmata.set_pin_mode(POTENTIOMETER, firmata.INPUT, firmata.ANALOG)
# allow some time for the first data to arrive from the Arduino and be
# processed.
time.sleep(.2)
print firmata.analog_read(POTENTIOMETER)
# set the button switch as a digital input
firmata.set_pin_mode(BUTTON_SWITCH, firmata.INPUT, firmata.DIGITAL)
# wait for the button switch to be pressed
while not firmata.digital_read(BUTTON_SWITCH):
time.sleep(.1)
pass
print firmata.digital_read(BUTTON_SWITCH)
# send out a beep in celebration of detecting the button press
# note that you don't need to set pin mode for play_tone
firmata.play_tone(BEEPER, firmata.TONE_TONE, 1000, 500)
# control the servo - note that you don't need to set pin mode
# configure the servo
firmata.servo_config(SERVO_MOTOR)
# move the servo to 20 degrees
firmata.analog_write(SERVO_MOTOR, 20)
time.sleep(.5)