コード例 #1
0
ファイル: CyberStick.c プロジェクト: dwickeroth/covise
bool find_receiver_frequency()
{
	bool msg_received = false;
	int frequency = 0;
	TCNT0 = 0x00;

	// start communication with receiver at pipe0
	rfm70SendPayload(buf, 32, -1, 0);
	while(true)
	{
		if (frequency == 83)  // max possible frequencies 2400Mhz-2483Mhz
		{
			frequency = 0;
		}

		if (rfm70ReceivePayload())
		{
			msg_received = true;
			break;
		}

		msg_receive_timeout = TCNT0 * 0.128;

		if ((msg_receive_timeout >= 5) & (msg_received == false))
		{
			msg_receive_timeout = 0.0;
			TCNT0 = 0x00;
			frequency++;

			// change frequency
			rfm70WriteRegValue(RFM70_CMD_WRITE_REG | 0x05, frequency);
			_delay_ms(1);
		}


	}

	return msg_received;
}
コード例 #2
0
ファイル: atmega8_receiver.c プロジェクト: nixz/covise
// ----------------------------------------------------------------------------
// main
// ----------------------------------------------------------------------------
int main(void)
{
    ///////////////////////////////////////////////////////////////////////////
    // low level init
    // IO-pins and pullups

    _delay_ms(100);

    //all unused ports as input with pullups

    DDRC &= ~((1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3) | (1 << DDC4) | (1 << DDC5));
    PORTC |= ((1 << DDC0) | (1 << DDC1) | (1 << DDC2) | (1 << DDC3) | (1 << DDC4) | (1 << DDC5));
    DDRB &= ~((1 << DDB1));
    PORTB |= ((1 << DDB1));
    DDRD &= ~((1 << DDD4) | (1 << DDD5) | (1 << DDD6) | (1 << DDD7));
    PORTD |= ((1 << DDD4) | (1 << DDD5) | (1 << DDD6) | (1 << DDD7));

    // leds
    // DDD2 - red led
    DDRD |= (1 << DDD1);
    PORTD &= ~((1 << DDD1));

    // do SPI init after seting CE to LOW, this is important, otherwise the RFM73 module does not always answer to SPI requests.

    //out
    RFM73_CSN_DIR |= (1 << RFM73_CSN_PIN);
    RFM73_CE_DIR |= (1 << RFM73_CE_PIN);
    //in
    RFM73_IRQ_DIR &= ~(1 << RFM73_IRQ_PIN);
    RFM73_CE_LOW;

    DDRD |= (1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT);

    uchar i;
    odDebugInit();
    usbDeviceDisconnect();
    for (i = 0; i < 20; i++)
    { /* 300 ms disconnect */
        _delay_ms(15);
    }
    usbDeviceConnect();

    usbInit();
    sei();
    while (1)
    {

        usbPoll();
        _delay_ms(10);
        if (usbInterruptIsReady() && nextDigit != NULL)
        { // we can send another key
            buildReport();
            usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
            if (*++nextDigit == 0xff) // this was terminator character
                nextDigit = NULL;
        }
    }

    // low level init
    // IO-pins and pullups
    spiInit();
    spiSelect(csNONE);
    cbi(PORTD, LED_RED);
    _delay_ms(500);

    // ADC
    // TODO

    // TIMER INIT
    // TODO

    //////////////////////////////////////////////////////////////////////////
    // schrott

    unsigned char buf[32];
    // char charValue [6] = "      ";
    // int len = 6;

    //////////////////////////////////////////////////////////////////////////
    // start main loop

    // state machine
    // state 0  - exit
    // state 1  - choose transmitter or receiver
    // state 2  - init of transmitter module
    // state 3  - wait for button press and send something
    // state 10 - init and power up RX modules

    uint8_t smState = 1;

    //sei();

    sbi(PORTD, LED_RED);

    // RFM70
    // write registers
    // light green led if rfm70 found
    // light red led if rfm70 not found
    if (rfm70InitRegisters())
    {
        cbi(PORTD, LED_RED);
    }
    else
    {
        sbi(PORTD, LED_RED);
    }

    while (smState != 0)
    {
        while (1)
        {

            usbPoll();
            _delay_ms(10);
            if (usbInterruptIsReady() && nextDigit != NULL)
            { // we can send another key
                buildReport();
                usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
                if (*++nextDigit == 0xff) // this was terminator character
                    nextDigit = NULL;
            }
        }
        //------------------------------------------------------------------------
        // Receiver State Machine
        //------------------------------------------------------------------------
        switch (smState)
        {
        case 1:
        {
            // start state
            // we are the receiver
            smState = 10;
            break;
        }
        case 10:
        {

            // init and power up modules
            // goto RX mode
            rfm70SetModeRX();

            smState = 11;
            break;
        }
        case 11:
        {
            // try to receive something

            // _delay_ms(100);
            rfm70ReceivePayload(buf);

            break;
        }
        }
    }
}
コード例 #3
0
ファイル: CyberStick.c プロジェクト: dwickeroth/covise
void CyberStick_Start()
{
    //bool stateChanged;
	uint8_t value;

	buf[3] = CYBERSTICK_ID;

    stateCurrent.buttons = 0x00;
    stateCurrent.touchpadX = 0;
    stateCurrent.touchpadY = 0;

    stateLast = stateCurrent;
    stateChanged = false;

    sei();

    bool beacon_msg_received = false;

    // chnage frequency to check find_receiver_frequency() can find the right frequency
	rfm70WriteRegValue(RFM70_CMD_WRITE_REG | 0x05, 0x40);
	_delay_ms(2);

	rfm70SetModeRX();
	_delay_ms(2);

	timer0_init();
	LED3ON;
	find_receiver_frequency();

    while (true)
    {
    	LED3OFF;
    	LED4ON;   // always means connection is established with the receiver
    	//rfm70ReceivePayload();
    	// check if beacon message is received
    	// if not for 100ms that means receiver has switched to another
    	// frequency and we need to find receiver frequency again
    	beacon_msg_received = rfm70ReceivePayload();
    	if ( beacon_msg_received == false)
    	{
    		if (beacon_msg_time>= 100)
    		{
				LED4OFF;
				find_receiver_frequency();
				beacon_msg_time = 0.0;
				LED4ON;
    		}
    	}
    	else
    	{
    		beacon_msg_time =0.0;
    	}

    	// if button is not pressed for 20s go to power down mode
    	/*if (time_out_cyberstick > 20000)  // time is in ms
    	{
    		LED4OFF;
    		power_down = true;
    		time_out_cyberstick = 0.0;
    		enable_PCINT();
    		power_down_mode();

    	}*/

       /* if ((PINC & (1 << DDC0)) == 0)
        {
            int8_t tmpdata[2];
            // data is ready, read it
            spiSelect(csTOUCH);
            spiReadMaster(&tmpdata, 2);
            spiSelect(csNONE);
            spiSelect(csTOUCH);
            spiReadMaster(&tmpdata, 3);
            spiSelect(csNONE);
            stateCurrent.touchpadY += tmpdata[1];
            spiSelect(csTOUCH);
            spiReadMaster(&tmpdata, 4);
            spiSelect(csNONE);
            stateCurrent.touchpadX += tmpdata[1];
            spiSelect(csTOUCH);
            spiReadMaster(&tmpdata, 2);
            spiSelect(csNONE);
        }
        else
        {
        }*/
        // get current state of all inputs
        // buttons

        if ((PINB & (1 << BUTTON_1)) == 0)
        {
            stateCurrent.buttons |= (1 << BUTTON_BIT0);
        }
        else
        {
            stateCurrent.buttons &= ~(1 << BUTTON_BIT0);
        }

        if ((PIND & (1 << BUTTON_2)) == 0)
        {
            stateCurrent.buttons |= (1 << BUTTON_BIT1);
        }
        else
        {
            stateCurrent.buttons &= ~(1 << BUTTON_BIT1);
        }

        if ((PIND & (1 << BUTTON_3)) == 0)
        {
            stateCurrent.buttons |= (1 << BUTTON_BIT2);
        }
        else
        {
            stateCurrent.buttons &= ~(1 << BUTTON_BIT2);
        }

        if ((PIND & (1 << BUTTON_4)) == 0)
        {
            stateCurrent.buttons |= (1 << BUTTON_BIT3);
        }
        else
        {
            stateCurrent.buttons &= ~(1 << BUTTON_BIT3);
        }

        /*if ((PINC & (1 << BUTTON_5)) == 0)
        {
            stateCurrent.buttons |= (1 << BUTTON_BIT4);
        }
        else
        {
            stateCurrent.buttons &= ~(1 << BUTTON_BIT4);
        }*/

        //check if something changed

        if ((stateCurrent.buttons != stateLast.buttons) || (stateCurrent.touchpadX != stateLast.touchpadX) || (stateCurrent.touchpadY != stateLast.touchpadY))
        {
            stateChanged = true;
            stateLast = stateCurrent;
        }

        if (stateChanged)
        {
        	time_out_cyberstick = 0.0;       //restart time, button is pressed

            // copy current state to buffer
            buf[0] = stateCurrent.buttons;
            buf[1] = stateCurrent.touchpadX;
            buf[2] = stateCurrent.touchpadY;
            buf[3] = CYBERSTICK_ID;
        }


    }
}