_i2c_connector.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:ulnoiot 作者: ulno 项目源码 文件源码
def _update(self):
        t = time.ticks_ms()
        #if self.suspend_start is not None \
        #        and time.ticks_diff(t,self.suspend_start) <= self.suspend_time:
        #    print("Bus access suspended.")
        if self.suspend_start is None \
            or time.ticks_diff(t,self.suspend_start) > self.suspend_time:
            self.suspend_start = None  # done waiting
            try:
                if self.msgq is not None:
                    self.pin.writeto(self.addr, self.msgq)
                    self.msgq = None
                s = self.pin.readfrom(self.addr,self.BUFFER_SIZE)
            except:
                print("Trouble accessing i2c.")
            else:
                if s[0]==255 and s[1]==255: # illegal read
                    print("Got garbage, waiting 1s before accessing bus again.")
                    print("data:", s)
                    self.suspend_time=1000 # take a break
                    self.suspend_start = time.ticks_ms();
                else:
                    count = s[0]*256+s[1]
                    if self.count != count:
                        l = s[2];
                        self.suspend_time = s[3];
                        # scale timer
                        if self.suspend_time & 128:
                            self.suspend_time = (self.suspend_time & 127) * 100
                            if self.suspend_time>5000: # should not happen
                                print("Garbage time -> waiting 1s before accessing bus again.")
                                print("data:",s)
                                self.suspend_time=1000
                        if self.suspend_time > 0:
                            self.suspend_start = time.ticks_ms();
                            print("Bus suspend for",self.suspend_time,"ms requested.")
                        if l <= self.BUFFER_SIZE: # discard if too big
                            self.count = count
                            self.current_value = s[4:4+l]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号