-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
211 lines (167 loc) · 4.85 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "thread.h"
#include "ztimer.h"
#include "mutex.h"
#include "lpsxxx.h"
#include "lpsxxx_params.h"
#include "msg.h"
#include "net/gcoap.h"
#include "net/ipv6/addr.h"
#include "net/sock/util.h"
#include "shell.h"
#include "net/utils.h"
#include "od.h"
#include "ztimer.h"
#include "mutex.h"
#include "gcoap_example.h"
#define ENABLE_DEBUG 1
#include "debug.h"
typedef struct {
char buffer[128];
int16_t tempList[5];
} data_t;
static data_t data;
static lpsxxx_t lpsxxx;
// static mutex_t lps_lock = MUTEX_INIT;
#define LPSXXX_REG_RES_CONF (0x10)
#define LPSXXX_REG_CTRL_REG2 (0x21)
#define DEV_I2C (dev->params.i2c)
#define DEV_ADDR (dev->params.addr)
#define DEV_RATE (dev->params.rate)
int write_register_value(const lpsxxx_t *dev, uint16_t reg, uint8_t value)
{
i2c_acquire(DEV_I2C);
if (i2c_write_reg(DEV_I2C, DEV_ADDR, reg, value, 0) < 0)
{
i2c_release(DEV_I2C);
return -LPSXXX_ERR_I2C;
}
i2c_release(DEV_I2C);
return LPSXXX_OK; // Success
}
int temp_sensor_write_CTRL_REG2_value(const lpsxxx_t *dev, uint8_t value)
{
return write_register_value(dev, LPSXXX_REG_CTRL_REG2, value);
}
int temp_sensor_write_res_conf(const lpsxxx_t *dev, uint8_t value)
{
return write_register_value(dev, LPSXXX_REG_RES_CONF, value);
}
int temp_sensor_reset(void)
{
lpsxxx_params_t paramts = {
.i2c = lpsxxx_params[0].i2c,
.addr = lpsxxx_params[0].addr,
.rate = LPSXXX_RATE_7HZ};
// .rate = lpsxxx_params[0].rate
// LPSXXX_RATE_7HZ = 5, /**< sample with 7Hz, default */
// LPSXXX_RATE_12HZ5 = 6, /**< sample with 12.5Hz */
// LPSXXX_RATE_25HZ = 7
if (lpsxxx_init(&lpsxxx, ¶mts) != LPSXXX_OK)
{
puts("Sensor initialization failed");
return 0;
}
// 7 6543 2 1 0
// BOOT RESERVED SWRESET AUTO_ZERO ONE_SHOT
// 1 0000 1 0 0
// 44
if (temp_sensor_write_CTRL_REG2_value(&lpsxxx, 0x44) != LPSXXX_OK)
{
puts("Sensor reset failed");
return 0;
}
ztimer_sleep(ZTIMER_MSEC, 5000);
// 0x40 -- 01000000
// AVGT2 AVGT1 AVGT0 100 -- Nr. internal average : 16
if (temp_sensor_write_res_conf(&lpsxxx, 0x40) != LPSXXX_OK)
{
puts("Sensor enable failed");
return 0;
}
if (lpsxxx_enable(&lpsxxx) != LPSXXX_OK)
{
puts("Sensor enable failed");
return 0;
}
ztimer_sleep(ZTIMER_MSEC, 1000);
return 1;
}
int calculate_odd_parity(int num) {
int parityBit = 0;
int count = 0; // To count the number of set bits
// Count the number of set bits (1-bits) in the given number
while (num) {
count += num & 1; // Increment count if rightmost bit is set
num >>= 1; // Right shift num to check the next bit
}
// Set parityBit to 1 if the count of set bits is even, else 0
parityBit = (count % 2 == 0) ? 1 : 0;
return parityBit;
}
#define MAIN_QUEUE_SIZE (4)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
void setup_coap_client(void) {
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
ztimer_sleep(ZTIMER_MSEC, 1000);
}
int main(void)
{
if (temp_sensor_reset() == 0) {
puts("Sensor failed");
return 1;
}
// int16_t avg_temp = 0;
int counter = 0;
int array_length = 0;
int parity;
while (1) {
int16_t temp = 0;
if (lpsxxx_read_temp(&lpsxxx, &temp) == LPSXXX_OK) {
if (array_length < 4) {
data.tempList[array_length++] = temp;
}
else {
data.tempList[array_length++] = temp;
int32_t sum = 0;
int numElements = array_length;
// printf("No of ele: %i\n", numElements);
for (int i = 0; i < numElements; i++) {
sum += (int32_t)data.tempList[i];
// printf("Temp List: %i.%u°C\n", (data.tempList[i] / 100), (data.tempList[i] % 100));
}
// printf("Sum: %li\n", sum);
// avg_temp = sum / numElements;
double avg_temp = (double)sum / numElements;
// Round to the nearest integer
int16_t rounded_avg_temp = (int16_t)round(avg_temp);
char temp_str[10];
char parity_bit[4];
sprintf(temp_str, "%i,", rounded_avg_temp);
// printf("Temp Str: %s°C\n", temp_str);
strcat(data.buffer, temp_str);
parity = calculate_odd_parity(rounded_avg_temp);
sprintf(parity_bit, "%i,", parity);
// printf("Temp Str: %s°C\n", temp_str);
strcat(data.buffer, parity_bit);
for (int i = 0; i < array_length - 1; ++i) {
data.tempList[i] = data.tempList[i + 1];
}
array_length--;
counter++;
}
}
if (counter == 10) {
DEBUG_PRINT("Data: %s\n", data.buffer);
ztimer_sleep(ZTIMER_MSEC, 1000);
gcoap_post(data.buffer, TEMP);
memset(data.buffer, 0, sizeof(data.buffer));
counter = 0;
}
ztimer_sleep(ZTIMER_MSEC, 1000);
}
return 0;
}