forked from SeeedJP/ReButtonApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoShutdown.cpp
More file actions
83 lines (71 loc) · 1.57 KB
/
Copy pathAutoShutdown.cpp
File metadata and controls
83 lines (71 loc) · 1.57 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
#include <Arduino.h>
#include "Config.h"
#include "AutoShutdown.h"
#include "Thread.h"
#include <ReButton.h>
static Thread AutoShutdownThread(osPriorityHigh);
static Mutex StartTimeMutex;
static volatile unsigned long StartTime;
static Mutex AutoShutdownTimeoutMutex;
static volatile int AutoShutdownTimeout;
static Mutex AutoShutdownBlocking;
void AutoShutdownUpdateStartTime()
{
StartTimeMutex.lock();
StartTime = millis();
StartTimeMutex.unlock();
}
static unsigned long AutoShutdownGetStartTime()
{
StartTimeMutex.lock();
int startTime = StartTime;
StartTimeMutex.unlock();
return startTime;
}
static int AutoShutdownGetTimeout()
{
AutoShutdownTimeoutMutex.lock();
int timeout = AutoShutdownTimeout;
AutoShutdownTimeoutMutex.unlock();
return timeout;
}
void AutoShutdownSetTimeout(int timeout)
{
AutoShutdownTimeoutMutex.lock();
AutoShutdownTimeout = timeout;
AutoShutdownTimeoutMutex.unlock();
}
static void AutoShutdownMain()
{
AutoShutdownUpdateStartTime();
for (;;)
{
while (millis() <= AutoShutdownGetStartTime() + AutoShutdownGetTimeout())
{
delay(100);
}
AutoShutdownBlocking.lock();
if (millis() > AutoShutdownGetStartTime() + AutoShutdownGetTimeout()) break;
AutoShutdownBlocking.unlock();
}
Serial.println("AUTO SHUTDOWN!");
delay(100);
for (;;)
{
ReButton::PowerSupplyEnable(false);
delay(1000);
}
}
void AutoShutdownBegin(int timeout)
{
AutoShutdownSetTimeout(timeout);
AutoShutdownThread.start(AutoShutdownMain);
}
void AutoShutdownSuspend()
{
AutoShutdownBlocking.lock();
}
void AutoShutdownResume()
{
AutoShutdownBlocking.unlock();
}