pengfei wu
/
ESP8266Program
For test
Diff: ESP8266.cpp
- Revision:
- 1:44f2f0e5d685
- Parent:
- 0:7868fa4f17d0
- Child:
- 4:962bf18523f4
diff -r 7868fa4f17d0 -r 44f2f0e5d685 ESP8266.cpp --- a/ESP8266.cpp Fri Jan 30 11:02:55 2015 +0000 +++ b/ESP8266.cpp Fri Jan 30 11:27:31 2015 +0000 @@ -1,4 +1,66 @@ #include "ESP8266.h" +static Serial uart(p28, p27); +static ring_buffer rx_buffer = { { 0 }, 0, 0}; + +static void write_char(unsigned char c) +{ + int i = (unsigned int)(rx_buffer.head + 1) % SERIAL_BUFFER_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != rx_buffer.tail) { + rx_buffer.buffer[rx_buffer.head] = c; + rx_buffer.head = i; + } +} + +static void uart_irq_callback() +{ + // Note: you need to actually read from the serial to clear the RX interrupt + while(uart.readable()) { + write_char(uart.getc()); + } +} + +int read_char(void) +{ + // if the head isn't ahead of the tail, we don't have any characters + if (rx_buffer.head == rx_buffer.tail) { + return -1; + } else { + unsigned char c = rx_buffer.buffer[rx_buffer.tail]; + rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % SERIAL_BUFFER_SIZE; + return c; + } +} + +int available(void) +{ + return (unsigned int)(SERIAL_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % SERIAL_BUFFER_SIZE; +} + +/** + * 使用uart.printf或者uart.puts方法时, 当发送数据大于14个字符时, 就会出现异常(串口不断的输出不可显示的字符) + * 因此, 相关字符串的打印使用下面的函数来完成, 虽然很简陋, 但是很管用! + */ +void uart_send_string(const char *str) +{ + if (!str) + return; + + while(*str) + uart.putc(*str++); +} + +void ESP8266_init(void) +{ + uart.attach(&uart_irq_callback); +} + + +