Projet de mesure Avec enregistrement sur une carte SD et envoie sur BLUETOOTH ble (bluenrg)

Dependencies:   SDFileSystem mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 //#include "BatteryService.h"
00018 #include "mbed.h"
00019 #include <FileBase.h>
00020 #include "SDFileSystem.h"
00021 #include "rtos.h"
00022 #include <Thread.h>
00023 #include <stdio.h>
00024 /* C++ header files */
00025 #include <string>
00026 #include <vector>
00027 /* C header files */
00028 #include <cstdio>
00029 #include <cstring>
00030 #include <cstdlib>
00031 //#include "ST_L152_32MHZ.h"
00032 //L152_init32 myinit(0);   // use the internal oscillator
00033 
00034 
00035 using namespace std;
00036 
00037 // DEFINE
00038 #define NBFICHIERPOSSIBLE 1000
00039 
00040 // STRUCTURE
00041 typedef struct {
00042 
00043     AnalogIn * laPin;
00044     DigitalOut * laPinEco;
00045 //    string leTypeGPIO; // Num ou Anal
00046 //    string leTypeE; // Entrée ou Sortie
00047     int laFrequence;
00048     int leModeEco; // Si -1 pas de mode eco sinon numero de pin
00049     std::string * leNom;
00050     std::string * Path;
00051 } laSonde;
00052 
00053 typedef struct {
00054     laSonde * saSonde;
00055     float saMesure;
00056     int sonTemps;
00057 } laMesure;
00058 
00059 // VAR GLOBAL
00060 Mail<laMesure, 30> mail_box;
00061 Serial pc1(USBTX, USBRX);
00062 //SDFileSystem sd(PB_15, PB_14, PB_13, D4, "sd"); // MOSI, MISO, SCK, CS
00063 SDFileSystem sd(D11, D12, D13, D4, "sd"); // MOSI, MISO, SCK, CS
00064 Timer t;
00065 volatile int a=0;
00066 Mutex mtx;
00067 
00068 PinName analog[6] = { A0, A1,A2,A3,A4,A5 };
00069 PinName digital[16] = {D0,D1,D2,NC,NC,NC,D6,D7,D8,D9,D10,NC,NC,NC,D14,D15 };
00070 
00071 /**
00072 * Récuperation de data
00073 */
00074 void getdata(void const *args)
00075 {
00076     laSonde * saSonde = (laSonde*) args;
00077     while(1) {
00078         //mtx.lock();
00079         if(saSonde->leModeEco != -1)
00080             saSonde->laPinEco->write(1);
00081         laMesure *message = mail_box.alloc();
00082         message->saSonde = saSonde;
00083         message->saMesure = saSonde->laPin->read();
00084         message->sonTemps = t.read();
00085         mail_box.put(message);
00086         if(saSonde->leModeEco != -1)
00087             saSonde->laPinEco->write(0);
00088         a++;
00089         //mtx.unlock();
00090         Thread::wait(saSonde->laFrequence/100);
00091         
00092     }
00093 }
00094 
00095 void configuration()
00096 {
00097     t.start();
00098     FILE *fp = fopen("/sd/config.csv", "r");  // Open "out.txt" on the local file system for writing
00099     if(fp == NULL) {
00100         pc1.printf("Le fichier de configuration n'existe pas");
00101     } else {
00102 
00103         char buf [128];
00104                   //  mtx.lock(); // init du mutex
00105         while(1) {
00106             if((fgets(buf, 64, fp)) == NULL)
00107                 break;
00108             int a =0;
00109             char delim[] = ",";
00110             char* token;
00111             laSonde * saSonde = (laSonde * ) malloc(sizeof(laSonde));
00112             for (token = strtok(buf, delim); token; token = strtok(NULL, delim)) {
00113                 switch(a) {
00114                     case 0 : { // Nom de la pin
00115                         printf("%s \n",token);
00116                         saSonde->leNom = new string(token);
00117                         saSonde->Path =  new string(token);
00118                         saSonde->Path->insert(0,"/sd/");
00119                         mkdir(saSonde->Path->c_str(),0777);
00120                         saSonde->Path->insert(saSonde->Path->size(),"/");
00121                         saSonde->Path->insert(saSonde->Path->size(),token);
00122                         break;
00123                     }
00124                     case 1 : { // Numéro de Pin
00125                         saSonde->laPin = new AnalogIn(analog[atoi((const char*)token)]);
00126                         break;
00127                     }
00128                     case 2 : { // Type Analogique ou Numerique
00129                         //saSonde->leTypeGPIO = laLigne.at(2); // Num ou Anal
00130                         break;
00131                     }
00132                     case 3 : { // Entrée ou Sortie
00133                         //saSonde->leTypeE = laLigne.at(3); // Entrée ou Sortie
00134 
00135                         break;
00136                     }
00137                     case 4 : { // Frequence pour Sortie
00138                         saSonde->laFrequence = atoi((const char*)token);
00139                         break;
00140                     }
00141                     case 5 : { // Mode éco
00142                         saSonde->leModeEco = atoi((const char*)token); // Si -1 pas de mode eco
00143                         break;
00144                     }
00145                     default : {
00146                         printf("error parsing\n)");
00147                     }
00148 
00149                 }
00150                 a++;
00151             }
00152 
00153             if(saSonde->leModeEco != -1)
00154                 saSonde->laPinEco = new DigitalOut(digital[saSonde->leModeEco]);
00155             printf("created thread \n");
00156             Thread* unThread = new  Thread(getdata,saSonde);
00157         }//mtx.unlock(); // init du mutex
00158     }   
00159  
00160 }
00161 
00162 int main(void)
00163 {
00164 #if 1
00165     FILE *fp;
00166     char cc[200];
00167     configuration();
00168     while (true) {
00169         osEvent evt = mail_box.get();
00170         if (evt.status == osEventMail) {
00171            // mtx.lock();
00172             a--;
00173             laMesure *mail = (laMesure*)evt.value.p;
00174             fp = fopen(mail->saSonde->Path->c_str(), "a");
00175             if (fp == 0) {
00176                 printf("file write failed: %s\n", mail->saSonde->Path->c_str());
00177             } else {
00178                 sprintf(cc,"%d,%f\n",mail->sonTemps,mail->saMesure);
00179                 printf("%d,%f\n",mail->sonTemps,mail->saMesure);
00180                 fwrite (cc , sizeof(char), sizeof(cc), fp);
00181             }
00182             fclose(fp);
00183             mail_box.free(mail);
00184           //  mtx.unlock();
00185         }
00186     }
00187 #else
00188     wait(3);
00189     AnalogIn  laPin(A0);
00190     AnalogOut  aout(A2);
00191     while (1) {
00192         // change the voltage on the digital output pin by 0.1 * VCC
00193         //  and print what the measured voltage should be (assuming VCC = 3.3v)
00194         for (float i = 0.0f; i < 1.0f; i += 0.1f) {
00195             aout = i;
00196             printf("aout = %1.2f volts", aout.read() * 3.3f);
00197             wait(0.4);
00198             pc1.printf("Test : %f volts \n",(laPin.read()*(3.33  )) );
00199             wait(5);
00200         }
00201     }
00202 #endif
00203 }
00204