-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_master.c
More file actions
94 lines (76 loc) · 2.74 KB
/
Copy patheasy_master.c
File metadata and controls
94 lines (76 loc) · 2.74 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
/**
* @file easy_master.c
* @brief Minimal SDI-12 data recorder using sdi12_easy.h macros.
*
* Shows the simplest way to scan, measure, and read SDI-12 sensors.
*
* Compile check:
* gcc -std=c11 -fsyntax-only -I.. easy_master.c
*/
#include "sdi12_easy.h"
#include <stdio.h>
/* ── Step 1: Write your hardware functions ─────────────────────────────── */
void my_send(const char *data, size_t len, void *ud) {
(void)ud; (void)data; (void)len;
/* TODO: uart_write(data, len); uart_flush(); */
}
size_t my_recv(char *buf, size_t max, uint32_t timeout_ms, void *ud) {
(void)ud; (void)buf; (void)max; (void)timeout_ms;
/* TODO: return uart_read_until_crlf(buf, max, timeout_ms); */
return 0;
}
void my_dir(sdi12_dir_t dir, void *ud) {
(void)ud; (void)dir;
/* TODO: gpio_write(DIR_PIN, dir == SDI12_DIR_TX); */
}
void my_break(void *ud) {
(void)ud;
/* TODO: hold SDI-12 line high for ≥12ms */
}
void my_delay(uint32_t ms, void *ud) {
(void)ud; (void)ms;
/* TODO: delay_ms(ms); */
}
/* ── Step 2: Define your master ────────────────────────────────────────── */
SDI12_MASTER_DEFINE(recorder, my_send, my_recv, my_dir, my_break, my_delay);
/* ── Step 3: Use it ────────────────────────────────────────────────────── */
void setup(void) {
SDI12_MASTER_SETUP(recorder);
}
void read_sensor(char addr) {
/* Wake the bus */
SDI12_MASTER_BREAK(recorder);
/* Check if sensor is present */
bool present = false;
SDI12_MASTER_PING(recorder, addr, &present);
if (!present) {
printf("No sensor at '%c'\n", addr);
return;
}
/* Identify it */
sdi12_ident_t id;
SDI12_MASTER_IDENTIFY(recorder, addr, &id);
printf("Sensor: %.8s %.6s\n", id.vendor, id.model);
/* Take a measurement */
sdi12_meas_response_t mresp;
SDI12_MASTER_MEASURE(recorder, addr, &mresp);
printf("Wait %ds for %d values\n", mresp.wait_seconds, mresp.value_count);
/* Wait if needed */
if (mresp.wait_seconds > 0) {
SDI12_MASTER_WAIT(recorder, addr,
(uint32_t)mresp.wait_seconds * 1000 + 1000);
}
/* Read data */
sdi12_data_response_t dresp;
SDI12_MASTER_GET_DATA(recorder, addr, 0, false, &dresp);
for (uint8_t i = 0; i < dresp.value_count; i++) {
printf(" Value[%d] = %.*f\n", i,
dresp.values[i].decimals,
(double)dresp.values[i].value);
}
}
/*
* That's it! To read sensor at address '0':
* setup();
* read_sensor('0');
*/