Communicate to bluetooth devices or wifi per at-commands
at.cpp@2:b8d28a4f6b02, 2018-08-19 (annotated)
- Committer:
- rainerraul
- Date:
- Sun Aug 19 07:51:33 2018 +0000
- Revision:
- 2:b8d28a4f6b02
- Parent:
- 1:ce7fb335aa1b
- Child:
- 3:cdabd56aec53
atterm for at command devices
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rainerraul | 0:413f3c13a00a | 1 | |
rainerraul | 0:413f3c13a00a | 2 | #include "mbed.h" |
rainerraul | 0:413f3c13a00a | 3 | #include "at.h" |
rainerraul | 0:413f3c13a00a | 4 | |
rainerraul | 0:413f3c13a00a | 5 | #define TX PA_9 |
rainerraul | 0:413f3c13a00a | 6 | #define RX PA_10 |
rainerraul | 0:413f3c13a00a | 7 | |
rainerraul | 0:413f3c13a00a | 8 | #define TX1 PC_6 |
rainerraul | 0:413f3c13a00a | 9 | #define RX1 PC_7 |
rainerraul | 0:413f3c13a00a | 10 | |
rainerraul | 0:413f3c13a00a | 11 | Serial atserial0(TX, RX); //UART1 |
rainerraul | 0:413f3c13a00a | 12 | Serial atserial1(TX1, RX1); //UART6 |
rainerraul | 0:413f3c13a00a | 13 | |
rainerraul | 0:413f3c13a00a | 14 | atterm at0; |
rainerraul | 0:413f3c13a00a | 15 | atterm at1; |
rainerraul | 1:ce7fb335aa1b | 16 | Timer t; |
rainerraul | 0:413f3c13a00a | 17 | |
rainerraul | 0:413f3c13a00a | 18 | void CallBack0() |
rainerraul | 0:413f3c13a00a | 19 | { |
rainerraul | 0:413f3c13a00a | 20 | at0.sign = atserial0.getc(); |
rainerraul | 0:413f3c13a00a | 21 | |
rainerraul | 0:413f3c13a00a | 22 | |
rainerraul | 0:413f3c13a00a | 23 | if(at0.sign != '\r') { //with no linefeed and carriage return, string is written in buffer |
rainerraul | 0:413f3c13a00a | 24 | at0.buffer[at0.a] = at0.sign; |
rainerraul | 0:413f3c13a00a | 25 | at0.a ++; |
rainerraul | 2:b8d28a4f6b02 | 26 | } else { //if cr and lf were received , the content of buffer is send to atcommand device |
rainerraul | 0:413f3c13a00a | 27 | |
rainerraul | 0:413f3c13a00a | 28 | if(at0.off == 0) { |
rainerraul | 0:413f3c13a00a | 29 | atserial0.printf("Read: %s\r\n", at0.buffer); // via printf formatted as string for debug use |
rainerraul | 0:413f3c13a00a | 30 | } |
rainerraul | 0:413f3c13a00a | 31 | |
rainerraul | 0:413f3c13a00a | 32 | } |
rainerraul | 0:413f3c13a00a | 33 | } |
rainerraul | 0:413f3c13a00a | 34 | |
rainerraul | 0:413f3c13a00a | 35 | void CallBack1() |
rainerraul | 0:413f3c13a00a | 36 | { |
rainerraul | 0:413f3c13a00a | 37 | at1.sign = atserial1.getc(); |
rainerraul | 0:413f3c13a00a | 38 | |
rainerraul | 0:413f3c13a00a | 39 | |
rainerraul | 0:413f3c13a00a | 40 | if(at1.sign != '\r') { //with no linefeed and carriage return, string is written in buffer |
rainerraul | 0:413f3c13a00a | 41 | at1.buffer[at1.a] = at1.sign; |
rainerraul | 0:413f3c13a00a | 42 | at1.a ++; |
rainerraul | 2:b8d28a4f6b02 | 43 | } else { //if cr and lf were received, the content of buffer is send to atcommand device |
rainerraul | 0:413f3c13a00a | 44 | |
rainerraul | 0:413f3c13a00a | 45 | if(at1.off == 0) { |
rainerraul | 0:413f3c13a00a | 46 | atserial1.printf("Read: %s\r\n", at1.buffer); // via printf formatted as string for debug use |
rainerraul | 0:413f3c13a00a | 47 | } |
rainerraul | 0:413f3c13a00a | 48 | |
rainerraul | 0:413f3c13a00a | 49 | } |
rainerraul | 0:413f3c13a00a | 50 | } |
rainerraul | 0:413f3c13a00a | 51 | |
rainerraul | 0:413f3c13a00a | 52 | void atterm::debug_off(bool l) |
rainerraul | 0:413f3c13a00a | 53 | { |
rainerraul | 0:413f3c13a00a | 54 | if(l) atterm::off = 1; |
rainerraul | 0:413f3c13a00a | 55 | if(!l) atterm::off = 0; |
rainerraul | 0:413f3c13a00a | 56 | } |
rainerraul | 0:413f3c13a00a | 57 | |
rainerraul | 0:413f3c13a00a | 58 | void atterm::at_send(char *format, char *buf) |
rainerraul | 0:413f3c13a00a | 59 | { |
rainerraul | 0:413f3c13a00a | 60 | atserial0.printf(format, buf); |
rainerraul | 0:413f3c13a00a | 61 | } |
rainerraul | 0:413f3c13a00a | 62 | |
rainerraul | 0:413f3c13a00a | 63 | void atterm::clear() |
rainerraul | 0:413f3c13a00a | 64 | { |
rainerraul | 0:413f3c13a00a | 65 | memset(atterm::buffer, 0, sizeof(atterm::buffer)); //set buffer to 0, for next clean read |
rainerraul | 0:413f3c13a00a | 66 | atterm::a = 0; //index pointer to zero |
rainerraul | 0:413f3c13a00a | 67 | |
rainerraul | 0:413f3c13a00a | 68 | } |
rainerraul | 0:413f3c13a00a | 69 | |
rainerraul | 1:ce7fb335aa1b | 70 | void atterm::device_init(unsigned long baud, _IRQ interrupt) |
rainerraul | 0:413f3c13a00a | 71 | { |
rainerraul | 0:413f3c13a00a | 72 | atterm::debug_off(1); |
rainerraul | 2:b8d28a4f6b02 | 73 | if(interrupt == ON) { |
rainerraul | 2:b8d28a4f6b02 | 74 | atserial0.attach(&CallBack0); |
rainerraul | 2:b8d28a4f6b02 | 75 | } |
rainerraul | 0:413f3c13a00a | 76 | atserial0.baud(baud); |
rainerraul | 2:b8d28a4f6b02 | 77 | |
rainerraul | 0:413f3c13a00a | 78 | } |
rainerraul | 0:413f3c13a00a | 79 | |
rainerraul | 0:413f3c13a00a | 80 | void atterm::at_send1(char *format, char *buf) |
rainerraul | 0:413f3c13a00a | 81 | { |
rainerraul | 0:413f3c13a00a | 82 | atserial1.printf(format, buf); |
rainerraul | 0:413f3c13a00a | 83 | } |
rainerraul | 0:413f3c13a00a | 84 | |
rainerraul | 0:413f3c13a00a | 85 | |
rainerraul | 0:413f3c13a00a | 86 | |
rainerraul | 1:ce7fb335aa1b | 87 | void atterm::device_init1(unsigned long baud, _IRQ interrupt) |
rainerraul | 0:413f3c13a00a | 88 | { |
rainerraul | 0:413f3c13a00a | 89 | atterm::debug_off(1); |
rainerraul | 2:b8d28a4f6b02 | 90 | if(interrupt == ON) { |
rainerraul | 2:b8d28a4f6b02 | 91 | atserial1.attach(&CallBack1); |
rainerraul | 2:b8d28a4f6b02 | 92 | } |
rainerraul | 0:413f3c13a00a | 93 | atserial1.baud(baud); |
rainerraul | 1:ce7fb335aa1b | 94 | at1.at_send1("AT\r\n", ""); |
rainerraul | 1:ce7fb335aa1b | 95 | |
rainerraul | 1:ce7fb335aa1b | 96 | } |
rainerraul | 1:ce7fb335aa1b | 97 | |
rainerraul | 1:ce7fb335aa1b | 98 | char *atterm::getAnswer(uint32_t timeout) |
rainerraul | 1:ce7fb335aa1b | 99 | { |
rainerraul | 1:ce7fb335aa1b | 100 | int b = 0; |
rainerraul | 1:ce7fb335aa1b | 101 | int t_time = 0; |
rainerraul | 1:ce7fb335aa1b | 102 | char sign, sign1; |
rainerraul | 1:ce7fb335aa1b | 103 | |
rainerraul | 1:ce7fb335aa1b | 104 | memset(read_timed_buffer, 0, sizeof(read_timed_buffer)); |
rainerraul | 1:ce7fb335aa1b | 105 | |
rainerraul | 1:ce7fb335aa1b | 106 | t.start(); |
rainerraul | 1:ce7fb335aa1b | 107 | t_time = t.read(); |
rainerraul | 0:413f3c13a00a | 108 | |
rainerraul | 1:ce7fb335aa1b | 109 | while(1) { |
rainerraul | 1:ce7fb335aa1b | 110 | if(atserial0.readable()) { |
rainerraul | 1:ce7fb335aa1b | 111 | sign = atserial0.getc(); |
rainerraul | 1:ce7fb335aa1b | 112 | read_timed_buffer[b] = sign; |
rainerraul | 1:ce7fb335aa1b | 113 | b ++; |
rainerraul | 1:ce7fb335aa1b | 114 | } |
rainerraul | 0:413f3c13a00a | 115 | |
rainerraul | 1:ce7fb335aa1b | 116 | else if(atserial1.readable()) { |
rainerraul | 1:ce7fb335aa1b | 117 | sign1 = atserial1.getc(); |
rainerraul | 1:ce7fb335aa1b | 118 | read_timed_buffer[b] = sign1; |
rainerraul | 1:ce7fb335aa1b | 119 | b ++; |
rainerraul | 1:ce7fb335aa1b | 120 | } |
rainerraul | 1:ce7fb335aa1b | 121 | if((t.read() - t_time) > timeout) { |
rainerraul | 1:ce7fb335aa1b | 122 | t.stop(); |
rainerraul | 1:ce7fb335aa1b | 123 | t.reset(); |
rainerraul | 1:ce7fb335aa1b | 124 | break; |
rainerraul | 1:ce7fb335aa1b | 125 | |
rainerraul | 1:ce7fb335aa1b | 126 | } |
rainerraul | 1:ce7fb335aa1b | 127 | |
rainerraul | 1:ce7fb335aa1b | 128 | } |
rainerraul | 1:ce7fb335aa1b | 129 | return read_timed_buffer; |
rainerraul | 0:413f3c13a00a | 130 | } |