def start(self): pigpio.set_mode(self.GPIO_TRIGGER, pigpio.OUTPUT) pigpio.set_mode(self.GPIO_ECHO, pigpio.INPUT) pigpio.write(self.GPIO_TRIGGER, 0) self.echo_cb = pigpio.callback( self.GPIO_ECHO, pigpio.EITHER_EDGE, self.on_echo) pigpio.set_watchdog(self.GPIO_ECHO, 100)
def trigger(self): """Trigger a new relative humidity and temperature reading.""" self.bit = -3 # header bits self.hH = 0 self.hL = 0 self.tH = 0 self.tL = 0 self.CS = 0 self.accumulating = True pigpio.write(self.gpio, 0) time.sleep(0.0001) pigpio.set_mode(self.gpio, pigpio.INPUT) pigpio.set_watchdog(self.gpio, 50)
def _cb(self, gpio, level, tick): if level != pigpio.TIMEOUT: if self.in_code == False: self.in_code = True pigpio.set_watchdog(self.gpio, self.code_timeout) self.hash_val = 2166136261 # FNV_BASIS_32 self.edges = 1 self.t1 = None self.t2 = None self.t3 = None self.t4 = tick else: self.edges += 1 self.t1 = self.t2 self.t2 = self.t3 self.t3 = self.t4 self.t4 = tick if self.t1 is not None: d1 = pigpio.tickDiff(self.t1, self.t2) d2 = pigpio.tickDiff(self.t3, self.t4) self._hash(d1, d2) else: if self.in_code: self.in_code = False pigpio.set_watchdog(self.gpio, 0) if self.edges > 12: self.callback(self.hash_val)
def t7(): global t7_count print("Watchdog tests.") # type of edge shouldn't matter for watchdogs t7cb = pigpio.callback(GPIO, pigpio.FALLING_EDGE, t7cbf) pigpio.set_watchdog(GPIO, 10) # 10 ms, 100 per second time.sleep(0.5) oc = t7_count time.sleep(2) c = t7_count - oc CHECK(7, 1, c, 200, 1, "set watchdog on count") pigpio.set_watchdog(GPIO, 0) # 0 switches watchdog off time.sleep(0.5) oc = t7_count time.sleep(2) c = t7_count - oc CHECK(7, 2, c, 0, 1, "set watchdog off count")
def _cb(self, gpio, level, tick): """ Accumulate bits until both gpios 0 and 1 timeout. """ if level < pigpio.TIMEOUT: if self.in_code == False: self.bits = 1 self.facility_num = 0 self.id_num = 0 self.in_code = True self.code_timeout = 0 pigpio.set_watchdog(self.gpio_0, self.bit_timeout) pigpio.set_watchdog(self.gpio_1, self.bit_timeout) else: self.bits += 1 if (self.bits > 1) and (self.bits <= 9): self.facility_num = self.facility_num << 1 if gpio == self.gpio_0: self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout else: self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout self.facility_num = self.facility_num | 1 elif (self.bits > 10) and (self.bits <= 25): self.id_num = self.id_num << 1 if gpio == self.gpio_0: self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout else: self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout self.id_num = self.id_num | 1 else: if self.in_code: if gpio == self.gpio_0: self.code_timeout = self.code_timeout | 1 # timeout gpio 0 else: self.code_timeout = self.code_timeout | 2 # timeout gpio 1 if self.code_timeout == 3: # both gpios timed out pigpio.set_watchdog(self.gpio_0, 0) pigpio.set_watchdog(self.gpio_1, 0) self.in_code = False self.callback(self.bits, self.facility_num, self.id_num)
def _cb(self, gpio, level, tick): """ Accumulate the 40 data bits. Format into 5 bytes, humidity high, humidity low, temperature high, temperature low, checksum. """ if self.accumulating: if level == 0: diff = pigpio.tickDiff(self.tick, tick) # edge length determines if bit is 1 or 0 if diff >= 50: val = 1 else: val = 0 if self.bit >= 32: # in checksum byte self.CS = (self.CS<<1) + val if self.bit >= 39: # 40 bits received self.accumulating = False pigpio.set_watchdog(self.gpio, 0) total = self.hH + self.hL + self.tH + self.tL if (total & 255) == self.CS: # is checksum ok self.rhum = ((self.hH<<8) + self.hL) * 0.1 if self.tH & 128: # negative temperature mult = -0.1 self.tH = self.tH & 127 else: mult = 0.1 self.temp = ((self.tH<<8) + self.tL) * mult self.tov = time.time() else: self.bad_CS += 1 elif self.bit >=24: # in temp low byte self.tL = (self.tL<<1) + val elif self.bit >=16: # in temp high byte self.tH = (self.tH<<1) + val elif self.bit >= 8: # in humidity low byte self.hL = (self.hL<<1) + val elif self.bit >= 0: # in humidity high byte self.hH = (self.hH<<1) + val else: # header bits pass self.bit += 1 elif level == 1: self.tick = tick if self.bit == -3: # correct for first reading self.bit = -2 else: # level == pigpio.TIMEOUT: # time out if less than 40 bits received self.accumulating = False pigpio.set_watchdog(self.gpio, 0) self.bad_TO += 1 else: # perhaps a repeated watchdog if level == pigpio.TIMEOUT: pigpio.set_watchdog(self.gpio, 0)
def stop(self): if self.echo_cb is None: return pigpio.set_watchdog(self.GPIO_ECHO, 0) self.echo_cb.cancel() self.echo_cb = None