-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceHandler.cpp
More file actions
176 lines (155 loc) · 4.35 KB
/
Copy pathVoiceHandler.cpp
File metadata and controls
176 lines (155 loc) · 4.35 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "VoiceHandler.h"
extern volatile uint8_t delayEspired;
VoiceHandler::VoiceHandler(Modem *m) {
modem = m;
}
void VoiceHandler::setSensorsData(uint8_t cool, int themp, uint8_t motions) {
coolThemperature = cool;
themperature = (themp > 0) ? themp : 1;
motionCounter = motions;
}
/*
call check on button pressed,
if yes - fill phone with caller number
and execute funk for play sound
else - make answer as themperature and motion
*/
uint8_t VoiceHandler::handleIncoming(char *phone) {
#if DEBUG
Serial.println(F("Handle voice call"));
#endif
phone[0] = '\0';
uint8_t ret;
if (digitalRead(BUTTON_PIN) == HIGH)ret = makeAnswer(true);
else {
fillPhoneFromCall(phone);
makeAnswer(false); // head func is assign admin phone
ret = ASSIGN_ADMIN;
}
modem->dropGSM();
return ret;
}
/*
if success parse phone number return 'SUCCESS'
othervice return 'WRONG_DATA'
*/
uint8_t VoiceHandler::fillPhoneFromCall(char *str) {
setEspiredTime(DELAY_FOR_OK);
while (modem->available() < 45 && delayEspired != 0)delay(2);
if (modem->available() < 40)return WRONG_DATA;
while (modem->read() != '"')delay(1);
uint8_t i = 0;
while (modem->available() > 0 && (str[i] = modem->read()) != '"' && i < 12)i++;
#if DEBUG
Serial.print(F(" incoming = "));
Serial.write(str, 12);
Serial.println();
#endif
if (i < 12) {
str[0] = '\0';
return WRONG_DATA;
}
str[12] = '\0';
return SUCCESS;
}
/*
make voice answer from module "amr" files
*/
uint8_t VoiceHandler::makeAnswer(bool isSayThemperature) {
modem->sendCommand(F("ATA"));
uint8_t retVal ;
modem->sendCommand(F("AT+SIMTONE=1,1070,200,200,2400"));
delay(2500);
if (!isSayThemperature) {
retVal = playFile(GOOD_SOUND);
modem->sendCommand(F("ATH \r"));
return retVal;
}
retVal = sayThemperature();
if (motionCounter > 0) {
if ( (retVal = playFile(MOTION_SOUND)) == SUCCESS) {
const char fName[] = { motionCounter + 48, '.', 'a', 'm', 'r', '\0'};
retVal = playFile(fName);
}
}
if (digitalRead(VOLTAGE_PIN) == LOW) {
retVal = playFile(NO_VOLTAGE);
// for (uint8_t i = 0; i < 3; i++)playFile((retVal == SUCCESS) ? CAT_SOUND : BAD_SOUND);
}
modem->sendCommand(F("ATH \r"));
return retVal;
}
/*
uint8_t VoiceHandler::sayThemperature() {
modem->dropGSM();
if (playFile(VAWE_SOUND) != SUCCESS)return NO_PLAY_SOUND;
if (playFile(FIRING_SOUND) != SUCCESS)return NO_PLAY_SOUND;
String str;
if (themperature < 21) {
str = themperature + ".amr";
if (playFile(str.c_str()) != SUCCESS)return NO_PLAY_SOUND;
} else {
uint8_t tens = 2;
while ((themperature - tens * 10) > 9)tens++;
str = String(tens) + "0.amr";
if (playFile(str.c_str()) != SUCCESS)return NO_PLAY_SOUND;
uint8_t ones = themperature - tens * 10;
if (ones != 0) {
str = String(ones) + ".amr";
if ( playFile(str.c_str()) != SUCCESS)return NO_PLAY_SOUND;
}
}
if (themperature < MIN_FIRING_THEMPERATURE)return playFile(BAD_SOUND);
return playFile(GOOD_SOUND);
}
*/
uint8_t VoiceHandler::sayThemperature() {
modem->dropGSM();
// playFile(VAWE_SOUND);
playFile(FIRING_SOUND);
String str;
if(themperature < 1) themperature = 0;
Serial.println(" t = " + String(themperature));
if (themperature < 21) {
str = String(themperature) + ".amr";
if (playFile(str.c_str()) != SUCCESS)return NO_PLAY_SOUND;
} else {
uint8_t tens = 2;
while ((themperature - tens * 10) > 9)tens++;
str = String(tens) + "0.amr";
playFile(str.c_str());
uint8_t ones = themperature - tens * 10;
if (ones != 0) {
str = String(ones) + ".amr";
playFile(str.c_str());
}
}
delayForPlay();
if (themperature < coolThemperature)return playFile(BAD_SOUND);
return playFile(GOOD_SOUND);
}
/*
play for caller sound from fileName
return NO_PLAY_SOUND in any error
*/
uint8_t VoiceHandler::playFile(const char *fileName) {
//#if DEBUD
Serial.print(" Try play file : ");
Serial.println(fileName);
//#endif
delay(10);
while (modem->available() > 0) {
modem->read();
delay(1);
}
modem->print("AT+CPAMR=\"" + String(fileName) + "\",0 \r");
if (!modem->checkOnOK(DELAY_FOR_OK)) {
return NO_PLAY_SOUND;
}
delayForPlay();
return SUCCESS;
}
void VoiceHandler::delayForPlay() {
setEspiredTime(DELAY_FOR_SOUND);
while ( modem->available() < 2 && delayEspired > 0){};
}