rf24-pio/examples/pingpair_sleepy/pingpair_sleepy.pde

289 lines
6.8 KiB
Plaintext
Raw Normal View History

/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
2011-07-09 05:15:37 +00:00
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.
*/
/**
* Example RF Radio Ping Pair which Sleeps between Sends
*
2011-05-11 21:50:07 +00:00
* This is an example of how to use the RF24 class to create a battery-
* efficient system. It is just like the pingpair.pde example, but the
* ping node powers down the radio and sleeps the MCU after every
* ping/pong cycle.
*
* As with the pingpair.pde example, write this sketch to two different nodes,
2011-07-09 05:15:37 +00:00
* connect the role_pin to ground on one. The ping node sends the current
2011-05-11 21:50:07 +00:00
* time to the pong node, which responds by sending the value back. The ping
* node can then see how long the whole cycle took.
*/
2011-07-09 05:15:37 +00:00
#include <SPI.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
2011-07-09 05:15:37 +00:00
// Leave open to be the 'ping' transmitter
const int role_pin = 7;
//
// Topology
//
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
//
// Role management
//
// Set up role. This sketch uses the same software for all the nodes
// in this system. Doing so greatly simplifies testing. The hardware itself specifies
// which node it is.
//
// This is done through the role_pin
//
// The various roles supported by this sketch
typedef enum { role_ping_out = 1, role_pong_back } role_e;
// The debug-friendly names of those roles
const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
// The role of the current running sketch
role_e role;
//
// Sleep declarations
//
typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms, wdt_1s, wdt_2s, wdt_4s, wdt_8s } wdt_prescalar_e;
void setup_watchdog(uint8_t prescalar);
void do_sleep(void);
const short sleep_cycles_per_transmission = 4;
volatile short sleep_cycles_remaining = sleep_cycles_per_transmission;
//
// Normal operation
//
void setup(void)
{
//
// Role
//
2011-07-09 05:15:37 +00:00
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
2011-07-09 05:15:37 +00:00
// read the address pin, establish our role
if ( digitalRead(role_pin) )
role = role_ping_out;
else
role = role_pong_back;
//
// Print preamble
//
2011-07-09 05:15:37 +00:00
2011-06-28 04:06:08 +00:00
Serial.begin(57600);
printf_begin();
printf("\n\rRF24/examples/pingpair_sleepy/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
//
// Prepare sleep parameters
//
// Only the ping out role sleeps. Wake up every 4s to send a ping
if ( role == role_ping_out )
setup_watchdog(wdt_1s);
//
// Setup and configure rf radio
//
2011-07-09 05:15:37 +00:00
radio.begin();
//
// Open pipes to other nodes for communication
//
2011-07-09 05:15:37 +00:00
// This simple sketch opens two pipes for these two nodes to communicate
// back and forth.
// Open 'our' pipe for writing
// Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
2011-07-09 05:15:37 +00:00
if ( role == role_ping_out )
{
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
}
else
{
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
//
// Start listening
//
2011-07-09 05:15:37 +00:00
radio.startListening();
2011-07-09 05:15:37 +00:00
//
// Dump the configuration of the rf unit for debugging
//
2011-07-09 05:15:37 +00:00
radio.printDetails();
}
void loop(void)
{
//
// Ping out role. Repeatedly send the current time
//
2011-07-09 05:15:37 +00:00
if (role == role_ping_out)
{
// First, stop listening so we can talk.
radio.stopListening();
2011-07-09 05:15:37 +00:00
// Take the time, and send it. This will block until complete
unsigned long time = millis();
printf("Now sending %lu...",time);
2011-07-09 05:15:37 +00:00
radio.write( &time, sizeof(unsigned long) );
// Now, continue listening
radio.startListening();
2011-07-09 05:15:37 +00:00
// Wait here until we get a response, or timeout (250ms)
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout )
if (millis() - started_waiting_at > 250 )
timeout = true;
2011-07-09 05:15:37 +00:00
// Describe the results
if ( timeout )
{
printf("Failed, response timed out.\n\r");
}
else
{
// Grab the response, compare, and send to debugging spew
unsigned long got_time;
radio.read( &got_time, sizeof(unsigned long) );
2011-07-09 05:15:37 +00:00
// Spew it
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
}
2011-07-09 05:15:37 +00:00
//
// Shut down the system
//
// Experiment with some delay here to see if it has an effect
delay(500);
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
radio.powerDown();
// Sleep the MCU. The watchdog timer will awaken in a short while, and
// continue execution here.
while( sleep_cycles_remaining )
do_sleep();
2011-07-09 05:15:37 +00:00
sleep_cycles_remaining = sleep_cycles_per_transmission;
}
2011-07-09 05:15:37 +00:00
//
// Pong back role. Receive each packet, dump it out, and send it back
//
// This is untouched from the pingpair example.
//
2011-07-09 05:15:37 +00:00
if ( role == role_pong_back )
{
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
unsigned long got_time;
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_time, sizeof(unsigned long) );
2011-07-09 05:15:37 +00:00
// Spew it. Include our time, because the ping_out millis counter is unreliable
2011-07-09 05:15:37 +00:00
// due to it sleeping
printf("Got payload %lu @ %lu...",got_time,millis());
}
2011-07-09 05:15:37 +00:00
// First, stop listening so we can talk
radio.stopListening();
2011-07-09 05:15:37 +00:00
// Send the final one back.
2011-07-09 05:15:37 +00:00
radio.write( &got_time, sizeof(unsigned long) );
printf("Sent response.\n\r");
2011-07-09 05:15:37 +00:00
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
}
//
2011-07-09 05:15:37 +00:00
// Sleep helpers
//
// 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
2011-07-09 05:15:37 +00:00
void setup_watchdog(uint8_t prescalar)
{
2011-07-09 05:15:37 +00:00
prescalar = min(9,prescalar);
uint8_t wdtcsr = prescalar & 7;
if ( prescalar & 8 )
wdtcsr |= _BV(WDP3);
MCUSR &= ~_BV(WDRF);
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE);
}
2011-07-09 05:15:37 +00:00
ISR(WDT_vect)
{
--sleep_cycles_remaining;
}
void do_sleep(void)
{
2011-07-09 05:15:37 +00:00
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
2011-07-09 05:15:37 +00:00
sleep_mode(); // System sleeps here
2011-07-09 05:15:37 +00:00
sleep_disable(); // System continues execution here when watchdog timed out
}
// vim:ai:cin:sts=2 sw=2 ft=cpp