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@2:aeecb6277fc7, 2018-01-09 (annotated)
- Committer:
- maskas
- Date:
- Tue Jan 09 20:07:04 2018 +0000
- Revision:
- 2:aeecb6277fc7
- Parent:
- 1:6d9353593c3c
- Child:
- 3:36ff03cb6bab
new serial
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 | 2:aeecb6277fc7 | 6 | #include "MODSERIAL.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 | 2:aeecb6277fc7 | 36 | |
maskas | 2:aeecb6277fc7 | 37 | #define PAYMENT_MODE "PAID" |
maskas | 2:aeecb6277fc7 | 38 | #define MESSAGE_BUFFER_SIZE 32 |
maskas | 2:aeecb6277fc7 | 39 | |
maskas | 2:aeecb6277fc7 | 40 | char messageBufferIncoming[MESSAGE_BUFFER_SIZE]; |
maskas | 2:aeecb6277fc7 | 41 | char messageBufferOutgoing[MESSAGE_BUFFER_SIZE]; |
maskas | 2:aeecb6277fc7 | 42 | int lastCommands[3]; |
maskas | 2:aeecb6277fc7 | 43 | int commandValue = 0; |
maskas | 2:aeecb6277fc7 | 44 | |
maskas | 2:aeecb6277fc7 | 45 | void Android::onMessage(MODSERIAL_IRQ_INFO *q) |
maskas | 2:aeecb6277fc7 | 46 | { |
maskas | 2:aeecb6277fc7 | 47 | MODSERIAL *sys = q->serial; |
maskas | 2:aeecb6277fc7 | 48 | sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE); |
maskas | 2:aeecb6277fc7 | 49 | |
maskas | 2:aeecb6277fc7 | 50 | this->processMessageBuffer(); |
maskas | 2:aeecb6277fc7 | 51 | |
maskas | 2:aeecb6277fc7 | 52 | printf("Command received: %s\n", messageBufferIncoming); |
maskas | 2:aeecb6277fc7 | 53 | } |
maskas | 2:aeecb6277fc7 | 54 | |
maskas | 0:9b41a36d03d9 | 55 | |
maskas | 1:6d9353593c3c | 56 | Android::Android(PinName txPin, PinName rxPin, Carousel& carousel): serial(txPin, rxPin), myled(LED1), carousel(carousel) { |
maskas | 0:9b41a36d03d9 | 57 | this->serial.baud(115200); |
maskas | 2:aeecb6277fc7 | 58 | //this->serial.attach(callback(this, &Android::onData)); |
maskas | 2:aeecb6277fc7 | 59 | this->serial.attach(this, &Android::onMessage, MODSERIAL::RxAutoDetect); |
maskas | 2:aeecb6277fc7 | 60 | this->serial.autoDetectChar('\n'); |
maskas | 0:9b41a36d03d9 | 61 | |
maskas | 1:6d9353593c3c | 62 | this->ticker.attach(callback(this, &Android::hearthBeat), 5.0f); |
maskas | 1:6d9353593c3c | 63 | |
maskas | 1:6d9353593c3c | 64 | this->carousel.callibrate(); |
maskas | 0:9b41a36d03d9 | 65 | } |
maskas | 0:9b41a36d03d9 | 66 | |
maskas | 2:aeecb6277fc7 | 67 | |
maskas | 2:aeecb6277fc7 | 68 | void Android::processMessageBuffer() |
maskas | 2:aeecb6277fc7 | 69 | { |
maskas | 2:aeecb6277fc7 | 70 | lastCommands[0] = lastCommands[1]; |
maskas | 2:aeecb6277fc7 | 71 | lastCommands[1] = lastCommands[2]; |
maskas | 2:aeecb6277fc7 | 72 | |
maskas | 2:aeecb6277fc7 | 73 | int newCommand = 0; |
maskas | 2:aeecb6277fc7 | 74 | if (strncmp(messageBufferIncoming, ANDROID_CONNECTED, sizeof(ANDROID_CONNECTED)) == 0) { |
maskas | 2:aeecb6277fc7 | 75 | newCommand = COMMAND_CONNECTED; |
maskas | 2:aeecb6277fc7 | 76 | } else if (strncmp(messageBufferIncoming, ANDROID_HEART_BEAT, sizeof(ANDROID_HEART_BEAT)) == 0) { |
maskas | 2:aeecb6277fc7 | 77 | newCommand = COMMAND_HEARTH_BEAT; |
maskas | 2:aeecb6277fc7 | 78 | } else if (strncmp(messageBufferIncoming, PRODUCT_SELECTED, sizeof(PRODUCT_SELECTED)) == 0) { |
maskas | 2:aeecb6277fc7 | 79 | newCommand = COMMAND_PRODUCT_SELECT; |
maskas | 2:aeecb6277fc7 | 80 | } else if (strncmp(messageBufferIncoming, TAKE_PAYMENT, sizeof(TAKE_PAYMENT)) == 0) { |
maskas | 2:aeecb6277fc7 | 81 | newCommand = COMMAND_TAKE_PAYMENT; |
maskas | 2:aeecb6277fc7 | 82 | } else if (strncmp(messageBufferIncoming, DISPENSING_NO_PAYMENT, sizeof(DISPENSING_NO_PAYMENT)) == 0) { |
maskas | 2:aeecb6277fc7 | 83 | newCommand = COMMAND_DISPENSE; |
maskas | 2:aeecb6277fc7 | 84 | } else if (atoi(messageBufferIncoming) != 0) { |
maskas | 2:aeecb6277fc7 | 85 | newCommand = COMMAND_VALUE; |
maskas | 2:aeecb6277fc7 | 86 | commandValue = atoi(messageBufferIncoming); |
maskas | 2:aeecb6277fc7 | 87 | } else { |
maskas | 2:aeecb6277fc7 | 88 | newCommand = COMMAND_UNKNOWN; |
maskas | 2:aeecb6277fc7 | 89 | printf("Did not understood Android message: "); |
maskas | 2:aeecb6277fc7 | 90 | printf(messageBufferIncoming); |
maskas | 2:aeecb6277fc7 | 91 | printf("\n"); |
maskas | 2:aeecb6277fc7 | 92 | } |
maskas | 2:aeecb6277fc7 | 93 | lastCommands[2] = newCommand; |
maskas | 2:aeecb6277fc7 | 94 | |
maskas | 2:aeecb6277fc7 | 95 | this->executeCommands(); |
maskas | 2:aeecb6277fc7 | 96 | } |
maskas | 2:aeecb6277fc7 | 97 | |
maskas | 2:aeecb6277fc7 | 98 | void Android::executeCommands() |
maskas | 2:aeecb6277fc7 | 99 | { |
maskas | 2:aeecb6277fc7 | 100 | switch(lastCommands[2]) { |
maskas | 2:aeecb6277fc7 | 101 | case COMMAND_VALUE: |
maskas | 2:aeecb6277fc7 | 102 | switch (lastCommands[1]) { |
maskas | 2:aeecb6277fc7 | 103 | case COMMAND_DISPENSE: |
maskas | 2:aeecb6277fc7 | 104 | this->carousel.dispenseProduct(lastCommands[2]); |
maskas | 2:aeecb6277fc7 | 105 | break; |
maskas | 2:aeecb6277fc7 | 106 | } |
maskas | 2:aeecb6277fc7 | 107 | break; |
maskas | 2:aeecb6277fc7 | 108 | case COMMAND_CONNECTED: |
maskas | 2:aeecb6277fc7 | 109 | this->sendToAndroid(OK); |
maskas | 2:aeecb6277fc7 | 110 | break; |
maskas | 2:aeecb6277fc7 | 111 | case COMMAND_DISPENSE: |
maskas | 2:aeecb6277fc7 | 112 | case COMMAND_TAKE_PAYMENT: |
maskas | 2:aeecb6277fc7 | 113 | case COMMAND_PRODUCT_SELECT: |
maskas | 2:aeecb6277fc7 | 114 | case COMMAND_HEARTH_BEAT: |
maskas | 2:aeecb6277fc7 | 115 | //do nothing |
maskas | 2:aeecb6277fc7 | 116 | break; |
maskas | 2:aeecb6277fc7 | 117 | } |
maskas | 2:aeecb6277fc7 | 118 | } |
maskas | 2:aeecb6277fc7 | 119 | |
maskas | 2:aeecb6277fc7 | 120 | |
maskas | 2:aeecb6277fc7 | 121 | |
maskas | 0:9b41a36d03d9 | 122 | void Android::hearthBeat(void) |
maskas | 0:9b41a36d03d9 | 123 | { |
maskas | 2:aeecb6277fc7 | 124 | this->sendToAndroid(ARDUINO_HEART_BEAT); |
maskas | 2:aeecb6277fc7 | 125 | } |
maskas | 0:9b41a36d03d9 | 126 | |
maskas | 2:aeecb6277fc7 | 127 | void Android::sendToAndroid(string message) |
maskas | 2:aeecb6277fc7 | 128 | { |
maskas | 2:aeecb6277fc7 | 129 | printf("Sent to Android: %s\n", message); |
maskas | 2:aeecb6277fc7 | 130 | this->serial.printf("%s\n", message); |
maskas | 2:aeecb6277fc7 | 131 | this->serial.putc(-91); |
maskas | 0:9b41a36d03d9 | 132 | } |
maskas | 0:9b41a36d03d9 | 133 | |
maskas | 0:9b41a36d03d9 | 134 | void Android::onData(void) |
maskas | 0:9b41a36d03d9 | 135 | { |
maskas | 0:9b41a36d03d9 | 136 | char command[50]; |
maskas | 2:aeecb6277fc7 | 137 | int id; |
maskas | 2:aeecb6277fc7 | 138 | |
maskas | 2:aeecb6277fc7 | 139 | command[0] = '\n'; |
maskas | 2:aeecb6277fc7 | 140 | id = -1; |
maskas | 2:aeecb6277fc7 | 141 | |
maskas | 0:9b41a36d03d9 | 142 | if (this->serial.readable()) { |
maskas | 2:aeecb6277fc7 | 143 | this->serial.scanf("%s\n", &command); |
maskas | 2:aeecb6277fc7 | 144 | printf("Command received: %s\n", command); |
maskas | 0:9b41a36d03d9 | 145 | |
maskas | 2:aeecb6277fc7 | 146 | if (strcmp(command, ANDROID_CONNECTED) == 0) { |
maskas | 2:aeecb6277fc7 | 147 | this->sendToAndroid(OK); |
maskas | 2:aeecb6277fc7 | 148 | } else if (command == ANDROID_HEART_BEAT) { |
maskas | 2:aeecb6277fc7 | 149 | //enjoy |
maskas | 2:aeecb6277fc7 | 150 | } else if (strcmp(command, PRODUCT_SELECTED) == 0) { |
maskas | 2:aeecb6277fc7 | 151 | this->serial.scanf("%d\n", &id); |
maskas | 2:aeecb6277fc7 | 152 | printf("%d\n", id); |
maskas | 2:aeecb6277fc7 | 153 | //do nothing |
maskas | 2:aeecb6277fc7 | 154 | //preTurnProduct(); |
maskas | 2:aeecb6277fc7 | 155 | } else if (strcmp(command, TAKE_PAYMENT) == 0) { |
maskas | 2:aeecb6277fc7 | 156 | //do nothing |
maskas | 2:aeecb6277fc7 | 157 | } else if (strcmp(command, DISPENSING_NO_PAYMENT) == 0) { |
maskas | 2:aeecb6277fc7 | 158 | this->serial.scanf("%d\n", &id); |
maskas | 2:aeecb6277fc7 | 159 | printf("%d\n", id); |
maskas | 2:aeecb6277fc7 | 160 | this->carousel.dispenseProduct(id); |
maskas | 2:aeecb6277fc7 | 161 | } else { |
maskas | 2:aeecb6277fc7 | 162 | printf("Did not understood Android message: "); |
maskas | 2:aeecb6277fc7 | 163 | printf(command); |
maskas | 2:aeecb6277fc7 | 164 | printf("\n"); |
maskas | 2:aeecb6277fc7 | 165 | } |
maskas | 2:aeecb6277fc7 | 166 | |
maskas | 2:aeecb6277fc7 | 167 | |
maskas | 2:aeecb6277fc7 | 168 | |
maskas | 0:9b41a36d03d9 | 169 | } |
maskas | 0:9b41a36d03d9 | 170 | } |