Пример #1
0
/**
 * \brief Lightsensor application
 *
 * This application will output the lightsensor value read by the ADC both as
 * a raw value and by drawing a bar that represent the amount of light hitting
 * the sensor.
 * The raw value is sampled 200 times and the average value is used.
 */
void lightsensor_application(void)
{
	struct keyboard_event input_key;
	char string_buf[10];
	uint32_t lightsensor_val = 0;
	uint8_t scaled;
	uint8_t iterations = 0;

	// Clear screen
	gfx_mono_draw_filled_rect(0, 0, 128, 32, GFX_PIXEL_CLR);

	// Put icons at the bottom of the screen indicating light intensity
	gfx_mono_put_bitmap(&bitmap_moon, 1, 24);    // ~1 lux
	gfx_mono_put_bitmap(&bitmap_cloud, 30, 24);  // ~200 lux
	gfx_mono_put_bitmap(&bitmap_indoor, 60, 24); // ~400 lux
	gfx_mono_put_bitmap(&bitmap_sun, 118, 24);   // ~850 lux

	// Draw static strings outside the loop
	gfx_mono_draw_string("Lightsensor", 0, 0, &sysfont);
	gfx_mono_draw_string("Raw value:", 0, 8, &sysfont);

	while (true) {
		// Start an ADC conversion of the lightsensor
		lightsensor_measure();

		while (!lightsensor_data_is_ready()) {
			// Wait until the conversion is complete
		}

		lightsensor_val += lightsensor_get_raw_value();

		// Let's average some samples to be outputted
		if (iterations++ >= LIGHTSENSOR_NUM_SAMPLES) {
			iterations = 0;
			lightsensor_val /= LIGHTSENSOR_NUM_SAMPLES;
			snprintf(string_buf, sizeof(string_buf), "%4ld", lightsensor_val);
			gfx_mono_draw_string(string_buf, 70, 8, &sysfont);
			// Scale down to LCD width for drawing bar
			scaled = lightsensor_val >> 4;

			// Graphic bar representing the light level
			gfx_mono_draw_filled_rect(0, 17,scaled, 7, GFX_PIXEL_SET);
			gfx_mono_draw_filled_rect(scaled, 17,128 - scaled, 7,
					GFX_PIXEL_CLR);

			lightsensor_val = 0;
		}

		// exit if the "back" button has been pressed and released
		keyboard_get_key_state(&input_key);
		if ((input_key.keycode == KEYBOARD_BACK) &&
				(input_key.type == KEYBOARD_RELEASE)) {
			break;
		}
	}
Пример #2
0
int main (void)
{
	sysclk_init();
	board_init();
	pmic_init();
	gfx_mono_init();
	adc_sensors_init();
	// Enable display backlight
	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	cpu_irq_enable();
	
	while(true){
		
		if(state==1){
			start_game();
			}else if(state==2){
			tc_enable(&TCC0);
			tc_set_overflow_interrupt_callback(&TCC0, sun_count);
			tc_set_wgm(&TCC0, TC_WG_NORMAL);
			tc_write_period(&TCC0, 13500);
			tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO);
			tc_write_clock_source(&TCC0, TC_CLKSEL_DIV256_gc);
			
			tc_enable(&TCC1);
			tc_set_overflow_interrupt_callback(&TCC1, button_press);
			tc_set_wgm(&TCC1, TC_WG_NORMAL);
			tc_write_period(&TCC1, 62500);
			tc_set_overflow_interrupt_level(&TCC1, TC_INT_LVL_LO);
			tc_write_clock_source(&TCC1, TC_CLKSEL_DIV8_gc);
			
			gfx_mono_draw_string("SUN:   0", 0, 0, &sysfont);
			gfx_mono_draw_string(">", 0, cursor_position, &sysfont);
			gfx_mono_draw_string("Score:  0", 63, 0, &sysfont);
			
			randomPeta();
			
			char* score_string = NULL;
			uint16_t old_score = 0;
			
			for(j = 0; j <= 70; j++){
				
				if(sun_value > 10){
					
					lightsensor_measure();
					while (!lightsensor_data_is_ready()) {
						// Wait until the conversion is complete
					}
					if(lightsensor_get_raw_value() > 250){
						sun_value -= 10;
						sunBurst();
						gfx_mono_draw_filled_rect(12,8,114,24,GFX_PIXEL_CLR);
					}
				}
				

				if(score > old_score){
					sprintf(score_string, "%3d", score);
					gfx_mono_draw_string(score_string, 100, 0, &sysfont);
					old_score = score;
				}
				
				if(lose){
					state=3;
					break;
					}else if(zombie==0){
					state=4;
					break;
				}
				
				
				tampilkanPeta();
				tampilkanTembak();
				delay_ms(1000);
			}
			}else if(state==3){
			cpu_irq_disable();
			gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
			while(true){
				gfx_mono_draw_string("GAME OVER",36,8,&sysfont)	;
				gfx_mono_draw_string("You Lose",39,20,&sysfont)	;
			}
			}else if(state==4){
			cpu_irq_disable();
			gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
			while(true){
				gfx_mono_draw_string("GAME OVER",36,2,&sysfont)	;
				gfx_mono_draw_string("You Win",42,12,&sysfont)	;
				gfx_mono_draw_string("Score = ",30,22,&sysfont)	;
				char* score_string = NULL;
				sprintf(score_string, "%3d", score);
				gfx_mono_draw_string(score_string, 79, 22, &sysfont);
			}
		}
	}
	
}
Пример #3
0
void adc_application(void)
{
	struct keyboard_event input_key;
	char string_buf[10];
	uint32_t adca0_val = 0;
	uint32_t adca1_val = 0;
	uint32_t adca2_val = 0;
	uint32_t adca3_val = 0;
	uint32_t adcb0_val = 0;
	uint32_t adcb1_val = 0;
	uint32_t adcb2_val = 0;
	uint32_t adcb3_val = 0;

	// Clear screen
	gfx_mono_draw_filled_rect(0, 0, 128, 32, GFX_PIXEL_CLR);

	// Draw static strings outside the loop
	gfx_mono_draw_string("ADC0", 100, 0, &sysfont);
	gfx_mono_draw_string("ADC1", 100, 8, &sysfont);
	gfx_mono_draw_string("ADC2", 100, 16, &sysfont);
	gfx_mono_draw_string("ADC3", 100, 24, &sysfont);
	gfx_mono_draw_string("ADC4", 0, 0, &sysfont);
	gfx_mono_draw_string("ADC5", 0, 8, &sysfont);
//	gfx_mono_draw_string("ADC6", 0, 16, &sysfont);
//	gfx_mono_draw_string("ADC7", 0, 24, &sysfont);
	gfx_mono_draw_string("Light", 0, 16, &sysfont);
	gfx_mono_draw_string("NTC", 0, 24, &sysfont);
	
	while (true) {
			// Start an ADC conversion
			/*adc_start_conversion(&ADCA, ADC_CH0);
			adc_start_conversion(&ADCA, ADC_CH1);
			adc_start_conversion(&ADCA, ADC_CH2);
			adc_start_conversion(&ADCA, ADC_CH3);*/
			lightsensor_measure();
			ntc_measure();
			adc_measure();
			adc2_measure();
			
			while (!adc_data_is_ready() && !adc2_data_is_ready() && !ntc_data_is_ready() && !lightsensor_data_is_ready()) {
				// Wait until the conversion is complete
			}
			
			//adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);
			/*adc0_val = adc_get_result(&ADCA, ADC_CH0);
			adc1_val = adc_get_result(&ADCA, ADC_CH1);
			adc2_val = adc_get_result(&ADCA, ADC_CH2);
			adc3_val = adc_get_result(&ADCA, ADC_CH3);*/
			adca0_val = lightsensor_get_raw_value();
			adca1_val = ntc_get_raw_value();
			adca2_val = adc_get_raw_value();
			adca3_val = adc2_get_raw_value();
			snprintf(string_buf, sizeof(string_buf), "%4ld", adca0_val);
			gfx_mono_draw_string(string_buf, 30, 16, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adca1_val);
			gfx_mono_draw_string(string_buf, 30, 24, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adca2_val);
			gfx_mono_draw_string(string_buf, 30, 0, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adca3_val);
			gfx_mono_draw_string(string_buf, 30, 8, &sysfont);
			
			// Start an ADCB conversion
			/*adc_start_conversion(&ADCB, ADC_CH0);
			adc_start_conversion(&ADCB, ADC_CH1);
			adc_start_conversion(&ADCB, ADC_CH2);
			adc_start_conversion(&ADCB, ADC_CH3);*/
			adcb0_measure();
			adcb1_measure();
			adcb2_measure();
			adcb3_measure();
			
			while (!adcb0_data_is_ready() && !adcb1_data_is_ready() && !adcb2_data_is_ready() && !adcb3_data_is_ready()) {
				// Wait until the conversion is complete
			}
			
			//adc_wait_for_interrupt_flag(&ADCB, ADC_CH0);
			/*adc0_val = adc_get_result(&ADCA, ADC_CH0);
			adc1_val = adc_get_result(&ADCA, ADC_CH1);
			adc2_val = adc_get_result(&ADCA, ADC_CH2);
			adc3_val = adc_get_result(&ADCA, ADC_CH3);*/
			adcb0_val = adcb0_get_raw_value();
			adcb1_val = adcb1_get_raw_value();
			adcb2_val = adcb2_get_raw_value();
			adcb3_val = adcb3_get_raw_value();
			snprintf(string_buf, sizeof(string_buf), "%4ld", adcb0_val);
			gfx_mono_draw_string(string_buf, 60, 0, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adcb1_val);
			gfx_mono_draw_string(string_buf, 60, 8, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adcb2_val);
			gfx_mono_draw_string(string_buf, 60, 16, &sysfont);
			snprintf(string_buf, sizeof(string_buf), "%4ld", adcb3_val);
			gfx_mono_draw_string(string_buf, 60, 24, &sysfont);
			
			keyboard_get_key_state(&input_key);
			if ((input_key.keycode == KEYBOARD_ENTER) &&
					(input_key.type == KEYBOARD_RELEASE)) {
				break;
			}
	}
}