00001
00002
00003
00004
00005
00006
00007
00008 #include <avr/io.h>
00009 #include <avr/interrupt.h>
00010 #include <avr/signal.h>
00011 #include <avr/eeprom.h>
00012
00013 #include "robotsound.h"
00014 #include "robotu.h"
00015 #include "robotdo.h"
00016 #include "basic.h"
00017 #include "ir.h"
00018
00019
00020 SIGNAL(SIG_ADC) {
00021 static u08 irfrcount;
00022 static s16 irposstart;
00023 switch (admodus) {
00024 case irpos:
00025 irfrcount = 0;
00026 admodus = irfr2;
00027 irposstart = ADC;
00028 return;
00029 case irfr2:
00030 irfrcount++;
00031 if (irfrcount < 20) {
00032 PORTB ^= _BV(PB4);
00033 return;
00034 } else {
00035 irpos_value = ADC - irposstart;
00036 PORTB &= ~_BV(PB4);
00037 }
00038 break;
00039 case irstation:
00040 irstation_value = ADC;
00041 break;
00042 case light:
00043 light_value = ADC >> 2;
00044 break;
00045 case vcc:
00046 vcc_value = 1000 - ADC;
00047 break;
00048 case vcco:
00049 vcco_value = ADC;
00050 break;
00051 }
00052 ADCSRA = 0;
00053 admodus = none;
00054 }
00055
00056 SIGNAL(SIG_OVERFLOW0) {
00057 static u08 odp;
00058 struct measuretype mmodus;
00059 cli();
00060 if ((modus == ir_r)|(modus == ir_s)) {
00061 TCNT0 = 255-111;
00062 do_ir();
00063 sei();
00064 return;
00065 }
00066 if (modus == radar) {
00067 if (radar_value++ == 0)
00068 PORTD |= _BV(PD5);
00069 if ((radar_value > 1000) || (PINC & _BV(PC2))) {
00070 TCCR0 = _BV(CS01);
00071 modus = oldmodus;
00072 PORTD &= ~_BV(PD5);
00073 PORTD |= _BV(PD6);
00074 }
00075 sei();
00076 return;
00077 }
00078
00079 time ++;
00080 decode_position();
00081 mmodus = measuremodus[modus];
00082 if (time % _BV(5) == 0) playi();
00083 if (time % _BV(7) == 1) drive();
00084 if ((mmodus.light) & ((time % _BV(8) == 2))) start_ad(light);
00085 if ((mmodus.vcc) & ((time % _BV(8) == 4))) start_ad(vcc);
00086 if ((mmodus.vcco) & ((time % _BV(8) == 6))) start_ad(vcco);
00087 if ((mmodus.irstation) & ((time % _BV(6) == 8))) start_ad(irstation);
00088 if ((mmodus.radar) & ((time % _BV(10)== 9))) {
00089 start_radar();
00090 return;
00091 }
00092 if ((mmodus.irpos) & ((time % _BV(7) == 10))) start_ad(irpos);
00093 if ((mmodus.irr) & (ISIRON)) {
00094 start_ir_r();
00095 return;
00096 }
00097 switch (modus) {
00098 case irstring_s:
00099 do_ir_string();
00100 break;
00101 case boredom:
00102 do_boredom();
00103 break;
00104 case sleep:
00105 do_sleep();
00106 break;
00107 case connected:
00108 do_connected();
00109 break;
00110 case searching:
00111 do_searching();
00112 break;
00113 case flee:
00114 do_flee();
00115 break;
00116 case empty:
00117 do_empty();
00118 break;
00119 #ifdef SCRIPT
00120 case script:
00121 do_script();
00122 break;
00123 #endif
00124 #ifdef IRCONT
00125 case ircont:
00126 do_ircont();
00127 break;
00128 #endif
00129 default: SETMODUS (boredom);
00130 }
00131 if (vcc_value < BATLOW) needpower = 1;
00132 if (vcc_value < BATEMPTY) {
00133 RESETP;
00134 modus = empty;
00135 }
00136 olddippos = DIPPOS;
00137 TCNT0 = 0;
00138 sei();
00139 }
00140
00141 int main(void) {
00142 u16 i;
00143 #ifndef TEST
00144 for (i=0; i < 60000;i++) _NOP;
00145 #endif
00146 admodus = none;
00147 vcc_value = BATFULL;
00148 light_value = 255;
00149 start_sound(start_up, 1);
00150 switch (olddippos = DIPPOS) {
00151 case 0: modus = boredom; break;
00152 case 1: modus = connected; break;
00153 case 2: modus = ircont; break;
00154 case 3: modus = script; break;
00155 }
00156
00157 DDRB = _BV(DDB0)|_BV(DDB1)|_BV(DDB2)|_BV(DDB3)|_BV(DDB4);
00158 DDRD = _BV(DDD5)|_BV(DDD6)|_BV(DDD7);
00159 PORTD = _BV(PD0)|_BV(PD1)|_BV(PD2)|_BV(PD3);
00160
00161 TIMSK = TIMSK | _BV(TOIE0);
00162 TCNT0 = 0;
00163 TCCR0 = _BV(CS01);
00164
00165 TCCR1A= _BV(COM1A1)|_BV(COM1B1)|_BV(WGM10);
00166 TCCR1B= _BV(CS11)|_BV(CS10);
00167 TCNT1 = 0;
00168 OCR1A = 0;
00169 OCR1B = 0;
00170
00171 sei();
00172 for (;;)
00173 ;
00174 }