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