ESPHome 2025.8.0b1
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("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 }
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(" ", "Firmware Version", this->fw_version_sensor_);
66 LOG_SENSOR(" ", "Average Radaition Dose per 1 minute", this->rad_1m_sensor_);
67 LOG_SENSOR(" ", "Average Radaition Dose per 10 minutes", this->rad_10m_sensor_);
68 LOG_SENSOR(" ", "Status", this->status_sensor_);
69 LOG_SENSOR(" ", "Measurement Duration", this->measurement_duration_sensor_);
70#endif // USE_SENSOR
71
72#ifdef USE_BINARY_SENSOR
73 LOG_BINARY_SENSOR(" ", "Vibration Status", this->vibration_binary_sensor_);
74#endif // USE_BINARY_SENSOR
75}
76
77float GDK101Component::get_setup_priority() const { return setup_priority::DATA; }
78
79bool GDK101Component::read_bytes_with_retry_(uint8_t a_register, uint8_t *data, uint8_t len) {
80 uint8_t retry = NUMBER_OF_READ_RETRIES;
81 bool status = false;
82 while (!status && retry) {
83 status = this->read_bytes(a_register, data, len);
84 retry--;
85 }
86 return status;
87}
88
89bool GDK101Component::reset_sensor_(uint8_t *data) {
90 // It looks like reset is not so well designed in that sensor
91 // After sending reset command it looks that sensor start performing reset and is unresponsible during read
92 // after a while we can send another reset command and read "0x01" as confirmation
93 // Documentation not going in to such details unfortunately
94 if (!this->read_bytes_with_retry_(GDK101_REG_RESET, data, 2)) {
95 ESP_LOGE(TAG, "Updating GDK101 failed!");
96 return false;
97 }
98
99 return true;
100}
101
103#ifdef USE_SENSOR
104 if (this->rad_1m_sensor_ != nullptr) {
105 if (!this->read_bytes(GDK101_REG_READ_1MIN_AVG, data, 2)) {
106 ESP_LOGE(TAG, "Updating GDK101 failed!");
107 return false;
108 }
109
110 const float dose = data[0] + (data[1] / 100.0f);
111
112 this->rad_1m_sensor_->publish_state(dose);
113 }
114#endif // USE_SENSOR
115 return true;
116}
117
119#ifdef USE_SENSOR
120 if (this->rad_10m_sensor_ != nullptr) {
121 if (!this->read_bytes(GDK101_REG_READ_10MIN_AVG, data, 2)) {
122 ESP_LOGE(TAG, "Updating GDK101 failed!");
123 return false;
124 }
125
126 const float dose = data[0] + (data[1] / 100.0f);
127
128 this->rad_10m_sensor_->publish_state(dose);
129 }
130#endif // USE_SENSOR
131 return true;
132}
133
134bool GDK101Component::read_status_(uint8_t *data) {
135 if (!this->read_bytes(GDK101_REG_READ_STATUS, data, 2)) {
136 ESP_LOGE(TAG, "Updating GDK101 failed!");
137 return false;
138 }
139
140#ifdef USE_SENSOR
141 if (this->status_sensor_ != nullptr) {
142 this->status_sensor_->publish_state(data[0]);
143 }
144#endif // USE_SENSOR
145
146#ifdef USE_BINARY_SENSOR
147 if (this->vibration_binary_sensor_ != nullptr) {
148 this->vibration_binary_sensor_->publish_state(data[1]);
149 }
150#endif // USE_BINARY_SENSOR
151
152 return true;
153}
154
156#ifdef USE_SENSOR
157 if (this->fw_version_sensor_ != nullptr) {
158 if (!this->read_bytes(GDK101_REG_READ_FIRMWARE, data, 2)) {
159 ESP_LOGE(TAG, "Updating GDK101 failed!");
160 return false;
161 }
162
163 const float fw_version = data[0] + (data[1] / 10.0f);
164
165 this->fw_version_sensor_->publish_state(fw_version);
166 }
167#endif // USE_SENSOR
168 return true;
169}
170
172#ifdef USE_SENSOR
173 if (this->measurement_duration_sensor_ != nullptr) {
174 if (!this->read_bytes(GDK101_REG_READ_MEASURING_TIME, data, 2)) {
175 ESP_LOGE(TAG, "Updating GDK101 failed!");
176 return false;
177 }
178
179 const float meas_time = (data[0] * 60) + data[1];
180
181 this->measurement_duration_sensor_->publish_state(meas_time);
182 }
183#endif // USE_SENSOR
184 return true;
185}
186
187} // namespace gdk101
188} // 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:85
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:134
bool read_bytes_with_retry_(uint8_t a_register, uint8_t *data, uint8_t len)
Definition gdk101.cpp:79
bool read_dose_1m_(uint8_t *data)
Definition gdk101.cpp:102
bool read_dose_10m_(uint8_t *data)
Definition gdk101.cpp:118
bool read_fw_version_(uint8_t *data)
Definition gdk101.cpp:155
bool reset_sensor_(uint8_t *data)
Definition gdk101.cpp:89
bool read_measurement_duration_(uint8_t *data)
Definition gdk101.cpp:171
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:216
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29