本文整理汇总了Python中pyfirmata.Arduino.iterate方法的典型用法代码示例。如果您正苦于以下问题:Python Arduino.iterate方法的具体用法?Python Arduino.iterate怎么用?Python Arduino.iterate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyfirmata.Arduino
的用法示例。
在下文中一共展示了Arduino.iterate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Arduino_Interface
# 需要导入模块: from pyfirmata import Arduino [as 别名]
# 或者: from pyfirmata.Arduino import iterate [as 别名]
class Arduino_Interface(object):
## ##
## CONSTANTS ##
## ##
## ##
PING = 0x01
TEST_DATA = 0x02
## ##
## Initalization ##
## ##
## ##
def __init__(self, port, log_handler):
## Initalize logging
logging.basicConfig(level=logging.DEBUG)
self.logger = logging.getLogger('Arduino_Interface')
self.logger.addHandler(log_handler) # Add Tk log handler
self.logger.debug('__init__')
## Board Initalization
self.board = Arduino(port)
self.board.add_cmd_handler(pyfirmata.START_SYSEX, self.handleIncomingSysEx)
self.board.add_cmd_handler(pyfirmata.STRING_DATA, self.handleIncomingString)
## Data Buffer Initalization
self.incoming_data = []
## Callback holder
self.callback_holder = dict()
## ##
## Runtime Functions ##
## ##
## ##
def begin_scanning(self):
self.board.iterate()
## ##
## Senders ##
## ##
## ##
def ping(self, pingCallback):
self.logger.debug('----Sending Ping----')
## Attach callback
self.callback_holder[Arduino_Interface.PING] = pingCallback
byte_array = bytearray()
byte_array.append(Arduino_Interface.PING)
self.sendSysEx(byte_array)
def requestTestData(self, testDataCallback):
self.logger.debug('----Requesting Test Data----')
## Attach callback
self.callback_holder[Arduino_Interface.TEST_DATA] = testDataCallback
byte_array = bytearray()
byte_array.append(Arduino_Interface.TEST_DATA)
self.sendSysEx(byte_array)
def sendSysEx(self, byteArray):
self.logger.debug('----sendSysEx----')
self.logger.debug('SEND Data: {}'.format(binascii.hexlify(byteArray)))
self.board.send_sysex(pyfirmata.START_SYSEX, byteArray)
self.begin_scanning()
def sendString(self, stringToSend):
#.........这里部分代码省略.........
示例2: Controls
# 需要导入模块: from pyfirmata import Arduino [as 别名]
# 或者: from pyfirmata.Arduino import iterate [as 别名]
class Controls(object):
iterator = ''
board = ''
servos = None
digitalouts = None
buttons = None
subsock = None
def __init__(self, session):
monkey.patch_socket()
import gevent_zeromq
gevent_zeromq.monkey_patch()
#we do use greenlets, but only patch sock stuff
#other stuff messes up the
config = session.arduino
boardglob = config['board']
boards = glob(boardglob)
if not len(boards):
raise Exception("No Arduino found")
self.board = Arduino(boards[0])
#self.iterator = util.Iterator(self.board)
#self.iterator.daemon = True
#self.iterator.start()
#initialize servo objects
self.servos = {}
if "servos" in config:
for servo in config["servos"]:
self.servos[servo['name']] = Servo(servo['pin'], self.board)
#initialize light objects
self.digitalouts = {}
if "digitalouts" in config:
for do in config["digitalouts"]:
self.digitalouts[do['name']] = DigitalOut(do['pin'], self.board)
if "digitalouts" in config or "servos" in config:
self.subsock = ChannelManager().subscribe("ControlOutput/")
self.buttons = []
if "buttons" in config:
for button in config["buttons"]:
self.buttons.append(Button(button['pin'], button['message'], self.board))
self.potentiometers = []
if "potentiometers" in config:
for pot in config["potentiometers"]:
self.buttons.append(Potentiometer(pot['pin'], pot['name'], self.board))
def checkSubscription(self):
if not self.subsock:
return
while True:
channel = self.subsock.recv()
message = self.subsock.recv()
message = jsondecode(message)
for name,value in message.items():
if name in self.servos:
self.servos[name].moveTo(value)
elif name in self.digitalouts:
self.digitalouts[name].write(value)
gevent.sleep(0)
def run(self):
if self.subsock and len(self.buttons):
gevent.spawn_link_exception(Controls.checkSubscription, self)
elif self.subsock:
self.checkSubscription()
return
while True:
while self.board.bytes_available():
self.board.iterate()
#make sure we have a clean buffer before bouncing the buttons
for b in self.buttons:
b.read()
for p in self.potentiometers:
p.read()
gevent.sleep(0)