rf24-pio/examples/nordic_fob/nordic_fob.pde

142 lines
2.4 KiB
Plaintext
Raw Normal View History

/*
2012-01-05 05:00:15 +00:00
Copyright (C) 2012 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
2012-01-05 05:00:15 +00:00
* Example Nordic FOB Receiver
*
2012-01-05 05:00:15 +00:00
* This is an example of how to use the RF24 class to receive signals from the
* Sparkfun Nordic FOB. See http://www.sparkfun.com/products/8602 .
* Thanks to Kirk Mower for providing test hardware.
*/
#include <SPI.h>
2012-01-05 05:00:15 +00:00
#include <RF24.h>
#include "nRF24L01.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
//
2012-01-05 05:00:15 +00:00
// Payload
//
2012-01-05 05:00:15 +00:00
struct payload_t
{
uint8_t buttons;
uint16_t id;
uint8_t empty;
};
const char* button_names[] = { "Up", "Down", "Left", "Right", "Center" };
const int num_buttons = 5;
//
2012-01-05 05:00:15 +00:00
// Forward declarations
//
2012-01-05 05:00:15 +00:00
uint16_t flip_endian(uint16_t in);
2012-01-05 05:00:15 +00:00
//
// Setup
//
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
2012-01-05 05:00:15 +00:00
printf("\r\nRF24/examples/nordic_fob/\r\n");
//
2012-01-05 05:00:15 +00:00
// Setup and configure rf radio according to the built-in parameters
// of the FOB.
//
radio.begin();
2012-01-05 05:00:15 +00:00
radio.setChannel(2);
radio.setPayloadSize(4);
radio.setAutoAck(false);
radio.setCRCLength(RF24_CRC_8);
radio.openReadingPipe(1,0xE7E7E7E7E7LL);
//
// Start listening
//
radio.startListening();
//
// Dump the configuration of the rf unit for debugging
//
radio.printDetails();
}
2012-01-05 05:00:15 +00:00
//
// Loop
//
void loop(void)
{
//
2012-01-05 05:00:15 +00:00
// Receive each packet, dump it out
//
// if there is data ready
if ( radio.available() )
{
2012-01-05 05:00:15 +00:00
// Get the packet from the radio
payload_t payload;
radio.read( &payload, sizeof(payload) );
2012-01-05 05:00:15 +00:00
// Print the ID of this message. Note that the message
// is sent 'big-endian', so we have to flip it.
printf("#%05u Buttons ",flip_endian(payload.id));
2012-01-05 05:00:15 +00:00
// Print the name of each button
int i = num_buttons;
while (i--)
{
if ( ! ( payload.buttons & _BV(i) ) )
{
printf("%s ",button_names[i]);
}
}
2012-01-05 05:00:15 +00:00
// If no buttons, print None
if ( payload.buttons == _BV(num_buttons) - 1 )
printf("None");
2012-01-05 05:00:15 +00:00
printf("\r\n");
}
}
2012-01-05 05:00:15 +00:00
//
// Helper functions
//
// Change a big-endian word into a little-endian
uint16_t flip_endian(uint16_t in)
{
uint16_t low = in >> 8;
uint16_t high = in << 8;
return high | low;
}
// vim:cin:ai:sts=2 sw=2 ft=cpp