From 65e0d1af8baa8f356808ec731342455eca610b44 Mon Sep 17 00:00:00 2001 From: ouki-wang <358023925@qq.com> Date: Fri, 16 Nov 2018 12:26:44 +0800 Subject: [PATCH] V0.4 --- RaspberryPi/Python/DFRobot_AS3935_Lib.py | 36 +++++++++++++++++-- .../Python/example/DFRobot_AS3935_detailed.py | 10 +++++- .../Python/example/DFRobot_AS3935_ordinary.py | 11 +++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/RaspberryPi/Python/DFRobot_AS3935_Lib.py b/RaspberryPi/Python/DFRobot_AS3935_Lib.py index 70aafb9..cca3de9 100644 --- a/RaspberryPi/Python/DFRobot_AS3935_Lib.py +++ b/RaspberryPi/Python/DFRobot_AS3935_Lib.py @@ -7,7 +7,11 @@ class DFRobot_AS3935: self.i2cbus = smbus.SMBus(bus) def writeByte(self, register, value): - self.i2cbus.write_byte_data(self.address, register, value) + try: + self.i2cbus.write_byte_data(self.address, register, value) + return 1 + except: + return 0 def readData(self, register): self.register = self.i2cbus.read_i2c_block_data(self.address, register) @@ -36,7 +40,7 @@ class DFRobot_AS3935: self.singRegWrite(0x08, 0x0F, capVal >> 3) #set capacitance bits self.singRegRead(0x08) - print('capacitance set to 8x%d'%(self.register[0] & 0x0F)) + #print('capacitance set to 8x%d'%(self.register[0] & 0x0F)) def powerUp(self): #register 0x00, PWD bit: 0 (clears PWD) @@ -56,17 +60,21 @@ class DFRobot_AS3935: def setIndoors(self): self.singRegWrite(0x00, 0x3E, 0x24) + print("set to indoors model") def setOutdoors(self): self.singRegWrite(0x00, 0x3E, 0x1C) + print("set to outdoors model") def disturberDis(self): #register 0x03, PWD bit: 5 (sets MASK_DIST) self.singRegWrite(0x03, 0x20, 0x20) + print("disenable disturber detection") def disturberEn(self): #register 0x03, PWD bit: 5 (sets MASK_DIST) self.singRegWrite(0x03, 0x20, 0x00) + print("enable disturber detection") def singRegWrite(self, regAdd, dataMask, regData): #start by reading original register data (only modifying what we need to) @@ -101,8 +109,9 @@ class DFRobot_AS3935: return 0 #interrupt result not expected def reset(self): - self.writeByte(0x3C, 0x96) + err = self.writeByte(0x3C, 0x96) time.sleep(0.002) #wait 2ms to complete + return err def setLcoFdiv(self,fdiv): self.singRegWrite(0x03, 0xC0, (fdiv & 0x03) << 6) @@ -207,5 +216,26 @@ class DFRobot_AS3935: #default value of 0010 #values should only be between 0x00 and 0x0F (0 and 7) self.singRegWrite(0x02, 0x0F, srej & 0x0F) + + def printAllRegs(self): + self.singRegRead(0x00) + print("Reg 0x00: %02x"%self.register[0]) + self.singRegRead(0x01) + print("Reg 0x01: %02x"%self.register[0]) + self.singRegRead(0x02) + print("Reg 0x02: %02x"%self.register[0]) + self.singRegRead(0x03) + print("Reg 0x03: %02x"%self.register[0]) + self.singRegRead(0x04) + print("Reg 0x04: %02x"%self.register[0]) + self.singRegRead(0x05) + print("Reg 0x05: %02x"%self.register[0]) + self.singRegRead(0x06) + print("Reg 0x06: %02x"%self.register[0]) + self.singRegRead(0x07) + print("Reg 0x07: %02x"%self.register[0]) + self.singRegRead(0x08) + print("Reg 0x08: %02x"%self.register[0]) + diff --git a/RaspberryPi/Python/example/DFRobot_AS3935_detailed.py b/RaspberryPi/Python/example/DFRobot_AS3935_detailed.py index 918b4cb..206ea19 100644 --- a/RaspberryPi/Python/example/DFRobot_AS3935_detailed.py +++ b/RaspberryPi/Python/example/DFRobot_AS3935_detailed.py @@ -44,7 +44,12 @@ AS3935_DIST = AS3935_DIST_EN GPIO.setmode(GPIO.BOARD) sensor = DFRobot_AS3935(AS3935_I2C_ADDR3, bus = 1) -sensor.reset() +if (sensor.reset()): + print("init sensor sucess.") +else: + print("init sensor fail") + while True: + pass #Configure sensor sensor.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST) @@ -76,6 +81,9 @@ sensor.setWatchdogThreshold(0) sensor.setSpikeRejection(2) #spikeRejection = sensor.getSpikeRejection() +#view all register data +#sensor.printAllRegs() + def callback_handle(channel): global sensor time.sleep(0.005) diff --git a/RaspberryPi/Python/example/DFRobot_AS3935_ordinary.py b/RaspberryPi/Python/example/DFRobot_AS3935_ordinary.py index 1a89c7b..ebc507f 100644 --- a/RaspberryPi/Python/example/DFRobot_AS3935_ordinary.py +++ b/RaspberryPi/Python/example/DFRobot_AS3935_ordinary.py @@ -44,7 +44,13 @@ AS3935_DIST = AS3935_DIST_EN GPIO.setmode(GPIO.BOARD) sensor = DFRobot_AS3935(AS3935_I2C_ADDR3, bus = 1) -sensor.reset() +if (sensor.reset()): + print("init sensor sucess.") +else: + print("init sensor fail") + while True: + pass + #Configure sensor sensor.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST) @@ -56,6 +62,9 @@ sensor.manualCal(AS3935_CAPACITANCE, AS3935_MODE, AS3935_DIST) # sensor.setLcoFdiv(0) # sensor.setIrqOutputSource(3) +#view all register data +#sensor.printAllRegs() + def callback_handle(channel): global sensor time.sleep(0.005)