Cleanup AS3935

This commit is contained in:
Otto Winter 2019-10-12 17:03:01 +02:00
parent 68e7e5a51c
commit fa351cd37c
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E
13 changed files with 165 additions and 135 deletions

View File

@ -0,0 +1,47 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import CONF_PIN, CONF_INDOOR, CONF_WATCHDOG_THRESHOLD, \
CONF_NOISE_LEVEL, CONF_SPIKE_REJECTION, CONF_LIGHTNING_THRESHOLD, \
CONF_MASK_DISTURBER, CONF_DIV_RATIO, CONF_CAPACITANCE
from esphome.core import coroutine
AUTO_LOAD = ['sensor', 'binary_sensor']
MULTI_CONF = True
CONF_AS3935_ID = 'as3935_id'
as3935_ns = cg.esphome_ns.namespace('as3935')
AS3935 = as3935_ns.class_('AS3935Component', cg.Component)
AS3935_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(AS3935),
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema,
pins.validate_has_interrupt),
cv.Optional(CONF_INDOOR, default=True): cv.boolean,
cv.Optional(CONF_NOISE_LEVEL, default=2): cv.int_range(min=1, max=7),
cv.Optional(CONF_WATCHDOG_THRESHOLD, default=2): cv.int_range(min=1, max=10),
cv.Optional(CONF_SPIKE_REJECTION, default=2): cv.int_range(min=1, max=11),
cv.Optional(CONF_LIGHTNING_THRESHOLD, default=1): cv.one_of(1, 5, 9, 16, int=True),
cv.Optional(CONF_MASK_DISTURBER, default=False): cv.boolean,
cv.Optional(CONF_DIV_RATIO, default=0): cv.one_of(0, 16, 22, 64, 128, int=True),
cv.Optional(CONF_CAPACITANCE, default=0): cv.int_range(min=0, max=15),
})
@coroutine
def setup_as3935(var, config):
yield cg.register_component(var, config)
pin = yield cg.gpio_pin_expression(config[CONF_PIN])
cg.add(var.set_pin(pin))
cg.add(var.set_indoor(config[CONF_INDOOR]))
cg.add(var.set_noise_level(config[CONF_NOISE_LEVEL]))
cg.add(var.set_watchdog_threshold(config[CONF_WATCHDOG_THRESHOLD]))
cg.add(var.set_spike_rejection(config[CONF_SPIKE_REJECTION]))
cg.add(var.set_lightning_threshold(config[CONF_LIGHTNING_THRESHOLD]))
cg.add(var.set_mask_disturber(config[CONF_MASK_DISTURBER]))
cg.add(var.set_div_ratio(config[CONF_DIV_RATIO]))
cg.add(var.set_capacitance(config[CONF_CAPACITANCE]))

View File

@ -1,10 +1,10 @@
#include "as3935_base.h"
#include "as3935.h"
#include "esphome/core/log.h"
namespace esphome {
namespace as3935_base {
namespace as3935 {
static const char *TAG = "as3935_base";
static const char *TAG = "as3935";
void AS3935Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up AS3935...");
@ -13,6 +13,16 @@ void AS3935Component::setup() {
this->store_.pin = this->pin_->to_isr();
LOG_PIN(" Interrupt Pin: ", this->pin_);
this->pin_->attach_interrupt(AS3935ComponentStore::gpio_intr, &this->store_, RISING);
// Write properties to sensor
this->write_indoor(this->indoor_);
this->write_noise_level(this->noise_level_);
this->write_watchdog_threshold(this->watchdog_threshold_);
this->write_spike_rejection(this->spike_rejection_);
this->write_lightning_threshold(this->lightning_threshold_);
this->write_mask_disturber(this->mask_disturber_);
this->write_div_ratio(this->div_ratio_);
this->write_capacitance(this->capacitance_);
}
void AS3935Component::dump_config() {
@ -46,8 +56,8 @@ void AS3935Component::loop() {
this->store_.interrupt = false;
}
void AS3935Component::set_indoor(bool indoor) {
ESP_LOGD(TAG, "Setting indoor to %d", indoor);
void AS3935Component::write_indoor(bool indoor) {
ESP_LOGV(TAG, "Setting indoor to %d", indoor);
if (indoor)
this->write_register(AFE_GAIN, GAIN_MASK, INDOOR, 1);
else
@ -56,11 +66,11 @@ void AS3935Component::set_indoor(bool indoor) {
// REG0x01, bits[3:0], manufacturer default: 0010 (2).
// This setting determines the threshold for events that trigger the
// IRQ Pin.
void AS3935Component::set_watchdog_threshold(uint8_t sensitivity) {
ESP_LOGD(TAG, "Setting watchdog sensitivity to %d", sensitivity);
if ((sensitivity < 1) || (sensitivity > 10)) // 10 is the max sensitivity setting
void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
ESP_LOGV(TAG, "Setting watchdog sensitivity to %d", watchdog_threshold);
if ((watchdog_threshold < 1) || (watchdog_threshold > 10)) // 10 is the max sensitivity setting
return;
this->write_register(THRESHOLD, THRESH_MASK, sensitivity, 0);
this->write_register(THRESHOLD, THRESH_MASK, watchdog_threshold, 0);
}
// REG0x01, bits [6:4], manufacturer default: 010 (2).
@ -68,44 +78,52 @@ void AS3935Component::set_watchdog_threshold(uint8_t sensitivity) {
// level is exceeded the chip will issue an interrupt to the IRQ pin,
// broadcasting that it can not operate properly due to noise (INT_NH).
// Check datasheet for specific noise level tolerances when setting this register.
void AS3935Component::set_noise_level(uint8_t floor) {
ESP_LOGD(TAG, "Setting noise level to %d", floor);
if ((floor < 1) || (floor > 7))
void AS3935Component::write_noise_level(uint8_t noise_level) {
ESP_LOGV(TAG, "Setting noise level to %d", noise_level);
if ((noise_level < 1) || (noise_level > 7))
return;
this->write_register(THRESHOLD, NOISE_FLOOR_MASK, floor, 4);
this->write_register(THRESHOLD, NOISE_FLOOR_MASK, noise_level, 4);
}
// REG0x02, bits [3:0], manufacturer default: 0010 (2).
// This setting, like the watchdog threshold, can help determine between false
// events and actual lightning. The shape of the spike is analyzed during the
// chip's signal validation routine. Increasing this value increases robustness
// at the cost of sensitivity to distant events.
void AS3935Component::set_spike_rejection(uint8_t spike_sensitivity) {
ESP_LOGD(TAG, "Setting spike rejection to %d", spike_sensitivity);
if ((spike_sensitivity < 1) || (spike_sensitivity > 11))
void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
ESP_LOGV(TAG, "Setting spike rejection to %d", spike_rejection);
if ((spike_rejection < 1) || (spike_rejection > 11))
return;
this->write_register(LIGHTNING_REG, SPIKE_MASK, spike_sensitivity, 0);
this->write_register(LIGHTNING_REG, SPIKE_MASK, spike_rejection, 0);
}
// REG0x02, bits [5:4], manufacturer default: 0 (single lightning strike).
// The number of lightning events before IRQ is set high. 15 minutes is The
// window of time before the number of detected lightning events is reset.
// The number of lightning strikes can be set to 1,5,9, or 16.
void AS3935Component::set_lightning_threshold(uint8_t strikes) {
ESP_LOGD(TAG, "Setting lightning threshold to %d", strikes);
if (strikes == 1)
void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
ESP_LOGV(TAG, "Setting lightning threshold to %d", lightning_threshold);
switch (lightning_threshold) {
case 1:
this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 0, 4); // Demonstrative
if (strikes == 5)
break;
case 5:
this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 1, 4);
if (strikes == 9)
break;
case 9:
this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 1, 5);
if (strikes == 16)
break;
case 16:
this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 3, 4);
break;
default:
return;
}
}
// REG0x03, bit [5], manufacturer default: 0.
// This setting will return whether or not disturbers trigger the IRQ Pin.
void AS3935Component::set_mask_disturber(bool enabled) {
ESP_LOGD(TAG, "Setting mask disturber to %d", enabled);
void AS3935Component::write_mask_disturber(bool enabled) {
ESP_LOGV(TAG, "Setting mask disturber to %d", enabled);
if (enabled) {
this->write_register(INT_MASK_ANT, (1 << 5), 1, 5);
} else {
@ -116,28 +134,33 @@ void AS3935Component::set_mask_disturber(bool enabled) {
// The antenna is designed to resonate at 500kHz and so can be tuned with the
// following setting. The accuracy of the antenna must be within 3.5 percent of
// that value for proper signal validation and distance estimation.
void AS3935Component::set_div_ratio(uint8_t div_ratio) {
ESP_LOGD(TAG, "Setting div ratio to %d", div_ratio);
if (div_ratio == 16)
void AS3935Component::write_div_ratio(uint8_t div_ratio) {
ESP_LOGV(TAG, "Setting div ratio to %d", div_ratio);
switch (div_ratio) {
case 16:
this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 0, 6);
else if (div_ratio == 22)
break;
case 22:
this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 1, 6);
else if (div_ratio == 64)
break;
case 64:
this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 1, 7);
else if (div_ratio == 128)
break;
case 128:
this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 3, 6);
break;
default:
return;
}
}
// REG0x08, bits [3:0], manufacturer default: 0.
// This setting will add capacitance to the series RLC antenna on the product
// to help tune its resonance. The datasheet specifies being within 3.5 percent
// of 500kHz to get optimal lightning detection and distance sensing.
// It's possible to add up to 120pF in steps of 8pF to the antenna.
void AS3935Component::set_cap(uint8_t eight_pico_farad) {
ESP_LOGD(TAG, "Setting tune cap to %d pF", eight_pico_farad * 8);
if (eight_pico_farad > 15)
return;
this->write_register(FREQ_DISP_IRQ, CAP_MASK, eight_pico_farad, 0);
void AS3935Component::write_capacitance(uint8_t capacitance) {
ESP_LOGV(TAG, "Setting tune cap to %d pF", capacitance * 8);
this->write_register(FREQ_DISP_IRQ, CAP_MASK, capacitance, 0);
}
// REG0x03, bits [3:0], manufacturer default: 0.
@ -195,12 +218,11 @@ uint32_t AS3935Component::get_lightning_energy_() {
uint8_t AS3935Component::read_register_(uint8_t reg, uint8_t mask) {
uint8_t value = this->read_register(reg);
value &= (~mask);
return value;
}
void ICACHE_RAM_ATTR AS3935ComponentStore::gpio_intr(AS3935ComponentStore *arg) { arg->interrupt = true; }
} // namespace as3935_base
} // namespace as3935
} // namespace esphome

View File

@ -5,7 +5,7 @@
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome {
namespace as3935_base {
namespace as3935 {
enum AS3935RegisterNames {
AFE_GAIN = 0x00,
@ -71,14 +71,22 @@ class AS3935Component : public Component {
void set_thunder_alert_binary_sensor(binary_sensor::BinarySensor *thunder_alert_binary_sensor) {
thunder_alert_binary_sensor_ = thunder_alert_binary_sensor;
}
void set_indoor(bool indoor);
void set_watchdog_threshold(uint8_t sensitivity);
void set_noise_level(uint8_t floor);
void set_spike_rejection(uint8_t spike_sensitivity);
void set_lightning_threshold(uint8_t strikes);
void set_mask_disturber(bool enabled);
void set_div_ratio(uint8_t div_ratio);
void set_cap(uint8_t eight_pico_farad);
void set_indoor(bool indoor) { indoor_ = indoor; }
void write_indoor(bool indoor);
void set_noise_level(uint8_t noise_level) { noise_level_ = noise_level; }
void write_noise_level(uint8_t noise_level);
void set_watchdog_threshold(uint8_t watchdog_threshold) { watchdog_threshold_ = watchdog_threshold; }
void write_watchdog_threshold(uint8_t watchdog_threshold);
void set_spike_rejection(uint8_t spike_rejection) { spike_rejection_ = spike_rejection; }
void write_spike_rejection(uint8_t write_spike_rejection);
void set_lightning_threshold(uint8_t lightning_threshold) { lightning_threshold_ = lightning_threshold; }
void write_lightning_threshold(uint8_t lightning_threshold);
void set_mask_disturber(bool mask_disturber) { mask_disturber_ = mask_disturber; }
void write_mask_disturber(bool enabled);
void set_div_ratio(uint8_t div_ratio) { div_ratio_ = div_ratio; }
void write_div_ratio(uint8_t div_ratio);
void set_capacitance(uint8_t capacitance) { capacitance_ = capacitance; }
void write_capacitance(uint8_t capacitance);
protected:
uint8_t read_interrupt_register_();
@ -96,7 +104,16 @@ class AS3935Component : public Component {
binary_sensor::BinarySensor *thunder_alert_binary_sensor_;
GPIOPin *pin_;
AS3935ComponentStore store_;
bool indoor_;
uint8_t noise_level_;
uint8_t watchdog_threshold_;
uint8_t spike_rejection_;
uint8_t lightning_threshold_;
bool mask_disturber_;
uint8_t div_ratio_;
uint8_t capacitance_;
};
} // namespace as3935_base
} // namespace as3935
} // namespace esphome

View File

@ -3,7 +3,7 @@ import esphome.config_validation as cv
from esphome.components import binary_sensor
from . import AS3935, CONF_AS3935_ID
DEPENDENCIES = ['as3935_base']
DEPENDENCIES = ['as3935']
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(CONF_AS3935_ID): cv.use_id(AS3935),

View File

@ -5,7 +5,7 @@ from esphome.const import CONF_DISTANCE, CONF_LIGHTNING_ENERGY, \
UNIT_KILOMETER, UNIT_EMPTY, ICON_SIGNAL_DISTANCE_VARIANT, ICON_FLASH
from . import AS3935, CONF_AS3935_ID
DEPENDENCIES = ['as3935_base']
DEPENDENCIES = ['as3935']
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(CONF_AS3935_ID): cv.use_id(AS3935),

View File

@ -1,54 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import CONF_PIN, CONF_INDOOR, CONF_WATCHDOG_THRESHOLD, \
CONF_NOISE_LEVEL, CONF_SPIKE_REJECTION, CONF_LIGHTNING_THRESHOLD, \
CONF_MASK_DISTURBER, CONF_DIV_RATIO, CONF_CAP
from esphome.core import coroutine
AUTO_LOAD = ['sensor', 'binary_sensor']
MULTI_CONF = True
CONF_AS3935_ID = 'as3935_id'
as3935_base_ns = cg.esphome_ns.namespace('as3935_base')
AS3935 = as3935_base_ns.class_('AS3935Component', cg.Component)
AS3935_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(AS3935),
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema,
pins.validate_has_interrupt),
cv.Optional(CONF_INDOOR): cv.boolean,
cv.Optional(CONF_WATCHDOG_THRESHOLD): cv.int_range(min=1, max=10),
cv.Optional(CONF_NOISE_LEVEL): cv.int_range(min=1, max=7),
cv.Optional(CONF_SPIKE_REJECTION): cv.int_range(min=1, max=11),
cv.Optional(CONF_LIGHTNING_THRESHOLD): cv.one_of(0, 1, 5, 9, 16, int=True),
cv.Optional(CONF_MASK_DISTURBER): cv.boolean,
cv.Optional(CONF_DIV_RATIO): cv.one_of(0, 16, 22, 64, 128, int=True),
cv.Optional(CONF_CAP): cv.int_range(min=0, max=15),
})
@coroutine
def setup_as3935(var, config):
yield cg.register_component(var, config)
pin = yield cg.gpio_pin_expression(config[CONF_PIN])
cg.add(var.set_pin(pin))
if CONF_INDOOR in config:
cg.add(var.set_indoor(config[CONF_INDOOR]))
if CONF_WATCHDOG_THRESHOLD in config:
cg.add(var.set_watchdog_threshold(config[CONF_WATCHDOG_THRESHOLD]))
if CONF_NOISE_LEVEL in config:
cg.add(var.set_noise_level(config[CONF_NOISE_LEVEL]))
if CONF_SPIKE_REJECTION in config:
cg.add(var.set_spike_rejection(config[CONF_SPIKE_REJECTION]))
if CONF_LIGHTNING_THRESHOLD in config:
cg.add(var.set_lightning_threshold(config[CONF_LIGHTNING_THRESHOLD]))
if CONF_MASK_DISTURBER in config:
cg.add(var.set_mask_disturber(config[CONF_MASK_DISTURBER]))
if CONF_DIV_RATIO in config:
cg.add(var.set_div_ratio(config[CONF_DIV_RATIO]))
if CONF_CAP in config:
cg.add(var.set_cap(config[CONF_CAP]))

View File

@ -1,20 +1,20 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import as3935_base, i2c
from esphome.const import CONF_ID, CONF_LAMBDA, CONF_PAGES
from esphome.components import as3935, i2c
from esphome.const import CONF_ID
AUTO_LOAD = ['as3935_base']
AUTO_LOAD = ['as3935']
DEPENDENCIES = ['i2c']
as3935_i2c_ns = cg.esphome_ns.namespace('as3935_i2c')
I2CAS3935 = as3935_i2c_ns.class_('I2CAS3935Component', as3935_base.AS3935, i2c.I2CDevice)
I2CAS3935 = as3935_i2c_ns.class_('I2CAS3935Component', as3935.AS3935, i2c.I2CDevice)
CONFIG_SCHEMA = cv.All(as3935_base.AS3935_SCHEMA.extend({
CONFIG_SCHEMA = cv.All(as3935.AS3935_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(I2CAS3935),
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x03)))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield as3935_base.setup_as3935(var, config)
yield as3935.setup_as3935(var, config)
yield i2c.register_i2c_device(var, config)

View File

@ -1,7 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/as3935_base/as3935_base.h"
#include "esphome/components/as3935/as3935.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
@ -9,9 +9,7 @@
namespace esphome {
namespace as3935_i2c {
class I2CAS3935Component : public as3935_base::AS3935Component, public i2c::I2CDevice {
public:
class I2CAS3935Component : public as3935::AS3935Component, public i2c::I2CDevice {
protected:
void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position) override;
uint8_t read_register(uint8_t reg) override;

View File

@ -1,20 +1,20 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import as3935_base, spi
from esphome.components import as3935, spi
from esphome.const import CONF_ID
AUTO_LOAD = ['as3935_base']
AUTO_LOAD = ['as3935']
DEPENDENCIES = ['spi']
as3935_spi_ns = cg.esphome_ns.namespace('as3935_spi')
SPIAS3935 = as3935_spi_ns.class_('SPIAS3935Component', as3935_base.AS3935, spi.SPIDevice)
SPIAS3935 = as3935_spi_ns.class_('SPIAS3935Component', as3935.AS3935, spi.SPIDevice)
CONFIG_SCHEMA = cv.All(as3935_base.AS3935_SCHEMA.extend({
CONFIG_SCHEMA = cv.All(as3935.AS3935_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPIAS3935)
}).extend(cv.COMPONENT_SCHEMA).extend(spi.SPI_DEVICE_SCHEMA))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield as3935_base.setup_as3935(var, config)
yield as3935.setup_as3935(var, config)
yield spi.register_spi_device(var, config)

View File

@ -1,7 +1,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/as3935_base/as3935_base.h"
#include "esphome/components/as3935/as3935.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
@ -11,7 +11,7 @@ namespace as3935_spi {
enum AS3935RegisterMasks { SPI_READ_M = 0x40 };
class SPIAS3935Component : public as3935_base::AS3935Component,
class SPIAS3935Component : public as3935::AS3935Component,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_2MHZ> {
public:

View File

@ -63,7 +63,7 @@ CONF_BUSY_PIN = 'busy_pin'
CONF_BUS_VOLTAGE = 'bus_voltage'
CONF_CALIBRATE_LINEAR = 'calibrate_linear'
CONF_CALIBRATION = 'calibration'
CONF_CAP = 'cap'
CONF_CAPACITANCE = 'capacitance'
CONF_CARRIER_DUTY_PERCENT = 'carrier_duty_percent'
CONF_CARRIER_FREQUENCY = 'carrier_frequency'
CONF_CHANGE_MODE_EVERY = 'change_mode_every'

View File

@ -599,7 +599,7 @@ sensor:
name: "ZyAura Temperature"
humidity:
name: "ZyAura Humidity"
- platform: as3935_base
- platform: as3935
lightning_energy:
name: "Lightning Energy"
distance:
@ -738,7 +738,7 @@ binary_sensor:
3700, -2263, 1712, -4254, 1711, -4249, 1715, -2266, 1710, -2267,
1709, -2265, 3704, -4250, 1712, -4254, 3700, -2260, 1714, -2265,
1712, -2262, 1714, -2267, 1709]
- platform: as3935_base
- platform: as3935
name: "Storm Alert"
pca9685:

View File

@ -129,7 +129,7 @@ sensor:
- platform: homeassistant
entity_id: sensor.hello_world
id: ha_hello_world
- platform: as3935_base
- platform: as3935
lightning_energy:
name: "Lightning Energy"
distance:
@ -172,7 +172,7 @@ binary_sensor:
- platform: homeassistant
entity_id: binary_sensor.hello_world
id: ha_hello_world_binary
- platform: as3935_base
- platform: as3935
name: "Storm Alert"
remote_receiver: