10 LOG_PIN(
" IRQ Pin: ", this->
irq_pin_);
13 this->write_indoor(this->
indoor_);
24 ESP_LOGCONFIG(TAG,
" Antenna tuning: ENABLED - lightning detection will not function in this mode");
27 this->calibrate_oscillator();
31void AS3935Component::dump_config() {
32 ESP_LOGCONFIG(TAG,
"AS3935:");
33 LOG_PIN(
" Interrupt Pin: ", this->
irq_pin_);
34#ifdef USE_BINARY_SENSOR
35 LOG_BINARY_SENSOR(
" ",
"Thunder alert", this->thunder_alert_binary_sensor_);
38 LOG_SENSOR(
" ",
"Distance", this->distance_sensor_);
39 LOG_SENSOR(
" ",
"Lightning energy", this->energy_sensor_);
43void AS3935Component::loop() {
49 ESP_LOGI(TAG,
"Noise was detected - try increasing the noise level value!");
51 ESP_LOGI(TAG,
"Disturber was detected - try increasing the spike rejection value!");
53 ESP_LOGI(TAG,
"Lightning has been detected!");
54#ifdef USE_BINARY_SENSOR
55 if (this->thunder_alert_binary_sensor_ !=
nullptr) {
56 this->thunder_alert_binary_sensor_->publish_state(
true);
57 this->
set_timeout(10, [
this]() { this->thunder_alert_binary_sensor_->publish_state(
false); });
62 PUBLISH_STATE(this->distance_sensor_, distance);
65 PUBLISH_STATE(this->energy_sensor_, energy);
70void AS3935Component::write_indoor(
bool indoor) {
71 ESP_LOGV(TAG,
"Setting indoor to %d", indoor);
81void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
82 ESP_LOGV(TAG,
"Setting watchdog sensitivity to %d", watchdog_threshold);
83 if ((watchdog_threshold < 1) || (watchdog_threshold > 10))
93void AS3935Component::write_noise_level(uint8_t noise_level) {
94 ESP_LOGV(TAG,
"Setting noise level to %d", noise_level);
95 if ((noise_level < 1) || (noise_level > 7))
105void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
106 ESP_LOGV(TAG,
"Setting spike rejection to %d", spike_rejection);
107 if ((spike_rejection < 1) || (spike_rejection > 11))
116void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
117 ESP_LOGV(TAG,
"Setting lightning threshold to %d", lightning_threshold);
118 switch (lightning_threshold) {
137void AS3935Component::write_mask_disturber(
bool enabled) {
138 ESP_LOGV(TAG,
"Setting mask disturber to %d", enabled);
149void AS3935Component::write_div_ratio(uint8_t div_ratio) {
150 ESP_LOGV(TAG,
"Setting div ratio to %d", div_ratio);
173void AS3935Component::write_capacitance(uint8_t capacitance) {
174 ESP_LOGV(TAG,
"Setting tune cap to %d pF", capacitance * 8);
190 ESP_LOGV(TAG,
"Calling read_interrupt_register_");
200 ESP_LOGV(TAG,
"Calling clear_statistics_");
210 ESP_LOGV(TAG,
"Calling get_distance_to_storm_");
215 ESP_LOGV(TAG,
"Calling get_lightning_energy_");
221 pure_light = temp << 16;
224 pure_light |= temp << 8;
237uint8_t AS3935Component::read_div_ratio() {
238 ESP_LOGV(TAG,
"Calling read_div_ratio");
244 }
else if (reg_val == 1) {
246 }
else if (reg_val == 2) {
248 }
else if (reg_val == 3) {
251 ESP_LOGW(TAG,
"Unknown response received for div_ratio");
255uint8_t AS3935Component::read_capacitance() {
256 ESP_LOGV(TAG,
"Calling read_capacitance");
266void AS3935Component::display_oscillator(
bool state, uint8_t osc) {
267 if ((osc < 1) || (osc > 3))
277bool AS3935Component::calibrate_oscillator() {
278 ESP_LOGI(TAG,
"Starting oscillators calibration");
281 this->display_oscillator(
true, 2);
283 this->display_oscillator(
false, 2);
293 if (!reg_val_srco && !reg_val_trco) {
294 ESP_LOGI(TAG,
"Calibration was succesful");
297 ESP_LOGW(TAG,
"Calibration was NOT succesful");
302void AS3935Component::tune_antenna() {
303 uint8_t div_ratio = this->read_div_ratio();
304 uint8_t tune_val = this->read_capacitance();
306 "Starting antenna tuning\n"
307 " Division Ratio is set to: %d\n"
308 " Internal Capacitor is set to: %d\n"
309 " Displaying oscillator on INT pin. Measure its frequency - multiply value by Division Ratio",
310 div_ratio, tune_val);
311 this->display_oscillator(
true, ANTFREQ);