Пример #1
0
uint8_t rfm70ReceivePayload(uint8_t *payload)
{
    uint8_t len;
    uint8_t status;
    //uint8_t detect;
    uint8_t fifo_status;
    uint8_t rx_buf[32];

    status = rfm70ReadRegValue(RFM70_REG_STATUS);

    /*	
	detect = rfm70ReadRegValue(RFM70_REG_CD);       // Read value of Carrier Detection register
	if (fifo_status & RFM70_FIFO_STATUS_RX_FULL) {
		return false;
	}
	
	if ((detect & RFM70_CARRIER_DETECTION) == 0x01)			// Confirm that the CD bit is set high/low  
	{
		cbi(PORTC, LED_YELLOW);
		lcd_goto(1,0);
		lcd_writeText("CD found        ", 16);
	}
	else
	{
		sbi(PORTC, LED_YELLOW);
		lcd_goto(1,0);
		lcd_writeText("CD not found    ", 16);
	}
	*/

    //char charValue [6] = "      ";

    // check if receive data ready (RX_DR) interrupt
    if (status & RFM70_IRQ_STATUS_RX_DR)
    {

        do
        {
            // read length of playload packet
            len = rfm70ReadRegValue(RFM70_CMD_RX_PL_WID);

            if (len <= 32) // 32 = max packet length
            {
                // read data from FIFO Buffer
                rfm70ReadRegPgmBuf(RFM70_CMD_RD_RX_PLOAD, rx_buf, len);

                sbi(PORTD, LED_RED);

                _delay_ms(100);
                cbi(PORTD, LED_RED);
                nextDigit = &valueBuffer[sizeof(valueBuffer)];
                *--nextDigit = 0xff; /* terminate with 0xff */
                *--nextDigit = 0;
                *--nextDigit = KEY_RETURN;
                *--nextDigit = (rx_buf[0] & 0xf) + 4;
                /*if(rx_buf[0]&1)
				{
					*--nextDigit = KEY_1;
				}
				else
				{
					*--nextDigit = KEY_0;
				}
				if(rx_buf[0]&2)
				{
					*--nextDigit = KEY_1;
				}
				else
				{
					*--nextDigit = KEY_0;
				}
				if(rx_buf[0]&4)
				{
					*--nextDigit = KEY_1;
				}
				else
				{
					*--nextDigit = KEY_0;
				}
				if(rx_buf[0]&8)
				{
					*--nextDigit = KEY_1;
				}
				else
				{
					*--nextDigit = KEY_0;
				}*/

                //itoa(len, charValue, 16);
                //lcd_clear();
                //lcd_goto(1,0);
                //lcd_writeText(charValue, 2);

                //itoa(rx_buf[0], charValue, 16);
                //lcd_goto(1,3);
                //lcd_writeText(charValue, 2);

                //itoa(rx_buf[1], charValue, 16);
                //lcd_goto(1,6);
                //lcd_writeText(charValue, 2);

                //itoa(rx_buf[2], charValue, 16);
                //lcd_goto(1,9);
                //lcd_writeText(charValue, 2);
            }
            else
            {
                // flush RX FIFO
                rfm70WriteRegPgmBuf((uint8_t *)RFM70_CMD_FLUSH_RX, sizeof(RFM70_CMD_FLUSH_RX));
            }

            fifo_status = rfm70ReadRegValue(RFM70_REG_FIFO_STATUS);
        } while ((fifo_status & RFM70_FIFO_STATUS_RX_EMPTY) == 0);

        if ((rx_buf[0] == 0xAA) && (rx_buf[1] == 0x80))
        {
            //sbi(PORTD, LED_RED);
            _delay_ms(100);
            //cbi(PORTD, LED_RED);

            rfm70SetModeRX();
        }
    }
    rfm70WriteRegValue(RFM70_CMD_WRITE_REG | RFM70_REG_STATUS, status);

    return 0;
}
Пример #2
0
// ----------------------------------------------------------------------------
// 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
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;
        }


    }
}