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
|
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include "data_manager.h"
#include "barometer.h"
static float pressure[PRESSURE_UNIT_MAX];
static float setting[PRESSURE_UNIT_MAX];
static float altitude[ALTITUDE_UNIT_MAX];
/* utility functions */
static float pressure_to_metres_asl(float real, float setting)
{
return 44330.f*(1.f-powf(real/setting, 1/5.255));
}
/* exported functions */
#define DATA_MANAGER_GETTER(name, store, bound, enum_type, return_type) \
float name(enum_type unit) \
{ \
if (unit >= 0 && unit < bound) { \
return store[unit]; \
} else { \
abort(); \
} \
}
DATA_MANAGER_GETTER(
data_manager_get_pressure,
pressure,
PRESSURE_UNIT_MAX,
enum pressure_unit,
float)
DATA_MANAGER_GETTER(
data_manager_get_setting,
setting,
PRESSURE_UNIT_MAX,
enum pressure_unit,
float)
DATA_MANAGER_GETTER(
data_manager_get_altitude,
altitude,
ALTITUDE_UNIT_MAX,
enum altitude_unit,
float)
void data_manager_init(void)
{
/* FIXME load initial setting and force a tick synchronously */
setting[PRESSURE_UNIT_HPA] = 1013.25;
data_manager_tick();
}
void data_manager_tick(void)
{
/* FIXME alt rate */
/* FIXME calculate on demand? */
/* FIXME hardcoding the conversion factors here is gross */
float p = barometer_read();
pressure[PRESSURE_UNIT_HPA] = p;
pressure[PRESSURE_UNIT_INHG] = p / 3.38639;
/* FIXME we need atomic access to alt setting once user can set it. Check
* if AVR allows multiple/nested ISRs to run concurrently, pretty sure not
*/
float a = pressure_to_metres_asl(p, setting[PRESSURE_UNIT_HPA]);
altitude[ALTITUDE_UNIT_METRE] = a;
altitude[ALTITUDE_UNIT_FT] = a * 3.048;
}
|