XIAOHUI TAO / Mbed 2 deprecated hw2_loopv1

main.cpp

Committer:
taoxh
Date:
2010-11-30
Revision:
0:ffe2b06e7d06

File content as of revision 0:ffe2b06e7d06:

#include "mbed.h"

#define numsamples 1
int touchSense1(void);    //t0.t1
int touchSense2(void);
int senderror ();
int test1();    //the time estimated for 5ms. In case for touch trigger
int test2();
int gtchar ();   //get the pc input
DigitalOut myled1(LED1);
AnalogIn input1(p20);
DigitalIn charger1(p19);
DigitalOut ground1(p18);

DigitalOut myled2(LED2);
AnalogIn input2(p15);
DigitalIn charger2(p16);
DigitalOut ground2(p17);
Serial pc(USBTX, USBRX); // tx, rx
char str[40];   //pc input storage
char str2[40];  //touch input storage
int main() {
    int length;   //the total length of the pc input
    length=gtchar();
    int count=0;
    int pre_cir_pro=0;
    int judge=1;
    int shown=0;
 //   int write=-1;
    while (1) {
        if (touchSense1()&& !touchSense2()) {  //t0 touches
            myled1 = 1;
            myled2=0;
            pc.putc('a');   // for reference only
            shown=0;    //match shown or not
           while(1) {if (!test1()){break;} if  (touchSense2()&& touchSense1()) {break;}}  //Emit the touch trigger
       //    if (str[count]=='0') {judge=0;}
       //    judge=1;
       //    pc.putc(str[count]);
            if (count>length){   //store the touch input
              for (int i=1;i<count;i++)
              { str2[i-1]=str2[i];
               pc.putc(str2[i-1]);
              }
             str2[count-1]='1';
             pc.putc(str2[count-1]);}
            else { str2[count]='1';count++;}

            //process_char();
        } else if (touchSense2()&& !touchSense1()) {  //t1 touches
           if (pre_cir_pro>30){
            myled1 = 0;
            myled2=1;
            pc.putc('b');
            shown=0;
             while(1) {if (!test2()){break;} if  (touchSense2()&& touchSense1()) {break;} }
            //if (str[count]=='1') {judge=0;}
            //judge=0;
            //pc.putc(str[count]);
            if (count>length){
              for (int i=1;i<count;i++)
              { str2[i-1]=str2[i];
               pc.putc(str2[i-1]);
              }
             str2[count-1]='0';
             pc.putc(str2[count-1]);}
            else { str2[count]='0';count++;}
            }
        } else if  (touchSense2()&& touchSense1()) {  //touch error
            myled1 = 1;
            myled2=1;
            senderror();
        } else {
            myled1=0;
            myled2=0;
        }
        pre_cir_pro++;   //In case of the initial trigger
        
        if (count>=length) {
          for (int i=0;i<=length;i++)
          { if (str[i]!=str2[i]) {judge=0;} }
          if (judge==1 && shown==0) {pc.printf("\nMATCH");shown=1;}
          judge=1;}
          }
}
      //  if () {pc.printf("\nMATCH");count=0;}
      //  for (int i=0;i<count;i++)
      //  {if (str[i]!=str2[i]) {judge2=0;}}        
      //   if (count>=length) {
      //   if (judge2==1) {pc.printf("\nMATCH");count=0;}
      //    else {pc.printf("\n not MATCH");count=0;}
      //    }  
      //   if (!touchSense2()&& !touchSense1()) {if (write!=-1) {
      //   if (write==1&&str[count]=='0') {judge=1;}
      //   else if (write==0&&str[count]=='1') {judge=1;}; write=-1;count++;}
      //   }
      //  if (count>5) {if (judge==1) {pc.printf("not match");} else {pc.printf("not match");}}

int touchSense1(void) {
    float sample;
    ground1 = 0;
    charger1.mode(PullUp);
    charger1.mode(PullNone);
    sample=input1.read();
    if (sample < 0.3) {
        return 1;
    } else {
        return 0;
    }
}

int touchSense2(void) {
    float sample;
    ground2 = 0;
    charger2.mode(PullUp);
    charger2.mode(PullNone);
    sample=input2.read();
    if (sample < 0.3) {
        return 1;
    } else {
        return 0;
    }
}

int senderror () {
    pc.printf("Touch Error");
    return 0;
    }

int senderror2 () {
    pc.printf("Host Error");
    return 0;
    }

int gtchar ()  //get pc input
{ pc.printf("plz input the trigger str\n");
   int length=0;
   while(pc.getc()!='S') {pc.printf("Plz input the start char (S):");}
   while(1)
     {  char temp=pc.getc();
        if (temp=='E'){ break;}
        else if (temp=='1'){
        str[length]='1';length++;}
        else if (temp=='0'){
        str[length]='0';length++;}
        else if (temp==' '){}
        else senderror2();
        if (length>40) {pc.printf("The allocated space is too small.");break;}
        }
        for (int i=0;i<length; i++)
        { pc.putc(str[i]);}
        pc.printf("\n");
   //     char p=char(length);
  //      pc.putc(p);
        return length-1;
 }
  
  
  int test1()
  {
     int j=1000;
     int judge=0;
     for (int i=0;i<j;i++)
     {   if(touchSense1()) {judge=1;}}
     return judge;
     }
    
  int test2()
  {
     int j=1000;
     int judge=0;
     for (int i=0;i<j;i++)
     {   if(touchSense2()) {judge=1;}}
     return judge;
     }