-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (70 loc) · 1.86 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (70 loc) · 1.86 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
#include "mbed.h"
#include <pb_encode.h>
#include <pb_decode.h>
#include "nanolog.pb.h"
//OS Objects
USBSerial usb(false);
EventFlags powerEvents;
EventFlags wakeupEvents;
InterruptIn usbPowerInt(SYS_WKUP1, PullDown);
LowPowerTimeout lpTimeout; //Internal counter
//Variables
volatile bool isUsbPowered = false;
volatile bool isBatPowered = true;
//Defines
#define ALARM_WAKEUP 0x00
#define USB_POWER 0x01
#define BAT_POWER 0x02
void power_change_irq()
{
if (usbPowerInt.read()) {
isUsbPowered = true;
powerEvents.set(USB_POWER);
} else {
IsUsbPowered = false;
powerEvents.clear(USB_POWER);
}
}
void lpTimeoutHendler(){
lpTimeout.attach(&lpTimeoutHendler, 1s);
wakeupEvents.set(ALARM_WAKEUP);
}
int main()
{
/* This is the buffer where we will store our message. */
uint8_t buffer[128];
size_t message_length;
bool status;
while(true){
//powerEvents.wait_all(POWER_USB, osWaitForever);
while(isUsbPowered){
lpTimeout.detach();
if(sleep_manager_can_deep_sleep()){
sleep_manager_lock_deep_sleep();//deny deep sleep
}
if(!usb.configured()){
usb.connect();
}
if(usb.configured()){
if(!usb.connected()){
usb.init();
}
}
if(usb.connected()){
if(usb.available()){
}
usb.printf("USB connection established\n");
}
else{
}
}
while(isBatPowered){//Battery powered
if(!sleep_manager_can_deep_sleep()){
sleep_manager_unlock_deep_sleep();//allow deep sleep
}
lpTimeout.attach(&lpTimeoutHendler, 1s);
wakeupEvents.wait_all(ALARM_WAKEUP);
}
}
return 0;
}