![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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
Diff: main.cpp
- Branch:
- jpa
- Revision:
- 3:2de8e4771674
- Parent:
- 2:462b5c0d3472
- Child:
- 4:fbbf5d1785e7
diff -r 462b5c0d3472 -r 2de8e4771674 main.cpp --- a/main.cpp Tue Sep 01 06:37:15 2020 +0000 +++ b/main.cpp Wed Sep 02 12:54:45 2020 +0000 @@ -54,9 +54,11 @@ #include "stm32746g_discovery_lcd.h" #include "stm32746g_discovery_ts.h" #include "CANMsg.h" +//#include "ManageBufferJPA.h" #include "main.h" + //#include "touchScreen.cpp" int touchInitJPA1(); int touchJPA1(); @@ -77,51 +79,48 @@ //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to. //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these. -const char gsStreamId1[] = "voltage"; -const char gsStreamId2[] = "temperature"; -char gsStreamIPdatas[] = "192.168.1.49"; -char ethernet_actif[] = "O"; -char wifi_actif[] = "O"; -char can_actif[] = "O"; -char serial_actif[] = "O"; -char read_sd[] = ""; -uint8_t text2[30]; -uint8_t counter = 0; -float voltage; -const int kMaxBufferSize = 100; -char buffer[kMaxBufferSize]; -int len = 0; -int i = 0; -int idx_ser =0; -int recv_ser = 0; -Timer timer; +const char gsStreamId1[] = "voltage"; +const char gsStreamId2[] = "temperature"; +char gsStreamIPdatas[] = "192.168.1.49"; +char ethernet_actif[] = "O"; +char wifi_actif[] = "O"; +char can_actif[] = "O"; +char serial_actif[] = "O"; +char read_sd[] = ""; +uint8_t text2[30]; +uint8_t counter = 0; +float voltage; + +Timer timer; AnalogIn analogIn(A0); // CAN ------------------------------------------------------------ -CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name -CANMsg rxMsg; -CANMsg txMsg; +CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name +CANMsg rxMsg; +CANMsg txMsg; // Other Settings -int updateFrequency = 20; // Update frequency in seconds. Change this to change your sample frequency. +int updateFrequency = 20; // Update frequency in seconds. Change this to change your sample frequency. + +AnalogIn adc_temp(ADC_TEMP); +AnalogIn adc_vref(ADC_VREF); +DigitalOut myled(LED1); -AnalogIn adc_temp(ADC_TEMP); -AnalogIn adc_vref(ADC_VREF); -DigitalOut myled(LED1); +void initSerial(); +void sendSerialUSB(char * str); +void receiveSerialUSB(); -// serial port sur connecteur arduino (D0,D1)-------------------- -Serial pc(USBTX, USBRX); // serial PC -Serial serial2(PC_6, PC_7); // TX, RX for udp bridge -Serial serial7(PF_7, PF_6); // TX, RX for extension +extern volatile char bufferSerialUSB[]; +extern volatile int msgSerialUSB; + +//extern Serial serialUSB(USBTX, USBRX); // serial PC +//extern Serial serial2(PC_6, PC_7); // TX, RX for udp bridge +//extern Serial serial7(PF_7, PF_6); // TX, RX for extension #define BOARD1 1 // comment out this line when compiling for board #2 -#if defined(BOARD1) - const unsigned int RX_ID = 0x100; - const unsigned int TX_ID = 0x101; -#else const unsigned int RX_ID = 0x101; const unsigned int TX_ID = 0x100; -#endif + // --------------------------------------------------------------------------------------- //* @brief Prints CAN message to PC's serial terminal @@ -158,36 +157,19 @@ can.read(rxMsg); CanRcv = 1; } -// --------------------------------------------------- -void pc_recv() -{ if(serial7.readable()) - { buffer[i]=(serial7.getc()); - if (buffer[i] == '\n') { - serial7.printf("%s",buffer); - len = 0; - idx_ser =0; - recv_ser = 1; - } - idx_ser++; - } -} + // ------------------------------------------------------------------------------------------ int main() -{ pc.baud(115200); - //serial2.baud(115200); // required for tasmota - serial2.baud(57600); // required for UDP bridge - serial7.baud(115200); // required for tasmota - //serial7.attach(&pc_recv, Serial::RxIrq); +{ + initSerial(); + // attach ISR to handle received messages -#if defined(BOARD1) timer.start(); // start timer - pc.printf("CAN_Hello board #1\r\n"); -#else - pc.printf("CAN_Hello board #2\r\n"); -#endif - buffer[0] = '\0'; + sendSerialUSB("CAN_Hello board serialUSB #1\r\n"); + + printf("\nInit SDCard ...\n"); mainSDCardJPA() ; // init SD - read prog parameters from SD (from sdcardjpa.cpp in sdfilesystem_warning_fixed lib // reaf following datas @@ -224,9 +206,9 @@ // 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); + can.attach(onCanReceived, CAN::RxIrq); - // main loop --------------------------------------------------- + // main loop --------------------------------------------------- while (true) { if (CanRcv > 0) @@ -237,30 +219,16 @@ if (rxMsg.id == RX_ID) { rxMsg >> counter; rxMsg >> voltage; - pc.printf(" counter = %d\r\n", counter); - pc.printf(" voltage = %e V\r\n", voltage); + //serialUSB.printf(" counter = %d\r\n", counter); + //serialUSB.printf(" voltage = %e V\r\n", voltage); } } - // lecture sur port serie from tasmota - if(serial7.readable()) - { buffer[i]=(serial7.getc()); - serial7.printf("%c",buffer[i]); - if (buffer[i] == '\n') { - serial7.printf("%s",buffer); - len = 0; - idx_ser =0; - recv_ser = 1; - } - idx_ser++; - } - if ( recv_ser == 1) - {serial7.printf("sent: %s",buffer); - pc.printf("sent: %s",buffer); - recv_ser =0; - } - + + if (msgSerialUSB > 0) {receiveSerialUSB();} + //sendSerialUSB(bufferSerialUSB); + // timer send can - if(timer.read_ms() >= 3000) // check for timeout + if(timer.read_ms() >= 10000) // check for timeout { timer.stop(); // stop the timer timer.reset(); // reset the timer counter = (counter+1) & 1; // increment the counter @@ -288,7 +256,7 @@ BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)"--------", CENTER_MODE); } else - { pc.printf("Transmission error\r\n"); + { //serialUSB.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); } @@ -297,32 +265,26 @@ //touch screen int xx = touchJPA1(); // if (xx > 0) - { pc.printf("Touche appuyee %d \r\n",xx); - // send data to serial bridge tasmota - serial2.printf(" Touche %d appuyee to Tasmota", xx); - serial7.printf(" Touche %d appuyee to Tasmota", xx); - if (xx ==4 ) + { + if (xx == 4) { // send trame to can + txMsg.id = 0x100130CB;//TX_ID; // set the message ID + txMsg.format = CANExtended ; //extended + txMsg.len=2; + txMsg.data[0]=8;txMsg.data[1]=2; // 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); - serial7.printf(" send trame can ok"); - } + } else - { 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); } } - pc.printf("data sent to tasmota serial bridge \n"); - // data sent to ethernet xx = sendTouchJPA(xx); lastToucheTime = time(NULL); @@ -332,7 +294,7 @@ { lastToucheTime = 0; } // send mqtt // Update sensor data to GroveStreams - seconds = time(NULL); + time_t seconds = time(NULL); if(seconds - lastSuccessfulUploadTime > updateFrequency) { ////lcd.clear(); ////lcd.printf("Getting Samples...");