mbed2_2_a

Dependencies:   mbed

main.cpp

Committer:
Robsonik16
Date:
2017-05-05
Revision:
0:a807bd13ba83

File content as of revision 0:a807bd13ba83:

#include "mbed.h"

//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------
 
RawSerial pc(USBTX,USBRX,9600);

DigitalOut led_green(LED1);
DigitalOut led_red(LED2);

//DigitalIn led_green_in(LED1);

InterruptIn user_button(USER_BUTTON);

float delay_on = 0.2;
float delay_off = 1.0;

void button_pressed()
{
    delay_on = 0.1;
    delay_off = 0.1;
}

void button_released()
{
    delay_on = 0.2;
    delay_off = 1.0;
}


int my_puts(char tab[],int size){
//retutn 0 if string is valid
    bool StringIsValid=false;
    for( int index=0;index<size;index++){
        if (tab[index]==NULL){
            StringIsValid = true;
            break;
        }
    }
    if(StringIsValid == false) return(1);
    
    for( int index=0;index<size;index++){
        if (tab[index]==NULL){
            pc.putc('\r');
            while(!pc.writeable());
            pc.putc('\n');
            return(0);
        }
        while(!pc.writeable());
        pc.putc(tab[index]);
    }
    return(0);
}


char *my_gets(char *str){
    if (!pc.readable())return (NULL);
    char c;
    for(int index=0;;index++){
         c =pc.getc ();
         if ((c=='\r')||(c=='\n')){
             str[index]=NULL;
             return (str);
             }
        str[index]=c;
        }
    
}

int main()
{
    //user_button.rise(&button_pressed);
    //user_button.fall(&button_released);
    
    //pc.format(8, Serial::None, 1);
 
    //pc.baud(9600);
    
    bool flag=false;
    char text[15];
    text[0]='0';
    text[1]='1';
    text[2]='2';
    text[3]='3';
    text[4]='4';
    text[5]='5';
    text[6]='6';
    text[7]=0;
    
    while(1) {
        
        //pc.printf("This program runs since %d seconds.\r\n", i);
 
     
      //wait(1);
      
      
      
      
      text = my_gets(text);
      if (text!=NULL) my_puts(text,size);
      
      /*
      if (pc.readable()){
            char c =pc.getc ();
            pc.putc(c);
            if (c == 's')flag = true;
            if (c == 'r')flag = false;
            if (c == 't')flag=!flag;
                led_green =flag;
                int size = 8;
                puts2(text,size);
                //int puts(char *tab,int size){

          }
      
      */
      

        /*
        for(char a = 'a';a<='z';a++){
            pc.putc(a);
            pc.putc('\r');
            pc.putc('\n');
            wait(0.1);
            }
            */
    }
}