aboutsummaryrefslogtreecommitdiff
path: root/control/sensors.c
blob: 0e7d9f8085e793c4a8abe3a8c17c66ca7276f579 (plain)
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
#include "sensors.h"
#include <bcl.h>

#define SERVICE_INTERVAL_INTERVAL (60 * 60 * 1000)
#define BATTERY_UPDATE_INTERVAL (60 * 60 * 1000)

#define TEMPERATURE_TAG_PUB_NO_CHANGE_INTEVAL (15 * 60 * 1000)
#define TEMPERATURE_TAG_PUB_VALUE_CHANGE 0.2f
#define TEMPERATURE_UPDATE_SERVICE_INTERVAL (5 * 1000)
#define TEMPERATURE_UPDATE_NORMAL_INTERVAL (10 * 1000)

#define APPLICATION_TASK_ID 0


typedef struct {
	uint8_t number;
	float value;
	bc_tick_t next_pub;
} event_param_t;

// Thermometer instance
bc_tmp112_t tmp112;
event_param_t temperature_event_param = { .next_pub = 0, .value = NAN };

void tmp112_event_handler(bc_tmp112_t *self, bc_tmp112_event_t event, void *event_param) {
	float value;
	event_param_t *param = (event_param_t *)event_param;

	if (event != BC_TMP112_EVENT_UPDATE)
		return;

	if (bc_tmp112_get_temperature_celsius(self, &value)) {
		if ((fabsf(value - param->value) >= TEMPERATURE_TAG_PUB_VALUE_CHANGE) || (param->next_pub < bc_scheduler_get_spin_tick())) {
			bc_radio_pub_temperature(BC_RADIO_PUB_CHANNEL_R1_I2C0_ADDRESS_ALTERNATE, &value);
			param->value = value;
			param->next_pub = bc_scheduler_get_spin_tick() + TEMPERATURE_TAG_PUB_NO_CHANGE_INTEVAL;
		}
	} else
		param->value = NAN;
}

void switch_to_normal_mode_task(void *param) {
	bc_tmp112_set_update_interval(&tmp112, TEMPERATURE_UPDATE_NORMAL_INTERVAL);
	bc_scheduler_unregister(bc_scheduler_get_current_task_id());
}

void init_temperature() {
	bc_tmp112_init(&tmp112, BC_I2C_I2C0, 0x49);
	bc_tmp112_set_event_handler(&tmp112, tmp112_event_handler, &temperature_event_param);
	bc_tmp112_set_update_interval(&tmp112, TEMPERATURE_UPDATE_SERVICE_INTERVAL);

	bc_scheduler_register(switch_to_normal_mode_task, NULL, SERVICE_INTERVAL_INTERVAL);
}


void battery_event_handler(bc_module_battery_event_t event, void *event_param) {
	if (event != BC_MODULE_BATTERY_EVENT_UPDATE)
		return;
	float voltage;
	 if (bc_module_battery_get_voltage(&voltage))
		bc_radio_pub_battery(&voltage);
}

void init_battery() {
	bc_module_battery_init();
	bc_module_battery_set_event_handler(battery_event_handler, NULL);
	bc_module_battery_set_update_interval(BATTERY_UPDATE_INTERVAL);
}