Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Android.cpp@3:36ff03cb6bab, 2018-01-13 (annotated)
- Committer:
- maskas
- Date:
- Sat Jan 13 18:53:38 2018 +0000
- Revision:
- 3:36ff03cb6bab
- Parent:
- 2:aeecb6277fc7
- Child:
- 4:5f1af832e593
Android communication;
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 | 1:6d9353593c3c | 36 | Android::Android(PinName txPin, PinName rxPin, Carousel& carousel): serial(txPin, rxPin), myled(LED1), carousel(carousel) { |
maskas | 0:9b41a36d03d9 | 37 | this->serial.baud(115200); |
maskas | 3:36ff03cb6bab | 38 | this->serial.attach(callback(this, &Android::onData)); |
maskas | 3:36ff03cb6bab | 39 | //this->serial.attach(this, &Android::onData, MODSERIAL::RxAutoDetect); |
maskas | 3:36ff03cb6bab | 40 | //this->serial.autoDetectChar('\n'); |
maskas | 0:9b41a36d03d9 | 41 | |
maskas | 1:6d9353593c3c | 42 | this->ticker.attach(callback(this, &Android::hearthBeat), 5.0f); |
maskas | 3:36ff03cb6bab | 43 | |
maskas | 3:36ff03cb6bab | 44 | //this->tickerReadSerial.attach(callback(this, &Android::onData), 0.1f); |
maskas | 1:6d9353593c3c | 45 | |
maskas | 1:6d9353593c3c | 46 | this->carousel.callibrate(); |
maskas | 3:36ff03cb6bab | 47 | |
maskas | 0:9b41a36d03d9 | 48 | } |
maskas | 0:9b41a36d03d9 | 49 | |
maskas | 2:aeecb6277fc7 | 50 | |
maskas | 0:9b41a36d03d9 | 51 | void Android::hearthBeat(void) |
maskas | 0:9b41a36d03d9 | 52 | { |
maskas | 2:aeecb6277fc7 | 53 | this->sendToAndroid(ARDUINO_HEART_BEAT); |
maskas | 2:aeecb6277fc7 | 54 | } |
maskas | 0:9b41a36d03d9 | 55 | |
maskas | 2:aeecb6277fc7 | 56 | void Android::sendToAndroid(string message) |
maskas | 2:aeecb6277fc7 | 57 | { |
maskas | 2:aeecb6277fc7 | 58 | printf("Sent to Android: %s\n", message); |
maskas | 2:aeecb6277fc7 | 59 | this->serial.printf("%s\n", message); |
maskas | 2:aeecb6277fc7 | 60 | this->serial.putc(-91); |
maskas | 0:9b41a36d03d9 | 61 | } |
maskas | 0:9b41a36d03d9 | 62 | |
maskas | 3:36ff03cb6bab | 63 | |
maskas | 0:9b41a36d03d9 | 64 | void Android::onData(void) |
maskas | 0:9b41a36d03d9 | 65 | { |
maskas | 0:9b41a36d03d9 | 66 | char command[50]; |
maskas | 3:36ff03cb6bab | 67 | |
maskas | 3:36ff03cb6bab | 68 | this->serial.scanf("%s", command); |
maskas | 2:aeecb6277fc7 | 69 | |
maskas | 3:36ff03cb6bab | 70 | if (strncmp(command, ANDROID_CONNECTED, sizeof(ANDROID_CONNECTED)-1) == 0) { |
maskas | 3:36ff03cb6bab | 71 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 72 | this->sendToAndroid(OK); |
maskas | 3:36ff03cb6bab | 73 | } else if (strncmp(command, ANDROID_HEART_BEAT, sizeof(ANDROID_HEART_BEAT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 74 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 75 | } else if (strncmp(command, PRODUCT_SELECTED, sizeof(PRODUCT_SELECTED)-1) == 0) { |
maskas | 3:36ff03cb6bab | 76 | int section = this->readNumber(); |
maskas | 3:36ff03cb6bab | 77 | printf("Selected %d\n", section); |
maskas | 3:36ff03cb6bab | 78 | } else if (strncmp(command, TAKE_PAYMENT, sizeof(TAKE_PAYMENT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 79 | printf("%s\n", command); |
maskas | 3:36ff03cb6bab | 80 | } else if (strncmp(command, DISPENSING_NO_PAYMENT, sizeof(DISPENSING_NO_PAYMENT)-1) == 0) { |
maskas | 3:36ff03cb6bab | 81 | int section = this->readNumber(); |
maskas | 3:36ff03cb6bab | 82 | printf("Dispensing %d\n", section); |
maskas | 3:36ff03cb6bab | 83 | this->carousel.dispenseProduct(section); |
maskas | 3:36ff03cb6bab | 84 | } else { |
maskas | 3:36ff03cb6bab | 85 | printf("Unknown command: |%s|\n", command); |
maskas | 3:36ff03cb6bab | 86 | } |
maskas | 3:36ff03cb6bab | 87 | |
maskas | 3:36ff03cb6bab | 88 | this->serial.getc();//read newline |
maskas | 3:36ff03cb6bab | 89 | } |
maskas | 2:aeecb6277fc7 | 90 | |
maskas | 3:36ff03cb6bab | 91 | int Android::readNumber() { |
maskas | 3:36ff03cb6bab | 92 | this->serial.getc();//read newline |
maskas | 3:36ff03cb6bab | 93 | char serialString[5]; |
maskas | 3:36ff03cb6bab | 94 | this->serial.scanf("%s", serialString); |
maskas | 3:36ff03cb6bab | 95 | |
maskas | 3:36ff03cb6bab | 96 | int answer; |
maskas | 3:36ff03cb6bab | 97 | char* end = NULL; |
maskas | 3:36ff03cb6bab | 98 | answer = strtol (serialString,&end,10); |
maskas | 3:36ff03cb6bab | 99 | return answer; |
maskas | 0:9b41a36d03d9 | 100 | } |