static void WaitSwitch(unsigned char sw){
	int cnt=0;
	while(ReadSwitch(sw)==0);
	while((ReadSwitch(sw)) || (cnt<20)){
		_delay_ms(1);
		if(ReadSwitch(sw)==0)
			cnt++;
		else
			cnt=0;
	}
}
예제 #2
0
파일: ByteBuffer.hpp 프로젝트: denghe/69net
 bool ByteBuffer::Read( T& v )
 {
     return ReadSwitch( *this, v );
 }
/*--------------------------------------------------------------------------------------------------*/
int InitEmoro(void){
	int ret_value = 0;
	int ADC_read;
	long Vin, Vin_decimal;

	char lcd[20];
	unsigned char d[7], chk=0;
	
	
	GLAM_EEPROMReadBytes(121,7, d);					/* address 121 -> LCD, 122 -> switches			*/
													/* 123 -> Accelerometer, 124 -> Gyroscope		*/
													/* 125 -> Compas, 126 -> Bluetooth, 127 -> Chk	*/
	for(unsigned char n=0; n<7; n++)				/* calculate checksum							*/
		chk^=*(d+n);
	
	if(chk^0xAA == 0){
		if(d[0] == 0xC8){							/* check LCD									*/
			if(Lcd.init() == 0){					/* initialize lcd								*/
				Lcd.backlightOn();					/* turn on lcd backlight						*/
				ret_value |= (LCD_AVAILABLE);		/* set return value -> LCD Available			*/
				initialized_emoro_hw = ret_value;
			}
		}
		if(d[1] == 0xC8){							/* check switches								*/
			InitSwitch();							/* initialize switch (SW1 - SW4)				*/
			ret_value |= (SW_AVAILABLE);			/* set return value -> Switch Available			*/
		}
		if(Acc.testConnection() & d[2] == 0xC8){	/* test Acc and check 							*/
			Acc.init();								/* initialize acceleration sensor BMA150		*/
			ret_value |= (ACC_AVAILABLE);			/* set return value -> ACC Available			*/
		}
		if(Gyr.testConnection() & d[3] == 0xC8){	/* test Gyr and check 							*/
			Gyr.initBasic();						/* initialize gyroscope L3GD20					*/
			ret_value |= (GYR_AVAILABLE);			/* set return value -> GYR Available			*/
		}
		if(Mag.testConnection() & d[4] == 0xC8){	/* test magnetometer and check 					*/
			Mag.init();								/* initialize magnetometer						*/
			ret_value |= (MAG_AVAILABLE);			/* set return value -> MAG Available			*/
		}
		if(d[5] == 0xC8){
			if(Bluetooth.init() == 0){				/* initialize bluetooth communication			*/				
				ret_value |= (BLUETOOTH_AVAILABLE);	/* set return value -> Bluetooth Available		*/
			}
		}
		
		ADC_read=CheckVoltage();						/* read input voltage				*/
		Vin=(long)(ADC_read*15/1024);					/* calculate voltage; whole number	*/
		Vin_decimal=(long)ADC_read*1500/1024-Vin*100;	/* calculate decimal places			*/
		if( Vin >= 5 & (Vin == 6 & Vin_decimal<20) ){	/* if Vin>=5V & Vin < 6.20V batt is */
			sprintf(lcd, "Vin=%d.%d", (int)Vin, (int)Vin_decimal);
			Serial.begin(9600);				    /* initialize Serial monitor UART 9600 bps	        */
			Serial.println((char*)"Voltage low:");
			Serial.println(lcd);
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();								/* low, for Vin<5V:USB power supply */
				Lcd.printString((char*)"Voltage low:");
				Lcd.locate(1,0);
				Lcd.printString(lcd);
			}
			for (int rep=0; rep<2; rep++){
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100); /* Mores code letter S(...) */
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(200);
					
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(200);/* Mores code letter O(---) */
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(200);
				tone(BUZ_BUILTIN, NOTE_C6, 150); _delay_ms(300);
					
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);	/* Mores code letter S(...) */
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(100);
				tone(BUZ_BUILTIN, NOTE_C6, 50); _delay_ms(400);
			}
			noTone(BUZ_BUILTIN);
			
			Lcd.clear();
			
		}
	}

	if(ret_value & MAG_AVAILABLE){					
		
		if(ReadSwitch(SW_2) == 1){				/* magnetometer calibration request?				*/
			Serial.begin(9600);
			Serial.println((char*)"Compass");          
			Serial.println((char*)"Calibration.....\n"); 
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Compass         ");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Calibration.....");
			}
			while(ReadSwitch(SW_2)==1);			/* wait button release								*/
			_delay_ms(300);
			
			/* mag calibration step 1																*/
			Serial.println((char*)"Lay board flat");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Lay board flat");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(1);
			_delay_ms(500);
			
			/* mag calibration step 2																*/
			Serial.println((char*)"Rotate 180 degre");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Rotate 180 degre");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");						
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(2);
			_delay_ms(300);
			
			/* mag calibration step 2																*/
			Serial.println((char*)"Flip board");          
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Flip board");
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");						
			}
			WaitSwitch(SW_1);	
			Mag.calibrate(3);
			_delay_ms(300);
			
			Serial.println((char*)"Calibration");          
			Serial.println((char*)"Complete\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();
				Lcd.printString((char*)"Calibration     ");	
				Lcd.locate(1,0);
				Lcd.printString((char*)"Complete        ");	
			}
			_delay_ms(1500);	
			Serial.println((char*)"Press button 1\n");          
			if(ret_value & LCD_AVAILABLE){
				Lcd.clear();	
				Lcd.locate(1,0);
				Lcd.printString((char*)"Press button 1");
			}
			while(ReadSwitch(SW_1)==0){
				int dir, inc, str;
				Mag.read(&dir, &inc, &str);
				sprintf(lcd, "Direction=%3d", dir);
				Serial.println(lcd);  
				if(ret_value & LCD_AVAILABLE){
					Lcd.locate(0, 0);
					Lcd.printString(lcd);
				}				        
			}
			Lcd.clear();
		}
	}		
	initialized_emoro_hw = ret_value; 
	return initialized_emoro_hw;	
}