Main Page | File List | Globals

station.c

Go to the documentation of this file.
00001 //
00002 // C Implementation: stationu
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: Leonhard Klein <leoklein@gmx.net>, (C) 2004
00008 
00009 #include <avr/io.h>
00010 #include <avr/interrupt.h>
00011 #include <avr/signal.h>
00012 #include <avr/eeprom.h>
00013 #include <avr/pgmspace.h>
00014 #include "basic.h"
00015 #include "stationu.h"
00016 #include "ir.h"
00017 
00018 
00019 const u08 __ATTR_PROGMEM__ Pmenu0[]="\033[2J----------------------\n\r"
00020                                     "- Welcome to Leobot  -\n\r"
00021                                     "----------------------\n\r"
00022                                     "Keys:\n\r"
00023                                     " 1: Change loading current\n\r"
00024                                     " 2: Upload IR-Data\n\r"
00025                                     " 3: See & learn incoming ir-Data\n\r"
00026                                     " 4: Communicate to robot\n\r"
00027                                     " a,c,e: set mode of station\n\r\n\r"
00028                                     "Actual mode: \0";
00029 const u08 __ATTR_PROGMEM__ Pmenu1[]="\033[2JSet loading current: (+ and - to change, 0 to leave)\n\r"
00030                                     " Actual Value: \0";
00031 const u08 __ATTR_PROGMEM__ Pmenu2[]="\033[2JPress 0 to abort, any other key to continue\n\r\0";
00032 const u08 __ATTR_PROGMEM__ Pmenu3[]="\033[2JIncoming IR-Data:\n\r"
00033                                     "Keys: 0: Leave, +-: Ir-speed, 1..4: Learn string\n\r"
00034                                     "      r: Reset learned strings, c: Test learned\n\r"
00035                                     "STRING UNTIL NOW -->\0";
00036 const u08 __ATTR_PROGMEM__ Pmenu4[]="\033[2JROBOT-SPEAK: (send empty to leave)\n\r\0";
00037 const u08 __ATTR_PROGMEM__ Pempty[]="\n\rLOADING STATION EMPTY!!\0";
00038 const u08 __ATTR_PROGMEM__ Pstart[]="Starting up\0";
00039 const u08 __ATTR_PROGMEM__ Pirpulse[]="\n\rIR PULSE-->\0";
00040 const u08 __ATTR_PROGMEM__ Pirstring[]="\n\rIR STRING -->\0";
00041 
00042 u08 sendbuf[IRMSLENGTH+1];
00043 u08 recbuf[IRKEYS][IRMLENGTH];
00044 u08 pulsecount;
00045 u08 ircmp[IRKEYS];
00046 u08 oldsw7;
00047 u08 ircontbittime;
00048 u08 robotneedspower;
00049 
00050 #define WAIT10 for (i=0; i < 100; i++) for (i2 = 0; i2 < 100; i2++) { _NOP; }
00051 
00052 
00053 SIGNAL(SIG_OVERFLOW0)                                    /* Timer0 Overflow for threating; normal op: 2 kKz */
00054 {
00055   static u16 time;
00056   static u16 empty_c, diff_c;
00057   static s16 diff;
00058   s16 ocr1atemp;
00059   cli();                                                    /* SIGNAL doesn't stop all interrupts correctly (?) */
00060   if ((modus == ir_r)|(modus == ir_s)) {                    /* common check for common timing */
00061     TCNT0 = 255-112;                                        /* 13.888 = 36000Hz  111 */
00062     do_ir();
00063     sei();
00064     return;
00065   }
00066   time++;
00067   if (modus == empty) {
00068     if (time % _BV(12)==0) {
00069       uart_send_p(Pempty);
00070       RLEDON;
00071     }
00072     if (time % _BV(12) == 100) RLEDOFF;
00073     OCR1A = 0;
00074     return;
00075   }
00076   if (time % _BV(3) == 1) {
00077     start_ad(vcc);
00078     if (vcc_value < 600) {
00079       if (empty_c++ > 100) {
00080         lastkey = 1;
00081         modus = empty;
00082         empty_c = 0;
00083       }
00084     } else
00085       empty_c =0;
00086   }
00087   if (time % _BV(3) == 4)
00088     start_ad(vout);
00089   if (time % _BV(7) == 6) {
00090     diff = (vcc_value - vout_value + diff) >> 1;              /* mean of the last two values */
00091     if ((diff) < loadcur)
00092       ocr1atemp = OCR1A + 4;
00093     else
00094       ocr1atemp = OCR1A - 4;
00095     if (ocr1atemp > 255)
00096       ocr1atemp = 255;
00097     if (ocr1atemp < 0)
00098       ocr1atemp = 0;
00099     OCR1A = ocr1atemp;
00100   }
00101 
00102   RLEDOFF;
00103   GLEDOFF;
00104   if (modus == irstring_s) {
00105     do_ir_string();
00106     RLEDON;
00107   } else {
00108     if (SW7 != oldsw7) modus = (modus==connected)?away:connected;
00109     if (modus == away) {
00110       DO_PING;                        /* 1kHz */
00111       if (diff > 30) {
00112         lastkey = 1;
00113         modus = connected;
00114         STOP_PING;
00115       }
00116     } else if (modus == connected) {
00117       GLEDON;
00118       if ((diff < 30) & (robotneedspower)) diff_c++; else diff_c = 0;
00119       if (diff_c > 8000) modus = away;
00120       if ((!pulsevalid) & (ISIRON)) {
00121         if (pulsecount > 5) start_ir_r();
00122         pulsecount = 0;
00123       } else if (pulsecount < 100) pulsecount++;
00124     }
00125   }
00126   oldsw7 = SW7;
00127   sei();
00128 }
00129 
00130 int main(void) {
00131   u08 menumode=0;
00132   u08 recmode;
00133   u08 oldmenumode;
00134   u16 i,i2;
00135   s08 s08_1;
00136   u08 irspos;
00137 
00138   modus = connected;
00139   admodus = none;
00140   vcc_value = 151;
00141   vout_value = 151;
00142   irerror = 0;
00143   oldsw7 = SW7;
00144   loadcur = 71;                                       /* at 2,2Ohm  350mA */
00145   irbittime = 12;
00146   initchip();
00147   CLRSCR;
00148   uart_send_p(Pstart);
00149 #ifndef TEST
00150   for (i=0; i < 8;i++) {
00151     for (i2=0; i2 < 50000;i2++) _NOP;
00152     uart_send(".");
00153     if (i%2==1) {
00154       GLEDON;
00155       RLEDOFF;
00156     } else {
00157       RLEDON;
00158       GLEDOFF;
00159     }
00160   }
00161   GLEDOFF;
00162   RLEDOFF;
00163 #endif
00164   while (1) {                                         /* Terminal */
00165     switch (menumode) {
00166     case 0:
00167       uart_send_p(Pmenu0);
00168       if (modus==away)
00169         uart_send("away\n\r");
00170       else if (modus==connected)
00171         uart_send("connected\n\r");
00172       else if (modus == empty)
00173         uart_send("empty\n\r");
00174       else uart_send("IR-Modus\n\r");
00175       break;
00176     case 1:
00177       uart_send_p(Pmenu1);
00178       uart_send_int(loadcur, 10);
00179       uart_send("\n\rvcc ->");
00180       uart_send_int(vcc_value, 10);
00181       uart_send("\n\rvout->");
00182       uart_send_int(vout_value, 10);
00183       uart_send("\n\rOCR ->");
00184       uart_send_int(OCR1A, 10);
00185       break;
00186     case 2:
00187       uart_send_p(Pmenu2);
00188       break;
00189     case 3:
00190       if (lastkey) {
00191         CLRSCR;
00192         uart_send_p(Pmenu3);
00193         irrstring[irrstring[0]+1]=0;
00194         uart_send_pascal(irrstring);
00195         uart_send("\n\rIR-Bittime: ");
00196         uart_send_int(irbittime,10);
00197         for (i2=0;i2 < IRKEYS;i2++) {
00198           uart_send("\n\rRECBUF");
00199           uart_send_int(i2+1,10);
00200           uart_send("-->");
00201           for (i=0; (i<=recbuf[i2][0]); i++) {
00202             uart_send_int(recbuf[i2][i], 16);
00203           }
00204         }
00205       }
00206       break;
00207     case 4:
00208       if (oldmenumode != menumode) {
00209         CLRSCR;
00210         uart_send_p(Pmenu4);
00211         uart_send_pascal(sendbuf);
00212       }
00213       uart_send("\033[2K\rStation->");
00214       uart_send_pascal(sendbuf);
00215       break;
00216     }
00217     pulsevalid = 0;
00218     stringvalid = 0;
00219     lastkey=0;
00220     oldmenumode = menumode;
00221     sei();
00222     do {
00223 #ifdef TEST
00224       SIG_OVERFLOW0();
00225       if (modus > empty) SIG_ADC();
00226 #endif
00227       _NOP;
00228     } while (!((lastkey)|(pulsevalid)|(stringvalid)));
00229     cli();
00230     if (stringvalid) {
00231       for (i = 0; i < irrstring[0]; i++) {
00232         if (irrstring[i]=='E') robotneedspower = 1;
00233         if (irrstring[i]=='F') robotneedspower = 0;
00234       }
00235     }
00236     switch (menumode) {
00237     case 0:
00238       switch (lastkey) {
00239       case '1':
00240       case '2':
00241       case '3':
00242       case '4':
00243         menumode = lastkey-'0';
00244         break;
00245       case 'a':
00246         modus = away;
00247         break;
00248       case 'c':
00249         modus = connected;
00250         STOP_PING;
00251         break;
00252       case 'e':
00253         modus = empty;
00254         break;
00255       }
00256       break;
00257     case 1:
00258       switch (lastkey) {
00259       case '+':
00260         loadcur++;
00261         break;
00262       case '-':
00263         loadcur--;
00264         break;
00265       case '0':
00266         menumode = 0;
00267         break;
00268       }
00269       break;
00270     case 2:
00271       if ((lastkey != '0') & (lastkey != 0)) {
00272         irbittime = IRDATABITTIME;                 // standard timing for data transmission
00273         uart_send("Transmitting..");
00274         sendbuf[0]='I';
00275         sendbuf[1]=0;
00276         ir_string_send(sendbuf);
00277         WAIT10;
00278         for (irspos = 0; irspos < IRKEYS*IRMLENGTH; irspos++) {
00279           itoa16((recbuf[irspos / IRMLENGTH][irspos % IRMLENGTH]), sendbuf);
00280           sendbuf[2]=0;
00281           if (irspos%20==0) uart_send("\n\r");
00282           uart_send(".");
00283           ir_string_send(sendbuf);
00284           WAIT10;
00285         }
00286         itoa16(ircontbittime, sendbuf);
00287         ir_string_send(sendbuf);
00288         WAIT10;
00289         sendbuf[1] = 'Q';
00290         ir_string_send(sendbuf + 1);
00291         sendbuf[0] = 0;
00292         uart_send("\n\rDone");
00293         lastkey = 0;
00294         sei();
00295         while (!lastkey);
00296         if (stringvalid) uart_send(irrstring+1);
00297         cli();
00298       }
00299       menumode = 0;
00300       break;
00301     case 3:
00302       if (pulsevalid) {
00303         uart_send_p(Pirpulse);
00304         for (i=0; (i <= irrbuf[0]); i++) {
00305           uart_send_int(irrbuf[i], 16);
00306         }
00307         if (recmode == 0) {
00308           uart_send("\n\rCMP:");
00309           i2 = 0;
00310           for (i=0;i < IRKEYS;i++) {
00311             uart_send_int(ircmp[i]=ir_comp(recbuf[i],irrbuf),10);
00312             if (i2<ircmp[i]) i2 = ircmp[i];
00313           }
00314           uart_send(" WINNER:----->");
00315           for (i = 0; i < IRKEYS; i++)
00316             if (i2 == ircmp[i]) uart_send_int(i,10);
00317         } else if (recmode < IRKEYS+1) {
00318           if (recbuf[recmode-1][0] == IRMLENGTH) {
00319             for (i2 = 0; i2 < IRKEYS; i2++) recbuf[i2][0] = irrbuf[0];
00320           }
00321           s08_1 = (recbuf[recmode-1][0]-irrbuf[0]);
00322           if ((s08_1 > -2) && (s08_1 < 2))
00323             ir_and(recbuf[recmode-1], irrbuf);
00324         }
00325         irrbuf[0]=0;
00326       }
00327       if (stringvalid) {
00328         uart_send_p(Pirstring);
00329         uart_send_pascal(irrstring);
00330         irrstring[0] = 0;
00331       }
00332       switch (lastkey) {
00333       case '+': if (irbittime <100) irbittime++;
00334         break;
00335       case '-': if (irbittime > 2) irbittime--;
00336         break;
00337       case '0': menumode = 0;
00338         break;
00339       case '1':
00340       case '2':
00341       case '3':
00342       case '4':
00343         recmode = lastkey - '0';
00344         break;
00345       case '9': recmode = 9;
00346         break;
00347       case 'c': recmode = 0;
00348         break;
00349       case 'r': for (i2 = 0; i2 < IRKEYS; i2++) {
00350           for (i = 1; i < IRMLENGTH; recbuf[i2][i++] = 0xff);
00351           recbuf[i2][0] = IRMLENGTH;
00352         }
00353         ircontbittime = irbittime;
00354         break;
00355       }
00356       break;
00357     case 4:
00358       irbittime = IRDATABITTIME;                 // standard timing for data transmission
00359       if (stringvalid) {
00360         uart_send("\033[2K\rRobot->");
00361         uart_send_pascal(irrstring);
00362         if (irrstring[1]=='S') {
00363           menumode = 0;
00364           modus = away;
00365         }
00366         irrstring[0]=0;
00367         uart_send("\n\r");
00368       }
00369       switch (lastkey) {
00370       case 13:
00371         if (!sendbuf[0])
00372           menumode = 0;
00373         else {
00374           sendbuf[sendbuf[0]+1]=0;
00375           uart_send("\n\rSending...");
00376           ir_string_send(sendbuf+1);
00377           sendbuf[0] = 0;
00378         }
00379       case 0:
00380         break;
00381       case 8:
00382       case 127:
00383         if (sendbuf[0]>0) sendbuf[0]--;
00384         break;
00385       default:
00386         if (sendbuf[0]<IRMSLENGTH) sendbuf[0]++;
00387         sendbuf[sendbuf[0]]=lastkey;
00388       }
00389     }
00390   }
00391 }

Generated on Wed Feb 2 21:19:12 2005 for Station by doxygen 1.3.6