summaryrefslogtreecommitdiffstats
path: root/apps/Sensor Watch BME280 Project/app.h
blob: e0bbe1c2514d250ad816d967c9759e9d20108a3a (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
69
70
71
72
73
74
75
76
// Sensor Watch: Hiking Log Demo App
// This app displays a clock and temperature data from a BME280 temperature and humidiity sensor.
// It also logs up to 36 hours of temperature data for playback.
// You can use this app on backcountry treks: take the watch off at night and place it outside your tent.
// It will log overnight low temperatures for review in the morning and optional transfer to your notepad.

#define MAX_DATA_POINTS 36

typedef enum ApplicationMode {
    MODE_CLOCK = 0, // Displays month, day and current time.
    MODE_TEMP,      // (TE) Displays temperature and an optional humidity reading (0-10 representing 0-100%)
    MODE_LOG,       // (LO) Plays back temperature data (temperature in seconds slot)
    MODE_PREFS,     // (PR) Allows setting options for the application
    MODE_SET,       // (ST) Set time and date
    NUM_MODES       // Last item in the enum, it's the number of cases.
} ApplicationMode;

typedef struct SensorReading {
    bool is_valid;
    uint8_t hour;
    int8_t temperature;
} SensorReading;

typedef struct ApplicationState {
    // Internal application state
    ApplicationMode mode;   // Current mode
    bool mode_changed;      // Lets us perform one-time setup for a given mode
    uint16_t mode_ticks;    // Timeout for the mode (returns to clock after timeout expires)
    uint8_t light_ticks;    // Timeout for the light
    bool led_on;            // Indicates that the LED is on
    uint8_t page;           // Tracks the current page in log, prefs or settings.
    bool is_fahrenheit;     // User preference, C or F

    // Data logging
    SensorReading logged_data[MAX_DATA_POINTS];

    // User preference
    bool show_humidity;     // Indicates that the LED is on

    // BME280 calibration values
    uint16_t dig_T1;
    int16_t dig_T2;
    int16_t dig_T3;
    uint8_t dig_H1;
    int16_t dig_H2;
    uint8_t dig_H3;
    int16_t dig_H4;
    int16_t dig_H5;
    int8_t dig_H6;
} ApplicationState;

float read_temperature(int32_t *p_t_fine);
float read_humidity(int32_t t_fine);

void log_data();

void do_clock_mode();

void do_temp_mode();
void temp_mode_handle_primary_button();

void do_log_mode();
void log_mode_handle_primary_button();

void do_prefs_mode();
void prefs_mode_handle_primary_button();
void prefs_mode_handle_secondary_button();

void do_set_time_mode();
void set_time_mode_handle_primary_button();
void set_time_mode_handle_secondary_button();

void cb_light_pressed();
void cb_mode_pressed();
void cb_alarm_pressed();
void cb_tick();