forked from muxedup/dive-computer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdive_controller.c
More file actions
108 lines (90 loc) · 3.02 KB
/
dive_controller.c
File metadata and controls
108 lines (90 loc) · 3.02 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdint.h>
#include <assert.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Mailbox.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/drivers/ADC.h>
#include "main.h"
#include "bsp.h"
#include "Board.h"
#include "dive_controller.h"
#include "display_task.h"
#include "scuba.h"
#include "alarm.h"
#include "alarmtask.h"
extern Event_Handle g_event_handle;
extern Event_Handle g_alarm_event_handle;
extern Mailbox_Handle g_display_mbox;
extern Mailbox_Handle g_adc_mbox;
void dive_controller_task(uint32_t arg0, uint32_t arg1) {
int32_t depth_mm = 0;
int32_t dive_rate_mm = 0; // meters per minute
int32_t gas_rate_cl = 0;
uint32_t oxygen_cl = START_O2_TANK_CL;
uint32_t oxygen_to_surf_cl = 0;
int32_t dive_time_elapsed_ms = 0;
uint16_t adc_value = 475;
int32_t dive_time_remaining_s = 0;
uint32_t eventReg = 0;
uint8_t alarm_status, prev_alarm_status;
disp_msg_t update_msg;
unsigned char unitsState = 0;
//char dispString[20];
for(;;) {
eventReg = Event_pend(g_event_handle, 0x0000,0x000F, BIOS_WAIT_FOREVER);
// Handle new event
switch(eventReg) {
case 0x0001: // Timer elapsed update dive parameters
depth_mm += depth_change_in_mm(dive_rate_mm);
if (depth_mm > 0) {
dive_time_elapsed_ms = 0;
}
if(dive_time_elapsed_ms > 0) {
oxygen_cl -= gas_rate_in_cl(depth_mm);
oxygen_to_surf_cl = gas_to_surface_in_cl(depth_mm);
dive_time_elapsed_ms += TIMER_TICK_INTERVAL;
}
break;
case 0x0002: // button 1 press
unitsState = (unitsState > 0 ? 0 : 1);
break;
case 0x0004: // button 2 press
if (depth_mm == 0) {
oxygen_cl += INC_O2_TANK_CL;
}
case 0x0008: // adc event
Mailbox_pend(g_adc_mbox, &adc_value, 10);
dive_rate_mm = adc_to_rate(adc_value);
break;
default:
break;
}
// set alerts
if (oxygen_to_surf_cl > oxygen_cl) {
// High alert
alarm_status = ALARM_H;
} else if (dive_rate_mm > 15) {
// Medium alert
alarm_status = ALARM_M;
} else if (depth_mm < MAX_DEPTH_MM) {
// low alert
alarm_status = ALARM_L;
} else {
alarm_status = ALARM_0;
}
// Post event update if necessary
if (alarm_status != prev_alarm_status) {
Event_post(g_alarm_event_handle, alarm_status);
alarm_status = prev_alarm_status;
}
// update display
update_msg.unit_is_meters = unitsState;
update_msg.depth_mm = depth_mm;
update_msg.dive_rate_mm = dive_rate_mm;
update_msg.dive_time_elapsed_ms = dive_time_elapsed_ms;
update_msg.oxygen_cl = oxygen_cl;
update_msg.alarm_status = alarm_status;
Mailbox_post(g_display_mbox, &update_msg, 0);
}
}