Esempio n. 1
0
void power_down_mode()
{
	// Power down for rfm73, PWR_UP = 0
    rfm70WriteRegValue(RFM70_CMD_WRITE_REG | RFM70_REG_CONFIG, (0<<1));

    // Power down mode for attiny88
    cli();
    SMCR |= (1<<SE);
    SMCR |= (1<<SM1) | (0<<SM0);
    sei();
    sleep_cpu();
}
Esempio n. 2
0
uint8_t rfm70ReceivePayload()
{
    uint8_t len;
    uint8_t status;

    uint8_t fifo_status;
    uint8_t rx_buf[32];

    bool msg_received = false;

    fifo_status = rfm70ReadRegValue(RFM70_REG_FIFO_STATUS);

    status = rfm70ReadRegValue(RFM70_REG_STATUS);

    // check if receive data ready (RX_DR) interrupt
    if (status & RFM70_IRQ_STATUS_RX_DR)
    {
    	msg_received = true;
    	do
        {
            // read length of playload packet
            len = rfm70ReadRegValue(RFM70_CMD_RX_PL_WID);

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

		// Send message with ack payload of the beacon message
		// if receiver allows and button is pressed
		if (stateChanged)
		{
			stateChanged = false;
			rfm70SendPayload(buf, 32, -1, 1);
		}

		}
            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);


    }
    rfm70WriteRegValue(RFM70_CMD_WRITE_REG | RFM70_REG_STATUS, status);

    return msg_received;
}
Esempio n. 3
0
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;
}
Esempio n. 4
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;
}
Esempio n. 5
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;
        }


    }
}