mbed-os github

Dependencies:   ADS1015 Faulhaber HTU21D_mod MS5837_potless Sensor_Head_RevB_3 USBDevice_dfu Utilsdfu beep

Fork of ARNSRS_testDFU by POTLESS

main.cpp

Committer:
POTLESS_2
Date:
2017-07-23
Revision:
6:ebed9093d661
Parent:
5:ff7aa975646c
Child:
7:ad15c386e960

File content as of revision 6:ebed9093d661:

#include "mbed.h"
#include <string>

Serial serialCozir (PC_12,PD_2,9600);

Serial serialMonit (USBTX,USBRX,9600);

//pour Cozir
const int size = 8;
char  reponse[size];
string commande;
int reponseInt;
int indexCozir;
bool newMessageFlag = false;

//pour Param Cozir
const int sizeParam = 6;
char  param[sizeParam ];
int indexParam;
bool newParamFlag = false;

void cozirSend(char* commande)
{
    if (serialCozir.writeable()) {
        serialCozir.puts(commande);
    }
}

void callbackCozir()
{
    while(serialCozir.readable()) {
        if ((indexCozir  == size) || newMessageFlag) //éviter la saturation du buffer
            serialCozir.getc();
        else
            reponse [indexCozir++] = serialCozir.getc();//chargement du buffer dans le message
        if ((indexCozir == size) || (reponse[indexCozir-1] == '\n')) {//le message est complet ou nouvelle ligne ou autre si on veut...
            reponse[indexCozir] = 0;
            newMessageFlag  = true;
        }
    }
}

void callbackParam()
{
    while(serialMonit.readable()) {
        if (indexParam  == sizeParam) //éviter la saturation du buffer
            serialMonit.getc();
        else
            param [indexParam++] = serialMonit.getc();//chargement du buffer dans le message
        if ((indexParam == sizeParam) || (param[indexParam -1] == '\n')) {//le message est complet ou nouvelle ligne ou autre si on veut...
            param[indexParam] = 0;
            newParamFlag  = true;
        }
    }
}

void decode_message(char rep[])
{

    sscanf (rep,"%s %5d", commande, &reponseInt);
    serialMonit.printf("Message complet = %s\r\n\n", rep);
    serialMonit.printf("Commande = %s\r\n", commande);
    serialMonit.printf("Reponse  = %d\r\n\n", reponseInt);

}

int main()
{
    serialCozir.attach(&callbackCozir, Serial::RxIrq);

    serialMonit.attach(&callbackParam, Serial::RxIrq);

    serialMonit.printf("  Demarrage...Entrez les comandes COZIR\r\n");
    
    //Voir Datasheet..
    //Ex : G
    //Pas besoin d'écrire \r\n dans lemoniteur
    //Passer en K 0 pour changer le A 32 ou A 16...sinon ça bloque...
    //Ensuite re passer en K 1 ou K 2....
    //Le Cozir restera dans le dernier mode rentré... 

    while (1) {

        if (newMessageFlag) {
            decode_message(reponse);
            strcpy(reponse," ");
            indexCozir = 0;
            newMessageFlag = false;
        }
        
        if (newParamFlag) {
            wait_ms(500);
            serialMonit.printf("Param = %s\r\n", param);
            cozirSend(param);
            wait_ms(500);
            strcpy(param," ");
            indexParam = 0;
            newParamFlag = false;
        }
    }
}