![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Serial communication between a F303RE and a mobile phone using a HC-05 BT.
main.cpp@0:95b1edb8d258, 2015-12-09 (annotated)
- Committer:
- jcabello7
- Date:
- Wed Dec 09 12:58:00 2015 +0000
- Revision:
- 0:95b1edb8d258
Initial version (not working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jcabello7 | 0:95b1edb8d258 | 1 | #include "mbed.h" |
jcabello7 | 0:95b1edb8d258 | 2 | #include "rtos.h" |
jcabello7 | 0:95b1edb8d258 | 3 | |
jcabello7 | 0:95b1edb8d258 | 4 | class Hc05 : public Serial |
jcabello7 | 0:95b1edb8d258 | 5 | { |
jcabello7 | 0:95b1edb8d258 | 6 | |
jcabello7 | 0:95b1edb8d258 | 7 | public: |
jcabello7 | 0:95b1edb8d258 | 8 | Timer timer; |
jcabello7 | 0:95b1edb8d258 | 9 | Hc05(PinName tx, PinName rx) : Serial(tx, rx) { |
jcabello7 | 0:95b1edb8d258 | 10 | baud(115200); |
jcabello7 | 0:95b1edb8d258 | 11 | timer.start(); |
jcabello7 | 0:95b1edb8d258 | 12 | //printf("Constructora"); |
jcabello7 | 0:95b1edb8d258 | 13 | } |
jcabello7 | 0:95b1edb8d258 | 14 | ~Hc05() { |
jcabello7 | 0:95b1edb8d258 | 15 | timer.stop(); |
jcabello7 | 0:95b1edb8d258 | 16 | //printf("Destructora"); |
jcabello7 | 0:95b1edb8d258 | 17 | } |
jcabello7 | 0:95b1edb8d258 | 18 | bool enviaString(char* str) { |
jcabello7 | 0:95b1edb8d258 | 19 | if (str[0] != '\0') |
jcabello7 | 0:95b1edb8d258 | 20 | printf("%s", str); |
jcabello7 | 0:95b1edb8d258 | 21 | return true; |
jcabello7 | 0:95b1edb8d258 | 22 | } |
jcabello7 | 0:95b1edb8d258 | 23 | |
jcabello7 | 0:95b1edb8d258 | 24 | void enviaStringAT(char* str) { |
jcabello7 | 0:95b1edb8d258 | 25 | //baud(38400); |
jcabello7 | 0:95b1edb8d258 | 26 | if (str[0] != '\0') |
jcabello7 | 0:95b1edb8d258 | 27 | printf("%s\r\n", str); |
jcabello7 | 0:95b1edb8d258 | 28 | } |
jcabello7 | 0:95b1edb8d258 | 29 | bool llegirStringAT(char* str) { |
jcabello7 | 0:95b1edb8d258 | 30 | if(readable()) { |
jcabello7 | 0:95b1edb8d258 | 31 | int i=0; |
jcabello7 | 0:95b1edb8d258 | 32 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 33 | //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10) |
jcabello7 | 0:95b1edb8d258 | 34 | while(str[i] != 10 && (i < 128)) { |
jcabello7 | 0:95b1edb8d258 | 35 | i++; |
jcabello7 | 0:95b1edb8d258 | 36 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 37 | } |
jcabello7 | 0:95b1edb8d258 | 38 | str[i]='\0'; |
jcabello7 | 0:95b1edb8d258 | 39 | return true; |
jcabello7 | 0:95b1edb8d258 | 40 | } else { |
jcabello7 | 0:95b1edb8d258 | 41 | str[0] = '\0'; |
jcabello7 | 0:95b1edb8d258 | 42 | return false; |
jcabello7 | 0:95b1edb8d258 | 43 | } |
jcabello7 | 0:95b1edb8d258 | 44 | } |
jcabello7 | 0:95b1edb8d258 | 45 | |
jcabello7 | 0:95b1edb8d258 | 46 | bool llegirString(char* str) { |
jcabello7 | 0:95b1edb8d258 | 47 | if(readable()) { |
jcabello7 | 0:95b1edb8d258 | 48 | timer.reset(); |
jcabello7 | 0:95b1edb8d258 | 49 | int i=0; |
jcabello7 | 0:95b1edb8d258 | 50 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 51 | if (str[i] == '@') |
jcabello7 | 0:95b1edb8d258 | 52 | return false; |
jcabello7 | 0:95b1edb8d258 | 53 | //Quan detecta un 10 o un 13 surt del bucle (en el mode AT, envia 13+10) |
jcabello7 | 0:95b1edb8d258 | 54 | while(str[i] != 13 && (i < 128)) { |
jcabello7 | 0:95b1edb8d258 | 55 | i++; |
jcabello7 | 0:95b1edb8d258 | 56 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 57 | } |
jcabello7 | 0:95b1edb8d258 | 58 | str[i]='\0'; |
jcabello7 | 0:95b1edb8d258 | 59 | return true; |
jcabello7 | 0:95b1edb8d258 | 60 | } else { |
jcabello7 | 0:95b1edb8d258 | 61 | str[0] = '\0'; |
jcabello7 | 0:95b1edb8d258 | 62 | return false; |
jcabello7 | 0:95b1edb8d258 | 63 | } |
jcabello7 | 0:95b1edb8d258 | 64 | } |
jcabello7 | 0:95b1edb8d258 | 65 | |
jcabello7 | 0:95b1edb8d258 | 66 | void modeAT(bool _bool) { |
jcabello7 | 0:95b1edb8d258 | 67 | if (_bool) { |
jcabello7 | 0:95b1edb8d258 | 68 | baud(38400); |
jcabello7 | 0:95b1edb8d258 | 69 | } else { |
jcabello7 | 0:95b1edb8d258 | 70 | baud(115200); |
jcabello7 | 0:95b1edb8d258 | 71 | } |
jcabello7 | 0:95b1edb8d258 | 72 | } |
jcabello7 | 0:95b1edb8d258 | 73 | |
jcabello7 | 0:95b1edb8d258 | 74 | bool connexioOK() { |
jcabello7 | 0:95b1edb8d258 | 75 | if(timer.read_ms() > 500) |
jcabello7 | 0:95b1edb8d258 | 76 | return false; |
jcabello7 | 0:95b1edb8d258 | 77 | else |
jcabello7 | 0:95b1edb8d258 | 78 | return true; |
jcabello7 | 0:95b1edb8d258 | 79 | } |
jcabello7 | 0:95b1edb8d258 | 80 | |
jcabello7 | 0:95b1edb8d258 | 81 | void checkConn() { |
jcabello7 | 0:95b1edb8d258 | 82 | if(readable()) |
jcabello7 | 0:95b1edb8d258 | 83 | timer.reset(); |
jcabello7 | 0:95b1edb8d258 | 84 | } |
jcabello7 | 0:95b1edb8d258 | 85 | }; |
jcabello7 | 0:95b1edb8d258 | 86 | |
jcabello7 | 0:95b1edb8d258 | 87 | class Pc : public Serial |
jcabello7 | 0:95b1edb8d258 | 88 | { |
jcabello7 | 0:95b1edb8d258 | 89 | public: |
jcabello7 | 0:95b1edb8d258 | 90 | Pc(PinName tx, PinName rx) : Serial(tx, rx) { |
jcabello7 | 0:95b1edb8d258 | 91 | baud(115200); |
jcabello7 | 0:95b1edb8d258 | 92 | printf("Constructora\n"); |
jcabello7 | 0:95b1edb8d258 | 93 | } |
jcabello7 | 0:95b1edb8d258 | 94 | ~Pc() { |
jcabello7 | 0:95b1edb8d258 | 95 | printf("Destructora\n"); |
jcabello7 | 0:95b1edb8d258 | 96 | } |
jcabello7 | 0:95b1edb8d258 | 97 | bool enviaString(char* str) { |
jcabello7 | 0:95b1edb8d258 | 98 | if (str[0] != '\0') |
jcabello7 | 0:95b1edb8d258 | 99 | printf("%s\n", str); |
jcabello7 | 0:95b1edb8d258 | 100 | return true; |
jcabello7 | 0:95b1edb8d258 | 101 | } |
jcabello7 | 0:95b1edb8d258 | 102 | |
jcabello7 | 0:95b1edb8d258 | 103 | bool llegirString(char* str) { |
jcabello7 | 0:95b1edb8d258 | 104 | if(readable()) { |
jcabello7 | 0:95b1edb8d258 | 105 | int i=0; |
jcabello7 | 0:95b1edb8d258 | 106 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 107 | while(str[i] != 13 && (i < 128)) { |
jcabello7 | 0:95b1edb8d258 | 108 | i++; |
jcabello7 | 0:95b1edb8d258 | 109 | str[i] = getc(); |
jcabello7 | 0:95b1edb8d258 | 110 | } |
jcabello7 | 0:95b1edb8d258 | 111 | str[i]='\0'; |
jcabello7 | 0:95b1edb8d258 | 112 | return true; |
jcabello7 | 0:95b1edb8d258 | 113 | } else { |
jcabello7 | 0:95b1edb8d258 | 114 | str[0] = '\0'; |
jcabello7 | 0:95b1edb8d258 | 115 | return false; |
jcabello7 | 0:95b1edb8d258 | 116 | } |
jcabello7 | 0:95b1edb8d258 | 117 | } |
jcabello7 | 0:95b1edb8d258 | 118 | }; |
jcabello7 | 0:95b1edb8d258 | 119 | |
jcabello7 | 0:95b1edb8d258 | 120 | |
jcabello7 | 0:95b1edb8d258 | 121 | |
jcabello7 | 0:95b1edb8d258 | 122 | |
jcabello7 | 0:95b1edb8d258 | 123 | |
jcabello7 | 0:95b1edb8d258 | 124 | void llegir_thread(void const *args); |
jcabello7 | 0:95b1edb8d258 | 125 | |
jcabello7 | 0:95b1edb8d258 | 126 | Mutex mutex; |
jcabello7 | 0:95b1edb8d258 | 127 | |
jcabello7 | 0:95b1edb8d258 | 128 | Pc pc(USBTX, USBRX); |
jcabello7 | 0:95b1edb8d258 | 129 | Hc05 bt(D8, D2); |
jcabello7 | 0:95b1edb8d258 | 130 | |
jcabello7 | 0:95b1edb8d258 | 131 | |
jcabello7 | 0:95b1edb8d258 | 132 | |
jcabello7 | 0:95b1edb8d258 | 133 | int main() |
jcabello7 | 0:95b1edb8d258 | 134 | { |
jcabello7 | 0:95b1edb8d258 | 135 | |
jcabello7 | 0:95b1edb8d258 | 136 | |
jcabello7 | 0:95b1edb8d258 | 137 | //Thread thread(llegir_thread); |
jcabello7 | 0:95b1edb8d258 | 138 | char str[128]; |
jcabello7 | 0:95b1edb8d258 | 139 | |
jcabello7 | 0:95b1edb8d258 | 140 | while(1) { |
jcabello7 | 0:95b1edb8d258 | 141 | //Thread::signal_wait(0x1); |
jcabello7 | 0:95b1edb8d258 | 142 | if(pc.llegirString(str)) |
jcabello7 | 0:95b1edb8d258 | 143 | bt.enviaString(str); |
jcabello7 | 0:95b1edb8d258 | 144 | if(bt.llegirString(str)) |
jcabello7 | 0:95b1edb8d258 | 145 | pc.enviaString(str); |
jcabello7 | 0:95b1edb8d258 | 146 | |
jcabello7 | 0:95b1edb8d258 | 147 | if(!(bt.connexioOK())) |
jcabello7 | 0:95b1edb8d258 | 148 | pc.printf("ERROR DE CONNEXIO\n"); |
jcabello7 | 0:95b1edb8d258 | 149 | Thread::wait(500); |
jcabello7 | 0:95b1edb8d258 | 150 | //Thread::signal_set(0x2); |
jcabello7 | 0:95b1edb8d258 | 151 | } |
jcabello7 | 0:95b1edb8d258 | 152 | |
jcabello7 | 0:95b1edb8d258 | 153 | } |
jcabello7 | 0:95b1edb8d258 | 154 | |
jcabello7 | 0:95b1edb8d258 | 155 | void llegir_thread(void const *args) { |
jcabello7 | 0:95b1edb8d258 | 156 | while(1){ |
jcabello7 | 0:95b1edb8d258 | 157 | //Thread::signal_wait(0x2); |
jcabello7 | 0:95b1edb8d258 | 158 | mutex.lock(); |
jcabello7 | 0:95b1edb8d258 | 159 | bt.checkConn(); |
jcabello7 | 0:95b1edb8d258 | 160 | mutex.unlock(); |
jcabello7 | 0:95b1edb8d258 | 161 | Thread::wait(300); |
jcabello7 | 0:95b1edb8d258 | 162 | |
jcabello7 | 0:95b1edb8d258 | 163 | //Thread::signal_set(0x1); |
jcabello7 | 0:95b1edb8d258 | 164 | } |
jcabello7 | 0:95b1edb8d258 | 165 | } |