Este programa se usa en recepcion de cadenas de caracteres, util para probar programas de conexion bluetooth en APPINVENTOR

Dependencies:   mbed

/media/uploads/tony63/voz1.apk

/media/uploads/tony63/voz1.aia

/media/uploads/tony63/app1.png

main.cpp

Committer:
tony63
Date:
2015-04-13
Revision:
0:9894ad6153eb
Child:
1:bf58c37db6af

File content as of revision 0:9894ad6153eb:

//PROGRAMA PARA RECIBIR CARACTERES DESDE EL PC Y ACTIVAR LEDS
//UTIL EN LECTURA DE CADENAS PARA APP INVENTOR
#include "mbed.h"
#include "stdio.h"
#include "string.h"
DigitalOut LedVerde(LED2);
DigitalOut LedRojo(LED1);
DigitalOut LedAzul(LED3);
Serial GSM(PTE0,PTE1);  //puertos del FRDM para el modem
Serial pc(USBTX,USBRX); //puertos del PC
char buffer[5];// TAMAÑO DEL BUFER
Timer t;   //VALOR DEL TIEMPO
int count;
int i = 0;
int c=0;
char r[]=""; 
 
int readBuffer(char *buffer,int count)   //esta funcion lee un bufer de datos
{
    int i=0; 
    t.start();    //CUENTA EL TIEMPO DE CONEXION E INICIA
    while(1) {
        while (GSM.readable()) {
            char c = GSM.getc();
            if (c == '\r' || c == '\n') c = '$';//si se envia fin de linea o de caracxter inserta $
            buffer[i++] = c;//mete al bufer el caracter leido
            if(i > count)break;//sale del loop si ya detecto terminacion
        }
        if(i > count)break;
        if(t.read() > 1) {  //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE
            t.stop();
            t.reset();
            break;
        }
    }
     return 0;
}

void cleanBuffer(char *buffer, int count)  //esta funcion limpia el bufer
{
    for(int i=0; i < count; i++) {
        buffer[i] = '\0';
    }
}

   
int main(void)
       { 
       LedVerde=1;
       LedRojo=1;
       LedAzul=1;
       LedVerde=0;
       wait(2);   //PRENDE EL LED VERDE POR 2 SEGUNDOS
       LedVerde=1;
       GSM.baud(9600);
       GSM.format(8,Serial::None,1); 
       LedVerde=0;
        
       while(1){ 
       if (GSM.readable()) {
          readBuffer(buffer,5);
          pc.printf("buffer= %s\n\r ",buffer);  //imprime el bufer
          pc.printf("buffer= %c  %c\n\r ",buffer[0],buffer[1]);//imprime el cero y el uno
          if(buffer[0]=='O' && buffer[1]=='N'){LedAzul=0;}  //PRENDE EL LED AZUL
          if(buffer[0]=='O' && buffer[1]=='F'){LedAzul=1;}  //APAGA EL LED AZUL
          } 
          cleanBuffer(buffer,5);           
}
}