-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdht11_sensor.c
More file actions
280 lines (244 loc) · 10.2 KB
/
dht11_sensor.c
File metadata and controls
280 lines (244 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// dht11_sensor.c
// Version 0.1 - Placeholder for next update
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <gui/elements.h>
#include <gui/view_port.h>
#include <input/input.h>
#include <stm32wb55xx.h>
#include <stdlib.h>
#include <string.h>
#include <storage/storage.h>
#define SETTINGS_PATH "/ext/dht11_sensor.cfg"
// Global variables to control app state
// 'volatile' ensures this flag is not optimized away by the compiler
// as it can be modified asynchronously, e.g., by interrupt or input event
volatile bool app_running = true;
float temperature = 0.0f;
float humidity = 0.0f;
// Configuration options and flags
uint8_t selected_pin_index = 0;
bool use_fahrenheit = false;
uint8_t read_interval = 0;
uint8_t selected_menu_item = 0;
bool in_menu = true;
bool in_config = false;
bool in_about = false;
// GPIO pin options and display strings
const GpioPin* pin_options[] = { &gpio_ext_pb3, &gpio_ext_pb2, &gpio_ext_pc3 };
const char* pin_names[] = { "PB3 (Pin 5)", "PB2 (Pin 6)", "PC3 (Pin 7)" };
const char* freq_names[] = { "1 sec", "1 min", "5 min" };
const GpioPin* dht11_pin = &gpio_ext_pb3;
// Initialize DWT (Data Watchpoint and Trace unit) for microsecond timing
// The DWT is a peripheral in ARM Cortex-M cores used for precise cycle counting
// Here, it's enabled and its cycle counter is reset and started
void dwt_init(void) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}
// Get current time in microseconds
uint32_t dwt_get_us(void) { return DWT->CYCCNT / (SystemCoreClock / 1000000); }
// Microsecond delay using DWT
void delay_us(uint32_t us) { uint32_t start = dwt_get_us(); while((dwt_get_us() - start) < us); }
// Send start signal to DHT11 sensor
static void dht11_send_start_signal(void) {
furi_hal_gpio_init_simple(dht11_pin, GpioModeOutputOpenDrain);
furi_hal_gpio_write(dht11_pin, false);
furi_delay_ms(18);
furi_hal_gpio_write(dht11_pin, true);
delay_us(30);
}
// Wait for DHT11 response signal
static bool dht11_wait_response(void) {
furi_hal_gpio_init_ex(dht11_pin, GpioModeInput, GpioPullUp, GpioSpeedLow, 0);
delay_us(10);
uint32_t timeout = dwt_get_us();
while(furi_hal_gpio_read(dht11_pin)) if((dwt_get_us() - timeout) > 100) return false;
timeout = dwt_get_us();
while(!furi_hal_gpio_read(dht11_pin)) if((dwt_get_us() - timeout) > 100) return false;
timeout = dwt_get_us();
while(furi_hal_gpio_read(dht11_pin)) if((dwt_get_us() - timeout) > 100) return false;
return true;
}
// Read 40 bits of data from the DHT11 sensor
static bool dht11_read_data(uint8_t data[5]) {
memset(data, 0, 5);
uint32_t timeout;
for(int i = 0; i < 40; i++) {
timeout = dwt_get_us();
while(!furi_hal_gpio_read(dht11_pin)) if((dwt_get_us() - timeout) > 70) return false;
uint32_t bit_start = dwt_get_us();
timeout = bit_start;
while(furi_hal_gpio_read(dht11_pin)) if((dwt_get_us() - timeout) > 120) break;
uint32_t pulse_length = dwt_get_us() - bit_start;
if(pulse_length > 50) data[i/8] |= (1 << (7 - (i%8)));
}
return true;
}
// Validate checksum of the received data
static bool dht11_validate_checksum(uint8_t data[5]) {
return data[4] == ((data[0]+data[1]+data[2]+data[3]) & 0xFF);
}
// Read temperature and humidity from DHT11
bool dht11_read(float* temperature, float* humidity) {
uint8_t data[5];
dht11_send_start_signal();
if(!dht11_wait_response()) return false;
if(!dht11_read_data(data)) return false;
furi_hal_gpio_init_simple(dht11_pin, GpioModeOutputOpenDrain);
furi_hal_gpio_write(dht11_pin, true);
if(!dht11_validate_checksum(data)) return false;
*humidity = (float)data[0];
*temperature = (float)data[2];
return true;
}
// Save configuration settings to file
void save_settings(void) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
char line[64];
snprintf(line, sizeof(line), "%d %d %d", selected_pin_index, use_fahrenheit, read_interval);
storage_file_write(file, line, strlen(line));
storage_file_close(file);
}
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
// Load configuration settings from file
void load_settings(void) {
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
if(storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
char line[64] = {0};
storage_file_read(file, line, sizeof(line) - 1);
sscanf(line, "%hhu %hhu %hhu", &selected_pin_index, (uint8_t*)&use_fahrenheit, &read_interval);
if(selected_pin_index > 2) selected_pin_index = 0;
if(read_interval > 2) read_interval = 0;
dht11_pin = pin_options[selected_pin_index];
storage_file_close(file);
}
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
}
// Handle input when in the main menu
static void handle_menu_input(InputKey key) {
if(key == InputKeyUp) selected_menu_item = (selected_menu_item + 3) % 4;
if(key == InputKeyDown) selected_menu_item = (selected_menu_item + 1) % 4;
if(key == InputKeyOk) {
if(selected_menu_item == 0) in_menu = false;
else if(selected_menu_item == 1) { in_menu = false; in_config = true; selected_menu_item = 0; }
else if(selected_menu_item == 2) { in_menu = false; in_about = true; }
else if(selected_menu_item == 3) app_running = false;
}
}
// Handle input when in configuration screen
static void handle_config_input(InputKey key) {
if(key == InputKeyBack) { in_config = false; in_menu = true; save_settings(); }
else if(key == InputKeyUp) selected_menu_item = (selected_menu_item + 2) % 3;
else if(key == InputKeyDown) selected_menu_item = (selected_menu_item + 1) % 3;
else if(key == InputKeyRight || key == InputKeyLeft) {
if(selected_menu_item == 0)
selected_pin_index = (selected_pin_index + 1) % 3;
else if(selected_menu_item == 1)
use_fahrenheit = !use_fahrenheit;
else if(selected_menu_item == 2)
read_interval = (read_interval + 1) % 3;
dht11_pin = pin_options[selected_pin_index];
}
}
// Handle input when in about screen or reading screen
static void handle_simple_back(InputKey key) {
if(key == InputKeyBack) {
if(in_about) { in_about = false; in_menu = true; }
else in_menu = true;
}
}
// Unified input handler
void input_callback(InputEvent* event, void* context) {
UNUSED(context);
if(event->type == InputTypeShort) {
if(in_menu) handle_menu_input(event->key);
else if(in_config) handle_config_input(event->key);
else handle_simple_back(event->key);
}
}
// Render current screen on the canvas
void render_gui(Canvas* canvas, void* context) {
UNUSED(context);
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
if(in_menu) {
canvas_draw_str(canvas, 24, 10, "DHT11 Sensor");
const char* menu[] = {"Acquire Data", "Configuration", "About", "Exit"};
for(int i = 0; i < 4; i++) {
if(i == selected_menu_item) canvas_draw_box(canvas, 2, 14 + i * 12, 120, 12);
canvas_set_color(canvas, i == selected_menu_item ? ColorWhite : ColorBlack);
canvas_draw_str(canvas, 6, 24 + i * 12, menu[i]);
canvas_set_color(canvas, ColorBlack);
}
} else if(in_config) {
canvas_draw_str(canvas, 24, 12, "Configuration");
char line[32];
for(int i = 0; i < 3; i++) {
if(i == selected_menu_item) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_box(canvas, 2, 18 + i * 14, 120, 14);
canvas_set_color(canvas, ColorWhite);
} else {
canvas_set_color(canvas, ColorBlack);
}
if(i == 0) snprintf(line, sizeof(line), "Pin: %s", pin_names[selected_pin_index]);
else if(i == 1) snprintf(line, sizeof(line), "Units: %s", use_fahrenheit ? "F" : "C");
else snprintf(line, sizeof(line), "Interval: %s", freq_names[read_interval]);
canvas_draw_str(canvas, 6, 28 + i * 14, line);
}
} else if(in_about) {
canvas_draw_str(canvas, 0, 14, ".DHT11 Sensor v0.1");
canvas_draw_str(canvas, 0, 28, ".Designed by Javier Canon");
canvas_draw_str(canvas, 0, 42, ".https://github.com/");
canvas_draw_str(canvas, 0, 56, "canonjc/flipper-dht11");
} else {
canvas_draw_str(canvas, 32, 12, "R e a d i n g . . .");
char line[32];
float display_temp = use_fahrenheit ? (temperature * 9.0 / 5.0 + 32.0) : temperature;
snprintf(line, sizeof(line), "Temp: %.1f %c", (double)display_temp, use_fahrenheit ? 'F' : 'C');
canvas_draw_str(canvas, 6, 28, line);
snprintf(line, sizeof(line), "Humidity: %.1f%%", (double)humidity);
canvas_draw_str(canvas, 6, 42, line);
}
} // unchanged
// Main application loop
int32_t dht11_sensor_app(void* p) {
UNUSED(p);
// Setup: initialize microsecond timing and load saved settings
dwt_init();
load_settings();
// GUI setup
Gui* gui = furi_record_open(RECORD_GUI);
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_gui, NULL);
view_port_input_callback_set(view_port, input_callback, NULL);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
// Timing setup for periodic reading
uint32_t last_read_time = furi_get_tick();
uint32_t interval_ms[] = {1000, 60000, 300000};
// Main loop: handles sensor reading and GUI update
while(app_running) {
if(!in_menu && !in_config && !in_about) {
if(furi_get_tick() - last_read_time >= interval_ms[read_interval]) {
dht11_read(&temperature, &humidity);
last_read_time = furi_get_tick();
}
}
view_port_update(view_port);
furi_delay_ms(100);
}
// Cleanup: remove GUI view and free resources
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_record_close(RECORD_GUI);
return 0;
} // unchanged