本文整理汇总了Python中pyfirmata.Arduino.add_cmd_handler方法的典型用法代码示例。如果您正苦于以下问题:Python Arduino.add_cmd_handler方法的具体用法?Python Arduino.add_cmd_handler怎么用?Python Arduino.add_cmd_handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyfirmata.Arduino
的用法示例。
在下文中一共展示了Arduino.add_cmd_handler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pyfirmata import Arduino [as 别名]
# 或者: from pyfirmata.Arduino import add_cmd_handler [as 别名]
def main():
board = Arduino("/dev/ttyACM0")
board.add_cmd_handler(pyfirmata.pyfirmata.STRING_DATA, on_string_received)
iter = util.Iterator(board)
iter.start()
write_loop(board)
示例2: Arduino_Interface
# 需要导入模块: from pyfirmata import Arduino [as 别名]
# 或者: from pyfirmata.Arduino import add_cmd_handler [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):
#.........这里部分代码省略.........