pengfei wu
/
ESP8266Program
For test
ArduinoSerial.cpp@35:90be2bc2a492, 2015-02-05 (annotated)
- Committer:
- shennongmin
- Date:
- Thu Feb 05 06:48:45 2015 +0000
- Revision:
- 35:90be2bc2a492
- Parent:
- 20:43451f36281e
compile okay and test 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 | 20:43451f36281e | 11 | p->data->writeChr(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 | 19:6c8b29734511 | 18 | ms_instance_counter++; |
shennongmin | 19:6c8b29734511 | 19 | if (ms_instance_counter == 1) { |
shennongmin | 19:6c8b29734511 | 20 | initHeadNode(&ms_list_head); |
shennongmin | 11:7bd1b2a67b1a | 21 | } |
shennongmin | 11:7bd1b2a67b1a | 22 | |
shennongmin | 19:6c8b29734511 | 23 | if (addNode(&ms_list_head, this) != NULL) { |
shennongmin | 18:37254b357abd | 24 | this->attach(&uartIrqCallback); |
shennongmin | 10:9d4ec0359a5c | 25 | } else { |
shennongmin | 10:9d4ec0359a5c | 26 | } |
shennongmin | 10:9d4ec0359a5c | 27 | } |
shennongmin | 15:f5682fb5b315 | 28 | |
shennongmin | 10:9d4ec0359a5c | 29 | ArduinoSerial::~ArduinoSerial(void) { |
shennongmin | 19:6c8b29734511 | 30 | ms_instance_counter--; |
shennongmin | 19:6c8b29734511 | 31 | if (delNode(&ms_list_head, this) != NULL) { |
shennongmin | 11:7bd1b2a67b1a | 32 | this->attach(NULL); |
shennongmin | 11:7bd1b2a67b1a | 33 | } else { |
shennongmin | 10:9d4ec0359a5c | 34 | } |
shennongmin | 10:9d4ec0359a5c | 35 | } |
shennongmin | 10:9d4ec0359a5c | 36 | |
shennongmin | 10:9d4ec0359a5c | 37 | void ArduinoSerial::begin(int baud_rate) { |
shennongmin | 10:9d4ec0359a5c | 38 | baud(baud_rate); |
shennongmin | 10:9d4ec0359a5c | 39 | flush(); |
shennongmin | 10:9d4ec0359a5c | 40 | } |
shennongmin | 10:9d4ec0359a5c | 41 | |
shennongmin | 10:9d4ec0359a5c | 42 | int ArduinoSerial::available(void) { |
shennongmin | 19:6c8b29734511 | 43 | return (unsigned int)(ARDUINOSERIAL_BUFFER_SIZE + m_rx_buffer.head - m_rx_buffer.tail) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 44 | } |
shennongmin | 10:9d4ec0359a5c | 45 | |
shennongmin | 10:9d4ec0359a5c | 46 | void ArduinoSerial::flush(void) |
shennongmin | 10:9d4ec0359a5c | 47 | { |
shennongmin | 19:6c8b29734511 | 48 | memset(&m_rx_buffer, 0, sizeof(m_rx_buffer)); |
shennongmin | 10:9d4ec0359a5c | 49 | } |
shennongmin | 10:9d4ec0359a5c | 50 | |
shennongmin | 20:43451f36281e | 51 | char ArduinoSerial::readChr(void) { |
shennongmin | 10:9d4ec0359a5c | 52 | // if the head isn't ahead of the tail, we don't have any characters |
shennongmin | 19:6c8b29734511 | 53 | if (m_rx_buffer.head == m_rx_buffer.tail) { |
shennongmin | 10:9d4ec0359a5c | 54 | return (char)-1; |
shennongmin | 10:9d4ec0359a5c | 55 | } else { |
shennongmin | 19:6c8b29734511 | 56 | unsigned char c = m_rx_buffer.buffer[m_rx_buffer.tail]; |
shennongmin | 19:6c8b29734511 | 57 | m_rx_buffer.tail = (unsigned int)(m_rx_buffer.tail + 1) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 58 | return c; |
shennongmin | 10:9d4ec0359a5c | 59 | } |
shennongmin | 10:9d4ec0359a5c | 60 | } |
shennongmin | 10:9d4ec0359a5c | 61 | |
shennongmin | 10:9d4ec0359a5c | 62 | void ArduinoSerial::setTimeout(unsigned long millisecond) { |
shennongmin | 19:6c8b29734511 | 63 | m_find_timeout = millisecond; |
shennongmin | 10:9d4ec0359a5c | 64 | } |
shennongmin | 10:9d4ec0359a5c | 65 | |
shennongmin | 10:9d4ec0359a5c | 66 | bool ArduinoSerial::find(const char *str) { |
shennongmin | 10:9d4ec0359a5c | 67 | bool ret = false; |
shennongmin | 10:9d4ec0359a5c | 68 | String data; |
shennongmin | 10:9d4ec0359a5c | 69 | char c; |
shennongmin | 10:9d4ec0359a5c | 70 | unsigned long i; |
shennongmin | 19:6c8b29734511 | 71 | for (i = 0; i < m_find_timeout; i++) { |
shennongmin | 10:9d4ec0359a5c | 72 | while(available() > 0) { |
shennongmin | 20:43451f36281e | 73 | c = readChr(); |
shennongmin | 10:9d4ec0359a5c | 74 | data += c; |
shennongmin | 10:9d4ec0359a5c | 75 | } |
shennongmin | 10:9d4ec0359a5c | 76 | if (data.indexOf(String(str)) != -1) { |
shennongmin | 10:9d4ec0359a5c | 77 | ret = true; |
shennongmin | 10:9d4ec0359a5c | 78 | break; |
shennongmin | 10:9d4ec0359a5c | 79 | } |
shennongmin | 20:43451f36281e | 80 | wait_ms(1); |
shennongmin | 10:9d4ec0359a5c | 81 | } |
shennongmin | 10:9d4ec0359a5c | 82 | return ret; |
shennongmin | 10:9d4ec0359a5c | 83 | } |
shennongmin | 10:9d4ec0359a5c | 84 | |
shennongmin | 10:9d4ec0359a5c | 85 | size_t ArduinoSerial::write(uint8_t data) { |
shennongmin | 10:9d4ec0359a5c | 86 | putc(data); |
shennongmin | 10:9d4ec0359a5c | 87 | return 1; |
shennongmin | 10:9d4ec0359a5c | 88 | } |
shennongmin | 10:9d4ec0359a5c | 89 | |
shennongmin | 20:43451f36281e | 90 | void ArduinoSerial::writeChr(unsigned char c) { |
shennongmin | 19:6c8b29734511 | 91 | int i = (unsigned int)(m_rx_buffer.head + 1) % ARDUINOSERIAL_BUFFER_SIZE; |
shennongmin | 10:9d4ec0359a5c | 92 | |
shennongmin | 10:9d4ec0359a5c | 93 | // if we should be storing the received character into the location |
shennongmin | 10:9d4ec0359a5c | 94 | // just before the tail (meaning that the head would advance to the |
shennongmin | 10:9d4ec0359a5c | 95 | // current location of the tail), we're about to overflow the buffer |
shennongmin | 10:9d4ec0359a5c | 96 | // and so we don't write the character or advance the head. |
shennongmin | 19:6c8b29734511 | 97 | if (i != m_rx_buffer.tail) { |
shennongmin | 19:6c8b29734511 | 98 | m_rx_buffer.buffer[m_rx_buffer.head] = c; |
shennongmin | 19:6c8b29734511 | 99 | m_rx_buffer.head = i; |
shennongmin | 10:9d4ec0359a5c | 100 | } |
shennongmin | 10:9d4ec0359a5c | 101 | } |
shennongmin | 11:7bd1b2a67b1a | 102 | |
shennongmin | 19:6c8b29734511 | 103 | ArduinoSerialLinkedNode *ArduinoSerial::initHeadNode(ArduinoSerialLinkedNode *head) { |
shennongmin | 11:7bd1b2a67b1a | 104 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 105 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 106 | } |
shennongmin | 11:7bd1b2a67b1a | 107 | head->data = NULL; |
shennongmin | 11:7bd1b2a67b1a | 108 | head->next = head; |
shennongmin | 11:7bd1b2a67b1a | 109 | return head; |
shennongmin | 11:7bd1b2a67b1a | 110 | } |
shennongmin | 11:7bd1b2a67b1a | 111 | |
shennongmin | 19:6c8b29734511 | 112 | ArduinoSerialLinkedNode *ArduinoSerial::addNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 113 | ArduinoSerialLinkedNode *p; |
shennongmin | 19:6c8b29734511 | 114 | ArduinoSerialLinkedNode *node = (ArduinoSerialLinkedNode *)malloc(sizeof(ArduinoSerialLinkedNode)); |
shennongmin | 11:7bd1b2a67b1a | 115 | if (node == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 116 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 117 | } |
shennongmin | 20:43451f36281e | 118 | /* Setting node */ |
shennongmin | 11:7bd1b2a67b1a | 119 | node->data = data; |
shennongmin | 11:7bd1b2a67b1a | 120 | node->next = head; |
shennongmin | 11:7bd1b2a67b1a | 121 | |
shennongmin | 20:43451f36281e | 122 | /* Add node to tail */ |
shennongmin | 11:7bd1b2a67b1a | 123 | for(p = head; p->next != head; p = p->next); |
shennongmin | 11:7bd1b2a67b1a | 124 | p->next = node; |
shennongmin | 11:7bd1b2a67b1a | 125 | |
shennongmin | 11:7bd1b2a67b1a | 126 | return head; |
shennongmin | 11:7bd1b2a67b1a | 127 | } |
shennongmin | 19:6c8b29734511 | 128 | ArduinoSerialLinkedNode *ArduinoSerial::delNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 129 | ArduinoSerialLinkedNode *p; |
shennongmin | 19:6c8b29734511 | 130 | ArduinoSerialLinkedNode *prev; |
shennongmin | 11:7bd1b2a67b1a | 131 | |
shennongmin | 11:7bd1b2a67b1a | 132 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 133 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 134 | } |
shennongmin | 11:7bd1b2a67b1a | 135 | |
shennongmin | 11:7bd1b2a67b1a | 136 | prev = head, p = head->next; |
shennongmin | 11:7bd1b2a67b1a | 137 | while(p != head) { |
shennongmin | 11:7bd1b2a67b1a | 138 | if (p->data == data) { |
shennongmin | 11:7bd1b2a67b1a | 139 | prev->next = p->next; |
shennongmin | 11:7bd1b2a67b1a | 140 | free(p); |
shennongmin | 11:7bd1b2a67b1a | 141 | p = prev->next; |
shennongmin | 11:7bd1b2a67b1a | 142 | } else { |
shennongmin | 11:7bd1b2a67b1a | 143 | prev = p; |
shennongmin | 11:7bd1b2a67b1a | 144 | p = p->next; |
shennongmin | 11:7bd1b2a67b1a | 145 | } |
shennongmin | 11:7bd1b2a67b1a | 146 | } |
shennongmin | 11:7bd1b2a67b1a | 147 | return head; |
shennongmin | 11:7bd1b2a67b1a | 148 | } |
shennongmin | 19:6c8b29734511 | 149 | ArduinoSerialLinkedNode *ArduinoSerial::findNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { |
shennongmin | 19:6c8b29734511 | 150 | ArduinoSerialLinkedNode *p; |
shennongmin | 11:7bd1b2a67b1a | 151 | if (head == NULL) { |
shennongmin | 11:7bd1b2a67b1a | 152 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 153 | } |
shennongmin | 11:7bd1b2a67b1a | 154 | |
shennongmin | 11:7bd1b2a67b1a | 155 | for (p = head->next; p != head; p = p->next) { |
shennongmin | 11:7bd1b2a67b1a | 156 | if (p->data == data) { |
shennongmin | 11:7bd1b2a67b1a | 157 | return p; |
shennongmin | 11:7bd1b2a67b1a | 158 | } |
shennongmin | 11:7bd1b2a67b1a | 159 | } |
shennongmin | 11:7bd1b2a67b1a | 160 | return NULL; |
shennongmin | 11:7bd1b2a67b1a | 161 | } |