Android.cpp@5:0df77b612c09, 2018-01-16 (annotated)
- Committer:
- maskas
- Date:
- Tue Jan 16 18:30:30 2018 +0000
- Revision:
- 5:0df77b612c09
- Parent:
- 4:5f1af832e593
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maskas | 0:9b41a36d03d9 | 1 | |
maskas | 0:9b41a36d03d9 | 2 | |
maskas | 0:9b41a36d03d9 | 3 | #include "mbed.h" |
maskas | 2:aeecb6277fc7 | 4 | #include<string> |
maskas | 0:9b41a36d03d9 | 5 | #include "Android.h" |
maskas | 3:36ff03cb6bab | 6 | #include "rtos.h" |
maskas | 2:aeecb6277fc7 | 7 | |
maskas | 2:aeecb6277fc7 | 8 | #define ARDUINO_STARTED "AR1" |
maskas | 2:aeecb6277fc7 | 9 | #define ANDROID_CONNECTED "AN1" |
maskas | 2:aeecb6277fc7 | 10 | #define OK "OK" |
maskas | 2:aeecb6277fc7 | 11 | #define ARDUINO_HEART_BEAT "ARD_HB" |
maskas | 2:aeecb6277fc7 | 12 | #define ANDROID_HEART_BEAT "AND_HB" |
maskas | 2:aeecb6277fc7 | 13 | #define DISPENSING_STARTED "DISP1" |
maskas | 2:aeecb6277fc7 | 14 | #define DISPENSING_COMPLETE "DISP2" |
maskas | 2:aeecb6277fc7 | 15 | #define DISPENSING_NO_PAYMENT "DISP0" |
maskas | 2:aeecb6277fc7 | 16 | #define PRODUCT_TAKEN "TAKEN" |
maskas | 2:aeecb6277fc7 | 17 | #define DISPENSING_FAILED "DISP3" |
maskas | 2:aeecb6277fc7 | 18 | #define TAKE_PAYMENT "PAY1" |
maskas | 2:aeecb6277fc7 | 19 | #define TAKING_PAYMENT "PAY2" |
maskas | 2:aeecb6277fc7 | 20 | #define PAYMENT_RECEIVED "PAY3" |
maskas | 2:aeecb6277fc7 | 21 | #define PAYMENT_FAILED "PAY4" |
maskas | 2:aeecb6277fc7 | 22 | #define SHOW_HOME_SCREEN "HOME" |
maskas | 2:aeecb6277fc7 | 23 | #define PRODUCT_SELECTED "SELECTION" |
maskas | 2:aeecb6277fc7 | 24 | |
maskas | 2:aeecb6277fc7 | 25 | #define COMMAND_CONNECTED 100 |
maskas | 2:aeecb6277fc7 | 26 | #define COMMAND_HEARTH_BEAT 101 |
maskas | 2:aeecb6277fc7 | 27 | #define COMMAND_PRODUCT_SELECT 102 |
maskas | 2:aeecb6277fc7 | 28 | #define COMMAND_TAKE_PAYMENT 103 |
maskas | 2:aeecb6277fc7 | 29 | #define COMMAND_DISPENSE 104 |
maskas | 2:aeecb6277fc7 | 30 | #define COMMAND_VALUE 200 |
maskas | 2:aeecb6277fc7 | 31 | #define COMMAND_UNKNOWN 900 |
maskas | 2:aeecb6277fc7 | 32 | |
maskas | 2:aeecb6277fc7 | 33 | |
maskas | 2:aeecb6277fc7 | 34 | |
maskas | 2:aeecb6277fc7 | 35 | |
maskas | 4:5f1af832e593 | 36 | Android::Android(Serial& serial, Carousel& carousel): serial(serial), myled(LED1), carousel(carousel) { |
maskas | 3:36ff03cb6bab | 37 | this->serial.attach(callback(this, &Android::onData)); |
maskas | 1:6d9353593c3c | 38 | this->ticker.attach(callback(this, &Android::hearthBeat), 5.0f); |
maskas | 3:36ff03cb6bab | 39 | |
maskas | 0:9b41a36d03d9 | 40 | } |
maskas | 0:9b41a36d03d9 | 41 | |
maskas | 2:aeecb6277fc7 | 42 | |
maskas | 0:9b41a36d03d9 | 43 | void Android::hearthBeat(void) |
maskas | 0:9b41a36d03d9 | 44 | { |
maskas | 2:aeecb6277fc7 | 45 | this->sendToAndroid(ARDUINO_HEART_BEAT); |
maskas | 2:aeecb6277fc7 | 46 | } |
maskas | 0:9b41a36d03d9 | 47 | |
maskas | 2:aeecb6277fc7 | 48 | void Android::sendToAndroid(string message) |
maskas | 2:aeecb6277fc7 | 49 | { |
maskas | 2:aeecb6277fc7 | 50 | printf("Sent to Android: %s\n", message); |
maskas | 2:aeecb6277fc7 | 51 | this->serial.printf("%s\n", message); |
maskas | 2:aeecb6277fc7 | 52 | this->serial.putc(-91); |
maskas | 0:9b41a36d03d9 | 53 | } |
maskas | 0:9b41a36d03d9 | 54 | |
maskas | 3:36ff03cb6bab | 55 | |
maskas | 0:9b41a36d03d9 | 56 | void Android::onData(void) |
maskas | 0:9b41a36d03d9 | 57 | { |
maskas | 0:9b41a36d03d9 | 58 | char command[50]; |
maskas | 3:36ff03cb6bab | 59 | |
maskas | 3:36ff03cb6bab | 60 | this->serial.scanf("%s", command); |
maskas | 2:aeecb6277fc7 | 61 | |
maskas | 3:36ff03cb6bab | 62 | if (strncmp(command, ANDROID_CONNECTED, sizeof(ANDROID_CONNECTED)-1) == 0) { |
maskas | 3:36ff03cb6bab | 63 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 64 | this->sendToAndroid(OK); |
maskas | 3:36ff03cb6bab | 65 | } else if (strncmp(command, ANDROID_HEART_BEAT, sizeof(ANDROID_HEART_BEAT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 66 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 67 | } else if (strncmp(command, PRODUCT_SELECTED, sizeof(PRODUCT_SELECTED)-1) == 0) { |
maskas | 3:36ff03cb6bab | 68 | int section = this->readNumber(); |
maskas | 3:36ff03cb6bab | 69 | printf("Selected %d\n", section); |
maskas | 3:36ff03cb6bab | 70 | } else if (strncmp(command, TAKE_PAYMENT, sizeof(TAKE_PAYMENT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 71 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 72 | } else if (strncmp(command, DISPENSING_NO_PAYMENT, sizeof(DISPENSING_NO_PAYMENT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 73 | int section = this->readNumber(); |
maskas | 3:36ff03cb6bab | 74 | printf("Dispensing %d\n", section); |
maskas | 3:36ff03cb6bab | 75 | this->carousel.dispenseProduct(section); |
maskas | 3:36ff03cb6bab | 76 | } else { |
maskas | 3:36ff03cb6bab | 77 | printf("Unknown command: |%s|\n", command); |
maskas | 3:36ff03cb6bab | 78 | } |
maskas | 3:36ff03cb6bab | 79 | |
maskas | 5:0df77b612c09 | 80 | if (this->serial.readable()){ |
maskas | 5:0df77b612c09 | 81 | this->serial.getc();//read newline |
maskas | 5:0df77b612c09 | 82 | } |
maskas | 3:36ff03cb6bab | 83 | } |
maskas | 2:aeecb6277fc7 | 84 | |
maskas | 4:5f1af832e593 | 85 | |
maskas | 3:36ff03cb6bab | 86 | int Android::readNumber() { |
maskas | 3:36ff03cb6bab | 87 | this->serial.getc();//read newline |
maskas | 3:36ff03cb6bab | 88 | char serialString[5]; |
maskas | 3:36ff03cb6bab | 89 | this->serial.scanf("%s", serialString); |
maskas | 3:36ff03cb6bab | 90 | |
maskas | 3:36ff03cb6bab | 91 | int answer; |
maskas | 3:36ff03cb6bab | 92 | char* end = NULL; |
maskas | 3:36ff03cb6bab | 93 | answer = strtol (serialString,&end,10); |
maskas | 3:36ff03cb6bab | 94 | return answer; |
maskas | 0:9b41a36d03d9 | 95 | } |