Feng Hong / Mbed OS Nucleo_rtos_basic
Revision:
8:6105ffbaf237
Parent:
7:e0c7e624c5fa
Child:
9:486f65124378
--- a/main.cpp	Sat Mar 23 07:08:27 2019 +0000
+++ b/main.cpp	Sat Mar 23 09:18:03 2019 +0000
@@ -9,7 +9,7 @@
 void init_scale();
 extern HX711 hx711;
 
-unsigned char rx[8], tx[8];
+unsigned char rx_buffer[8], tx_buffer[8];
 unsigned char rx_length, tx_length;
 
 #define LCD_1602
@@ -37,11 +37,12 @@
     fflush(stdout);
 }
 
-Thread thread;
-
+Thread can_receivethread;
+Thread can_handlethread;
 
 CANMessage  msg;
-
+MemoryPool<CANMessage, 16> can_mpool;
+Queue<CANMessage, 16> can_queue;
 
 InterruptIn button1(USER_BUTTON);
 volatile bool button1_pressed = false; // Used in the main loop
@@ -64,7 +65,7 @@
     }
 }
 
-void print_thread()
+void can_rxthread()
 {
     while (true) {
  #if 1       
@@ -73,6 +74,15 @@
             printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
 //            b = *reinterpret_cast<int*>(msg.data);
             b = msg.data[0];
+            CANMessage *can_message = can_mpool.alloc();
+            memcpy((void *)can_message, (void *)&msg, sizeof(msg)); 
+            if (!can_queue.full())
+                can_queue.put(can_message);
+            else
+            {
+                printf("message queue is full. \r\n");    
+            }
+            
             printf("got data %d 0x%08x \r\n", b, b);
             if(msg.id == 1337) {
                 led2 = !led2;
@@ -91,14 +101,13 @@
 int main()
 {
     wait(1);
- #ifdef LCD_1621
+#ifdef LCD_1621
     lcd.clear();            // clears display
     lcd.allsegson();
    
  //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
 #endif  
     printf("\n\n*** RTOS basic example ***\r\n");
-    analyzePayload();
 #ifdef LCD_1602
     lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
     lcd.printf( 1, "pi = %.6f", 3.14159265 );
@@ -106,8 +115,8 @@
     lcd.printf(5, 0, "UUU");
 #endif    
     init_scale();
-    thread.start(print_thread);
-
+    can_receivethread.start(can_rxthread);
+    can_handlethread.start(analyzePayload);
 //    flashIAP.init();
 //    printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
 //    can1.reset();