version with updated libs

Dependencies:   F7_Ethernet mbed MbedJSONValue LCD_DISCO_F746NG mbed-rtos BSP_DISCO_F746NG CANMsg NetworkAPI SDFileSystem_Warning_Fixed GroveStreamsmodified LcdDiscoF746NgTracer JSON

Branch:
jpa
Revision:
2:462b5c0d3472
Parent:
0:48863a0964f5
Child:
3:2de8e4771674
--- a/main.cpp	Mon Aug 31 19:25:19 2020 +0000
+++ b/main.cpp	Tue Sep 01 06:37:15 2020 +0000
@@ -128,69 +128,39 @@
 void printMsg(CANMessage& msg, int dir)
 {   if (dir ==0)
     {  //BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"Message sent    ", LEFT_MODE); 
-         serial7.printf(" Can message sent ok\n");
-         serial7.printf("ID = 0x%.3x Data=", msg.id);
-         for(int i = 0; i < msg.len; i++)
-        serial7.printf(" 0x%.2X", msg.data[i]);
-        serial7.printf("\r\n");       
+         printf(" Can message sent ok\n");
+         printf("ID = 0x%.3X Data=", msg.id);
+         for(int i = 0; i < msg.len; i++) printf(" 0x%.2X", msg.data[i]);
+        printf("\r\n");       
     }    
     else
-    {  //BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)"Message received", LEFT_MODE); 
+    {  
+        printf(" Can message receive ok\n");
+        printf("  ID      = 0x%.3X\r\n", msg.id);
+        printf("  Type    = %d\r\n", msg.type);
+        printf("  Format  = %d\r\n", msg.format);
+        printf("  Length  = %d\r\n", msg.len);
+        printf("  Data    =");
+        for(int i = 0; i < msg.len; i++)
+            printf(" 0x%.2X", msg.data[i]);
+        printf("\r\n");
     }
-    pc.printf("  ID      = 0x%.3x\r\n", msg.id);
-    pc.printf("  Type    = %d\r\n", msg.type);
-    pc.printf("  Format  = %d\r\n", msg.format);
-    pc.printf("  Length  = %d\r\n", msg.len);
-    pc.printf("  Data    =");
-    for(int i = 0; i < msg.len; i++)
-        pc.printf(" 0x%.2X", msg.data[i]);
-    pc.printf("\r\n");
-    serial7.printf(" Can message received ok\n");
-             serial7.printf("ID = 0x%.3x Data= ", msg.id);
-         for(int i = 0; i < msg.len; i++)
-        serial7.printf(" 0x%.2X", msg.data[i]);
-        serial7.printf("\r\n");       
 
-    sprintf((char*)text2, "ID: %d", msg.id);
-    //BSP_LCD_DisplayStringAt(2, LINE(7), (uint8_t *)&text2, LEFT_MODE);
-    sprintf((char*)text2, "Type: %d", msg.type);
-    //BSP_LCD_DisplayStringAt(2, LINE(8), (uint8_t *)&text2, LEFT_MODE);
-    sprintf((char*)text2, "Format: %d", msg.format);
-    //BSP_LCD_DisplayStringAt(2, LINE(9), (uint8_t *)&text2, LEFT_MODE);
-    sprintf((char*)text2, "Length: %d", msg.len);
-    //BSP_LCD_DisplayStringAt(2, LINE(10), (uint8_t *)&text2, LEFT_MODE);
-    sprintf((char*)text2, "Data: %d", msg.data[0]);
-    //BSP_LCD_DisplayStringAt(2, LINE(11), (uint8_t *)&text2, LEFT_MODE);   
 }
 
 /** -----------------------------------------------------------------------------------------
  * @brief   Handles received CAN messages
  * @note    Called on 'CAN message received' interrupt.
  */
+int CanRcv = 0;
 void onCanReceived(void)
-{   can.read(rxMsg);
-    pc.printf("-------------------------------------\r\n");
-    pc.printf("CAN message received\r\n");
-    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Can msg rec", CENTER_MODE);
-    printMsg(rxMsg,1);   
-    serial7.printf("ID: 0x%.3x ", rxMsg.id);
-    serial7.printf("D: ");
-    for(int i = 0; i < rxMsg.len; i++)
-        serial7.printf(" 0x%.2X", rxMsg.data[i]);
-    serial7.printf("\r\n");   
-    if (rxMsg.id == RX_ID) {
-        // extract data from the received CAN message 
-        // in the same order as it was added on the transmitter side
-        rxMsg >> counter;
-        rxMsg >> voltage;    
-        pc.printf("  counter = %d\r\n", counter);
-        pc.printf("  voltage = %e V\r\n", voltage);
-    }
-    timer.start(); // to transmit next message in main
+{   
+    can.read(rxMsg);
+    CanRcv = 1;
 }
 // ---------------------------------------------------
 void pc_recv()
-{    if(serial7.readable()) 
+{   if(serial7.readable()) 
     {   buffer[i]=(serial7.getc());
     if (buffer[i] == '\n') {
            serial7.printf("%s",buffer);
@@ -210,10 +180,7 @@
     serial7.baud(115200);  // required for tasmota
     //serial7.attach(&pc_recv, Serial::RxIrq);
     
-   // config can --------------------------------------
-    can.frequency(125000); // set CAN bit rate to 125 kbps
-    //can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID
-  //--  can.attach(onCanReceived);                // attach ISR to handle received messages
+   // attach ISR to handle received messages
 #if defined(BOARD1)
     timer.start();          // start timer
     pc.printf("CAN_Hello board #1\r\n");
@@ -253,8 +220,27 @@
     printf("Starting...\n");
     lastToucheTime = time(NULL);
      char data[5];
+     
+   // config can --------------------------------------
+    can.frequency(125000); // set CAN bit rate to 125 kbps
+    //can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID
+    can.attach(onCanReceived);   
+    
      // main loop ---------------------------------------------------
     while (true) {
+        
+        if (CanRcv > 0)
+            {
+            CanRcv = 0;    
+            printMsg(rxMsg,1);  
+            BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Can msg rec", CENTER_MODE);
+            if (rxMsg.id == RX_ID) {
+                rxMsg >> counter;
+                rxMsg >> voltage;    
+                pc.printf("  counter = %d\r\n", counter);
+                pc.printf("  voltage = %e V\r\n", voltage);
+                 }
+            }
         // lecture sur port serie from tasmota
     if(serial7.readable()) 
     {   buffer[i]=(serial7.getc());
@@ -274,14 +260,16 @@
       }
                
         // timer send can 
-       if(timer.read_ms() >= 1000)            // check for timeout
+       if(timer.read_ms() >= 3000)            // check for timeout
        {    timer.stop();                       // stop the timer
             timer.reset();                      // reset the timer
-            counter++;                          // increment the counter
-            voltage = (analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input
+            counter = (counter+1) & 1;                          // increment the counter
+            voltage = 0;////(analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input
             txMsg.clear();                      // clear the Tx message storage
             //txMsg.id = TX_ID;            // set ID
-            txMsg.id = 0x18881001;//TX_ID;                   // set the message ID
+            //txMsg.type=0;
+            //txMsg.format=4;
+            txMsg.id = 0x100130CA;//TX_ID;                   // set the message ID
             txMsg.format = CANExtended ; //extended
             // We are about to transmit two data items to the CAN bus.
             //     counter: uint_8 (unsigned eight bits int) value (one byte).
@@ -290,25 +278,21 @@
             // We'll use the "<<" (append) operator to add data to the CAN message.
             // The usage is same as of the similar C++ io-stream operators.
             // NOTE: The data length of CAN message is automatically updated when using "<<" operators.  
-            txMsg << counter << voltage;        // append data (total data length must be <= 8 bytes!)
+            txMsg.len=2;
+            txMsg.data[0]=8;txMsg.data[1]=counter;        // append data (total data length must be <= 8 bytes!)
             
             if(can.write(txMsg))       // transmit message
             {   //if(can->write(txMsg)) {             // transmit the CAN message
-                //led = OFF;                      // turn the LED off
-                pc.printf("-------------------------------------\r\n");
-                pc.printf("CAN message sent\r\n");
                 printMsg(txMsg,0);
-                pc.printf("  counter = %d\r\n", counter);
-                pc.printf("  voltage = %e V\r\n", voltage);
                 BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"TX CAN OK", CENTER_MODE);
                 BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE);
             }
             else
-            {    pc.printf("Transmission error\r\n");
+            {   pc.printf("Transmission error\r\n");
                 BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"TX CAN Fail", CENTER_MODE);
                 BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"-----------", CENTER_MODE);
             }    
-            //timer.start();                  // insert transmission lag
+            timer.start();                  // insert transmission lag
         }  // end if timer
         //touch screen
         int xx = touchJPA1();  //
@@ -355,8 +339,8 @@
             printf("Getting Samples...\n");          
             //Assemble the samples into URL parameters which are seperated with the "&" character
             // Example: &s1=6.2&s2=78.231
-            int temperature = adc_temp.read() * 100.0f;
-            int voltage = adc_vref.read() * 100.0f;
+            int temperature = 0; //adc_temp.read() * 100.0f;
+            int voltage = 0; //adc_vref.read() * 100.0f;
             char samples[64] = {0};
             sprintf(samples, "&%s=%d&%s=%d", gsStreamId1, voltage, gsStreamId2, temperature);           
             //Append on command requests (request stream values)