ESPHome  2022.1.1
as3935.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
7 
8 namespace esphome {
9 namespace as3935 {
10 
12  AFE_GAIN = 0x00,
21  CALIB_TRCO = 0x3A,
22  CALIB_SRCO = 0x3B,
23  DEFAULT_RESET = 0x3C,
24  CALIB_RCO = 0x3D
25 };
26 
28  GAIN_MASK = 0x3E,
29  SPIKE_MASK = 0xF,
30  IO_MASK = 0xC1,
31  DISTANCE_MASK = 0xC0,
32  INT_MASK = 0xF0,
33  THRESH_MASK = 0x0F,
34  R_SPIKE_MASK = 0xF0,
35  ENERGY_MASK = 0xF0,
36  CAP_MASK = 0xF0,
37  LIGHT_MASK = 0xCF,
38  DISTURB_MASK = 0xDF,
40  OSC_MASK = 0xE0,
41  CALIB_MASK = 0x7F,
42  DIV_MASK = 0x3F
43 };
44 
46  AS3935_ADDR = 0x03,
47  INDOOR = 0x12,
48  OUTDOOR = 0xE,
49  LIGHTNING_INT = 0x08,
50  DISTURBER_INT = 0x04,
51  NOISE_INT = 0x01
52 };
53 
54 class AS3935Component : public Component {
55  public:
56  void setup() override;
57  void dump_config() override;
58  float get_setup_priority() const override;
59  void loop() override;
60 
61  void set_irq_pin(GPIOPin *irq_pin) { irq_pin_ = irq_pin; }
62  void set_distance_sensor(sensor::Sensor *distance_sensor) { distance_sensor_ = distance_sensor; }
63  void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
64  void set_thunder_alert_binary_sensor(binary_sensor::BinarySensor *thunder_alert_binary_sensor) {
65  thunder_alert_binary_sensor_ = thunder_alert_binary_sensor;
66  }
67  void set_indoor(bool indoor) { indoor_ = indoor; }
68  void write_indoor(bool indoor);
69  void set_noise_level(uint8_t noise_level) { noise_level_ = noise_level; }
70  void write_noise_level(uint8_t noise_level);
71  void set_watchdog_threshold(uint8_t watchdog_threshold) { watchdog_threshold_ = watchdog_threshold; }
72  void write_watchdog_threshold(uint8_t watchdog_threshold);
73  void set_spike_rejection(uint8_t spike_rejection) { spike_rejection_ = spike_rejection; }
75  void set_lightning_threshold(uint8_t lightning_threshold) { lightning_threshold_ = lightning_threshold; }
76  void write_lightning_threshold(uint8_t lightning_threshold);
77  void set_mask_disturber(bool mask_disturber) { mask_disturber_ = mask_disturber; }
78  void write_mask_disturber(bool enabled);
79  void set_div_ratio(uint8_t div_ratio) { div_ratio_ = div_ratio; }
80  void write_div_ratio(uint8_t div_ratio);
81  void set_capacitance(uint8_t capacitance) { capacitance_ = capacitance; }
82  void write_capacitance(uint8_t capacitance);
83 
84  protected:
85  uint8_t read_interrupt_register_();
86  void clear_statistics_();
87  uint8_t get_distance_to_storm_();
88  uint32_t get_lightning_energy_();
89 
90  virtual uint8_t read_register(uint8_t reg) = 0;
91  uint8_t read_register_(uint8_t reg, uint8_t mask);
92 
93  virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position) = 0;
94 
99 
100  bool indoor_;
101  uint8_t noise_level_;
106  uint8_t div_ratio_;
107  uint8_t capacitance_;
108 };
109 
110 } // namespace as3935
111 } // namespace esphome
virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position)=0
void set_spike_rejection(uint8_t spike_rejection)
Definition: as3935.h:73
void set_energy_sensor(sensor::Sensor *energy_sensor)
Definition: as3935.h:63
void set_indoor(bool indoor)
Definition: as3935.h:67
virtual uint8_t read_register(uint8_t reg)=0
void write_spike_rejection(uint8_t write_spike_rejection)
Definition: as3935.cpp:93
float get_setup_priority() const override
Definition: as3935.cpp:34
uint8_t read_register_(uint8_t reg, uint8_t mask)
Definition: as3935.cpp:219
void set_distance_sensor(sensor::Sensor *distance_sensor)
Definition: as3935.h:62
void write_watchdog_threshold(uint8_t watchdog_threshold)
Definition: as3935.cpp:69
void set_div_ratio(uint8_t div_ratio)
Definition: as3935.h:79
void set_mask_disturber(bool mask_disturber)
Definition: as3935.h:77
binary_sensor::BinarySensor * thunder_alert_binary_sensor_
Definition: as3935.h:97
void set_irq_pin(GPIOPin *irq_pin)
Definition: as3935.h:61
void write_div_ratio(uint8_t div_ratio)
Definition: as3935.cpp:137
void write_lightning_threshold(uint8_t lightning_threshold)
Definition: as3935.cpp:104
void set_thunder_alert_binary_sensor(binary_sensor::BinarySensor *thunder_alert_binary_sensor)
Definition: as3935.h:64
void set_watchdog_threshold(uint8_t watchdog_threshold)
Definition: as3935.h:71
void set_lightning_threshold(uint8_t lightning_threshold)
Definition: as3935.h:75
void dump_config() override
Definition: as3935.cpp:26
void set_capacitance(uint8_t capacitance)
Definition: as3935.h:81
void write_mask_disturber(bool enabled)
Definition: as3935.cpp:125
sensor::Sensor * distance_sensor_
Definition: as3935.h:95
void write_noise_level(uint8_t noise_level)
Definition: as3935.cpp:81
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
void write_indoor(bool indoor)
Definition: as3935.cpp:59
sensor::Sensor * energy_sensor_
Definition: as3935.h:96
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
Base-class for all sensors.
Definition: sensor.h:47
void set_noise_level(uint8_t noise_level)
Definition: as3935.h:69
void write_capacitance(uint8_t capacitance)
Definition: as3935.cpp:161