pengfei wu
/
ESP8266Program
For test
ArduinoSerial.cpp@19:6c8b29734511, 2015-02-04 (annotated)
- Committer:
- shennongmin
- Date:
- Wed Feb 04 09:28:28 2015 +0000
- Revision:
- 19:6c8b29734511
- Parent:
- 18:37254b357abd
- Child:
- 20:43451f36281e
compile okay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shennongmin | 10:9d4ec0359a5c | 1 | #include "ArduinoSerial.h" |
shennongmin | 10:9d4ec0359a5c | 2 | |
shennongmin | 19:6c8b29734511 | 3 | ArduinoSerialLinkedNode ArduinoSerial::ms_list_head = {NULL, NULL}; |
shennongmin | 19:6c8b29734511 | 4 | unsigned int ArduinoSerial::ms_instance_counter = 0; |
shennongmin | 10:9d4ec0359a5c | 5 | |
shennongmin | 18:37254b357abd | 6 | void ArduinoSerial::uartIrqCallback(void) { |
shennongmin | 19:6c8b29734511 | 7 | ArduinoSerialLinkedNode *p; |
shennongmin | 19:6c8b29734511 | 8 | for (p = ms_list_head.next; p != &ms_list_head; p = p->next) { |
shennongmin | 11:7bd1b2a67b1a | 9 | if (p->data != NULL) { |
shennongmin | 11:7bd1b2a67b1a | 10 | while(p->data->readable()) { |
shennongmin | 11:7bd1b2a67b1a | 11 | p->data->write_char(p->data->getc()); |
shennongmin | 10:9d4ec0359a5c | 12 | } |
shennongmin | 10:9d4ec0359a5c | 13 | } |
shennongmin | 10:9d4ec0359a5c | 14 | } |
shennongmin | 10:9d4ec0359a5c | 15 | } |
shennongmin | 10:9d4ec0359a5c | 16 | |
shennongmin | 10:9d4ec0359a5c | 17 | ArduinoSerial::ArduinoSerial(PinName tx, PinName rx):Serial(tx, rx) { |
shennongmin | 12:4e4e72f18047 | 18 | |
shennongmin | 19:6c8b29734511 | 19 | ms_instance_counter++; |
shennongmin | 19:6c8b29734511 | 20 | if (ms_instance_counter == 1) { |
shennongmin | 19:6c8b29734511 | 21 | initHeadNode(&ms_list_head); |
shennongmin | 11:7bd1b2a67b1a | 22 | } |
shennongmin | 11:7bd1b2a67b1a | 23 | |
shennongmin | 19:6c8b29734511 | 24 | if (addNode(&ms_list_head, this) != NULL) { |
shennongmin | 18:37254b357abd | 25 | this->attach(&uartIrqCallback); |
shennongmin | 10:9d4ec0359a5c | 26 | } else { |
shennongmin | 10:9d4ec0359a5c | 27 | } |
shennongmin | 10:9d4ec0359a5c | 28 | } |
shennongmin | 15:f5682fb5b315 | 29 | |
shennongmin | 10:9d4ec0359a5c | 30 | ArduinoSerial::~ArduinoSerial(void) { |
shennongmin | 19:6c8b29734511 | 31 | ms_instance_counter--; |
shennongmin | 19:6c8b29734511 | 32 | if (delNode(&ms_list_head, this) != NULL) { |
shennongmin | 11:7bd1b2a67b1a | 33 | this->attach(NULL); |
shennongmin | 11:7bd1b2a67b1a | 34 | } else { |
shennongmin | 10:9d4ec0359a5c | 35 | } |
shennongmin | 10:9d4ec0359a5c | 36 | } |
shennongmin | 10:9d4ec0359a5c | 37 | |
shennongmin | 10:9d4ec0359a5c | 38 | void ArduinoSerial::begin(int baud_rate) { |
shennongmin | 10:9d4ec0359a5c | 39 | baud(baud_rate); |
shennongmin | 10:9d4ec0359a5c | 40 | flush(); |
shennongmin | 10:9d4ec0359a5c | 41 | } |
shennongmin | 10:9d4ec0359a5c | 42 | |
shennongmin | 10:9d4ec0359a5c | 43 | int ArduinoSerial::available(void) { |
shennongmin | 19:6c8b29734511 | 44 | return (unsigned int)(ARDUINOSERIAL_BUFFER_SIZE + m_rx_buffer.head - m_rx_buffer.tail) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 45 | } |
shennongmin | 10:9d4ec0359a5c | 46 | |
shennongmin | 10:9d4ec0359a5c | 47 | /* 清空接收缓冲区的数据 */ |
shennongmin | 10:9d4ec0359a5c | 48 | void ArduinoSerial::flush(void) |
shennongmin | 10:9d4ec0359a5c | 49 | { |
shennongmin | 19:6c8b29734511 | 50 | memset(&m_rx_buffer, 0, sizeof(m_rx_buffer)); |
shennongmin | 10:9d4ec0359a5c | 51 | } |
shennongmin | 10:9d4ec0359a5c | 52 | |
shennongmin | 10:9d4ec0359a5c | 53 | char ArduinoSerial::read_char(void) { |
shennongmin | 10:9d4ec0359a5c | 54 | // if the head isn't ahead of the tail, we don't have any characters |
shennongmin | 19:6c8b29734511 | 55 | if (m_rx_buffer.head == m_rx_buffer.tail) { |
shennongmin | 10:9d4ec0359a5c | 56 | return (char)-1; |
shennongmin | 10:9d4ec0359a5c | 57 | } else { |
shennongmin | 19:6c8b29734511 | 58 | unsigned char c = m_rx_buffer.buffer[m_rx_buffer.tail]; |
shennongmin | 19:6c8b29734511 | 59 | m_rx_buffer.tail = (unsigned int)(m_rx_buffer.tail + 1) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 60 | return c; |
shennongmin | 10:9d4ec0359a5c | 61 | } |
shennongmin | 10:9d4ec0359a5c | 62 | } |
shennongmin | 10:9d4ec0359a5c | 63 | |
shennongmin | 10:9d4ec0359a5c | 64 | void ArduinoSerial::setTimeout(unsigned long millisecond) { |
shennongmin | 19:6c8b29734511 | 65 | m_find_timeout = millisecond; |
shennongmin | 10:9d4ec0359a5c | 66 | } |
shennongmin | 10:9d4ec0359a5c | 67 | |
shennongmin | 10:9d4ec0359a5c | 68 | /* 在超时之前, 如果在串口接收数据缓冲区中找到了定长字符串 str 就返回 true, 如果超时, 返回 false */ |
shennongmin | 10:9d4ec0359a5c | 69 | bool ArduinoSerial::find(const char *str) { |
shennongmin | 10:9d4ec0359a5c | 70 | bool ret = false; |
shennongmin | 10:9d4ec0359a5c | 71 | String data; |
shennongmin | 10:9d4ec0359a5c | 72 | char c; |
shennongmin | 10:9d4ec0359a5c | 73 | unsigned long i; |
shennongmin | 19:6c8b29734511 | 74 | for (i = 0; i < m_find_timeout; i++) { |
shennongmin | 10:9d4ec0359a5c | 75 | while(available() > 0) { |
shennongmin | 10:9d4ec0359a5c | 76 | c = read_char(); |
shennongmin | 10:9d4ec0359a5c | 77 | data += c; |
shennongmin | 10:9d4ec0359a5c | 78 | } |
shennongmin | 10:9d4ec0359a5c | 79 | if (data.indexOf(String(str)) != -1) { |
shennongmin | 10:9d4ec0359a5c | 80 | ret = true; |
shennongmin | 10:9d4ec0359a5c | 81 | break; |
shennongmin | 10:9d4ec0359a5c | 82 | } |
shennongmin | 10:9d4ec0359a5c | 83 | delay(1); |
shennongmin | 10:9d4ec0359a5c | 84 | } |
shennongmin | 10:9d4ec0359a5c | 85 | return ret; |
shennongmin | 10:9d4ec0359a5c | 86 | } |
shennongmin | 10:9d4ec0359a5c | 87 | |
shennongmin | 10:9d4ec0359a5c | 88 | size_t ArduinoSerial::write(uint8_t data) { |
shennongmin | 10:9d4ec0359a5c | 89 | putc(data); |
shennongmin | 10:9d4ec0359a5c | 90 | return 1; |
shennongmin | 10:9d4ec0359a5c | 91 | } |
shennongmin | 10:9d4ec0359a5c | 92 | |
shennongmin | 10:9d4ec0359a5c | 93 | void ArduinoSerial::write_char(unsigned char c) { |
shennongmin | 19:6c8b29734511 | 94 | int i = (unsigned int)(m_rx_buffer.head + 1) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 95 | |
shennongmin | 10:9d4ec0359a5c | 96 | // if we should be storing the received character into the location |
shennongmin | 10:9d4ec0359a5c | 97 | // just before the tail (meaning that the head would advance to the |
shennongmin | 10:9d4ec0359a5c | 98 | // current location of the tail), we're about to overflow the buffer |
shennongmin | 10:9d4ec0359a5c | 99 | // and so we don't write the character or advance the head. |
shennongmin | 19:6c8b29734511 | 100 | if (i != m_rx_buffer.tail) { |
shennongmin | 19:6c8b29734511 | 101 | m_rx_buffer.buffer[m_rx_buffer.head] = c; |
shennongmin | 19:6c8b29734511 | 102 | m_rx_buffer.head = i; |
shennongmin | 10:9d4ec0359a5c | 103 | } |
shennongmin | 10:9d4ec0359a5c | 104 | } |
shennongmin | 11:7bd1b2a67b1a | 105 | |
shennongmin | 19:6c8b29734511 | 106 | ArduinoSerialLinkedNode *ArduinoSerial::initHeadNode(ArduinoSerialLinkedNode *head) { |
shennongmin | 11:7bd1b2a67b1a | 107 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 108 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 109 | } |
shennongmin | 11:7bd1b2a67b1a | 110 | head->data = NULL; |
shennongmin | 11:7bd1b2a67b1a | 111 | head->next = head; |
shennongmin | 11:7bd1b2a67b1a | 112 | return head; |
shennongmin | 11:7bd1b2a67b1a | 113 | } |
shennongmin | 11:7bd1b2a67b1a | 114 | |
shennongmin | 19:6c8b29734511 | 115 | ArduinoSerialLinkedNode *ArduinoSerial::addNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 116 | ArduinoSerialLinkedNode *p; |
shennongmin | 19:6c8b29734511 | 117 | ArduinoSerialLinkedNode *node = (ArduinoSerialLinkedNode *)malloc(sizeof(ArduinoSerialLinkedNode)); |
shennongmin | 11:7bd1b2a67b1a | 118 | if (node == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 119 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 120 | } |
shennongmin | 11:7bd1b2a67b1a | 121 | /* 设置该节点的值 */ |
shennongmin | 11:7bd1b2a67b1a | 122 | node->data = data; |
shennongmin | 11:7bd1b2a67b1a | 123 | node->next = head; |
shennongmin | 11:7bd1b2a67b1a | 124 | |
shennongmin | 11:7bd1b2a67b1a | 125 | /* 在末尾插入该节点 */ |
shennongmin | 11:7bd1b2a67b1a | 126 | for(p = head; p->next != head; p = p->next); |
shennongmin | 11:7bd1b2a67b1a | 127 | p->next = node; |
shennongmin | 11:7bd1b2a67b1a | 128 | |
shennongmin | 11:7bd1b2a67b1a | 129 | return head; |
shennongmin | 11:7bd1b2a67b1a | 130 | } |
shennongmin | 19:6c8b29734511 | 131 | ArduinoSerialLinkedNode *ArduinoSerial::delNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 132 | ArduinoSerialLinkedNode *p; |
shennongmin | 19:6c8b29734511 | 133 | ArduinoSerialLinkedNode *prev; |
shennongmin | 11:7bd1b2a67b1a | 134 | |
shennongmin | 11:7bd1b2a67b1a | 135 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 136 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 137 | } |
shennongmin | 11:7bd1b2a67b1a | 138 | |
shennongmin | 11:7bd1b2a67b1a | 139 | prev = head, p = head->next; |
shennongmin | 11:7bd1b2a67b1a | 140 | while(p != head) { |
shennongmin | 11:7bd1b2a67b1a | 141 | if (p->data == data) { |
shennongmin | 11:7bd1b2a67b1a | 142 | prev->next = p->next; |
shennongmin | 11:7bd1b2a67b1a | 143 | free(p); |
shennongmin | 11:7bd1b2a67b1a | 144 | p = prev->next; |
shennongmin | 11:7bd1b2a67b1a | 145 | } else { |
shennongmin | 11:7bd1b2a67b1a | 146 | prev = p; |
shennongmin | 11:7bd1b2a67b1a | 147 | p = p->next; |
shennongmin | 11:7bd1b2a67b1a | 148 | } |
shennongmin | 11:7bd1b2a67b1a | 149 | } |
shennongmin | 11:7bd1b2a67b1a | 150 | return head; |
shennongmin | 11:7bd1b2a67b1a | 151 | } |
shennongmin | 19:6c8b29734511 | 152 | ArduinoSerialLinkedNode *ArduinoSerial::findNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 153 | ArduinoSerialLinkedNode *p; |
shennongmin | 11:7bd1b2a67b1a | 154 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 155 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 156 | } |
shennongmin | 11:7bd1b2a67b1a | 157 | |
shennongmin | 11:7bd1b2a67b1a | 158 | for (p = head->next; p != head; p = p->next) { |
shennongmin | 11:7bd1b2a67b1a | 159 | if (p->data == data) { |
shennongmin | 11:7bd1b2a67b1a | 160 | return p; |
shennongmin | 11:7bd1b2a67b1a | 161 | } |
shennongmin | 11:7bd1b2a67b1a | 162 | } |
shennongmin | 11:7bd1b2a67b1a | 163 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 164 | } |