Denver Lodge / LabWk2
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 
00004 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
00005 PwmOut led(p25);
00006 Serial device(p9, p10);  // tx, rx
00007 DigitalIn Opt(p5);       // option button
00008 DigitalIn Sel(p6);       // select/tag button
00009 DigitalOut IRled(LED1);  // Blinks for serial input from the IR sensor
00010 DigitalOut tagled(LED2); // Blinks for a confirmed tag
00011 Timer timer;            // used to timestamp incoming tags
00012 int State;
00013 char Tags[500];
00014 float Tstamp[500];
00015 int TagsElem=0;
00016 char TaggerID=0;
00017 char MyTagID=0;
00018 char LCDText[][9]=
00019    {"CHOOSE  ",
00020     "ID      ",
00021     "ABCDEFGH",
00022     "IJKLMNOP",
00023     "QRSTUVWX",
00024     "YZ      ",
00025     "TAGGED  ",
00026     "00 TIMES",
00027     "BY 0    ",};
00028 //int main() {
00029     //lcd.printf("Hello World!\n");
00030 //}
00031 
00032 void callback() {
00033     // Note: you need to actually read from the serial to clear the RX interrupt
00034     IRled=1;
00035     Tags[TagsElem] = device.getc();
00036     Tstamp[TagsElem] = timer.read();
00037     
00038     if (Tags[TagsElem] != MyTagID)
00039     {
00040     TagsElem++;
00041     }
00042     IRled=0;    
00043 }
00044 
00045 int tagcount()
00046 {
00047 float LastTagTime=0;
00048 int counter=0;
00049 for (int i=0;i<=TagsElem-2;i++)
00050 {
00051     if (TagsElem>2)
00052     {
00053         if ((Tags[i]==Tags[i+1]) && (Tags[i+1]==Tags[i+2]) && ('A'<=Tags[i]<='Z') && ((Tstamp[i+2]-Tstamp[i])<.25) && ((LastTagTime+.25)<Tstamp[i+2]))
00054         {
00055         TaggerID=Tags[i];
00056         LastTagTime = Tstamp[i+2];
00057         counter++;
00058         }
00059         
00060         
00061     }
00062 }
00063 return counter;
00064 }
00065 
00066 void scrnslct(char n)
00067 {
00068 int alpha;
00069 int row;
00070 int col;
00071 lcd.cls();
00072     if (n=='0')
00073         {
00074         lcd.printf("%s\n%s",LCDText[0],LCDText[1]);
00075         }
00076     if (n=='1')
00077         {
00078         lcd.printf("%s\n%s",LCDText[6],LCDText[7]);
00079         }
00080     if (n=='2')
00081         {
00082         lcd.printf("%s\n%s",LCDText[6],LCDText[8]);
00083         }
00084     if ('A'<=n<='Z')
00085         {
00086         alpha=n-'A';
00087         row=alpha/8;
00088         col=alpha%8;
00089         lcd.printf("%c",LCDText[2+row][col]);
00090         }
00091     else
00092     lcd.printf("ERROR1");                   //ERROR1= No screen selected by program, error not caused by user
00093 }
00094 
00095 int State0()
00096 {
00097     scrnslct('0');
00098 while(1)
00099 {
00100     if (Opt==1)
00101     {
00102     wait(.25);
00103     return 0;
00104     }
00105     if (Sel==1)
00106     {
00107     wait(.25);
00108     return 1;
00109     }
00110 }
00111 }
00112 
00113 int State1()
00114 {
00115 scrnslct('A');
00116 char c='A';
00117 while(1)
00118 {
00119 
00120     if (Opt==1)
00121     {
00122     wait(.2);
00123     
00124         if (c=='Z')
00125         {
00126             c='A';
00127         }
00128         else
00129         c++;
00130         scrnslct(c);
00131     }
00132     if (Sel==1)
00133     {
00134     wait(.25);
00135     MyTagID=c;
00136     return 2;
00137     }
00138 
00139 }
00140 }
00141 
00142 int State2()
00143 {
00144 timer.reset();
00145 TagsElem=0;
00146 memset(&Tags[0], '0', sizeof(Tags));
00147 int check=TagsElem;
00148 int c=tagcount();
00149 int compare=c;
00150 float timer1;
00151 int TagsOnes = 48;                  // 48 is the decimal value of the ASCII char '0'
00152 int TagsTens = 48;                  // a variable for the tens place and the ones place
00153 //LCDText[7][0]=TagsTens;
00154 //LCDText[7][1]=TagsOnes;
00155 scrnslct('1');
00156 timer.start();
00157 while (1)
00158 {
00159     c=tagcount();
00160     if(check != TagsElem)                              //only true if the IR sensor recieves data, probably a tag
00161     {
00162         if (compare != c)
00163         {
00164             tagled=1;
00165             LCDText[8][3]=TaggerID;
00166             scrnslct('2');
00167             wait(2);
00168             LCDText[7][0]=TagsTens+c/10;
00169             LCDText[7][1]=TagsOnes+c%10;
00170             scrnslct('1');
00171             compare = c;
00172             tagled=0;
00173         }
00174         check = TagsElem;
00175     }
00176     
00177     if (Sel==1)
00178     {
00179         wait(.1);
00180         timer1=timer.read();
00181         while ((timer.read()-timer1)<.25)                         //sends out my tag constantly for the amount of seconds to the right of the "<"
00182         {
00183             device.putc(MyTagID);
00184         }
00185     }
00186     if (Opt==1)
00187     {
00188         wait(.25);
00189         return 0;
00190     }
00191     
00192 }
00193 }
00194 
00195 int main() {
00196     device.attach(&callback);
00197     //lcd.cls();
00198     led.period_us(26.3);
00199     led=.5;
00200     device.baud(1200);
00201     State=0;
00202     while(1)
00203     {   
00204     
00205         if (State==0)
00206         {
00207         State=State0();
00208         }
00209         
00210         if (State==1)
00211         {
00212         State=State1();
00213         }
00214         
00215         if (State==2)
00216         {
00217         State=State2();
00218         }
00219     }    
00220 }