PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/RPi_AS3935/RPi_AS3935.py

https://github.com/piliwilliam0306/RaspberryPi-AS3935
Python | 111 lines | 89 code | 18 blank | 4 comment | 10 complexity | ddb8cd44713d80e47abd3e6df65e697c MD5 | raw file
  1import smbus
  2import time
  3
  4class RPi_AS3935:
  5    """A basic class used for interacting with the AS3935 lightning
  6    sensor from a Raspberry Pi over I2C"""
  7
  8    def __init__(self, address, bus=0):
  9        self.address = address
 10        self.i2cbus = smbus.SMBus(bus)
 11
 12    def calibrate(self, tun_cap=None):
 13        """Calibrate the lightning sensor - this takes up to half a second
 14        and is blocking"""
 15        time.sleep(0.08)
 16        self.read_data()
 17        if tun_cap != None:
 18            if tun_cap < 0x10 and tun_cap > -1:
 19                self.set_byte(0x08, (self.registers[0x08] & 0xF0) | tun_cap)
 20                time.sleep(0.002)
 21            else:
 22                raise Exception("Value of TUN_CAP must be between 0 and 15")
 23        self.set_byte(0x3D, 0x96)
 24        time.sleep(0.002)
 25        self.set_byte(0x08, self.registers[0x08] | 0x20)
 26        time.sleep(0.002)
 27        self.read_data()
 28        self.set_byte(0x08, self.registers[0x08] & 0xDF)
 29        time.sleep(0.002)
 30        
 31    def get_interrupt(self):
 32        self.read_data()
 33        return self.registers[0x03] & 0x0F
 34
 35    def get_distance(self):
 36        self.read_data()
 37        if self.registers[0x07] & 0x3F == 0x3F:
 38            return False
 39        else:
 40            return self.registers[0x07] & 0x3F
 41
 42    def get_noise_floor(self):
 43        self.read_data()
 44        return (self.registers[0x01] & 0x38) >> 4
 45
 46    def set_noise_floor(self, noisefloor):
 47        self.read_data()
 48        noisefloor = (noisefloor & 0x07) << 4
 49        write_data = (self.registers[0x01] & 0xC7) + noisefloor
 50        self.set_byte(0x01, write_data)
 51
 52    def lower_noise_floor(self, min_noise=0):
 53        floor = self.get_noise_floor()
 54        if floor > min_noise:
 55            floor = floor - 1
 56            self.set_noise_floor(floor)
 57        return floor
 58
 59    def raise_noise_floor(self, max_noise=7):
 60        floor = self.get_noise_floor()
 61        if floor < max_noise:
 62            floor = floor + 1
 63            self.set_noise_floor(floor)
 64        return floor
 65
 66    def get_min_strikes(self):
 67        self.read_data()
 68        return (self.registers[0x02] >> 4) & 0x03
 69
 70    def set_min_strikes(self, minstrikes):
 71        self.read_data()
 72        minstrikes = (minstrikes & 0x03) << 4
 73        write_data = (self.registers[0x02] & 0xE7) + minstrikes
 74        self.set_byte(0x02, write_data)
 75
 76    def get_indoors(self):
 77        self.read_data()
 78        if self.registers[0x00] & 0x10 == 0x10:
 79            return True
 80        else:
 81            return False
 82
 83    def set_indoors(self, indoors):
 84        self.read_data()
 85        if indoors:
 86            write_value = (self.registers[0x00] & 0xE0) + 0x12
 87        else:
 88            write_value = (self.registers[0x00] & 0xE0) + 0x0E
 89        self.set_byte(0x00, write_value)
 90
 91    def set_mask_disturber(self, mask_dist):
 92        self.read_data()
 93        if mask_dist:
 94            write_value = self.registers[0x03] | 0x20
 95        else:
 96            write_value = self.registers[0x03] & 0xDF
 97        self.set_byte(0x03, write_value)
 98
 99    def get_mask_disturber(self):
100        self.read_data()
101        if self.registers[0x03] & 0x20 == 0x20:
102            return True
103        else:
104            return False
105
106    def set_byte(self, register, value):
107        self.i2cbus.write_byte_data(self.address, register, value)
108
109    def read_data(self):
110        self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
111