Ejemplo n.º 1
0
void acc_dump(void) {
#ifdef ENABLE_DEBUG_SERIAL_DUMP
	serial_write_str("ACC: ");
	for (uint8_t i=0; i<3; i++) {
		serial_write_int(acc_data[i]);
		serial_write(' ');
	}
	serial_write('\n');
#endif
}
Ejemplo n.º 2
0
int16_t main(void)
{
    /* DDD4 = Yellow LED, DDD5 = Green LED, DDD6 = Red LED */
    DDRD = _BV(DDD4) | _BV(DDD5) | _BV(DDD6);
    PORTD &= ~_BV(PORTD4);
    PORTD &= ~_BV(PORTD5);
    PORTD &= ~_BV(PORTD6);

    /* Don't forget to check these in real code */
    const serial_init_res_t serial_init_res = serial_init((uint32_t)F_CPU, 9600u);

    if (serial_init_res == SI_SUCCESS) {
        PORTD |= _BV(PORTD5);
    }
    else {
        PORTD |= _BV(PORTD6);
    }

    sei();

    uint8_t last_byte_read = 0;
    bool msg_available = false;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
    while (1) {
        serial_write_str("Hello Serial lib!\n");

        msg_available = serial_read(&last_byte_read);

        if (msg_available) {
            serial_write_str("Got msg: ");
            serial_write(last_byte_read);
            serial_write('\n');
        }

        _delay_ms(1000);
    }
#pragma clang diagnostic pop

    return 0;
}
Ejemplo n.º 3
0
void serial_printf(uint16_t port, const char *format, ...)
{
	static char buffer[1024];
	memset(buffer, 0, sizeof(buffer));

	va_list vl;
	va_start(vl, format);
	vsprintf(buffer, format, vl);
	va_end(vl);

	serial_write_str(port, buffer);
}
Ejemplo n.º 4
0
void setup(void) {
	uint8_t s = SREG;
	uint8_t m = MCUCR;
	uint8_t i, addrs[6];

	wdt_disable();
	serial_init();
	timer_init();
	spi_init();
	nrf24_init();
	sei();

	/*
	 * Set our radio address and the remote end's radio address, read
	 * the addresses from EEPROM where they need to be saved first.
	 */
	for (i = 0; i < 6; i ++)
		addrs[i] = eeprom_read(i);

	nrf24_set_rx_addr(addrs + 0);
	nrf24_set_tx_addr(addrs + 3);

	/* Write something to say hello */
	serial_write_str("SREG:");
	serial_write_hex16(s);
	serial_write_str(", MCUCR:");
	serial_write_hex16(m);
	serial_write_str(", our addr: ");
	serial_write1(addrs[0]);
	serial_write1(addrs[1]);
	serial_write1(addrs[2]);
	serial_write_eol();

	nrf24_rx_mode();

	serial_set_handler(handle_input);
}
Ejemplo n.º 5
0
int main(void) {
    wdt_enable(WDTO_4S);
    wdt_reset();
    /* configure PPM output port */
    PPM_DDR |= (1<<PPM_BIT);
    PPM_PORT &= ~(1<<PPM_BIT);

    /* configure LED output port */
    LED_DDR |= (1<<LED_BIT);
    LED_PORT |= (1<<LED_BIT);

    /* configure VOL(tage) warning port */
    VOL_DDR &= ~(1<<VOL_BIT);
    VOL_PORT |= (1<<VOL_BIT); // enable pullup

#ifdef ENABLE_SERIAL
    serial_init();
#endif
#ifdef ENABLE_TWI
    twi_init();
#endif
#ifdef USE_NUNCHUK
    nunchuk_init();
    wdt_reset();
#endif

    /* configure switches */
    sw_init();

    /* configure ADC */
    adc_init();

#if defined(USE_TWI_ADC)
    twi_adc_init();
    wdt_reset();
#endif
#if defined(USE_MAG)
    mag_init();
    wdt_reset();
#endif
#if defined(USE_ACC)
    acc_init();
#endif
#if defined(USE_LCD)
    /* initialize LCD twice (due to timing issues?) */
    lcd_init();
    wdt_reset();
    _delay_ms(100);
    wdt_reset();
    lcd_init();
    wdt_reset();
    lcd_splash();
#endif

    /* configure watchfog timer to reset after 60ms */
    wdt_enable(WDTO_60MS);

    /* configure timer */

    /* enable CTC waveform generation (TOP == OCR1A) */
    TCCR1B |= (1<<WGM12);
    /* set compare value for the stop pulse to 300µs */
    OCR1B = STOP_US;
    /* set pulse width to max for now */
    OCR1A = ~0;
    /* set Timer 1 to clk/8, giving us ticks of 1 µs */
    TCCR1B |= (1<<CS11);

    /* Timer 2 generates overflows at 1kHz */
#if defined(TCCR2) /* e.g. ATMega8 */
#define TIMER2_COMP_IRQ TIMER2_COMP_vect
    TCCR2 = (1<<WGM21 | 1<<CS22);
    OCR2 = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK = (1<<OCIE2 | 1<<OCIE1B | 1<<OCIE1A);
#elif defined(TCCR2A) /* e.g. ATMega{8,16,32}8 */
#define TIMER2_COMP_IRQ TIMER2_COMPA_vect
    TCCR2A = (1<<WGM21);
    TCCR2B = (1<<CS22);
    OCR2A = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK1 = (1<<OCIE1B | 1<<OCIE1A);
    TIMSK2 = (1<<OCIE2A);
#else
#error "Unable to determine timer 2 configuration registers"
#endif

    /* initialize channel data */
    start_ppm_frame();
    set_ppm(1);
    start_ppm_pulse();

    /* enable interrupts */
    sei();

    serial_write_str("Welcome!\n");
    while (1) {
        /* reset watchdog */
        wdt_reset();

        /* keep sampling adc data */
        adc_query();

        /* query switches */
        sw_query();

        /* prepare Datenschlag data frames */
        ds_prepare();

#ifdef USE_TWI_ADC
        /* query TWI/I²C ADC */
        twi_adc_query();
#endif
#if defined(USE_MAG)
#ifdef MAG_CENTER_CALIBRATION_TRIGGER_INPUT
        if (get_input_scaled( (MAG_CENTER_CALIBRATION_TRIGGER_INPUT), 1, -1) == (MAG_CENTER_CALIBRATION_TRIGGER_VALUE)) {
            mag_set_calibration(millis + 10000L);
        }
#endif
        if (mag_is_calibrating()) {
            mag_calibrate(0);
        } else {
            mag_calibrate(1);
            mag_query();
            mag_dump();
        }
#endif
#if defined(USE_ACC)
        acc_query();
        acc_dump();
#endif

#ifdef USE_NUNCHUK
        nunchuk_query();
#endif

        check_voltage();

        /* switch LED */
        if (!low_voltage || (millis/250 % 2)) {
            LED_PORT |= (1<<LED_BIT);
        } else {
            LED_PORT &= ~(1<<LED_BIT);
        }

#ifdef USE_LCD
        static enum {
            LCD_MODE_STATUS,
#ifdef LCD_MENU
            LCD_MODE_MENU,
#endif
            LCD_MODE_CNT
        } lcd_mode;

        uint8_t lcd_mode_changed = 0;
#ifdef LCD_MODE_SWITCH_INPUT
        static int8_t old_sw_state = 0;
        int8_t sw_state = get_input_scaled(LCD_MODE_SWITCH_INPUT, -1, 1);
        if (old_sw_state != sw_state) {
            lcd_mode += LCD_MODE_CNT;
            lcd_mode += sw_state;
            lcd_mode %= LCD_MODE_CNT;
            old_sw_state = sw_state;
            lcd_mode_changed = 1;
        }
#endif
        switch (lcd_mode) {
        case LCD_MODE_STATUS:
            lcd_status_update(lcd_mode_changed);
            break;
#ifdef LCD_MENU
        case LCD_MODE_MENU:
            lcd_menu_update(lcd_mode_changed);
            break;
#endif
        default:
            break;
        }
#endif
    }
    return 0;
}
Ejemplo n.º 6
0
int main(void) {
	/* configure PPM output port */
	PPM_DDR |= (1<<PPM_BIT);
	PPM_PORT &= ~(1<<PPM_BIT);

	/* configure LED output port */
	LED_DDR |= (1<<LED_BIT);
	LED_PORT |= (1<<LED_BIT);

	/* configure VOL(tage) warning port */
	VOL_DDR &= ~(1<<VOL_BIT);
	VOL_PORT |= (1<<VOL_BIT); // enable pullup

	serial_init();
	serial_write_str("Serial Active\n\r");
	/* configure switches */
	sw_init();
	serial_write_str("Switches Inited\n\r");
	/* configure PS2 pad */
	serial_write_str("Activating PS2 Pad...");
	psx_init(PSX_SPI,PSX_DAT,PSX_SPI,PSX_CLK,PSX_SPI,PSX_COM,PSX_ATT,PSX_ATN);
	serial_write_str("Done\n\r");
	/* configure ADC */
	adc_init();
/*
	// configure watchfog timer to reset after 60ms 
	wdt_enable(WDTO_60MS);

	// configure timer 

	// enable CTC waveform generation (TOP == OCR1A) 
	TCCR1B |= (1<<WGM12);
	// set compare value for the stop pulse to 300µs 
	OCR1B = STOP_US;
	// set pulse width to max for now 
	OCR1A = ~0;
	// set Timer 1 to clk/8, giving us ticks of 1 µs 
	TCCR1B |= (1<<CS11);

	// Timer 2 generates overflows at 1kHz 
#if defined(TCCR2) // e.g. ATMega8 
#define TIMER2_COMP_IRQ TIMER2_COMP_vect
	TCCR2 = (1<<WGM21 | 1<<CS22);
	OCR2 = 0x7D;
	// enable compare and overflow interrupts 
	TIMSK = (1<<OCIE2 | 1<<OCIE1B | 1<<OCIE1A);
#elif defined(TCCR2A) // e.g. ATMega{8,16,32}8 
#define TIMER2_COMP_IRQ TIMER2_COMPA_vect
	TCCR2A = (1<<WGM21);
	TCCR2B = (1<<CS22);
	OCR2A = 0x7D;
	// enable compare and overflow interrupts 
	TIMSK1 = (1<<OCIE1B | 1<<OCIE1A);
	TIMSK2 = (1<<OCIE2A);
#else
#error "Unable to determine timer 2 configuration registers"
#endif

	// initialize channel data 
	start_ppm_frame();
	set_ppm(1);
	start_ppm_pulse();

	// enable interrupts 
	sei();
*/
	serial_write_str("Welcome!\n");
	while (1) {
		/* reset watchdog */
//		wdt_reset();

		/* keep sampling adc data */
//		adc_query();

		/* query switches */
//		sw_query();

		/* query PS2 Pad */
		ps2_query();

		/* prepare Datenschlag data frames */
//		ds_prepare();

		//if (psx_stick(5)>=140) serial_write_str("Over  140\r");
		//if (psx_stick(5)<=100) serial_write_str("Under 100\r");
		serial_write_str("Triangle=");
		if (psx_button(PSB_TRIANGLE)) serial_write_str("1");
		else serial_write_str("0");
		
		serial_write_str(" Square=");
		if (psx_button(PSB_SQUARE)) serial_write_str("1");
		else serial_write_str("0");
		
		serial_write_str(" Cross=");
		if (psx_button(PSB_CROSS)) serial_write_str("1");
		else serial_write_str("0");

		serial_write_str(" Circle=");
		if (psx_button(PSB_CIRCLE)) serial_write_str("1");
		else serial_write_str("0");
		serial_write_str("\r");
		/* check voltage */
		//if ((~VOL_PIN) & 1<<VOL_BIT) {
		if ((VOL_PIN) & 1<<VOL_BIT) {
			// everything OK
			//LED_PORT |= (1<<LED_BIT);
			LED_PORT &= ~(1<<LED_BIT);
		} else {
			// voltage dropped, alert the user!
			if (millis/250 % 2) {
				LED_PORT |= (1<<LED_BIT);
			} else {
				LED_PORT &= ~(1<<LED_BIT);
			}

		}
	}
	return 0;
}