-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsleep.c
80 lines (66 loc) · 1.79 KB
/
sleep.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
#include "ch.h"
#include "hal.h"
#include "sleep.h"
#include "padc.h"
#include "tracking.h"
#include "debug.h"
#include "padc.h"
/**
* Sleeping method. Returns true if sleeping condition are given.
*/
bool p_sleep(const sleep_config_t *config)
{
switch(config->type)
{
case SLEEP_WHEN_VBAT_BELOW_THRES:
return getBatteryVoltageMV() < config->vbat_thres;
case SLEEP_WHEN_VSOL_BELOW_THRES:
return getSolarVoltageMV() < config->vsol_thres;
case SLEEP_WHEN_VBAT_ABOVE_THRES:
return getBatteryVoltageMV() > config->vbat_thres;
case SLEEP_WHEN_VSOL_ABOVE_THRES:
return getSolarVoltageMV() > config->vsol_thres;
case SLEEP_WHEN_DISCHARGING:
case SLEEP_WHEN_CHARGING:
case SLEEP_WHEN_INSIDE_ITU1:
case SLEEP_WHEN_INSIDE_ITU2:
case SLEEP_WHEN_INSIDE_ITU3:
case SLEEP_WHEN_SAT_NOT_VIS:
TRACE_WARN("Sleeping method not implemented");
return false;
case SLEEP_DISABLED:
return false;
}
return false;
}
systime_t waitForTrigger(systime_t prev, trigger_config_t *config)
{
switch(config->type)
{
case TRIG_EVENT: // Wait for new tracking point
switch(config->event)
{
case EVENT_NEW_POINT:
waitForNewTrackPoint();
return chVTGetSystemTimeX();
case NO_EVENT: // No event defined
while(1); // Assert
}
case TRIG_TIMEOUT: // Wait for specified timeout
return chThdSleepUntilWindowed(prev, prev + S2ST(config->timeout));
case TRIG_CONTINOUSLY: // Immediate trigger
return chVTGetSystemTimeX();
case TRIG_ONCE: // No trigger defined
while(1); // Assert
}
return chVTGetSystemTimeX();
}
void trigger_new_tracking_point(void)
{
uint32_t oldID = getLastTrackPoint()->id;
trackPoint_t *newtp;
do { // Wait for new serial ID to be deployed
chThdSleepMilliseconds(100);
newtp = getLastTrackPoint();
} while(newtp->id == oldID);
}