For test

Dependencies:   mbed

Committer:
shennongmin
Date:
Tue Feb 03 07:31:13 2015 +0000
Revision:
11:7bd1b2a67b1a
Parent:
10:9d4ec0359a5c
Child:
12:4e4e72f18047
debug linked list

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shennongmin 10:9d4ec0359a5c 1 #include "ArduinoSerial.h"
shennongmin 10:9d4ec0359a5c 2
shennongmin 11:7bd1b2a67b1a 3 LinkedListNode ArduinoSerial::list_head = {NULL, NULL};
shennongmin 10:9d4ec0359a5c 4 unsigned int ArduinoSerial::instance_counter = 0;
shennongmin 10:9d4ec0359a5c 5
shennongmin 10:9d4ec0359a5c 6 void ArduinoSerial::uart_irq_callback(void) {
shennongmin 11:7bd1b2a67b1a 7 LinkedListNode *p;
shennongmin 11:7bd1b2a67b1a 8 for (p = list_head.next; p != &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 11:7bd1b2a67b1a 18 printf("Constructor\r\n");
shennongmin 11:7bd1b2a67b1a 19 instance_counter++;
shennongmin 11:7bd1b2a67b1a 20 if (instance_counter == 1) {
shennongmin 11:7bd1b2a67b1a 21 init_list_head_node(&list_head);
shennongmin 11:7bd1b2a67b1a 22 }
shennongmin 11:7bd1b2a67b1a 23
shennongmin 11:7bd1b2a67b1a 24 if (add_node_to_tail(&list_head, this) != NULL) {
shennongmin 11:7bd1b2a67b1a 25 this->attach(&uart_irq_callback);
shennongmin 11:7bd1b2a67b1a 26 printf("Create instance 0x%X ok!\r\n", this);
shennongmin 10:9d4ec0359a5c 27 } else {
shennongmin 11:7bd1b2a67b1a 28 printf("Create instance 0x%X failed!\r\n", this);
shennongmin 10:9d4ec0359a5c 29 }
shennongmin 10:9d4ec0359a5c 30 }
shennongmin 10:9d4ec0359a5c 31
shennongmin 10:9d4ec0359a5c 32 ArduinoSerial::~ArduinoSerial(void) {
shennongmin 10:9d4ec0359a5c 33 instance_counter--;
shennongmin 11:7bd1b2a67b1a 34 if (del_node_by_data(&list_head, this) != NULL) {
shennongmin 11:7bd1b2a67b1a 35 this->attach(NULL);
shennongmin 11:7bd1b2a67b1a 36 printf("Release instance 0x%X ok!\r\n", this);
shennongmin 11:7bd1b2a67b1a 37 } else {
shennongmin 11:7bd1b2a67b1a 38 printf("Release instance 0x%X failed!\r\n", this);
shennongmin 10:9d4ec0359a5c 39 }
shennongmin 10:9d4ec0359a5c 40 }
shennongmin 10:9d4ec0359a5c 41
shennongmin 10:9d4ec0359a5c 42 void ArduinoSerial::begin(int baud_rate) {
shennongmin 10:9d4ec0359a5c 43 baud(baud_rate);
shennongmin 10:9d4ec0359a5c 44 flush();
shennongmin 10:9d4ec0359a5c 45 }
shennongmin 10:9d4ec0359a5c 46
shennongmin 10:9d4ec0359a5c 47 int ArduinoSerial::available(void) {
shennongmin 10:9d4ec0359a5c 48 return (unsigned int)(ARDUINOSERIAL_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % ARDUINOSERIAL_BUFFER_SIZE;
shennongmin 10:9d4ec0359a5c 49 }
shennongmin 10:9d4ec0359a5c 50
shennongmin 10:9d4ec0359a5c 51 /* 清空接收缓冲区的数据 */
shennongmin 10:9d4ec0359a5c 52 void ArduinoSerial::flush(void)
shennongmin 10:9d4ec0359a5c 53 {
shennongmin 10:9d4ec0359a5c 54 memset(&rx_buffer, 0, sizeof(rx_buffer));
shennongmin 10:9d4ec0359a5c 55 }
shennongmin 10:9d4ec0359a5c 56
shennongmin 10:9d4ec0359a5c 57 char ArduinoSerial::read_char(void) {
shennongmin 10:9d4ec0359a5c 58 // if the head isn't ahead of the tail, we don't have any characters
shennongmin 10:9d4ec0359a5c 59 if (rx_buffer.head == rx_buffer.tail) {
shennongmin 10:9d4ec0359a5c 60 return (char)-1;
shennongmin 10:9d4ec0359a5c 61 } else {
shennongmin 10:9d4ec0359a5c 62 unsigned char c = rx_buffer.buffer[rx_buffer.tail];
shennongmin 10:9d4ec0359a5c 63 rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % ARDUINOSERIAL_BUFFER_SIZE;
shennongmin 10:9d4ec0359a5c 64 return c;
shennongmin 10:9d4ec0359a5c 65 }
shennongmin 10:9d4ec0359a5c 66 }
shennongmin 10:9d4ec0359a5c 67
shennongmin 10:9d4ec0359a5c 68 void ArduinoSerial::setTimeout(unsigned long millisecond) {
shennongmin 10:9d4ec0359a5c 69 find_timeout = millisecond;
shennongmin 10:9d4ec0359a5c 70 }
shennongmin 10:9d4ec0359a5c 71
shennongmin 10:9d4ec0359a5c 72 /* 在超时之前, 如果在串口接收数据缓冲区中找到了定长字符串 str 就返回 true, 如果超时, 返回 false */
shennongmin 10:9d4ec0359a5c 73 bool ArduinoSerial::find(const char *str) {
shennongmin 10:9d4ec0359a5c 74 bool ret = false;
shennongmin 10:9d4ec0359a5c 75 String data;
shennongmin 10:9d4ec0359a5c 76 char c;
shennongmin 10:9d4ec0359a5c 77 unsigned long i;
shennongmin 11:7bd1b2a67b1a 78 printf("1.0\r\n");
shennongmin 10:9d4ec0359a5c 79 for (i = 0; i < find_timeout; i++) {
shennongmin 10:9d4ec0359a5c 80 while(available() > 0) {
shennongmin 10:9d4ec0359a5c 81 c = read_char();
shennongmin 10:9d4ec0359a5c 82 data += c;
shennongmin 10:9d4ec0359a5c 83 }
shennongmin 10:9d4ec0359a5c 84 if (data.indexOf(String(str)) != -1) {
shennongmin 10:9d4ec0359a5c 85 ret = true;
shennongmin 10:9d4ec0359a5c 86 break;
shennongmin 10:9d4ec0359a5c 87 }
shennongmin 10:9d4ec0359a5c 88 delay(1);
shennongmin 10:9d4ec0359a5c 89 }
shennongmin 11:7bd1b2a67b1a 90 printf("1.9\r\n");
shennongmin 10:9d4ec0359a5c 91 return ret;
shennongmin 10:9d4ec0359a5c 92 }
shennongmin 10:9d4ec0359a5c 93
shennongmin 10:9d4ec0359a5c 94 size_t ArduinoSerial::write(uint8_t data) {
shennongmin 10:9d4ec0359a5c 95 putc(data);
shennongmin 10:9d4ec0359a5c 96 return 1;
shennongmin 10:9d4ec0359a5c 97 }
shennongmin 10:9d4ec0359a5c 98
shennongmin 10:9d4ec0359a5c 99 void ArduinoSerial::write_char(unsigned char c) {
shennongmin 10:9d4ec0359a5c 100 int i = (unsigned int)(rx_buffer.head + 1) % ARDUINOSERIAL_BUFFER_SIZE;
shennongmin 10:9d4ec0359a5c 101
shennongmin 10:9d4ec0359a5c 102 // if we should be storing the received character into the location
shennongmin 10:9d4ec0359a5c 103 // just before the tail (meaning that the head would advance to the
shennongmin 10:9d4ec0359a5c 104 // current location of the tail), we're about to overflow the buffer
shennongmin 10:9d4ec0359a5c 105 // and so we don't write the character or advance the head.
shennongmin 10:9d4ec0359a5c 106 if (i != rx_buffer.tail) {
shennongmin 10:9d4ec0359a5c 107 rx_buffer.buffer[rx_buffer.head] = c;
shennongmin 10:9d4ec0359a5c 108 rx_buffer.head = i;
shennongmin 10:9d4ec0359a5c 109 }
shennongmin 10:9d4ec0359a5c 110 }
shennongmin 11:7bd1b2a67b1a 111
shennongmin 11:7bd1b2a67b1a 112 LinkedListNode *ArduinoSerial::init_list_head_node(LinkedListNode *head) {
shennongmin 11:7bd1b2a67b1a 113 if (head == NULL) {
shennongmin 11:7bd1b2a67b1a 114 return NULL;
shennongmin 11:7bd1b2a67b1a 115 }
shennongmin 11:7bd1b2a67b1a 116 head->data = NULL;
shennongmin 11:7bd1b2a67b1a 117 head->next = head;
shennongmin 11:7bd1b2a67b1a 118 return head;
shennongmin 11:7bd1b2a67b1a 119 }
shennongmin 11:7bd1b2a67b1a 120
shennongmin 11:7bd1b2a67b1a 121 LinkedListNode *ArduinoSerial::add_node_to_tail(LinkedListNode *head, ArduinoSerial* data) {
shennongmin 11:7bd1b2a67b1a 122 LinkedListNode *p;
shennongmin 11:7bd1b2a67b1a 123 LinkedListNode *node = (LinkedListNode *)malloc(sizeof(LinkedListNode));
shennongmin 11:7bd1b2a67b1a 124 if (node == NULL) {
shennongmin 11:7bd1b2a67b1a 125 return NULL;
shennongmin 11:7bd1b2a67b1a 126 }
shennongmin 11:7bd1b2a67b1a 127 /* 设置该节点的值 */
shennongmin 11:7bd1b2a67b1a 128 node->data = data;
shennongmin 11:7bd1b2a67b1a 129 node->next = head;
shennongmin 11:7bd1b2a67b1a 130
shennongmin 11:7bd1b2a67b1a 131 /* 在末尾插入该节点 */
shennongmin 11:7bd1b2a67b1a 132 for(p = head; p->next != head; p = p->next);
shennongmin 11:7bd1b2a67b1a 133 p->next = node;
shennongmin 11:7bd1b2a67b1a 134
shennongmin 11:7bd1b2a67b1a 135 return head;
shennongmin 11:7bd1b2a67b1a 136 }
shennongmin 11:7bd1b2a67b1a 137 LinkedListNode *ArduinoSerial::del_node_by_data(LinkedListNode *head, ArduinoSerial* data) {
shennongmin 11:7bd1b2a67b1a 138 LinkedListNode *p;
shennongmin 11:7bd1b2a67b1a 139 LinkedListNode *prev;
shennongmin 11:7bd1b2a67b1a 140
shennongmin 11:7bd1b2a67b1a 141 if (head == NULL) {
shennongmin 11:7bd1b2a67b1a 142 return NULL;
shennongmin 11:7bd1b2a67b1a 143 }
shennongmin 11:7bd1b2a67b1a 144
shennongmin 11:7bd1b2a67b1a 145 prev = head, p = head->next;
shennongmin 11:7bd1b2a67b1a 146 while(p != head) {
shennongmin 11:7bd1b2a67b1a 147 if (p->data == data) {
shennongmin 11:7bd1b2a67b1a 148 prev->next = p->next;
shennongmin 11:7bd1b2a67b1a 149 free(p);
shennongmin 11:7bd1b2a67b1a 150 p = prev->next;
shennongmin 11:7bd1b2a67b1a 151 } else {
shennongmin 11:7bd1b2a67b1a 152 prev = p;
shennongmin 11:7bd1b2a67b1a 153 p = p->next;
shennongmin 11:7bd1b2a67b1a 154 }
shennongmin 11:7bd1b2a67b1a 155 }
shennongmin 11:7bd1b2a67b1a 156 return head;
shennongmin 11:7bd1b2a67b1a 157 }
shennongmin 11:7bd1b2a67b1a 158 LinkedListNode *ArduinoSerial::find_node_by_data(LinkedListNode *head, ArduinoSerial* data) {
shennongmin 11:7bd1b2a67b1a 159 LinkedListNode *p;
shennongmin 11:7bd1b2a67b1a 160 if (head == NULL) {
shennongmin 11:7bd1b2a67b1a 161 return NULL;
shennongmin 11:7bd1b2a67b1a 162 }
shennongmin 11:7bd1b2a67b1a 163
shennongmin 11:7bd1b2a67b1a 164 for (p = head->next; p != head; p = p->next) {
shennongmin 11:7bd1b2a67b1a 165 if (p->data == data) {
shennongmin 11:7bd1b2a67b1a 166 return p;
shennongmin 11:7bd1b2a67b1a 167 }
shennongmin 11:7bd1b2a67b1a 168 }
shennongmin 11:7bd1b2a67b1a 169 return NULL;
shennongmin 11:7bd1b2a67b1a 170 }