ESPHome  2024.10.2
as3935.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/hal.h"
6 #ifdef USE_SENSOR
8 #endif
9 #ifdef USE_BINARY_SENSOR
11 #endif
12 
13 namespace esphome {
14 namespace as3935 {
15 
16 static const uint8_t DIRECT_COMMAND = 0x96;
17 static const uint8_t ANTFREQ = 3;
18 
20  AFE_GAIN = 0x00,
29  CALIB_TRCO = 0x3A,
30  CALIB_SRCO = 0x3B,
31  DEFAULT_RESET = 0x3C,
32  CALIB_RCO = 0x3D
33 };
34 
36  WIPE_ALL = 0x0,
37  GAIN_MASK = 0x3E,
38  SPIKE_MASK = 0xF,
39  IO_MASK = 0xC1,
40  DISTANCE_MASK = 0xC0,
41  INT_MASK = 0xF0,
42  THRESH_MASK = 0x0F,
43  R_SPIKE_MASK = 0xF0,
44  ENERGY_MASK = 0xF0,
45  CAP_MASK = 0xF0,
46  LIGHT_MASK = 0xCF,
47  DISTURB_MASK = 0xDF,
49  OSC_MASK = 0xE0,
50  CALIB_MASK = 0x7F,
52  DIV_MASK = 0x3F
53 };
54 
56  AS3935_ADDR = 0x03,
57  INDOOR = 0x12,
58  OUTDOOR = 0xE,
59  LIGHTNING_INT = 0x08,
60  DISTURBER_INT = 0x04,
61  NOISE_INT = 0x01
62 };
63 
64 class AS3935Component : public Component {
65 #ifdef USE_SENSOR
66  SUB_SENSOR(distance)
67  SUB_SENSOR(energy)
68 #endif
69 
70 #ifdef USE_BINARY_SENSOR
71  SUB_BINARY_SENSOR(thunder_alert)
72 #endif
73 
74  public:
75  void setup() override;
76  void dump_config() override;
77  float get_setup_priority() const override;
78  void loop() override;
79 
80  void set_irq_pin(GPIOPin *irq_pin) { irq_pin_ = irq_pin; }
81 
82  void set_indoor(bool indoor) { indoor_ = indoor; }
83  void write_indoor(bool indoor);
84  void set_noise_level(uint8_t noise_level) { noise_level_ = noise_level; }
85  void write_noise_level(uint8_t noise_level);
86  void set_watchdog_threshold(uint8_t watchdog_threshold) { watchdog_threshold_ = watchdog_threshold; }
87  void write_watchdog_threshold(uint8_t watchdog_threshold);
88  void set_spike_rejection(uint8_t spike_rejection) { spike_rejection_ = spike_rejection; }
89  void write_spike_rejection(uint8_t write_spike_rejection);
90  void set_lightning_threshold(uint8_t lightning_threshold) { lightning_threshold_ = lightning_threshold; }
91  void write_lightning_threshold(uint8_t lightning_threshold);
92  void set_mask_disturber(bool mask_disturber) { mask_disturber_ = mask_disturber; }
93  void write_mask_disturber(bool enabled);
94  void set_div_ratio(uint8_t div_ratio) { div_ratio_ = div_ratio; }
95  void write_div_ratio(uint8_t div_ratio);
96  void set_capacitance(uint8_t capacitance) { capacitance_ = capacitance; }
97  void write_capacitance(uint8_t capacitance);
98  uint8_t read_div_ratio();
99  uint8_t read_capacitance();
100  bool calibrate_oscillator();
101  void display_oscillator(bool state, uint8_t osc);
102  void tune_antenna();
103  void set_tune_antenna(bool tune_antenna) { tune_antenna_ = tune_antenna; }
104  void set_calibration(bool calibration) { calibration_ = calibration; }
105 
106  protected:
107  uint8_t read_interrupt_register_();
108  void clear_statistics_();
109  uint8_t get_distance_to_storm_();
110  uint32_t get_lightning_energy_();
111 
112  virtual uint8_t read_register(uint8_t reg) = 0;
113  uint8_t read_register_(uint8_t reg, uint8_t mask);
114 
115  virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position) = 0;
116 
118 
119  bool indoor_;
120  uint8_t noise_level_;
125  uint8_t div_ratio_;
126  uint8_t capacitance_;
129 };
130 
131 } // namespace as3935
132 } // namespace esphome
virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position)=0
virtual uint8_t read_register(uint8_t reg)=0
uint8_t read_register_(uint8_t reg, uint8_t mask)
Definition: as3935.cpp:319
virtual void setup()
Where the component's initialization should happen.
Definition: component.cpp:48
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool state
Definition: fan.h:34