Skip to content

Commit fd3fcd9

Browse files
committed
Add LowPowerButton Example
1 parent 139e56b commit fd3fcd9

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Show usage of POWER_ON pin (J1-2).
3+
*
4+
* Board goes to low power mode as soon as started.
5+
* Pressing a momentary button connected to POWER_ON pin
6+
* will wake up the board for 5 seconds.
7+
*
8+
* Requirments:
9+
* - Connect a momentary button between POWER_ON (J1-2) and GND (J1-9)
10+
* - Connect the PC/Mac to USB and open the Arduino Serial Monitor
11+
* or any serial communication application.
12+
*
13+
*/
14+
15+
#include <Arduino_EdgeControl.h>
16+
#include <USB/PluggableUSBDevice.h>
17+
18+
constexpr unsigned long printInterval { 250 };
19+
unsigned long printNow { 0 };
20+
constexpr unsigned long wakeUpInterval { printInterval * 20 };
21+
unsigned long sleepNow { 0 };
22+
volatile bool sleeping { true };
23+
volatile bool poweredOn { false };
24+
25+
void wakeUp()
26+
{
27+
sleeping = false;
28+
poweredOn = true;
29+
}
30+
31+
void setup()
32+
{
33+
EdgeControl.begin();
34+
Power.on(PWR_3V3);
35+
36+
Serial.begin(9600);
37+
38+
auto startNow = millis() + 2500;
39+
while (!Serial && millis() < startNow)
40+
;
41+
42+
delay(1000);
43+
Serial.println("Testing Low Power");
44+
Serial.println("and Power On Button.");
45+
46+
pinMode(POWER_ON, INPUT);
47+
attachInterrupt(digitalPinToInterrupt(POWER_ON), wakeUp, FALLING);
48+
49+
sleepNow = millis();
50+
printNow = millis() + printInterval;
51+
52+
powerDown();
53+
54+
}
55+
56+
void loop()
57+
{
58+
if (poweredOn) {
59+
poweredOn = false;
60+
61+
powerOn();
62+
63+
delay(1000); // Wait for Serial Monitor/Serial Communication Application
64+
Serial.println("");
65+
Serial.println("Woke Up!");
66+
sleepNow = millis() + wakeUpInterval;
67+
}
68+
69+
if (millis() > sleepNow && !sleeping) {
70+
// Going to sleep
71+
powerDown();
72+
sleeping = true;
73+
}
74+
75+
if (sleeping)
76+
delay(10000); // delay() puts the board in low power mode.
77+
else
78+
if (millis() > printNow) {
79+
Serial.print('.');
80+
printNow = millis() + printInterval;
81+
}
82+
}
83+
84+
void powerOn()
85+
{
86+
// Enable Gated 3V3 to devices and peripherals
87+
Power.on(PWR_3V3);
88+
89+
// Enable Gated 12V and 5V to output pins, devices and peripherals
90+
Power.on(PWR_VBAT);
91+
92+
// Enable Gated 12V and 5V to output pins, devices and peripherals
93+
Power.on(PWR_19V);
94+
95+
// Attach USB
96+
PluggableUSBD().begin();
97+
98+
// Enable stdin
99+
mbed::mbed_file_handle(STDIN_FILENO)->enable_input(true);
100+
101+
// Open Serial and wait for connection
102+
Serial.begin(9600);
103+
while(!Serial);
104+
}
105+
106+
void powerDown()
107+
{
108+
Serial.println();
109+
Serial.println("Powering down");
110+
111+
// Disable Gated 3V3
112+
Power.off(PWR_3V3);
113+
114+
// Disable Gated 12V and 5V
115+
Power.off(PWR_VBAT);
116+
117+
// Disable Gated 19V
118+
Power.off(PWR_19V);
119+
120+
// Close Serial
121+
Serial.end();
122+
Serial1.end();
123+
// Disable stdin
124+
mbed::mbed_file_handle(STDIN_FILENO)->enable_input(false);
125+
// Detach USB
126+
PluggableUSBD().deinit();
127+
// Put pins in threestate
128+
powerDownPins();
129+
}
130+
131+
void powerDownPins()
132+
{
133+
pinMode(I2C_SDA, INPUT_PULLDOWN);
134+
pinMode(I2C_SCL, INPUT_PULLDOWN);
135+
pinMode(I2C_SDA1, INPUT_PULLDOWN);
136+
pinMode(I2C_SCL1, INPUT_PULLDOWN);
137+
138+
pinMode(VBAT_PROBE, INPUT_PULLDOWN);
139+
140+
pinMode(CMD_TRIAC_1, INPUT_PULLDOWN);
141+
pinMode(CMD_TRIAC_2, INPUT_PULLDOWN);
142+
pinMode(CMD_TRIAC_3, INPUT_PULLDOWN);
143+
pinMode(CMD_TRIAC_4, INPUT_PULLDOWN);
144+
145+
pinMode(SENSOR_COMMON, INPUT_PULLDOWN);
146+
pinMode(SENSOR_CALIB, INPUT_PULLDOWN);
147+
pinMode(SENSOR_INPUT_ADC, INPUT_PULLDOWN);
148+
pinMode(SENSOR_CAPTURE_A, INPUT_PULLDOWN);
149+
pinMode(SENSOR_CAPTURE, INPUT_PULLDOWN);
150+
151+
pinMode(PULSE_DIRECTION, INPUT_PULLDOWN);
152+
pinMode(PULSE_STROBE, INPUT_PULLDOWN);
153+
154+
pinMode(SD_CS, INPUT_PULLDOWN);
155+
156+
pinMode(QSPIDCS, INPUT_PULLDOWN);
157+
pinMode(GPIOCLK, INPUT_PULLDOWN);
158+
pinMode(QSPID0, INPUT_PULLDOWN);
159+
pinMode(QSPID1, INPUT_PULLDOWN);
160+
pinMode(QSPID2, INPUT_PULLDOWN);
161+
pinMode(QSPID3, INPUT_PULLDOWN);
162+
163+
pinMode(IRQ_CH1, INPUT);
164+
pinMode(IRQ_CH2, INPUT);
165+
pinMode(IRQ_CH3, INPUT);
166+
pinMode(IRQ_CH4, INPUT);
167+
pinMode(IRQ_CH5, INPUT);
168+
pinMode(IRQ_CH6, INPUT);
169+
}

0 commit comments

Comments
 (0)