Exemple #1
0
int main(void) {
	
	// Initialise the LED.
	led_init();
	
	// Initialise the IR decoder.
	ir_init();
	
	// Initialise the USB keyboard handler.
	usb_keyboard_init();

	// Initialise timer 0 to use it as a 4ms timer.
	TCCR0A |= _BV(WGM01); // Mode = 2, CTC.
	TCCR0B |= _BV(CS02); // clkIO/256
	OCR0A = ((256 * 4) / ((1000.0 * 256 * 256) / F_CPU)) - 1; // Desired interrupt frequency is once every 4ms.
	TIMSK0 |= _BV(OCIE0A);
	
	// Enable interrupts.
	sei();
	
	// Flash the LED to indicate we're running.
	led_blink(LED_BLINK_PATTERN_3_MEDIUM);
	
	// Infinite loop.
	for(;;) {
		// USB polling.
		usb_keyboard_poll();
	}
}
Exemple #2
0
void lf_init() {
	motor_init();
	ir_init();
	
	sum = 0;
	previous = 0;
}
Exemple #3
0
void init_all() {
	init();
	motor_init();
	ir_init();
	init_lcd();
	wait();
}
Exemple #4
0
int main(void)
{
	char buf[16];
	int  r;

	ir_init();

	r = ir_snprintf(buf, sizeof(buf), "hello");
	assert(streq(buf, "hello"));
	assert(r == 5);

	r = ir_snprintf(buf, sizeof(buf), "nums: %d %u!", 32, 46);
	assert(streq(buf, "nums: 32 46!"));
	assert(r == 12);

	ir_tarval *tv = new_tarval_from_long(123, mode_Iu);
	r = ir_snprintf(buf, sizeof(buf), "tv: %+F\n", tv);
	assert(streq(buf, "tv: 0x7B\n"));
	assert(r == 9);

	r = ir_snprintf(buf, sizeof(buf), "%d %d %d %d %d %d %d", 1234, 1234, 1234, 1234, 1234, 1234, 1234);
	assert(streq(buf, "1234 1234 1234 "));
	assert(r == 34);

	r = ir_snprintf(buf, 4, "%+F\n", tv);
	assert(streq(buf, "0x7"));
	assert(r == 5);

	r = ir_snprintf(buf, 8, "%I", new_id_from_str("Hello World"));
	assert(streq(buf, "Hello W"));
	assert(r == 11);

	return 0;
}
Exemple #5
0
void all_init() {
	init();
	ir_init();
	init_lcd();
	motor_init();
	wait();
	
}
Exemple #6
0
int main( void ) 
{
  uint8_t init_cpt;
  /* init peripherals */
  timer_init(); 
  modem_init();
  adc_init();
#ifdef CTL_BRD_V1_1  
  adc_buf_channel(ADC_CHANNEL_BAT, &buf_bat);
#endif
  spi_init();
  link_fbw_init();
  gps_init();
  nav_init();
  ir_init();
  estimator_init();
#	ifdef PAPABENCH_SINGLE
		fbw_init();
#	endif

  /* start interrupt task */
  //sei(); /*Fadia*/

  /* Wait 0.5s (for modem init ?) */
  init_cpt = 30;
  _Pragma("loopbound min 31 max 31")
  while (init_cpt) {
    if (timer_periodic())
      init_cpt--;
  }

  /*  enter mainloop */
#ifndef NO_MAINLOOP
  while( 1 ) {
#endif
    if(timer_periodic()) {
      periodic_task();
#		if PAPABENCH_SINGLE
			fbw_schedule();
#		endif
	}
    if (gps_msg_received) 
    {
	/*receive_gps_data_task()*/
	parse_gps_msg();
	send_gps_pos();
        send_radIR();
        send_takeOff();
    }
    if (link_fbw_receive_complete) {
      link_fbw_receive_complete = FALSE;
      radio_control_task();
    }
#ifndef NO_MAINLOOP
  } 
#endif
  return 0;
}
void Fable_init(){
		DDRA=0xFF;//PORTA is output

		//serial_initialize(9600);
		serial_initialize(57600);

		//serial_set_rx_callback(&serial_receive_data);
		//Init Timer
//		TCCR0A = (1<<COM0A1)|(1<<COM0A0);  //Set OC0A on Compare Match
//		TCCR0B = (1<<FOC0A)|(1<<CS01)|(1<<CS00); //Force Output Compare A and 64 prescaler
//		OCR0A = 125;	//1ms according to KAVRCalc (8Mhz, 1msm, 64 prescaler, 0.0% error)
//		TIMSK0 = 1<<OCIE0A; 	// Enable Timer 0 Output Compare A Match Event Interrupt

//		TCCR0A = (1<<WGM01);//Clear on compare |(1<<COM0A0);  //Set OC0A on Compare Match
//		TCCR0B = (1<<CS01)|(1<<CS00); //64 prescaler
//		OCR0A = 125;	//1ms -> 8M/2*64*1000
//		TIMSK0 = 1<<OCIE0A; 	// Enable Timer 0 Output Compare A Match Event Interrupt
		start_system_timer();
		accel_init();
		i2c_init(400);// init i2c 100 khz
		ext_uart_init();
		ir_init(EXT_UART_0);
		ir_init(EXT_UART_1);
		ir_init(EXT_UART_2);
		ir_init(EXT_UART_3);

		gyro_set_mode(GYRO_MODE_ON);

		sei();//enable interrupts
		ase_printf("fw initialized\n");
/*
		//ext_adc_init();//only on new board
		ext_uart_enable_int_rx(1,&ext_uart_receive);


		ir_init(EXT_UART_2);
		ir_init(EXT_UART_3);

		gyro_set_mode(GYRO_MODE_ON);// turn on gyroscope

		//dxl_initialize(0,207); //init dinamixel, baud= 57600 (to check)

		//printf("External UART %s\n",i2c_read_byte(0xd8,0x1c)==1?"OK":"NOT OK");//check communication works
*/
}
Exemple #8
0
char ir_getkey(unsigned char codebell){   //bien x dung xac nhan dat timeout, va dieu khien coi cac loai
	if((codebell&(~diir_))==offbell){bell=offbell;ring=1;}
	ir_init();
	thulai:
	flagstatus|=chongat_;
	//Hearder       3554  1637
	for ( n=0;n<100;n++){
		delay_us(20);
		if(ir_in())goto thulai;
	}
	//if((bell!=offbell)){bell=offbell;ring=1;}	
	
	while(!ir_in());
	for(n=0;n<10;n++){
		delay_us(100);
		if(!ir_in()){goto thulai;}
	}
	while (ir_in());
	flagstatus&=(~chongat_);//xoa ko cho ngat thuc hien ham fuc tap
	//Het Hearder bat dau thu address hay con goi predata
	address=0;
	for(n=0;n<16;n++){
		while(!ir_in());
		delay_us(chobit);
		address=(address<<1);
		if(ir_in()==0){address|=1;}
		while(ir_in());
	}
	if((int)loaidk!=address){goto thulai;}  //kiem tra toi day la dung toi phan sau thi sai ????
	//Thu xong dia chi tiep theo thu lenh
	for(n=0;n<32;n++){
		while(!ir_in());
		delay_us(chobit);
		command=(command<<1);
		if(ir_in()==0){command|=1;}
		while(ir_in());
	}
	
	if((int)(command>>16)!=loailenh){goto thulai;} //toi day van dang dung
	
	if( ((char)(((int)command)>>8))!=((char)command+1)){goto thulai;}
	
	for(n=0;n<sizeof(lenhgoc);n++){    //chuyen lenh sang lenh chuan hoa;
		if(lenhgoc[n]==(char)command){
			led_lcd=0;
			n=lenhchuanhoa[n];
			setbell(codebell);//cai dat am bao dua tren thong so truyen vao codebell
			if(codebell&diir_){sprintf(lcd_buff,"%02u",(uint)n);lcd_puts(lcd_buff);}
			if(n==hienthoigian){lcd_clr(lines);hienthithoigian(&timesys);}
			if(n==thoat){eeprom_reads(adee_env(0),sizeof(env),env);longjmp(env,'i');};
			//delay_ms(100);
			return n;
		}
	}
	goto thulai;
}
Exemple #9
0
int main(void)
{
    wdt_disable();
    cli();

    DDRC=0xFE;

    DDRD=0xFA;//P2 input
    PORTD=0xFF;//pullup
    DDRB=0xFf;
    uart_init();
    uart_stdio();

    ///set time
    char buff[10];
    strcpy(buff, __TIME__);
    buff[2]='\0';
    buff[5]='\0';

    hour=atoi(&buff[0]);
    min=atoi(&buff[3]);
    sec=atoi(&buff[6]);

    timer2();
    sei();
    int m=0;
    /////EXTERNAL LIBS INIT

    ir_init();

    ////////////////////////
    for(;;){    /* main event loop */

    leds_put(hour/10,m);
    leds_put(hour%10,m);
    leds_put(min/10,m);
    leds_put(min%10,m);
    leds_put(sec/10,m);
    leds_put(sec%10,m);
    leds_strobe();
    _delay_ms(1);

    leds_put(17,m);
    leds_put(17,m);
    leds_put(17,m);
    leds_put(17,m);
    leds_put(17,m);
    leds_put(17,m);
    leds_put(17,m);
    leds_strobe();

    _delay_ms(10);
    }
    return 0;
}
Exemple #10
0
int s_video_helper_irman_init (char *port)
{
	int ret;
	ret = ir_open_port(port);
	if (ret == -1)
		return -1;
	ret = ir_init(port);
	if (ret == -1)
		return -1;
	return ir_get_portfd();
}
int main(void)
{
	ir_init();

	check_mode(mode_F);
	check_mode(mode_D);
#if LDBL_MANT_DIG == 64
	ir_mode *mode_E = new_float_mode("E", irma_x86_extended_float, 15, 64,
	                                 ir_overflow_min_max);
	check_mode(mode_E);
#endif
}
Exemple #12
0
void main(void)
{  	
  unsigned int  channel = CHANNEL;
  unsigned char data = 0x07;
  
  // DBGU output configuration
  TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
  
  // Configuration PIT (Periodic Interrupt Timer)
  ConfigurePit();
  // Configuration TC (Timer Counter)
  ConfigureTc();
  // Configuration PIO (Paralell In and Out port), Init Interrupt on PIO
  ConfigureButtons();
  ConfigureLeds();
  // Configuration Radio Module nRF24L (PIO and SPI), ConfigureButtons must be executed before
  ConfigureNRF24L();
  ConfigureUSART0();
  ConfigureUSART1();
  //initialize proximity sensor
  ir_init();
  Global_Variable_Init();
  
  while(Timer0Tick<2); // wait until NRF24L01 power up
  nrf24l01_power_up(True);
  while(Timer0Tick<4); // wait until NRF24L01 stand by
  Timer0Tick = 0;
  //initialize the 24L01 to the debug configuration as RX and auto-ack disabled
  nrf24l01_initialize_debug(True, nrf_TX_RX_SIZE, False);
  nrf24l01_write_register(0x06, &data, 1);
  nrf24l01_set_as_rx(True);
  Delay_US(130);
  nrf24l01_set_rf_ch(channel);
  nrf24l01_flush_rx();
  Delay_US(300);
  
  while (1) { 
    if(Timer0Tick!=0){
      Timer0Tick = 0;
      Check_Battery(0);
      odometry(0);
      ProxRead_m();
      Send_Coord();
      Delay_US(10000);//give time for the coming message
      feedbackController(goalx, goaly, goaldist);      
    }
    Check_Wireless();
  }//while
}//main
Exemple #13
0
int main (void)
{
    uint8_t size = 0;
    code_t codeseq[CODESEQ_LEN_MAX];
    uint8_t count = 0;

    system_init ();
    tinygl_init (LOOP_RATE);
    tinygl_font_set (&font3x5_1);
    tinygl_text_mode_set (TINYGL_TEXT_MODE_SCROLL);
    tinygl_text_dir_set (TINYGL_TEXT_DIR_ROTATE);
    tinygl_text_speed_set (MESSAGE_RATE);

    navswitch_init ();
    ir_init ();

    pacer_init (LOOP_RATE);

    show_num ('W', count);

    /* Paced loop.  */
    while (1)
    {
        /* Wait for next tick.  */
        pacer_wait ();

        tinygl_update ();

        navswitch_update ();

        if (navswitch_push_event_p (NAVSWITCH_PUSH))
        {
            transmit (codeseq, size);
            show_char ('T');
        }

        if (ir_rx_get ())
        {
            size = capture (codeseq, CODESEQ_LEN_MAX);
            show_num (size == 0 ? 'E' : 'R', size);
            count++;
//            size = capture (codeseq, 9);
//            show_char ('0' + size);
        }
    }

    return 0;
}
Exemple #14
0
int irman_init(void)
{
	if(!tty_create_lock(hw.device))
	{
		logprintf(LOG_ERR,"could not create lock files");
		return(0);
	}
	if((hw.fd=ir_init(hw.device))<0)
	{
		logprintf(LOG_ERR,"could not open %s",hw.device);
		logperror(LOG_ERR,"irman_init()");
		tty_delete_lock();
		return(0);
	}
	return(1);
}
Exemple #15
0
void readings_stream_mode()
{
	const uint8_t BUF_LENGTH = 100;
	char buf[BUF_LENGTH];

	enum {
		reading_ir = 1,
		reading_sonar = 2,
		reading_sonar_mode = 3
	} reading;

	init_push_buttons();
	lcd_init();

	ir_init();
	sonar_init();

	usart_init();
	usart_drain_rx();

	while (true) {
		switch (wait_button("Readings Stream")) {
		case reading_ir:
			snprintf(buf, BUF_LENGTH, "IR: %u\n", ir_raw_reading());
			break;

		case reading_sonar:
			snprintf(buf, BUF_LENGTH, "Sonar: %u\n", sonar_raw_reading());
			break;

		case reading_sonar_mode:
			while (true) {
				snprintf(buf, BUF_LENGTH, "Sonar: %u\n", sonar_raw_reading());
				usart_tx_buf(buf);
			}
			break;

		default:
			buf[0] = '\0';
		}

		usart_tx_buf(buf);
	}
}
Exemple #16
0
int main (void)
{
	uint8_t c;

	serial_init();
	fdevopen(serial_putchar, serial_getchar);

	ir_init();

	sei(); /* enable interrupts */

	while (1)
	{
		c = ir_get();
		printf("got ir cmd %d\n\r", c);
	}

	return 1;
}
Exemple #17
0
void doIrLoop()
{
    //adc_set_vref(ADC_INTERNAL_VREF);//apply 3.1 volts to ADC_AREF (see )
    //TODO setup where to get ref voltage from
    //TODO create method find_good_prescaler_for_adc_based_on_f_cpu

    //TODO this table is for Jim's IR sensor with 3V applied to AREF
    //TODO this table's distance values is in inches * 10 (e.g. 36" = 360)
    //TODO convert to mm or cm
    list_t *lookup_table = create_jims_ir_sensor_lookup_table();

    ir_init(ADC_ONE_64TH, ADC_AREF, 2);
    while (1)
    {
        unsigned voltage = ir_read_voltage_avg(5);
        unsigned calculatedDist = ir_lookup_distance(lookup_table, voltage);
        printf0("%d volts \t\t calculatedDist= %u\r\n", voltage, calculatedDist);
    }
}
int main(void)
{
	_delay_ms(250);

	servo_init();
	ir_init();
	bluetooth_init();
	ultrasonic_init();
	buttons_init();
	sei();
	
    while(1) {
		comm_exec();
		move_proc();
		
		if (button_get_state(1)) move_enable(false);
		if (button_get_state(2)) move_enable(true);
    }
}
Exemple #19
0
void vPapabenchInit()
{
    mode = MODE_AUTO;
    pprz_mode = PPRZ_MODE_HOME;
    timer_init();
    modem_init();
    adc_init();
#ifdef CTL_BRD_V1_1
    adc_buf_channel(uint8_t adc_channel, struct adc_buf *s);
#endif 
    spi_init();
    link_fbw_init();
    gps_init();
    nav_init();
    ir_init();
    estimator_init();
#ifdef PAPABENCH_SINGLE
    fbw_init();
#endif
}
/*
 * Initialize start code
 * save_func - Pointer to function to save data (mandatory)
 * load_func - Pointer to function to load data (mandatory)
 * programming_done_func - Function to be called when programming is done (can be left as 0)
 * state_change_func - Function to be called when the state changes (can be left as 0)
 */
char start_init(void(*save_func)(START_DATA *data),
				void(*load_func)(START_DATA *data),
				void(*programming_done_func)(char new_stop_cmd),
				void(*state_change_func)(char state)) {

	if (!save_func || !load_func) {
		return -1;
	}

	// Set up function pointers
	start_save_func = save_func;
	start_load_func = load_func;
	start_programming_done_func = programming_done_func;
	start_state_change_func = state_change_func;

	// Load data from user-provided function
	start_load_func((START_DATA*)&start_save_data);

	// Make sure state is valid
	if (start_save_data.state != START_STATE_POWER_ON &&
		start_save_data.state != START_STATE_STARTED &&
		start_save_data.state != START_STATE_STOPPED_SAFE &&
		start_save_data.state != START_STATE_STOPPED) {
		start_save_data.state = START_STATE_POWER_ON;
		// Assume stop cmd is invalid as well, so set the default one
		start_save_data.stop_cmd = START_DEFAULT_STOP_CMD;
	}

	start_state = start_save_data.state;

	// Initialize IR module
	ir_init();
	ir_set_data_handler(start_irdata_handler);

	// Notify user about state update
	if (start_state_change_func) {
		start_state_change_func(start_state);
	}

	return 0;
}
Exemple #21
0
int main(void)
{
	ir_init();

	ir_type   *type   = new_type_primitive(get_modeIs());
	ident     *id1    = new_id_from_str("foo");
	ir_type   *glob   = get_glob_type();
	ir_entity *x      = new_global_entity(glob, id1, type, ir_visibility_external, IR_LINKAGE_DEFAULT);
	assert(get_entity_owner(x) == glob);

	ident     *id2    = new_id_from_str("bar");
	ir_type   *cls    = new_type_class(id2);
	set_entity_owner(x, cls);
	assert(get_entity_owner(x) == cls);

	ir_entity *gx = ir_get_global(id1);
	assert (NULL == gx);

	set_entity_owner(x, glob);
	assert(get_entity_owner(x) == glob);

	return 0;
}
Exemple #22
0
int main(void) {

#ifdef USART_DEBUG
	usart0_init();
#endif

	init_pwm();
	ir_init();
	control_init();
	fe_init();
	
	sei();

#ifdef USART_DEBUG
	usart0_puts("Initialized\n\r");
#endif

	while (1) {
		ir_handler();
		control_handler();
		pwm_handler();
		fe_handler();
	}
}
Exemple #23
0
static void init(void)
{
    stack_init(&stack);
    ir_init();
}
/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval <0      Error.
 */
MODULE_EXPORT int
irmanin_init (Driver *drvthis)
{
	PrivateData *p;
	char *ptrdevice = NULL;
	char *ptrconfig = NULL;
	int i;

	/* Allocate and store private data */
	p = (PrivateData *) calloc(1, sizeof(PrivateData));
	if (p == NULL)
		return -1;
	if (drvthis->store_private_ptr(drvthis, p))
		return -1;

	/* Read config file */

	/* What device should be used */
	strncpy(p->device, drvthis->config_get_string(drvthis->name, "Device", 0,
						   ""), sizeof(p->device));
	p->device[sizeof(p->device)-1] = '\0';
	if (p->device[0] != '\0') {
		report(RPT_INFO, "%s: using Device %s", drvthis->name, p->device);
		ptrdevice = p->device;
	}

	/* What config file should be used */
	strncpy(p->config, drvthis->config_get_string(drvthis->name, "Config", 0,
						   ""), sizeof(p->config));
	p->config[sizeof(p->config)-1] = '\0';
	if (p->config[0] != '\0')
		ptrconfig = p->config;

	/* End of config file parsing */

	if (ir_init_commands(ptrconfig, 1) < 0) {
		report(RPT_ERR, "%s: error initialising commands: %s", drvthis->name, strerror(errno));
		return -1;
	}

	p->portname = ir_default_portname();
	if (p->portname == NULL) {
		if (ptrdevice != NULL) {
			p->portname = ptrdevice;
		} else {
			report(RPT_ERR, "%s: error no device defined", drvthis->name);
			return -1;
		}
	}

	for (i = 1; codemap[i].irman != NULL; i++) {
		if (ir_register_command((char *) codemap[i].irman, i) < 0) {
			if (errno == ENOENT) {
				report(RPT_WARNING, "%s: no code set for `%s'",
					drvthis->name, codemap[i].irman);
			} else {
				report(RPT_WARNING, "%s: error registering `%s': %s",
					drvthis->name, codemap[i].irman, strerror(errno));
			}
		}
	}

	errno = 0;
	if (ir_init(p->portname) < 0) {
		report(RPT_ERR, "%s: error initialising Irman %s: %s",
			drvthis->name, p->portname, strerror(errno));
		return -1;
	}

	report(RPT_DEBUG, "%s: init() done", drvthis->name);

	return 0;  // return success
}
Exemple #25
0
int
main(void)
{
  wdt_disable();

  // un-reset ethernet
  ENC28J60_RESET_DDR  |= _BV( ENC28J60_RESET_BIT );
  ENC28J60_RESET_PORT |= _BV( ENC28J60_RESET_BIT );
  
  led_init();
  LED_ON();

  spi_init();

  eeprom_init();

  // if we had been restarted by watchdog check the REQ BootLoader byte in the
  // EEPROM ...
  if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) {
    eeprom_write_byte( EE_REQBL, 0 ); // clear flag
// TBD: This is useless as button needs to be pressed - needs moving into bootloader directly
//    start_bootloader();
  }


// Setup OneWire and make a full search at the beginning (takes some time)
#ifdef HAS_ONEWIRE
  i2c_init();
  onewire_Init();
  onewire_FullSearch();
#endif

  // Setup the timers. Are needed for watchdog-reset

#if defined (HAS_IRRX) || defined (HAS_IRTX)
  ir_init();
  // IR uses highspeed TIMER0 for sampling 
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif

  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;
#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel = (DISPLAY_USB|DISPLAY_RFROUTER);
#else
  display_channel = DISPLAY_USB;
#endif

  ethernet_init();
    
  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_IRRX
    ir_task();
#endif
#ifdef HAS_ETHERNET
    Ethernet_Task();
#endif
  }

}
Exemple #26
0
int
main(void)
{
  wdt_disable();

#ifdef CSMV4

  LED_ON_DDR  |= _BV( LED_ON_PIN );
  LED_ON_PORT |= _BV( LED_ON_PIN );

#endif

  led_init();
  LED_ON();

  spi_init();

//  eeprom_factory_reset("xx");
  eeprom_init();

//  led_mode = 2;

  // if we had been restarted by watchdog check the REQ BootLoader byte in the
  // EEPROM ...
//  if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) {
//    eeprom_write_byte( EE_REQBL, 0 ); // clear flag
//    start_bootloader();
//  }

  // Setup the timers. Are needed for watchdog-reset
#ifdef HAS_IRRX
  ir_init();
  // IR uses highspeed TIMER0 for sampling
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

#ifdef HAS_DOGM
  dogm_init();
#endif

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

#ifdef HAS_DOGM
  display_channel |= DISPLAY_DOGM;
#endif

  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_IRRX
    ir_task();
#endif
  }

}
Exemple #27
0
void doSweepLoop()
{
    sei();

    servo_data_t *servo = create_jim_servo();

    ir_init(ADC_ONE_64TH, ADC_AREF, 2);
    timer_prescaler_t prescaler = TIMER_ONE_1024TH;
    ping_init(prescaler);
    oi_t *oiSensor = malloc(sizeof(oi_t));
    oi_tare_encoders(&(oiSensor->left_encoder), &(oiSensor->right_encoder));
    int velocity = 0;
    int radius = 0;
    int leftWheelVelocity = 0;
    int rightWheelVelocity = 0;
    oi_full_mode();

    ir_enable_continous_mode();
    sendPing();

    printf0("creating stored ir sensor lookup table...\r\n");
    list_t *lookup_table = create_jims_ir_sensor_lookup_table();
    printf0("done...\r\n");
    while (1)
    {
        char c = '\0';

        char ping_available = 0;
        unsigned p_cm;
        if (volatile_ping_capture_complete)
        {
            ping_available = 1;
            unsigned long end_capture_count = tmr1_read_input_capture_count();
            unsigned long end_time_cap = (volatile_timer1_overflows << 16) | end_capture_count;
            unsigned long delta = end_time_cap - volatile_ping_send_pulse_start_time;
            p_cm = ping_count_to_cm(prescaler, delta);

            //send again
            sendPing();
        }

        int requestIrCalibration = 0;
        handleInput(servo, &leftWheelVelocity, &rightWheelVelocity, &requestIrCalibration);

        if (requestIrCalibration)
        {
            lfreefree(lookup_table);
            cli();
            lookup_table = create_ir_lookup_table_from_ping(servo, prescaler);
            sei();
        }

        unsigned voltage = ir_read_voltage_avg(1);
        unsigned calculatedDist = ir_lookup_distance(lookup_table, voltage);
        double ir_cm = calculatedDist * 0.254;
        ir_cm = calculatedDist / 10;
        char buff[200];
        ftoa(buff, ir_cm);

        //unsigned p_cm; //= ping_cm_busy_wait(prescaler);
        //p_cm = ping_count_to_cm(prescaler, volatile_timer1_capture_count);

        unsigned curDeg = servo_calculate_position_deg(servo);

        //printf0("%d volts \t\t calculatedDist= %u \r\n", voltage, calculatedDist);
        printf0("%uº \t %s ir_cm [%u v]\t", curDeg, buff, voltage);
        printf0(" pw=%u\t", servo->cur_pulse_width);
        if (ping_available)
            printf0("%u p_cm\t", p_cm);
        printf0("\r\n");
    }
}
Exemple #28
0
void DC16_RX_Mode (void)
{
  unsigned int i, j, k;
  volatile UINT16 u16Error;
  UINT16 u16CRC;
  UINT32 fileSize, progressCount, ledcnt;

  RTCSC_RTCPS = 0xA; // re-enable RTC (set prescaler = /4, interrupt every 4ms)
  IR_TX_Off;
      
  // enable IR receive mode
  // prepare TPM2 to be used for IR TPM2MOD = IR_RC_PERIOD; TPM2C1 as falling edge
  ir_init(IR_RC_PERIOD);    
 
  (void)memset(gau8Minicom, 0x00, MINICOM_BUFFER_SIZE); // clear buffer

  // wait to start receiving data
  // and when we do, get filename (should be in 8.3 format + NULL)
  k = 0; 
  do
  {
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (SD_PRESENT == HIGH) // if no SD card, exit mode
      {
        delay_ms(500);
        return;
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state_change_flag = 1; // go to the TX state, since we haven't started receiving yet
       }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    gau8Minicom[k] = ir_command;
    ++k;
  } while ((ir_command != '\0') && (k < 13));
  gau8Minicom[k-1] = '\0'; // add a null in case we've received incorrect filename data

  if (gau8Minicom[0] == '\0') // if no filename is received, abort transfer
  {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filename invalid.\n\r");
      DC16_Error(errRX, 0);
      return;     
  }
  
  // ensure the filename is uppercase before working with it
  // gotta love FAT16!
  for (j = 0; gau8Minicom[j] != '\0'; ++j)
    gau8Minicom[j] = (UINT8)toupper(gau8Minicom[j]);
     
  // get file size (4 bytes long)
  fileSize = 0;
  for (i = 0; i < 4; ++i)
  {
    timeout = 0;
    while (!ir_rx_byte) // wait to start receiving IR data...
    {
      if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
      {             
        DC16_Error(errRX, 0);
        return;                          
      }
        
      if (!SW_MODE)
      {
        delay_ms(50); // poor man's debounce
          
        while (!SW_MODE)
        {
          IR_RX_Off; // disable RX when we're done
          led_state = ALL_OFF;
          state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
          state_change_flag = 1;
        }
        return; // get outta here!
      }
    }

    // if we get here, that means we've received a valid byte
    ir_rx_byte = FALSE;

    fileSize <<= 8;
    fileSize |= ir_command; 
  }
  
  if (fileSize == 0xFFFFFFFF) // we get this if one badge receives another badge's power on IR test string (the Sony TV power off code), so just ignore it
  {
    delay_ms(500);
    return;
  }
  //else if ((fileSize > FAT16_MAX_FILE_SIZE) || (fileSize == 0)) // if the received file size is bigger than the possible FAT16 limit, we obviously have a corrupted transfer
  else if ((fileSize > 131072) || (fileSize == 0)) // limit filesize to 128KB to reduce transfer errors and prevent people from standing in front of each other for hours!
  {
     // so abort...
     if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("Filesize error!\n\r");
     DC16_Error(errRX, 0);
     return;        
  }
  
  // if the filename and file size appear to be valid, we can now proceed...
  if (SD_PRESENT != HIGH)
  {
    u16Error=SD_Init(); // SD card init
    if (u16Error == OK) 
    {
      FAT_Read_Master_Block(); // FAT driver init
    }
    else 
    {
      DC16_Error(errSD, u16Error);
      return;
    }
    
    // see if a file already exists with the name we've just received
    u16Error=FAT_FileOpen(gau8Minicom, READ); 
    if (u16Error == FILE_FOUND) 
    { 
      for (j = '0'; j <= '9'; ++j)
      {
        // replace the last character in the filename with a numeral
        // this will let us receive 10 more files with the same name (0-9) 
        gau8Minicom[k-2] = (UINT8)j; // k is the index of the filename string         
        u16Error=FAT_FileOpen(gau8Minicom, READ); 
    
        if (u16Error != FILE_FOUND) break; // once we've found a filename that doesn't exist, we can move on...
      }
    }
          
    if (WP_ENABLED == HIGH)
    {
      if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("SD card write protect enabled.\n\r");
      DC16_Error(errRX, 0);
      return;    
    }
    else
    {
      // create a new file on the SD card
      u16Error=FAT_FileOpen(gau8Minicom, CREATE); 
      if (u16Error != FILE_CREATE_OK) 
      { 
        DC16_Error(errFAT, u16Error);
        return;
      }
    }
  }
  else
  {
    if (usb_enabled_flag && USB_DETECT) Terminal_Send_String("No SD card inserted.\n\r");
    state = TX; // go to SLEEP mode to avoid receiving unsynchronized data
    state_change_flag = 1;
    return;
  }
  
  if (usb_enabled_flag && USB_DETECT)
  { 
    Terminal_Send_String("File name: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String("\n\r");
     
    itoa(fileSize, gau8Minicom, 12);      
    Terminal_Send_String("File size: ");
    Terminal_Send_String(gau8Minicom);
    Terminal_Send_String(" bytes\n\r");
    Terminal_Send_String("Starting IR file receive.\n\r");
  }
  
  led_state = IGNORE; // set this so the timer interrupt routine doesn't mess with our LED output
  do_cool_LED_stuff(ALL_OFF); // we're going to use LEDs as a transfer progress indicator, so we want complete control

  progressCount = fileSize >> 3;
  ledcnt = 0;
  gu8Led = 0x00;
  
  if (fileSize >= MINICOM_BUFFER_SIZE)
  {
    do
    {
      // assuming each block sent is MINICOM_BUFFER_SIZE (512 bytes), except for the last one which might be shorter
      for (i = 0; i < MINICOM_BUFFER_SIZE; ++i) 
      {  
        timeout = 0;  
        while (!ir_rx_byte) // wait to start receiving IR data...
        {
          if (timeout == 1250) // if we haven't received a byte for ~5 seconds, abort
          {
            FAT_FileClose();
            DC16_Error(errRX, 0);
            return;                          
          }
          
          if (!SW_MODE)
          {
            delay_ms(50); // poor man's debounce
              
            while (!SW_MODE)
            {
              IR_RX_Off; // disable RX when we're done
              led_state = ALL_OFF;
              state = TX; // go to SLEEP mode to avoid sending data as soon as we cancel a receive
              state_change_flag = 1;
            }
            
            FAT_FileClose(); // properly close the file if we're aborting the transfer
            return; // get outta here!
          }
        }

        // if we get here, that means we've received a valid byte
        ir_rx_byte = FALSE; 
        gau8Minicom[i] = ir_command;
        fileSize--;
        ledcnt++;
        if (ledcnt == progressCount) // decrement LEDs as transfer progresses
        {
          ledcnt = 0;
          gu8Led <<= 1;
          gu8Led |= 1;
          do_cool_LED_stuff(BYTE);
        }    
      }
      
      u16CRC = DC16_RX_CRC();
      if (u16CRC == 0) return;
      
      // calculate CRC16 checksum on this block 
      j = crc16_ccitt(gau8Minicom, MINICOM_BUFFER_SIZE);
      
      if (usb_enabled_flag && USB_DETECT)
      {
        Terminal_Send_String("\n\rCRC16 = 0x");
        Terminal_Send_String(num2asc((j >> 8) & 0xFF));
        Terminal_Send_String(num2asc(j & 0xFF));
      }    
       
      // if it doesn't match what we've just received, abort the transfer
      if (u16CRC != j)
      {
        FAT_FileClose();
        DC16_Error(errRX, 0);
        return;      
      }
      else // if it matches, write the data to the card
      {
        FAT_FileWrite(gau8Minicom,MINICOM_BUFFER_SIZE); 
      }
    } while (fileSize >= MINICOM_BUFFER_SIZE);
  }
Exemple #29
0
int
main(void)
{
  wdt_disable();
  clock_prescale_set(clock_div_1);

  LED_ON_DDR  |= _BV( LED_ON_PIN );
  LED_ON_PORT |= _BV( LED_ON_PIN );

  led_init();
  LED_ON();

  spi_init();

//  eeprom_factory_reset("xx");
  eeprom_init();

// Setup OneWire and make a full search at the beginning (takes some time)
#ifdef HAS_ONEWIRE
  i2c_init();
	onewire_Init();
	onewire_FullSearch();
#endif

  // Setup the timers. Are needed for watchdog-reset
#if defined (HAS_IRRX) || defined (HAS_IRTX)
  ir_init();
  // IR uses highspeed TIMER0 for sampling 
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8


  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

  checkFrequency(); 
  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_RWE
    rf_rwe_task();
#endif
#if defined(HAS_IRRX) || defined(HAS_IRTX)
    ir_task();
#endif
#ifdef HAS_MBUS
    rf_mbus_task();
#endif
  }

}
Exemple #30
0
int main( void )
{
    init_system();      // initialise host app, pins, watchdog, etc 
    
    init_timer_isr();      // configure timer ISR to fire regularly 

    softuart_init();
    softuart_puts("\nbattir3\n");

    ir_init();

    i2c_init();
    
    blinkm_stop();
    blinkm_setRGB( 0,0,0 );
    _delay_ms(300);

    // a little hello fanfare
    for( int i=0;i<2; i++ ) {
        softuart_printHex16( i );
        softuart_puts("!");
        blinkm_setRGB( 0x02,0x02,0x02);
        _delay_ms(50);
        blinkm_setRGB( 0x01,0x01,0x01);
        _delay_ms(50);
    }
    blinkm_setRGB( 0,0,0 );
    blinkm_playScript(0,0);

    sei();       // enable interrupts 

    softuart_puts(":\n");

    int power_on = 1;
    // loop forever 
    for( ; ; ) {
        if( time_to_measure ) {
            time_to_measure = 0; // clear flag: time to measure touch 

            uint16_t key = ir_getkey();
            if( key==0 )  // no key
                continue;

            softuart_puts("k:");
            softuart_printHex16( key );
            softuart_putc('\n');

            if(      key == IRKEY_ONE ) {
                blinkm_fadeToRGB( 0xff,0x00,0x00 );
            }
            else if( key == IRKEY_TWO ) {
                blinkm_fadeToRGB( 0x00,0xff,0x00 );
            }
            else if( key == IRKEY_THREE ) {
                blinkm_fadeToRGB( 0x00,0x00,0xff );
            }
            else if( key == IRKEY_FOUR ) {
                blinkm_fadeToRGB( 0xff,0xff,0x00 );
            }
            else if( key == IRKEY_FIVE ) {
                blinkm_fadeToRGB( 0x00,0xff,0xff );
            }
            else if( key == IRKEY_SIX ) {
                blinkm_fadeToRGB( 0xff,0x00,0xff );
            }
            else if( key == IRKEY_SEVEN ) {
                blinkm_fadeToRGB( 0x11,0x11,0x11 );
            }
            else if( key == IRKEY_EIGHT ) {
                blinkm_fadeToRGB( 0x80,0x80,0x80 );
            }
            else if( key == IRKEY_NINE ) {
                blinkm_fadeToRGB( 0xff,0xff,0xff );
            }
            else if( key == IRKEY_ZERO ) {
                blinkm_fadeToRGB( 0x00,0x00,0x00 );
            }
            else if( key == IRKEY_VOLUP ) {
                blinkm_setFadespeed( 80 );
            }
            else if( key == IRKEY_VOLDN ) {
                blinkm_setFadespeed( 5 );
            }
            else if( key == IRKEY_POWER ) {
                if( power_on ) { 
                    blinkm_stop();
                    blinkm_fadeToRGB( 0x00,0x00,0x00 );
                    power_on = 0;
                } else {
                    blinkm_playScript(0,0);
                    power_on = 1;
                }
                _delay_ms(500);  // arbitrary wait to approximate key debounce
            }
             else if( key == IRKEY_PLAY ) { 
                blinkm_playScript( 0, 0 );
            }
        }

    } // for
}