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


Python Arduino.bytes_available方法代码示例

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


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

示例1: Controls

# 需要导入模块: from pyfirmata import Arduino [as 别名]
# 或者: from pyfirmata.Arduino import bytes_available [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)
开发者ID:carriercomm,项目名称:SimpleSeer,代码行数:88,代码来源:Controls.py


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