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


Python Arduino.register_listener方法代码示例

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


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

示例1: __init__

# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import register_listener [as 别名]
class ArduinoController:

    BAUD_RATE = 115200

    def __init__(self):
        self.parser = Parser()

        self.arduino = Arduino(ArduinoController.BAUD_RATE)
        self.arduino.register_listener(self)

        self.msg_handlers = {
            'log_format': self.parser.register_log_format,
            'sensor_readings': self.notify_listeners
        }

        self.log_received_listeners = []

    def register_listener(self, listener):
        """Register a listener to the log received event."""
        # Make sure the listener has the on_log_received method defined.
        m = getattr(listener, 'on_log_received', None)
        if callable(m):
            self.log_received_listeners.append(listener)

    def on_msg_received(self, msg):
        """Attempt to parse & handle the received serial message."""
        try:
            parsed_msg = self.parser.parse_msg(msg)
            handler = self.msg_handlers[parsed_msg['header']]
            handler(parsed_msg['data'])
        except KeyError:
            raise BadSerialMessageError(
                'Unknown command: {}'.format(parsed_msg['header']))
        except BadSerialMessageError as e:
            print 'Ignoring bad msg: {}'.format(e)


    def start(self):
        self.arduino.start()  # For now simply delegate to the Arduino class.

    def stop(self):
        self.arduino.stop()  # For now simply delegate to the Arduino class.

    def notify_listeners(self, data):
        """Notify all listeners of the log received event."""
        try:
            readings = self.parser.parse_sensor_readings(data)
            for listener in self.log_received_listeners:
                listener.on_log_received(readings)
        except BadSerialMessageError as e:
            print "Ignoring bad log: {}".format(e)
开发者ID:ZachMassia,项目名称:BoostControl,代码行数:53,代码来源:parser.py


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