ESPHome  2024.9.0
gdk101.cpp
Go to the documentation of this file.
1 #include "gdk101.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace gdk101 {
7 
8 static const char *const TAG = "gdk101";
9 static const uint8_t NUMBER_OF_READ_RETRIES = 5;
10 
11 void GDK101Component::update() {
12  uint8_t data[2];
13  if (!this->read_dose_1m_(data)) {
14  this->status_set_warning("Failed to read dose 1m");
15  return;
16  }
17 
18  if (!this->read_dose_10m_(data)) {
19  this->status_set_warning("Failed to read dose 10m");
20  return;
21  }
22 
23  if (!this->read_status_(data)) {
24  this->status_set_warning("Failed to read status");
25  return;
26  }
27 
28  if (!this->read_measurement_duration_(data)) {
29  this->status_set_warning("Failed to read measurement duration");
30  return;
31  }
32  this->status_clear_warning();
33 }
34 
36  uint8_t data[2];
37  ESP_LOGCONFIG(TAG, "Setting up GDK101...");
38  // first, reset the sensor
39  if (!this->reset_sensor_(data)) {
40  this->status_set_error("Reset failed!");
41  this->mark_failed();
42  return;
43  }
44  // sensor should acknowledge success of the reset procedure
45  if (data[0] != 1) {
46  this->status_set_error("Reset not acknowledged!");
47  this->mark_failed();
48  return;
49  }
50  delay(10);
51  // read firmware version
52  if (!this->read_fw_version_(data)) {
53  this->status_set_error("Failed to read firmware version");
54  this->mark_failed();
55  return;
56  }
57 }
58 
59 void GDK101Component::dump_config() {
60  ESP_LOGCONFIG(TAG, "GDK101:");
61  LOG_I2C_DEVICE(this);
62  if (this->is_failed()) {
63  ESP_LOGE(TAG, "Communication with GDK101 failed!");
64  }
65 #ifdef USE_SENSOR
66  LOG_SENSOR(" ", "Firmware Version", this->fw_version_sensor_);
67  LOG_SENSOR(" ", "Average Radaition Dose per 1 minute", this->rad_1m_sensor_);
68  LOG_SENSOR(" ", "Average Radaition Dose per 10 minutes", this->rad_10m_sensor_);
69  LOG_SENSOR(" ", "Status", this->status_sensor_);
70  LOG_SENSOR(" ", "Measurement Duration", this->measurement_duration_sensor_);
71 #endif // USE_SENSOR
72 
73 #ifdef USE_BINARY_SENSOR
74  LOG_BINARY_SENSOR(" ", "Vibration Status", this->vibration_binary_sensor_);
75 #endif // USE_BINARY_SENSOR
76 }
77 
78 float GDK101Component::get_setup_priority() const { return setup_priority::DATA; }
79 
80 bool GDK101Component::read_bytes_with_retry_(uint8_t a_register, uint8_t *data, uint8_t len) {
81  uint8_t retry = NUMBER_OF_READ_RETRIES;
82  bool status = false;
83  while (!status && retry) {
84  status = this->read_bytes(a_register, data, len);
85  retry--;
86  }
87  return status;
88 }
89 
90 bool GDK101Component::reset_sensor_(uint8_t *data) {
91  // It looks like reset is not so well designed in that sensor
92  // After sending reset command it looks that sensor start performing reset and is unresponsible during read
93  // after a while we can send another reset command and read "0x01" as confirmation
94  // Documentation not going in to such details unfortunately
95  if (!this->read_bytes_with_retry_(GDK101_REG_RESET, data, 2)) {
96  ESP_LOGE(TAG, "Updating GDK101 failed!");
97  return false;
98  }
99 
100  return true;
101 }
102 
103 bool GDK101Component::read_dose_1m_(uint8_t *data) {
104 #ifdef USE_SENSOR
105  if (this->rad_1m_sensor_ != nullptr) {
106  if (!this->read_bytes(GDK101_REG_READ_1MIN_AVG, data, 2)) {
107  ESP_LOGE(TAG, "Updating GDK101 failed!");
108  return false;
109  }
110 
111  const float dose = data[0] + (data[1] / 100.0f);
112 
113  this->rad_1m_sensor_->publish_state(dose);
114  }
115 #endif // USE_SENSOR
116  return true;
117 }
118 
119 bool GDK101Component::read_dose_10m_(uint8_t *data) {
120 #ifdef USE_SENSOR
121  if (this->rad_10m_sensor_ != nullptr) {
122  if (!this->read_bytes(GDK101_REG_READ_10MIN_AVG, data, 2)) {
123  ESP_LOGE(TAG, "Updating GDK101 failed!");
124  return false;
125  }
126 
127  const float dose = data[0] + (data[1] / 100.0f);
128 
129  this->rad_10m_sensor_->publish_state(dose);
130  }
131 #endif // USE_SENSOR
132  return true;
133 }
134 
135 bool GDK101Component::read_status_(uint8_t *data) {
136  if (!this->read_bytes(GDK101_REG_READ_STATUS, data, 2)) {
137  ESP_LOGE(TAG, "Updating GDK101 failed!");
138  return false;
139  }
140 
141 #ifdef USE_SENSOR
142  if (this->status_sensor_ != nullptr) {
143  this->status_sensor_->publish_state(data[0]);
144  }
145 #endif // USE_SENSOR
146 
147 #ifdef USE_BINARY_SENSOR
148  if (this->vibration_binary_sensor_ != nullptr) {
149  this->vibration_binary_sensor_->publish_state(data[1]);
150  }
151 #endif // USE_BINARY_SENSOR
152 
153  return true;
154 }
155 
157 #ifdef USE_SENSOR
158  if (this->fw_version_sensor_ != nullptr) {
159  if (!this->read_bytes(GDK101_REG_READ_FIRMWARE, data, 2)) {
160  ESP_LOGE(TAG, "Updating GDK101 failed!");
161  return false;
162  }
163 
164  const float fw_version = data[0] + (data[1] / 10.0f);
165 
166  this->fw_version_sensor_->publish_state(fw_version);
167  }
168 #endif // USE_SENSOR
169  return true;
170 }
171 
173 #ifdef USE_SENSOR
174  if (this->measurement_duration_sensor_ != nullptr) {
175  if (!this->read_bytes(GDK101_REG_READ_MEASURING_TIME, data, 2)) {
176  ESP_LOGE(TAG, "Updating GDK101 failed!");
177  return false;
178  }
179 
180  const float meas_time = (data[0] * 60) + data[1];
181 
182  this->measurement_duration_sensor_->publish_state(meas_time);
183  }
184 #endif // USE_SENSOR
185  return true;
186 }
187 
188 } // namespace gdk101
189 } // namespace esphome
bool reset_sensor_(uint8_t *data)
Definition: gdk101.cpp:90
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition: i2c.h:212
bool read_fw_version_(uint8_t *data)
Definition: gdk101.cpp:156
void status_set_error(const char *message="unspecified")
Definition: component.cpp:159
virtual void setup()
Where the component's initialization should happen.
Definition: component.cpp:48
void status_clear_warning()
Definition: component.cpp:166
bool read_bytes_with_retry_(uint8_t a_register, uint8_t *data, uint8_t len)
Definition: gdk101.cpp:80
uint8_t status
Definition: bl0942.h:74
bool read_dose_1m_(uint8_t *data)
Definition: gdk101.cpp:103
std::string size_t len
Definition: helpers.h:292
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
bool read_status_(uint8_t *data)
Definition: gdk101.cpp:135
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool read_measurement_duration_(uint8_t *data)
Definition: gdk101.cpp:172
bool read_dose_10m_(uint8_t *data)
Definition: gdk101.cpp:119
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26