For test

Dependencies:   mbed

Revision:
11:7bd1b2a67b1a
Parent:
10:9d4ec0359a5c
Child:
12:4e4e72f18047
--- a/ArduinoSerial.cpp	Mon Feb 02 09:46:16 2015 +0000
+++ b/ArduinoSerial.cpp	Tue Feb 03 07:31:13 2015 +0000
@@ -1,34 +1,41 @@
 #include "ArduinoSerial.h"
 
+LinkedListNode ArduinoSerial::list_head = {NULL, NULL};
 unsigned int ArduinoSerial::instance_counter = 0;
-ArduinoSerial* ArduinoSerial::instance_reference[ARDUINOSERIAL_INSTANCE_MAX] = {0};
 
 void ArduinoSerial::uart_irq_callback(void) {
-    unsigned int index;
-    for (index = 0; index < instance_counter; index++) {
-        if (instance_reference[index] != NULL) {
-            while(instance_reference[index]->readable()) {
-                instance_reference[index]->write_char(instance_reference[index]->getc());
+    LinkedListNode *p;
+    for (p = list_head.next; p != &list_head; p = p->next) {
+        if (p->data != NULL) {
+            while(p->data->readable()) {
+                p->data->write_char(p->data->getc());
             }
         }
     }
 }
 
 ArduinoSerial::ArduinoSerial(PinName tx, PinName rx):Serial(tx, rx) {
-    if (instance_counter < ARDUINOSERIAL_INSTANCE_MAX) {
-        instance_reference[instance_counter++] = this;
-        if (1 == instance_counter) {
-            attach(&uart_irq_callback);
-        }
+    printf("Constructor\r\n");
+    instance_counter++;
+    if (instance_counter == 1) {
+        init_list_head_node(&list_head);
+    }
+    
+    if (add_node_to_tail(&list_head, this) != NULL) {
+        this->attach(&uart_irq_callback);    
+        printf("Create instance 0x%X ok!\r\n", this);
     } else {
-        printf("Create instance 0x%u failed: more instances created!\r\n");
+        printf("Create instance 0x%X failed!\r\n", this);
     }
 }
     
 ArduinoSerial::~ArduinoSerial(void) {
     instance_counter--;
-    if (0 == instance_counter) {
-        attach(NULL);
+    if (del_node_by_data(&list_head, this) != NULL) {
+        this->attach(NULL); 
+        printf("Release instance 0x%X ok!\r\n", this);
+    } else {
+        printf("Release instance 0x%X failed!\r\n", this);
     }
 }
     
@@ -68,7 +75,7 @@
     String data;
     char c;
     unsigned long i;
-    
+    printf("1.0\r\n");
     for (i = 0; i < find_timeout; i++) {
         while(available() > 0) {
             c = read_char();
@@ -80,7 +87,7 @@
         }
         delay(1);
     }
-    
+    printf("1.9\r\n");
     return ret;
 }
 
@@ -101,3 +108,63 @@
         rx_buffer.head = i;
     }
 }
+
+LinkedListNode *ArduinoSerial::init_list_head_node(LinkedListNode *head) {
+    if (head == NULL) {
+        return NULL;
+    }
+    head->data = NULL;
+    head->next = head;
+    return head;
+}
+
+LinkedListNode *ArduinoSerial::add_node_to_tail(LinkedListNode *head, ArduinoSerial* data) {
+    LinkedListNode *p;
+    LinkedListNode *node = (LinkedListNode *)malloc(sizeof(LinkedListNode));
+    if (node == NULL) {
+        return NULL;
+    }
+    /* 设置该节点的值 */
+    node->data = data;
+    node->next = head;
+    
+    /* 在末尾插入该节点 */
+    for(p = head; p->next != head; p = p->next);
+    p->next = node;
+    
+    return head;
+}
+LinkedListNode *ArduinoSerial::del_node_by_data(LinkedListNode *head, ArduinoSerial* data) {
+    LinkedListNode *p;
+    LinkedListNode *prev;
+    
+    if (head == NULL) {
+        return NULL;
+    }
+    
+    prev = head, p = head->next; 
+    while(p != head) {
+        if (p->data == data) {
+            prev->next = p->next;
+            free(p);
+            p = prev->next;
+        } else {
+            prev = p;
+            p = p->next;    
+        }
+    }
+    return head;
+}
+LinkedListNode *ArduinoSerial::find_node_by_data(LinkedListNode *head, ArduinoSerial* data) {
+    LinkedListNode *p;
+    if (head == NULL) {
+        return NULL;
+    }
+    
+    for (p = head->next; p != head; p = p->next) {
+        if (p->data == data) {
+            return p;
+        }
+    }
+    return NULL;
+}
\ No newline at end of file