une 30aine passés sans pb entre les trames

Dependencies:   ADS1015 ARNSRS_SENSORS DmTftLibrary MS5803_14BA SDFileSystem mbed

Fork of ARNSRS_SERVOS_USB_TFT by POTLESS

Revision:
21:28a3f60b4bc2
Parent:
20:bb8d2cea2076
Child:
22:8e4ace42e4f5
--- a/main.cpp	Thu May 18 06:03:24 2017 +0000
+++ b/main.cpp	Thu May 18 17:33:38 2017 +0000
@@ -9,19 +9,18 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include "ARNSRS_SENSORS.h"
-#include "ble/BLE.h"
 
-#include "ble/services/UARTService.h"
 #include <string>
 
-//#include "DmTftHX8353C.h"
-//#include "DmTftS6D0164.h"
-//#include "DmTftIli9325.h"
-//#include "DmTftIli9341.h"
-//#include "DmTftSsd2119.h"
-//#include "DmTftRa8875.h"
+#include "DmTftHX8353C.h"
+#include "DmTftS6D0164.h"
+#include "DmTftIli9325.h"
+#include "DmTftIli9341.h"
+#include "DmTftSsd2119.h"
+#include "DmTftRa8875.h"
 
-
+//Ecrit dans le moniteur série de la tablette à 9600 bds si sur 1, penser à mettre NEED_CONSOLE_OUTPUT à 0
+#define NEED_ANDROID_OUTPUT 0
 //Ecrit dans le moniteur série de l'ordi à 9600 bds si sur 1
 #define NEED_CONSOLE_OUTPUT 1
 //Pour utiliser le traceur du moniteur Arduino. Mettre NEED_CONSOLE_OUTPUT 0 si utilisé.
@@ -30,6 +29,12 @@
 //Datalog carte SD si sur 1
 #define NEED_SD_DATA_LOG 0
 
+#if NEED_ANDROID_OUTPUT
+#define ANDROID(...) { serialMonit.printf(__VA_ARGS__); }
+#else
+#define ANDROID(...)
+#endif
+
 #if NEED_CONSOLE_OUTPUT
 #define DEBUG(...) { serialMonit.printf(__VA_ARGS__); } //tft.drawString(0,0,__VA_ARGS__);
 #else
@@ -46,6 +51,129 @@
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
+//TFT
+DmTftHX8353C tft(D2, D3, D4, D5, D6);  /* DmTftHX8353C(PinName mosi, PinName clk, PinName cs, PinName dc, PinName rst) DM_TFT18_101 */
+
+//Bool des pages
+bool FLAG_PAGE_1 = true;
+bool FLAG_PAGE_2 = false;
+
+//Changement de pages
+InterruptIn mybutton(USER_BUTTON);
+
+//Dessin du logo en t^te de page
+int bmpWidth, bmpHeight;
+uint8_t bmpImageoffset;
+extern uint8_t dmlogo[];
+
+// LITTLE ENDIAN!
+uint16_t read16(uint8_t *src)
+{
+  uint16_t d;
+  uint8_t b;
+  b = *src;
+  d = *(src+1);
+  d <<= 8;
+  d |= b;
+  return d;
+}
+
+// LITTLE ENDIAN!
+uint32_t read32(uint8_t *src)
+{
+  uint32_t d;
+  uint16_t b;
+
+  b = read16(src);
+  d = read16(src+2);
+  d <<= 16;
+  d |= b;
+  return d;
+}
+
+void drawBmpFromFlash(int x, int y)
+{
+  uint16_t pos = bmpImageoffset;
+
+  uint16_t p;  // pixel
+  uint8_t g, b;
+  int i, j; // line, column
+
+  for(i=bmpHeight; i>0; i--) {
+    for(j=0; j<bmpWidth; j++) {
+      b = *(dmlogo+pos++);
+      g = *(dmlogo+pos++);
+      p = *(dmlogo+pos++);
+
+      p >>= 3;
+      p <<= 6;
+
+      g >>= 2;
+      p |= g;
+      p <<= 5;
+
+      b >>= 3;
+      p |= b;
+
+      // write out the 16 bits of color
+      tft.setPixel(j+x, i+y, p);
+    }
+  }
+}
+
+
+int bmpReadHeader() {
+  uint32_t fileSize;
+  uint32_t headerSize;
+  uint16_t bmpDepth;
+  uint16_t pos = 0;
+ 
+  if (read16(dmlogo) !=0x4D42){ // read magic byte
+    return false;
+  }
+  pos += 2;
+
+  // read file size
+  fileSize = read32(dmlogo+pos);
+  pos += 4;
+
+  pos += 4; // Skip creator bytes
+
+  bmpImageoffset = read32(dmlogo+pos);
+  pos += 4;
+
+  // read DIB header
+  headerSize = read32(dmlogo+pos);
+  pos +=4;
+  bmpWidth = read32(dmlogo+pos);
+  pos += 4;
+  bmpHeight = read32(dmlogo+pos);
+  pos += 4;
+
+  if (read16(dmlogo+pos) != 1){
+    // number of color planes must be 1
+    return false;
+  }
+  pos += 2;
+
+  bmpDepth = read16(dmlogo+pos);
+  pos +=2;
+
+  if (read16(dmlogo+pos) != 0) {
+    // compression not supported!
+    return false;
+  }
+  pos += 2; // Should really be 2??
+
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+
+
+
 //Contrôle des servos
 PwmOut servo1(PB_15);
 AnalogIn FeedBack1(PC_2);
@@ -59,9 +187,9 @@
 float Limit_min_S2, Limit_max_S2;
 float Delta_FB_2;
 
-//Init BLE
-BLEDevice  ble;
-UARTService *uartServicePtr;
+//Buffer pour Commandes servo
+char commande_servo[20];
+int indexCommande;
 
 //Init de la lib ARNSRS;
 ARNSRS arnsrs;
@@ -84,11 +212,7 @@
 
 //SD card
 SDFileSystem sd(D11, D12, D13, D10, "sd"); // MOSI, MISO, SCK, CS
-//DigitalInOut csSDCard(D10, PIN_OUTPUT, PullUp, 1);
-
-//TFT
-//DmTftHX8353C tft(D2, D3, D4, D5, D6);  /* DmTftHX8353C(PinName mosi, PinName clk, PinName cs, PinName dc, PinName rst) DM_TFT18_101 */
-
+DigitalInOut csSDCard(D10, PIN_OUTPUT, PullUp, 1);
 FILE *fp;
 char fileName[32];
 int points = 1;
@@ -116,7 +240,7 @@
 {
     char buf[50];
     sprintf(buf,"%s%.1f:%.1f\r\n", com.c_str(), A, B);
-    uartServicePtr->writeString(buf);
+    ANDROID(buf);
     wait_ms(100);
 }
 
@@ -125,26 +249,17 @@
 {
     char buf[50];
     sprintf(buf,"%s%d:%d\r\n", com.c_str(), A, B);
-    uartServicePtr->writeString(buf);
+    ANDROID(buf);
     wait_ms(100);
 }
 
+
 ////////////////////////////////////////////////////////////////////////////////
-////                              BLE                                       ////
+////                  Fonction commande servos par port série                      ////
 ////////////////////////////////////////////////////////////////////////////////
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    //DEBUG("Disconnected!\n\r");
-    //DEBUG("Restarting the advertising process\n\r");
-    ble.startAdvertising();
-}
 
-////////////////////////////////////////////////////////////////////////////////
-////                  Fonction commande servos par BLE                      ////
-////////////////////////////////////////////////////////////////////////////////
-
-void Servo_BLE(char message_Android [])
+void Servo_USB(char message_Android [])
 {
 
     //Variables pour décomposition du message
@@ -164,34 +279,22 @@
     if(strcmp(commande, "[S1]") == 0) {
         servo1 = atoi(valeur) / 100.f;
         DEBUG("  Servo 1    = %s\n", valeur);
+        indexCommande = 0;
     }
 
     //Commande servo2
     if(strcmp(commande, "[S2]") == 0) {
         servo2 = atoi(valeur) / 100.f;
         DEBUG("Servo 2    =  %f\r\n", valeur);
+        indexCommande = 0;
     }
 }
 
-////////////////////////////////////////////////////////////////////////////////
-////                            Echanges UART                               ////
-////////////////////////////////////////////////////////////////////////////////
-
-void onDataWritten(const GattWriteCallbackParams *params)
+void callbackServo()
 {
-    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
-        uint16_t bytesRead = params->len;
-
-        // chaine reçue de l'app android (à dimensionner selon besoin)
-        char message [bytesRead] ;
-
-        // conversion de la commande uint8_t en chaine pour permettre comparaison avec commandes connues
-        for (int j=0; j<bytesRead; j++) {
-            message [j] = (*((params->data)+j));
-        }
-
-        Servo_BLE(message);
-    }
+   
+    commande_servo [indexCommande++] = serialMonit.getc();
+          
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -363,23 +466,107 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+///                          PAGE tft 1                        //
+////////////////////////////////////////////////////////////////////////////////
+
+void tft_page_1()
+{
+        FLAG_PAGE_2 = false;
+        FLAG_PAGE_1 = true;
+        
+        //tft.clearScreen(BLACK);
+        
+        if (!bmpReadHeader()) {   
+        return;
+        }
+  
+        drawBmpFromFlash(30, 2);
+    
+        tft.drawString(5,35, "CO2  ");
+        tft.drawString(90,35, " ppm");
+        
+        tft.drawString(5,55, "ppO2 ");
+        tft.drawString(90,55, " mb");
+        
+        tft.drawString(5,75, "T_1  ");
+        tft.drawString(90,75, " C");
+        
+        tft.drawString(5,95, "T_2  ");
+        tft.drawString(90,95, " C");
+        
+        tft.drawString(5,115, "Hum  ");
+        tft.drawString(90,115, " %");
+        
+        tft.drawString(5,135, "P    ");
+        tft.drawString(90,135, " m");
+        
+        tft.drawRectangle(0,0,126,32, WHITE);
+        tft.drawRectangle(0,32,126,52, WHITE);
+        tft.drawRectangle(0,52,126,72, WHITE);
+        tft.drawRectangle(0,72,126,92, WHITE);
+        tft.drawRectangle(0,92,126,112, WHITE);
+        tft.drawRectangle(0,112,126,132, WHITE);
+        tft.drawRectangle(0,132,126,152, WHITE);
+        
+ 
+ } 
+ 
+ ////////////////////////////////////////////////////////////////////////////////
+///                          PAGE tft 2                        //
+////////////////////////////////////////////////////////////////////////////////
+
+void tft_page_2()
+{
+        FLAG_PAGE_1 = false;
+        FLAG_PAGE_2 = true;
+        
+        //tft.clearScreen(BLACK);
+        
+        if (!bmpReadHeader()) {   
+        return;
+        }
+  
+        drawBmpFromFlash(30, 2);
+    
+        tft.drawString(5,35, "CO2  ");
+        tft.drawString(90,35, " ppm");
+        
+        tft.drawRectangle(0,0,126,32, WHITE);
+        tft.drawRectangle(0,32,126,52, WHITE);
+ } 
+
+void pressed()
+{
+     tft.clearScreen(BLACK);
+     
+     if (FLAG_PAGE_1){
+        tft_page_2();
+     }
+      
+     if (FLAG_PAGE_2){      
+        tft_page_1();
+     }   
+} 
+       
+////////////////////////////////////////////////////////////////////////////////
 ///                          fonction initialisation                          //
 ////////////////////////////////////////////////////////////////////////////////
 
 void setup()
 {
-
     wait(2);
-
-    DEBUG("Initialisation TFT\r\n");
-    //tft.init();
-
+    
     //Réglage de l'heure
     //SetTime();
 
     //Calibration des limites des servos pour remapper les feedback
-    Calibration_servo();
+    //Calibration_servo();
 
+    //Démarrage de l'écran
+    DEBUG("Initialisation TFT\r\n");
+    tft.init();
+    tft_page_1();
+    
     //Initialisation du Data log
     if (NEED_SD_DATA_LOG == 1) init_DATALOG();
 
@@ -398,29 +585,14 @@
 {
     setup();
 
-    //Initialisation BLE / ne marche pas si mise dans une fonction appelée ici ou dans le setup...
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(onDataWritten);
-
-    //setup advertising
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"ARNSRS", sizeof("ARNSRS") - 1);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
-
-    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
-    ble.startAdvertising();
-
-    UARTService uartService(ble);
-    uartServicePtr = &uartService;
-
+    //interruption sur le boutton user pour changer de page
+    mybutton.fall(&pressed);
+    
+    //Commande éventuelle pour les servos
+    serialMonit.attach(&callbackServo, Serial::RxIrq);
+        
     while (1) {
 
-        ble.waitForEvent();
-
         //CO2 / H / T sur Cozir
         co2 = arnsrs.requestCO2();
         Humi = arnsrs.requestHUMI();
@@ -447,7 +619,22 @@
         build_send_Message_int("t", co2, ppO2);
         build_send_Message_float("u", Temp1, Temp2);
         build_send_Message_float("v", Humi, pression);
-        //build_send_Message_float("f", Servo1_POS, Servo2_POS);
+        build_send_Message_float("f", Servo1_POS, Servo2_POS);
+          
+        Servo_USB(commande_servo);
+        
+        if(FLAG_PAGE_1) {
+        tft.drawNumber(65,35,co2 ,3, false);
+        tft.drawNumber(65,55,ppO2 ,3, false);      
+        tft.drawNumber(65,75,Temp1 ,3, false);      
+        tft.drawNumber(65,95,Temp2 ,3, false);    
+        tft.drawNumber(65,115,Humi ,3, false);
+        tft.drawNumber(65,135,pression ,3, false);
+        }
+        
+        if(FLAG_PAGE_2) {   
+        tft.drawNumber(65,35,co2 ,3, false);
+        }
     }
 }