Communicate to bluetooth devices or wifi per at-commands
at.cpp@5:31306be5fde6, 2018-08-26 (annotated)
- Committer:
- rainerraul
- Date:
- Sun Aug 26 20:16:16 2018 +0000
- Revision:
- 5:31306be5fde6
- Parent:
- 4:96aa57261092
- Child:
- 6:194ceedbb7c3
correct errors
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 | 4:96aa57261092 | 13 | Serial terminal(USBTX, USBRX); //UART Terminal |
rainerraul | 0:413f3c13a00a | 14 | |
rainerraul | 0:413f3c13a00a | 15 | atterm at0; |
rainerraul | 0:413f3c13a00a | 16 | atterm at1; |
rainerraul | 4:96aa57261092 | 17 | atterm pc; |
rainerraul | 4:96aa57261092 | 18 | |
rainerraul | 1:ce7fb335aa1b | 19 | Timer t; |
rainerraul | 0:413f3c13a00a | 20 | |
rainerraul | 0:413f3c13a00a | 21 | void CallBack0() |
rainerraul | 0:413f3c13a00a | 22 | { |
rainerraul | 0:413f3c13a00a | 23 | at0.sign = atserial0.getc(); |
rainerraul | 0:413f3c13a00a | 24 | |
rainerraul | 0:413f3c13a00a | 25 | |
rainerraul | 0:413f3c13a00a | 26 | if(at0.sign != '\r') { //with no linefeed and carriage return, string is written in buffer |
rainerraul | 0:413f3c13a00a | 27 | at0.buffer[at0.a] = at0.sign; |
rainerraul | 0:413f3c13a00a | 28 | at0.a ++; |
rainerraul | 2:b8d28a4f6b02 | 29 | } else { //if cr and lf were received , the content of buffer is send to atcommand device |
rainerraul | 0:413f3c13a00a | 30 | |
rainerraul | 0:413f3c13a00a | 31 | if(at0.off == 0) { |
rainerraul | 0:413f3c13a00a | 32 | atserial0.printf("Read: %s\r\n", at0.buffer); // via printf formatted as string for debug use |
rainerraul | 0:413f3c13a00a | 33 | } |
rainerraul | 0:413f3c13a00a | 34 | |
rainerraul | 0:413f3c13a00a | 35 | } |
rainerraul | 0:413f3c13a00a | 36 | } |
rainerraul | 0:413f3c13a00a | 37 | |
rainerraul | 0:413f3c13a00a | 38 | void CallBack1() |
rainerraul | 0:413f3c13a00a | 39 | { |
rainerraul | 0:413f3c13a00a | 40 | at1.sign = atserial1.getc(); |
rainerraul | 0:413f3c13a00a | 41 | |
rainerraul | 0:413f3c13a00a | 42 | |
rainerraul | 0:413f3c13a00a | 43 | if(at1.sign != '\r') { //with no linefeed and carriage return, string is written in buffer |
rainerraul | 0:413f3c13a00a | 44 | at1.buffer[at1.a] = at1.sign; |
rainerraul | 0:413f3c13a00a | 45 | at1.a ++; |
rainerraul | 2:b8d28a4f6b02 | 46 | } else { //if cr and lf were received, the content of buffer is send to atcommand device |
rainerraul | 0:413f3c13a00a | 47 | |
rainerraul | 0:413f3c13a00a | 48 | if(at1.off == 0) { |
rainerraul | 0:413f3c13a00a | 49 | atserial1.printf("Read: %s\r\n", at1.buffer); // via printf formatted as string for debug use |
rainerraul | 0:413f3c13a00a | 50 | } |
rainerraul | 0:413f3c13a00a | 51 | |
rainerraul | 0:413f3c13a00a | 52 | } |
rainerraul | 0:413f3c13a00a | 53 | } |
rainerraul | 0:413f3c13a00a | 54 | |
rainerraul | 4:96aa57261092 | 55 | void CallBack2() |
rainerraul | 4:96aa57261092 | 56 | { |
rainerraul | 4:96aa57261092 | 57 | pc.sign = terminal.getc(); |
rainerraul | 4:96aa57261092 | 58 | |
rainerraul | 4:96aa57261092 | 59 | |
rainerraul | 4:96aa57261092 | 60 | if(pc.sign != '\r') { //with no linefeed and carriage return, string is written in buffer |
rainerraul | 5:31306be5fde6 | 61 | pc.buffer[pc.a] = pc.sign; |
rainerraul | 4:96aa57261092 | 62 | pc.a ++; |
rainerraul | 4:96aa57261092 | 63 | } else { //if cr and lf were received, the content of buffer is send to atcommand device |
rainerraul | 4:96aa57261092 | 64 | |
rainerraul | 4:96aa57261092 | 65 | if(at1.off == 0) { |
rainerraul | 4:96aa57261092 | 66 | terminal.printf("Read: %s\r\n", pc.buffer); // via printf formatted as string for debug use |
rainerraul | 4:96aa57261092 | 67 | } |
rainerraul | 4:96aa57261092 | 68 | |
rainerraul | 4:96aa57261092 | 69 | } |
rainerraul | 4:96aa57261092 | 70 | } |
rainerraul | 4:96aa57261092 | 71 | |
rainerraul | 0:413f3c13a00a | 72 | void atterm::debug_off(bool l) |
rainerraul | 0:413f3c13a00a | 73 | { |
rainerraul | 0:413f3c13a00a | 74 | if(l) atterm::off = 1; |
rainerraul | 0:413f3c13a00a | 75 | if(!l) atterm::off = 0; |
rainerraul | 0:413f3c13a00a | 76 | } |
rainerraul | 0:413f3c13a00a | 77 | |
rainerraul | 0:413f3c13a00a | 78 | void atterm::at_send(char *format, char *buf) |
rainerraul | 0:413f3c13a00a | 79 | { |
rainerraul | 0:413f3c13a00a | 80 | atserial0.printf(format, buf); |
rainerraul | 0:413f3c13a00a | 81 | } |
rainerraul | 0:413f3c13a00a | 82 | |
rainerraul | 0:413f3c13a00a | 83 | void atterm::clear() |
rainerraul | 0:413f3c13a00a | 84 | { |
rainerraul | 0:413f3c13a00a | 85 | memset(atterm::buffer, 0, sizeof(atterm::buffer)); //set buffer to 0, for next clean read |
rainerraul | 0:413f3c13a00a | 86 | atterm::a = 0; //index pointer to zero |
rainerraul | 0:413f3c13a00a | 87 | |
rainerraul | 0:413f3c13a00a | 88 | } |
rainerraul | 0:413f3c13a00a | 89 | |
rainerraul | 1:ce7fb335aa1b | 90 | void atterm::device_init(unsigned long baud, _IRQ interrupt) |
rainerraul | 0:413f3c13a00a | 91 | { |
rainerraul | 0:413f3c13a00a | 92 | atterm::debug_off(1); |
rainerraul | 2:b8d28a4f6b02 | 93 | if(interrupt == ON) { |
rainerraul | 2:b8d28a4f6b02 | 94 | atserial0.attach(&CallBack0); |
rainerraul | 2:b8d28a4f6b02 | 95 | } |
rainerraul | 0:413f3c13a00a | 96 | atserial0.baud(baud); |
rainerraul | 2:b8d28a4f6b02 | 97 | |
rainerraul | 0:413f3c13a00a | 98 | } |
rainerraul | 0:413f3c13a00a | 99 | |
rainerraul | 0:413f3c13a00a | 100 | void atterm::at_send1(char *format, char *buf) |
rainerraul | 0:413f3c13a00a | 101 | { |
rainerraul | 0:413f3c13a00a | 102 | atserial1.printf(format, buf); |
rainerraul | 0:413f3c13a00a | 103 | } |
rainerraul | 0:413f3c13a00a | 104 | |
rainerraul | 0:413f3c13a00a | 105 | |
rainerraul | 0:413f3c13a00a | 106 | |
rainerraul | 1:ce7fb335aa1b | 107 | void atterm::device_init1(unsigned long baud, _IRQ interrupt) |
rainerraul | 0:413f3c13a00a | 108 | { |
rainerraul | 0:413f3c13a00a | 109 | atterm::debug_off(1); |
rainerraul | 2:b8d28a4f6b02 | 110 | if(interrupt == ON) { |
rainerraul | 2:b8d28a4f6b02 | 111 | atserial1.attach(&CallBack1); |
rainerraul | 2:b8d28a4f6b02 | 112 | } |
rainerraul | 0:413f3c13a00a | 113 | atserial1.baud(baud); |
rainerraul | 1:ce7fb335aa1b | 114 | at1.at_send1("AT\r\n", ""); |
rainerraul | 1:ce7fb335aa1b | 115 | |
rainerraul | 1:ce7fb335aa1b | 116 | } |
rainerraul | 1:ce7fb335aa1b | 117 | |
rainerraul | 4:96aa57261092 | 118 | void atterm::pc_send(char *format, char *buf) |
rainerraul | 4:96aa57261092 | 119 | { |
rainerraul | 4:96aa57261092 | 120 | terminal.printf(format, buf); |
rainerraul | 4:96aa57261092 | 121 | } |
rainerraul | 4:96aa57261092 | 122 | |
rainerraul | 5:31306be5fde6 | 123 | void atterm::terminal_init(unsigned long baud, _IRQ interrupt) |
rainerraul | 4:96aa57261092 | 124 | { |
rainerraul | 4:96aa57261092 | 125 | atterm::debug_off(1); |
rainerraul | 4:96aa57261092 | 126 | if(interrupt == ON) { |
rainerraul | 4:96aa57261092 | 127 | terminal.attach(&CallBack2); |
rainerraul | 4:96aa57261092 | 128 | } |
rainerraul | 4:96aa57261092 | 129 | terminal.baud(baud); |
rainerraul | 4:96aa57261092 | 130 | pc.pc_send("*****Terminal*******\r\n", ""); |
rainerraul | 4:96aa57261092 | 131 | |
rainerraul | 4:96aa57261092 | 132 | } |
rainerraul | 4:96aa57261092 | 133 | |
rainerraul | 4:96aa57261092 | 134 | |
rainerraul | 1:ce7fb335aa1b | 135 | char *atterm::getAnswer(uint32_t timeout) |
rainerraul | 1:ce7fb335aa1b | 136 | { |
rainerraul | 5:31306be5fde6 | 137 | int b = 0, b1 = 0, b2 = 0; |
rainerraul | 3:cdabd56aec53 | 138 | uint32_t t_time = 0; |
rainerraul | 4:96aa57261092 | 139 | char sign, sign1, sign2; |
rainerraul | 1:ce7fb335aa1b | 140 | |
rainerraul | 1:ce7fb335aa1b | 141 | memset(read_timed_buffer, 0, sizeof(read_timed_buffer)); |
rainerraul | 1:ce7fb335aa1b | 142 | |
rainerraul | 1:ce7fb335aa1b | 143 | t.start(); |
rainerraul | 3:cdabd56aec53 | 144 | t_time = t.read_ms(); |
rainerraul | 0:413f3c13a00a | 145 | |
rainerraul | 1:ce7fb335aa1b | 146 | while(1) { |
rainerraul | 1:ce7fb335aa1b | 147 | if(atserial0.readable()) { |
rainerraul | 1:ce7fb335aa1b | 148 | sign = atserial0.getc(); |
rainerraul | 1:ce7fb335aa1b | 149 | read_timed_buffer[b] = sign; |
rainerraul | 1:ce7fb335aa1b | 150 | b ++; |
rainerraul | 1:ce7fb335aa1b | 151 | } |
rainerraul | 0:413f3c13a00a | 152 | |
rainerraul | 1:ce7fb335aa1b | 153 | else if(atserial1.readable()) { |
rainerraul | 1:ce7fb335aa1b | 154 | sign1 = atserial1.getc(); |
rainerraul | 5:31306be5fde6 | 155 | read_timed_buffer[b1] = sign1; |
rainerraul | 5:31306be5fde6 | 156 | b1 ++; |
rainerraul | 1:ce7fb335aa1b | 157 | } |
rainerraul | 4:96aa57261092 | 158 | |
rainerraul | 4:96aa57261092 | 159 | else if(terminal.readable()) { |
rainerraul | 4:96aa57261092 | 160 | sign2 = terminal.getc(); |
rainerraul | 5:31306be5fde6 | 161 | read_timed_buffer[b2] = sign2; |
rainerraul | 5:31306be5fde6 | 162 | b2 ++; |
rainerraul | 4:96aa57261092 | 163 | } |
rainerraul | 4:96aa57261092 | 164 | |
rainerraul | 3:cdabd56aec53 | 165 | if((t.read_ms() - t_time) > timeout) { |
rainerraul | 1:ce7fb335aa1b | 166 | t.stop(); |
rainerraul | 1:ce7fb335aa1b | 167 | t.reset(); |
rainerraul | 1:ce7fb335aa1b | 168 | break; |
rainerraul | 1:ce7fb335aa1b | 169 | |
rainerraul | 1:ce7fb335aa1b | 170 | } |
rainerraul | 1:ce7fb335aa1b | 171 | |
rainerraul | 1:ce7fb335aa1b | 172 | } |
rainerraul | 1:ce7fb335aa1b | 173 | return read_timed_buffer; |
rainerraul | 0:413f3c13a00a | 174 | } |