Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 16d2378

Browse files
committed
Release candidate for v2.0.0 with I2C support
2 parents 5ae5eb6 + 56a69e4 commit 16d2378

4 files changed

Lines changed: 39 additions & 249 deletions

File tree

biffobear_as3935.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ def _value_is_in_range(value, *, lo_limit, hi_limit):
9898
assert lo_limit <= value <= hi_limit
9999
except AssertionError as error:
100100
raise ValueError(
101-
"Value must be in the range %s to %s inclusive." % (lo_limit, hi_limit)
101+
"Value must be in the range %s to %s, inclusive." % (lo_limit, hi_limit)
102102
) from error
103103
return value
104104

105105

106106
class AS3935_Sensor:
107-
"""Driver for the Franklin AS3935 lightning detector chip."""
107+
"""Register handling for the Franklin AS3935 PSI and I2C drivers."""
108108

109109
# Constants to make register values human readable in the code
110110
DATA_PURGE = _0X00 # 0x00 - Distance recalculated after purging old data
@@ -199,8 +199,8 @@ def indoor(self, value):
199199

200200
@property
201201
def watchdog(self):
202-
"""int: Watchdog thresholdin the range 0 - 10 (default is 2). Higher thresholds reduce
203-
triggers by disturbers but decrease sensitivity to lightning strikes.
202+
"""int: Watchdog threshold in the range 0 - 10 (default is 2). Higher thresholds reduce
203+
triggers from disturbers but decrease sensitivity to lightning strikes.
204204
"""
205205
return self._get_register(self._WDTH)
206206

@@ -265,7 +265,7 @@ def interrupt_status(self):
265265
"""int: Status of the interrupt register. These constants are defined as helpers:
266266
LIGHTNING, DISTURBER. NOISE, DATA_PURGE.
267267
268-
note:: This register is automatically cleared by the sensor after it is read.
268+
Note: This register is automatically cleared by the sensor after it is read.
269269
"""
270270
# Wait a minimum of 2 ms between the interrupt pin going high and reading the register
271271
time.sleep(0.0002)
@@ -330,7 +330,7 @@ def power_down(self, value):
330330
self._set_register(self._PWD, 0x00)
331331
# RCO clocks need to be calibrated when powering back up from a power_down
332332
# Procedure as per AS3935 datasheet
333-
self._calibrate_clocks()
333+
self.calibrate_clocks()
334334
self._check_clock_calibration()
335335
self._set_register(self._DISP_FLAGS, 0x02)
336336
time.sleep(0.002)
@@ -399,9 +399,9 @@ def output_trco(self, value):
399399
@property
400400
def tuning_capacitance(self):
401401
"""int: The tuning capacitance for the RLC antenna in pF. This capacitance
402-
is added to the antenna to tune it within 3.5 % of 500 kHz (483 - 517 kHz).
402+
is added to the antenna to tune it to within 3.5 % of 500 kHz (483 - 517 kHz).
403403
404-
Capacitance must be in the range 0 - 120. Any of these values may be set,
404+
Capacitance must be in the range 0 - 120. Any of these values may be used,
405405
however, the capacitance is set in steps of 8 pF, so values less than 120
406406
will be rounded down to the nearest step. Default is 0.
407407
"""
@@ -432,8 +432,9 @@ def _check_clock_calibration(self):
432432
if _0X01 in [trco_result, srco_result]:
433433
raise RuntimeError("AS3935 RCO clock calibration failed.")
434434

435-
def _calibrate_clocks(self):
436-
"""Recalibrate the internal clocks."""
435+
def calibrate_clocks(self):
436+
"""Recalibrate the internal clocks. The clocks rely on the tuning frequency of
437+
the antenna, so adjust that to 500 KHz +/- 3.5 % before calibrating."""
437438
# Send the direct command to the CALIB_RCO register to start automatic RCO calibration
438439
# then check that the calibration has succeeded
439440
self._set_register(self._CALIB_RCO, self.DIRECT_COMMAND)
@@ -466,7 +467,7 @@ def _startup_checks(self):
466467
# checking the clock calibration status tells the that the clocks are OK and if
467468
# the calibration times out, we know that there are no comms with the sensor
468469
self.reset()
469-
self._calibrate_clocks()
470+
self.calibrate_clocks()
470471
self._check_clock_calibration()
471472

472473

tests/test_biffobear_as3935.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def test_power_down_setter_false_and_power_is_down(
403403
# If turning on power after power_down was previously set, clocks must be calibrated.
404404
get_reg.return_value = 0x01
405405
mock_calibrate_clocks = mocker.patch.object(
406-
as3935.AS3935_Sensor, "_calibrate_clocks", autospec=True
406+
as3935.AS3935_Sensor, "calibrate_clocks", autospec=True
407407
)
408408
mock_check_clock_calibration = mocker.patch.object(
409409
as3935.AS3935_Sensor, "_check_clock_calibration", autospec=True
@@ -430,7 +430,7 @@ def test_power_down_setter_false_and_power_is_up(mocker, set_reg, get_reg, test_
430430
# If turning on power with power already on then do nothing.
431431
get_reg.return_value = 0x00
432432
mock_calibrate_clocks = mocker.patch.object(
433-
as3935.AS3935_Sensor, "_calibrate_clocks", autospec=True
433+
as3935.AS3935_Sensor, "calibrate_clocks", autospec=True
434434
)
435435
test_device.power_down = False
436436
get_reg.assert_called_once_with(test_device, as3935.AS3935_Sensor._PWD)
@@ -565,7 +565,7 @@ def test_calibrate_clocks_calls_correct_register_then_checks_calibration(
565565
mock_check_clock_calibration = mocker.patch.object(
566566
as3935.AS3935_Sensor, "_check_clock_calibration"
567567
)
568-
test_device._calibrate_clocks()
568+
test_device.calibrate_clocks()
569569
set_reg.assert_called_once_with(test_device, as3935.AS3935_Sensor._CALIB_RCO, 0x96)
570570
mock_check_clock_calibration.assert_called_once()
571571

@@ -640,7 +640,7 @@ def test_startup_checks(mocker):
640640
as3935.AS3935_Sensor, "reset", autospec=True, return_value=None
641641
)
642642
mock_calibrate_clocks = mocker.patch.object(
643-
as3935.AS3935_Sensor, "_calibrate_clocks", autospec=True, return_value=None
643+
as3935.AS3935_Sensor, "calibrate_clocks", autospec=True, return_value=None
644644
)
645645
mock_check_clock_calibration = mocker.patch.object(
646646
as3935.AS3935_Sensor,
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
# All tests skipped unless environment variable SENSOR_ATTACHED is set to any value.
3333
try:
34-
sensor_attached = int(os.environ["SENSOR_ATTACHED"])
34+
sensor_attached = os.environ["SENSOR_ATTACHED"]
3535
except (KeyError, AttributeError):
3636
sensor_attached = False
3737

3838
try:
3939
pytestmark = pytest.mark.skipif(
40-
sensor_attached is False, reason="No as3935 board connected."
40+
sensor_attached not in ["SPI", "I2C"], reason="No as3935 board connected."
4141
)
4242
except NameError:
4343
pass
@@ -48,18 +48,26 @@
4848

4949
def setup_module():
5050
# Returns an instance of the AS3935 driver
51-
print("Setting up SPI connection...")
5251
global device
53-
spi = board.SPI()
54-
try:
55-
cs = board.D24
56-
interrupt = board.D25
57-
except AttributeError:
58-
cs = board.D5
59-
interrupt = board.D7
60-
61-
device = as3935.AS3935_SPI(spi, cs, interrupt_pin=interrupt)
62-
device.reset()
52+
if sensor_attached == "SPI":
53+
print("Setting up SPI connection...")
54+
spi = board.SPI()
55+
try:
56+
cs = board.D24
57+
interrupt = board.D25
58+
except AttributeError:
59+
cs = board.D5
60+
interrupt = board.D7
61+
device = as3935.AS3935(spi, cs, interrupt_pin=interrupt)
62+
63+
else:
64+
print("Setting up I2C connection...")
65+
i2c = board.I2C()
66+
try:
67+
interrupt = board.D25
68+
except AttributeError:
69+
interrupt = board.D7
70+
device = as3935.AS3935_I2C(i2c, interrupt_pin=interrupt)
6371

6472

6573
def teardown_module():
@@ -165,7 +173,7 @@ def test_reset():
165173
def test_commands_which_do_not_change_readable_values():
166174
# Call to see if an exception is raised
167175
device.clear_stats()
168-
device._calibrate_clocks()
176+
device.calibrate_clocks()
169177

170178

171179
def test_registers_with_unpredictable_states():
@@ -184,6 +192,7 @@ def test_read_interrupt_pin():
184192

185193
print("setup...")
186194
setup_module()
195+
device.reset()
187196
print("test_indoor_outdoor...")
188197
test_indoor_outdoor()
189198
print("power_down...")

0 commit comments

Comments
 (0)