· 5 years ago · Nov 04, 2020, 08:34 PM
1#include<Keypad.h> // Aurduino Library for keypad
2#include <SDHCI.h> // Sony Spresense SD Card Library
3#include <Audio.h> // Sony Spresense Audio Library
4
5SDClass theSD; // Instance of the class SDClass
6AudioClass *theAudio; // Instance of the class AudioClass
7File myFile; // Instance of the class File
8
9#define ldr1 A0 // ldr on the inner left side of the headphone
10#define Password_Length 5 // Define the password length + the null character
11
12bool ErrEnd = false;
13char Data[Password_Length]; // Declare the array to store the pin
14char Master[Password_Length] = "1234"; // Declare the password *Here it is declared for prototype purposes(Can use API)
15byte data_count = 0, master_count = 0;
16char customKey; // To store the typed character on the keypad
17const byte ROWS = 4; // Declare the number of rows for the keypad
18const byte COLS = 4; // Declare the number of columns for the keypad
19byte rowPins[ROWS] = {8, 7, 6,5}; // Delcare the pins to which the corresponding row is attached on the Spresense board
20byte colPins[COLS] = { 4, 3, 2}; // Declare the pins to which the corresponding column is attached on the Spresense board
21// Here I have used a 4 * 3 keypad hence there are 7 keys
22char hexaKeys[ROWS][COLS] = {
23 {'1', '2', '3'},
24 {'4', '5', '6'},
25 {'7', '8', '9'},
26 {'*', '0', '#'}
27};
28/* Delaring the amount to be paid by the user */
29/* For prototype purpose I am defining an amount */
30/* In the real world it can be fetched from API or software the seller is using*/
31int pylen =4; // The length of the array in which the amount for payemnt is stored.
32int payment_amt = 100; // Amount to be paid by the user to complete his payment.
33int amt_balance = 1000; // Amount remaining in the account of user
34int val = 0; // This is used in the program to store the value of the numpad function
35bool verified = false; // Store the value here if the pin is verified
36
37/*Delcaration of audio_attention_cb funtion */
38static void audio_attention_cb(const ErrorAttentionParam *atprm){
39 puts("Attention!");
40 if (atprm->error_code >= AS_ATTENTION_CODE_WARNING){
41 ErrEnd = true;
42 }
43}
44
45Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // Create instance of the class keypad from the Keypad.h library
46
47void setup(){
48 Serial.begin(115200); // Here the baud rate is set to 115200. It can be varied.
49 pinMode(ldr1,INPUT); // Declares ldr pins at input
50 pinMode(LED0,OUTPUT); // LED0 represents the led on the Spresense board
51 pinMode(13,OUTPUT); // Gives the signal to arduino uno to check for rfid card
52 pinMode(12,INPUT); // Receives the signal that the card is verified
53}
54
55void loop()
56{
57 /* Setup the audio player */
58 theAudio = AudioClass::getInstance();
59 theAudio->begin(audio_attention_cb);
60 puts("initialization Audio Library");
61 theAudio->setRenderingClockMode(AS_CLKMODE_NORMAL);
62 theAudio->setPlayerMode(AS_SETPLAYER_OUTPUTDEVICE_SPHP, AS_SP_DRV_MODE_LINEOUT);
63 err_t err = theAudio->initPlayer(AudioClass::Player0, AS_CODECTYPE_MP3, "/mnt/spif/BIN", AS_SAMPLINGRATE_AUTO, AS_CHANNEL_STEREO);
64 bool check_headphone = ldr_detect(800); // Check headphone stores the value whether headphones are put on or not
65 while(check_headphone){
66 /* The entire process runs under this while loop*/
67 /***********CHECKS WHETHER VALID CARD IS DETECTED WITH ARDUINO*********************/
68 /*******The below code part can be replaced with actual code of rfid after sepresense supports c++11**/
69 while(1){
70 //Serial.println("Check whether card is valid"); // Uncomment this line for testing
71 //Serial.println(digitalRead(12)); // Uncomment this line for testing
72 if(digitalRead(12)){
73 break;
74 }
75 }
76 //Serial.println("Check card program ended"); // Uncomment this line for testing
77 /***************************************************************************/
78
79 /*Add "Headphones are put on properly. The square shaped numpad is placed on the center of the instrument. It has braille stickers on it."*/
80 //Serial.println("Headphones are put on hence the function is working"); // Uncomment this line for testing
81 if (err != AUDIOLIB_ECODE_OK){
82 printf("Player0 initialize error\n");
83 }
84 myFile = theSD.open("headph1.mp3"); // Takes the file name as the function parameter
85 if (!myFile){
86 printf("File open error\n");
87 }
88 printf("Open! %d\n",myFile);
89 err = theAudio->writeFrames(AudioClass::Player0, myFile);
90 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
91 printf("File Read Error! =%d\n",err);
92 myFile.close();
93 }
94 puts("Play!");
95 theAudio->setVolume(-160);
96 theAudio->startPlayer(AudioClass::Player0);
97 while(1){
98 puts("loop!!");
99 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
100 if (err == AUDIOLIB_ECODE_FILEEND){
101 printf("Main player File End!\n");
102 break;
103 }
104 if (err){
105 printf("Main player error code: %d\n", err);
106 sleep(1);
107 theAudio->stopPlayer(AudioClass::Player0);
108 myFile.close();
109 break; // Break the while loop
110 }
111 if (ErrEnd){
112 printf("Error End\n");
113 sleep(1);
114 theAudio->stopPlayer(AudioClass::Player0);
115 myFile.close();
116 break; // Break the while loop
117 }
118 usleep(40000);
119 }
120
121 while(val!=1){
122 delay(3000); // This wait is added so that there is a pause before playing the mp3 again
123
124 /*** Add "press.mp3" or "Press hash '#' to continue or press asterisk to go back"*/
125 if (err != AUDIOLIB_ECODE_OK){
126 printf("Player0 initialize error\n");
127 }
128 myFile = theSD.open("press1.mp3"); // Takes the file name as the function parameter
129 if (!myFile){
130 printf("File open error\n");
131 }
132 printf("Open! %d\n",myFile);
133 err = theAudio->writeFrames(AudioClass::Player0, myFile);
134 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
135 printf("File Read Error! =%d\n",err);
136 myFile.close();
137 }
138 puts("Play!");
139 theAudio->setVolume(-160);
140 theAudio->startPlayer(AudioClass::Player0);
141 while(1){
142 puts("loop!!");
143 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
144 if (err == AUDIOLIB_ECODE_FILEEND){
145 printf("Main player File End!\n");
146 break;
147 }
148 if (err){
149 printf("Main player error code: %d\n", err);
150 sleep(1);
151 theAudio->stopPlayer(AudioClass::Player0);
152 myFile.close();
153 break; // Break the while loop
154 }
155 if (ErrEnd){
156 printf("Error End\n");
157 sleep(1);
158 theAudio->stopPlayer(AudioClass::Player0);
159 myFile.close();
160 break; // Break the while loop
161 }
162 usleep(40000);
163 }
164
165 //Serial.println("Press # to continue or press * to go back"); // Uncomment this line for testing
166 val = numpad();
167 if(val==1){
168 val = 0;
169 /*** Continue with the next process */
170
171 /*** Add "do you want to make a payemnt of xxxx dollars" */
172 if (err != AUDIOLIB_ECODE_OK){
173 printf("Player0 initialize error\n");
174 }
175 myFile = theSD.open("proceed1.mp3"); // Takes the file name as the function parameter
176 if (!myFile){
177 printf("File open error\n");
178 }
179 printf("Open! %d\n",myFile);
180 err = theAudio->writeFrames(AudioClass::Player0, myFile);
181 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
182 printf("File Read Error! =%d\n",err);
183 myFile.close();
184 }
185 puts("Play!");
186 theAudio->setVolume(-160);
187 theAudio->startPlayer(AudioClass::Player0);
188 while(1){
189 puts("loop!!");
190 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
191 if (err == AUDIOLIB_ECODE_FILEEND){
192 printf("Main player File End!\n");
193 break;
194 }
195 if (err){
196 printf("Main player error code: %d\n", err);
197 sleep(1);
198 theAudio->stopPlayer(AudioClass::Player0);
199 myFile.close();
200 break; // Break the while loop
201 }
202 if (ErrEnd){
203 printf("Error End\n");
204 sleep(1);
205 theAudio->stopPlayer(AudioClass::Player0);
206 myFile.close();
207 break; // Break the while loop
208 }
209 usleep(40000);
210 }
211
212 while(val!=1){
213 delay(3000); // This wait is added so that there is a pause before playing the mp3 again
214
215 /*** Add "press.mp3" or "Press hash '#' to continue or press asterisk to go back"*/
216 //Serial.println("Press # to continue or press * to go back"); // Uncomment this line for testing
217 if (err != AUDIOLIB_ECODE_OK){
218 printf("Player0 initialize error\n");
219 }
220 myFile = theSD.open("press1.mp3"); // Takes the file name as the function parameter
221 if (!myFile){
222 printf("File open error\n");
223 }
224 printf("Open! %d\n",myFile);
225 err = theAudio->writeFrames(AudioClass::Player0, myFile);
226 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
227 printf("File Read Error! =%d\n",err);
228 myFile.close();
229 }
230 puts("Play!");
231 theAudio->setVolume(-160);
232 theAudio->startPlayer(AudioClass::Player0);
233 while(1){
234 puts("loop!!");
235 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
236 if (err == AUDIOLIB_ECODE_FILEEND){
237 printf("Main player File End!\n");
238 break;
239 }
240 if (err){
241 printf("Main player error code: %d\n", err);
242 sleep(1);
243 theAudio->stopPlayer(AudioClass::Player0);
244 myFile.close();
245 break; // Break the while loop
246 }
247 if (ErrEnd){
248 printf("Error End\n");
249 sleep(1);
250 theAudio->stopPlayer(AudioClass::Player0);
251 myFile.close();
252 break; // Break the while loop
253 }
254 usleep(40000);
255 }
256 val = numpad();
257 if(val==1){
258 val = 0;
259 /*** Continue the next process */
260
261 /*** Add "Enter your pin "*/
262 //Serial.println("Enter your pin"); // Uncomment this line for testing
263 if (err != AUDIOLIB_ECODE_OK){
264 printf("Player0 initialize error\n");
265 }
266 myFile = theSD.open("enter1.mp3"); // Takes the file name as the function parameter
267 if (!myFile){
268 printf("File open error\n");
269 }
270 printf("Open! %d\n",myFile);
271 err = theAudio->writeFrames(AudioClass::Player0, myFile);
272 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
273 printf("File Read Error! =%d\n",err);
274 myFile.close();
275 }
276 puts("Play!");
277 theAudio->setVolume(-160);
278 theAudio->startPlayer(AudioClass::Player0);
279 while(1){
280 puts("loop!!");
281 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
282 if (err == AUDIOLIB_ECODE_FILEEND){
283 printf("Main player File End!\n");
284 break;
285 }
286 if (err){
287 printf("Main player error code: %d\n", err);
288 sleep(1);
289 theAudio->stopPlayer(AudioClass::Player0);
290 myFile.close();
291 break; // Break the while loop
292 }
293 if (ErrEnd){
294 printf("Error End\n");
295 sleep(1);
296 theAudio->stopPlayer(AudioClass::Player0);
297 myFile.close();
298 break; // Break the while loop
299 }
300 usleep(40000);
301 }
302 check_headphone = ldr_detect(800); // Take a precautionary check whther headphones are put on before entering the pin
303 if(!check_headphone){
304 break; // Break the whole transaction process if headphones are not on
305 }
306 verified = check_pin();
307 if(verified){
308
309 /*** Add "your transaction is being processed" ****/
310 //Serial.println("Your transaction is being processed"); // Uncomment this line for testing
311 if (err != AUDIOLIB_ECODE_OK){
312 printf("Player0 initialize error\n");
313 }
314 myFile = theSD.open("transp1.mp3"); // Takes the file name as the function parameter
315 if (!myFile){
316 printf("File open error\n");
317 }
318 printf("Open! %d\n",myFile);
319 err = theAudio->writeFrames(AudioClass::Player0, myFile);
320 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
321 printf("File Read Error! =%d\n",err);
322 myFile.close();
323 }
324 puts("Play!");
325 theAudio->setVolume(-160);
326 theAudio->startPlayer(AudioClass::Player0);
327 while(1){
328 puts("loop!!");
329 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
330 if (err == AUDIOLIB_ECODE_FILEEND){
331 printf("Main player File End!\n");
332 break;
333 }
334 if (err){
335 printf("Main player error code: %d\n", err);
336 sleep(1);
337 theAudio->stopPlayer(AudioClass::Player0);
338 myFile.close();
339 break; // Break the while loop
340 }
341 if (ErrEnd){
342 printf("Error End\n");
343 sleep(1);
344 theAudio->stopPlayer(AudioClass::Player0);
345 myFile.close();
346 break; // Break the while loop
347 }
348 usleep(40000);
349 }
350 amt_balance = amt_balance - payment_amt;
351 Serial.println("The remaining balance amount is: ");
352 Serial.println(amt_balance);
353 /*** Add "Add your transaction is completed Thank you" ***/
354 if (err != AUDIOLIB_ECODE_OK){
355 printf("Player0 initialize error\n");
356 }
357 myFile = theSD.open("transcc1.mp3"); // Takes the file name as the function parameter
358 if (!myFile){
359 printf("File open error\n");
360 }
361 printf("Open! %d\n",myFile);
362 err = theAudio->writeFrames(AudioClass::Player0, myFile);
363 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
364 printf("File Read Error! =%d\n",err);
365 myFile.close();
366 }
367 puts("Play!");
368 theAudio->setVolume(-160);
369 theAudio->startPlayer(AudioClass::Player0);
370 while(1){
371 puts("loop!!");
372 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
373 if (err == AUDIOLIB_ECODE_FILEEND){
374 printf("Main player File End!\n");
375 break;
376 }
377 if (err){
378 printf("Main player error code: %d\n", err);
379 sleep(1);
380 theAudio->stopPlayer(AudioClass::Player0);
381 myFile.close();
382 break; // Break the while loop
383 }
384 if (ErrEnd){
385 printf("Error End\n");
386 sleep(1);
387 theAudio->stopPlayer(AudioClass::Player0);
388 myFile.close();
389 break; // Break the while loop
390 }
391 usleep(40000);
392 }
393
394 //Serial.println("Your transaction is completed"); // Uncomment this line for testing
395 while(1){ // Wait untill the user removes the headphones
396 check_headphone = ldr_detect(800); // Take a precautionary check whther headphones are put on before entering the pin
397 if(!check_headphone){
398 break; // Break the whole transaction process if headphones are not on
399 }
400 }
401 break;
402 }
403 else{
404 /*** Add "The pin you entered is incorrect" ***/
405 if (err != AUDIOLIB_ECODE_OK){
406 printf("Player0 initialize error\n");
407 }
408 myFile = theSD.open("entern1.mp3"); // Takes the file name as the function parameter
409 if (!myFile){
410 printf("File open error\n");
411 }
412 printf("Open! %d\n",myFile);
413 err = theAudio->writeFrames(AudioClass::Player0, myFile);
414 if ((err != AUDIOLIB_ECODE_OK) && (err != AUDIOLIB_ECODE_FILEEND)){
415 printf("File Read Error! =%d\n",err);
416 myFile.close();
417 }
418 puts("Play!");
419 theAudio->setVolume(-160);
420 theAudio->startPlayer(AudioClass::Player0);
421 while(1){
422 puts("loop!!");
423 int err = theAudio->writeFrames(AudioClass::Player0, myFile);
424 if (err == AUDIOLIB_ECODE_FILEEND){
425 printf("Main player File End!\n");
426 break;
427 }
428 if (err){
429 printf("Main player error code: %d\n", err);
430 sleep(1);
431 theAudio->stopPlayer(AudioClass::Player0);
432 myFile.close();
433 break; // Break the while loop
434 }
435 if (ErrEnd){
436 printf("Error End\n");
437 sleep(1);
438 theAudio->stopPlayer(AudioClass::Player0);
439 myFile.close();
440 break; // Break the while loop
441 }
442 usleep(40000);
443 }
444 break; // Break the whole transaction process if the pin is not verified
445 }
446
447 }
448 }
449 }
450 }
451
452
453 }
454 // ***Do not add any time consuming program here: It can cause delay in detection of headphones
455 //Serial.println("Headphones are not put on but the function is working"); // Uncomment this line for testing
456}
457
458bool ldr_detect(int light_threshold){
459 //Pass the value of light_threshold which it should detect that the headphones are put on (0-1023).
460 //Generally the value is 300
461 /* This function is used to detect whether the headphone is put on or not */
462 int ldr1_val = analogRead(ldr1);
463 //Serial.println("ldr1_val: "+ldr1_val); // Uncomment this line for testing
464 if(ldr1_val<=light_threshold){
465 //Serial.println("The headphones are put on by the user"); // Uncomment this line for testing
466 return true;
467 }
468 else{
469 //Serial.println("The headphones are not put on by the user"); // Uncomment this line for testing
470 return false;
471 }
472}
473
474/* This function blinks the LED0 on the spresense board and is used to indicate the excecution of certain programs*/
475void blink_led0(int choice,int time_delay){
476 while(1)
477 {
478 digitalWrite(LED0,HIGH);
479 delay(time_delay);
480 digitalWrite(LED0,LOW);
481 delay(time_delay);
482 if(choice=0)
483 {
484 break;
485 }
486 }
487 return;
488}
489
490/* The check_pin function returns a boolean value and does not take any parameters*/
491bool check_pin(){
492 bool pass_check =false; // False if the password is incorrect
493 while(1){
494 //Serial.print("Enter Password:"); //Uncomment the line for testing
495 customKey = customKeypad.getKey(); // the getKey() method returns the character pressed on the keypad
496 if (customKey){
497 Data[data_count] = customKey;
498 data_count++;
499 }
500 if(data_count == Password_Length-1){
501 /* If the number of digits entered matches the number of digits in the set password */
502 if(!strcmp(Data, Master)){
503 /* If the entered password matches the set password */
504 //Serial.print("Correct"); // Uncomment the line for testing
505 clearData(); // Clears the data in the array that holds the user typed pin
506 /* If the password matches then enter this loop */
507 //Serial.println("Password Matching"); // Uncomment the line for testing
508 pass_check = true;
509 break;
510 }
511 else{
512 //Serial.print("Incorrect"); // Uncomment the line for testing
513 delay(1000); // This delay is used to ensure proper detection of the keys pressed
514 }
515 }
516 }
517 if(pass_check){
518 return true;
519 }
520 else{
521 return false;
522 }
523}
524
525/* The numpad function returns a integer value and does not take any parameters*/
526/* It 1 true if # is pressed while it returns 2 if * is pressed and it returns 3 if invalid key is pressed*/
527int numpad(){
528 int key_return = 0; // Used to return which key is pressed
529 while(1){
530 customKey = customKeypad.getKey(); // the getKey() method returns the character pressed on the keypad
531 if (customKey=='#'){
532 //Serial.println("Continue"); // Uncomment this line for testing
533 key_return = 1;
534 break;
535 }
536 else if(customKey=='*'){
537 //Serial.println("Repeat the previous sentence"); // Uncomment this line for testing
538 key_return = 2;
539 break;
540 }
541 else{
542 //Serial.println("Invalid key pressed"); // Uncomment this line for testing
543 }
544 }
545 return key_return;
546}
547
548/* Clears the temp data stored in the array*/
549void clearData(){
550 while(data_count !=0){
551 Data[data_count--] = 0;
552 }
553 return;
554}