pengfei wu
/
ESP8266Program
For test
Embed:
(wiki syntax)
Show/hide line numbers
ArduinoSerial.cpp
00001 #include "ArduinoSerial.h" 00002 00003 ArduinoSerialLinkedNode ArduinoSerial::ms_list_head = {NULL, NULL}; 00004 unsigned int ArduinoSerial::ms_instance_counter = 0; 00005 00006 void ArduinoSerial::uartIrqCallback(void) { 00007 ArduinoSerialLinkedNode *p; 00008 for (p = ms_list_head.next; p != &ms_list_head; p = p->next) { 00009 if (p->data != NULL) { 00010 while(p->data->readable()) { 00011 p->data->writeChr(p->data->getc()); 00012 } 00013 } 00014 } 00015 } 00016 00017 ArduinoSerial::ArduinoSerial(PinName tx, PinName rx):Serial(tx, rx) { 00018 ms_instance_counter++; 00019 if (ms_instance_counter == 1) { 00020 initHeadNode(&ms_list_head); 00021 } 00022 00023 if (addNode(&ms_list_head, this) != NULL) { 00024 this->attach(&uartIrqCallback); 00025 } else { 00026 } 00027 } 00028 00029 ArduinoSerial::~ArduinoSerial(void) { 00030 ms_instance_counter--; 00031 if (delNode(&ms_list_head, this) != NULL) { 00032 this->attach(NULL); 00033 } else { 00034 } 00035 } 00036 00037 void ArduinoSerial::begin(int baud_rate) { 00038 baud(baud_rate); 00039 flush(); 00040 } 00041 00042 int ArduinoSerial::available(void) { 00043 return (unsigned int)(ARDUINOSERIAL_BUFFER_SIZE + m_rx_buffer.head - m_rx_buffer.tail) % ARDUINOSERIAL_BUFFER_SIZE; 00044 } 00045 00046 void ArduinoSerial::flush(void) 00047 { 00048 memset(&m_rx_buffer, 0, sizeof(m_rx_buffer)); 00049 } 00050 00051 char ArduinoSerial::readChr(void) { 00052 // if the head isn't ahead of the tail, we don't have any characters 00053 if (m_rx_buffer.head == m_rx_buffer.tail) { 00054 return (char)-1; 00055 } else { 00056 unsigned char c = m_rx_buffer.buffer[m_rx_buffer.tail]; 00057 m_rx_buffer.tail = (unsigned int)(m_rx_buffer.tail + 1) % ARDUINOSERIAL_BUFFER_SIZE; 00058 return c; 00059 } 00060 } 00061 00062 void ArduinoSerial::setTimeout(unsigned long millisecond) { 00063 m_find_timeout = millisecond; 00064 } 00065 00066 bool ArduinoSerial::find(const char *str) { 00067 bool ret = false; 00068 String data; 00069 char c; 00070 unsigned long i; 00071 for (i = 0; i < m_find_timeout; i++) { 00072 while(available() > 0) { 00073 c = readChr(); 00074 data += c; 00075 } 00076 if (data.indexOf(String(str)) != -1) { 00077 ret = true; 00078 break; 00079 } 00080 wait_ms(1); 00081 } 00082 return ret; 00083 } 00084 00085 size_t ArduinoSerial::write(uint8_t data) { 00086 putc(data); 00087 return 1; 00088 } 00089 00090 void ArduinoSerial::writeChr(unsigned char c) { 00091 int i = (unsigned int)(m_rx_buffer.head + 1) % ARDUINOSERIAL_BUFFER_SIZE; 00092 00093 // if we should be storing the received character into the location 00094 // just before the tail (meaning that the head would advance to the 00095 // current location of the tail), we're about to overflow the buffer 00096 // and so we don't write the character or advance the head. 00097 if (i != m_rx_buffer.tail) { 00098 m_rx_buffer.buffer[m_rx_buffer.head] = c; 00099 m_rx_buffer.head = i; 00100 } 00101 } 00102 00103 ArduinoSerialLinkedNode *ArduinoSerial::initHeadNode(ArduinoSerialLinkedNode *head) { 00104 if (head == NULL) { 00105 return NULL; 00106 } 00107 head->data = NULL; 00108 head->next = head; 00109 return head; 00110 } 00111 00112 ArduinoSerialLinkedNode *ArduinoSerial::addNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { 00113 ArduinoSerialLinkedNode *p; 00114 ArduinoSerialLinkedNode *node = (ArduinoSerialLinkedNode *)malloc(sizeof(ArduinoSerialLinkedNode)); 00115 if (node == NULL) { 00116 return NULL; 00117 } 00118 /* Setting node */ 00119 node->data = data; 00120 node->next = head; 00121 00122 /* Add node to tail */ 00123 for(p = head; p->next != head; p = p->next); 00124 p->next = node; 00125 00126 return head; 00127 } 00128 ArduinoSerialLinkedNode *ArduinoSerial::delNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { 00129 ArduinoSerialLinkedNode *p; 00130 ArduinoSerialLinkedNode *prev; 00131 00132 if (head == NULL) { 00133 return NULL; 00134 } 00135 00136 prev = head, p = head->next; 00137 while(p != head) { 00138 if (p->data == data) { 00139 prev->next = p->next; 00140 free(p); 00141 p = prev->next; 00142 } else { 00143 prev = p; 00144 p = p->next; 00145 } 00146 } 00147 return head; 00148 } 00149 ArduinoSerialLinkedNode *ArduinoSerial::findNode(ArduinoSerialLinkedNode *head, ArduinoSerial* data) { 00150 ArduinoSerialLinkedNode *p; 00151 if (head == NULL) { 00152 return NULL; 00153 } 00154 00155 for (p = head->next; p != head; p = p->next) { 00156 if (p->data == data) { 00157 return p; 00158 } 00159 } 00160 return NULL; 00161 }
Generated on Tue Jul 12 2022 18:59:49 by 1.7.2