-
Notifications
You must be signed in to change notification settings - Fork 14
/
SIM808.Power.cpp
84 lines (62 loc) · 1.93 KB
/
SIM808.Power.cpp
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 "SIM808.h"
TOKEN_TEXT(CBC, "+CBC");
TOKEN_TEXT(CFUN, "+CFUN");
bool SIM808::powered()
{
if(_statusPin == SIM808_UNAVAILABLE_PIN) {
sendAT();
return waitResponse(SIMCOMAT_DEFAULT_TIMEOUT) != -1;
}
return digitalRead(_statusPin) == HIGH;
}
bool SIM808::powerOnOff(bool power)
{
if (_pwrKeyPin == SIM808_UNAVAILABLE_PIN) return false;
bool currentlyPowered = powered();
if (currentlyPowered == power) return false;
SIM808_PRINT_P("powerOnOff: %t", power);
digitalWrite(_pwrKeyPin, LOW);
delay(2000);
digitalWrite(_pwrKeyPin, HIGH);
uint16_t timeout = 2000;
do {
delay(150);
timeout -= 150;
currentlyPowered = powered();
} while(currentlyPowered != power && timeout > 0);
return currentlyPowered == power;
}
SIM808ChargingStatus SIM808::getChargingState()
{
uint8_t state;
uint8_t level;
uint16_t voltage;
sendAT(TO_F(TOKEN_CBC));
if (waitResponse(TO_F(TOKEN_CBC)) == 0 &&
parseReply(',', (uint8_t)SIM808BatteryChargeField::Bcs, &state) &&
parseReply(',', (uint8_t)SIM808BatteryChargeField::Bcl, &level) &&
parseReply(',', (uint16_t)SIM808BatteryChargeField::Voltage, &voltage) &&
waitResponse() == 0)
return { (SIM808ChargingState)state, level, voltage };
return { SIM808ChargingState::Error, 0, 0 };
}
SIM808PhoneFunctionality SIM808::getPhoneFunctionality()
{
uint8_t state;
sendAT(TO_F(TOKEN_CFUN), TO_F(TOKEN_READ));
if (waitResponse(10000L, TO_F(TOKEN_CFUN)) == 0 &&
parseReply(',', 0, &state) &&
waitResponse() == 0)
return (SIM808PhoneFunctionality)state;
return SIM808PhoneFunctionality::Fail;
}
bool SIM808::setPhoneFunctionality(SIM808PhoneFunctionality fun)
{
sendAT(TO_F(TOKEN_CFUN), TO_F(TOKEN_WRITE), (uint8_t)fun);
return waitResponse(10000L) == 0;
}
bool SIM808::setSlowClock(SIM808SlowClock mode)
{
sendAT(S_F("+CSCLK"), TO_F(TOKEN_WRITE), (uint8_t)mode);
return waitResponse() == 0;
}