astyle -A1 -s2
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Copyright (C) 2011 James Coliz, Jr. <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.
|
||||
@@ -15,11 +15,11 @@
|
||||
* ping/pong cycle.
|
||||
*
|
||||
* As with the pingpair.pde example, write this sketch to two different nodes,
|
||||
* connect the role_pin to ground on one. The ping node sends the current
|
||||
* connect the role_pin to ground on one. The ping node sends the current
|
||||
* time to the pong node, which responds by sending the value back. The ping
|
||||
* node can then see how long the whole cycle took.
|
||||
*/
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h>
|
||||
@@ -36,7 +36,7 @@
|
||||
RF24 radio(8,9);
|
||||
|
||||
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
|
||||
// Leave open to be the 'ping' transmitter
|
||||
// Leave open to be the 'ping' transmitter
|
||||
const int role_pin = 7;
|
||||
|
||||
//
|
||||
@@ -86,12 +86,12 @@ void setup(void)
|
||||
//
|
||||
// Role
|
||||
//
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
|
||||
// read the address pin, establish our role
|
||||
if ( digitalRead(role_pin) )
|
||||
role = role_ping_out;
|
||||
@@ -101,7 +101,7 @@ void setup(void)
|
||||
//
|
||||
// Print preamble
|
||||
//
|
||||
|
||||
|
||||
Serial.begin(57600);
|
||||
printf_begin();
|
||||
printf("\n\rRF24/examples/pingpair_sleepy/\n\r");
|
||||
@@ -118,18 +118,18 @@ void setup(void)
|
||||
//
|
||||
// Setup and configure rf radio
|
||||
//
|
||||
|
||||
|
||||
radio.begin();
|
||||
|
||||
//
|
||||
// Open pipes to other nodes for communication
|
||||
//
|
||||
|
||||
|
||||
// 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)
|
||||
|
||||
|
||||
if ( role == role_ping_out )
|
||||
{
|
||||
radio.openWritingPipe(pipes[0]);
|
||||
@@ -144,13 +144,13 @@ void setup(void)
|
||||
//
|
||||
// Start listening
|
||||
//
|
||||
|
||||
|
||||
radio.startListening();
|
||||
|
||||
|
||||
//
|
||||
// Dump the configuration of the rf unit for debugging
|
||||
//
|
||||
|
||||
|
||||
radio.printDetails();
|
||||
}
|
||||
|
||||
@@ -159,27 +159,27 @@ void loop(void)
|
||||
//
|
||||
// Ping out role. Repeatedly send the current time
|
||||
//
|
||||
|
||||
|
||||
if (role == role_ping_out)
|
||||
{
|
||||
// First, stop listening so we can talk.
|
||||
radio.stopListening();
|
||||
|
||||
|
||||
// Take the time, and send it. This will block until complete
|
||||
unsigned long time = millis();
|
||||
printf("Now sending %lu...",time);
|
||||
radio.write( &time, sizeof(unsigned long) );
|
||||
|
||||
radio.write( &time, sizeof(unsigned long) );
|
||||
|
||||
// Now, continue listening
|
||||
radio.startListening();
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
|
||||
// Describe the results
|
||||
if ( timeout )
|
||||
{
|
||||
@@ -190,11 +190,11 @@ void loop(void)
|
||||
// Grab the response, compare, and send to debugging spew
|
||||
unsigned long got_time;
|
||||
radio.read( &got_time, sizeof(unsigned long) );
|
||||
|
||||
|
||||
// Spew it
|
||||
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Shut down the system
|
||||
//
|
||||
@@ -210,16 +210,16 @@ void loop(void)
|
||||
// continue execution here.
|
||||
while( sleep_cycles_remaining )
|
||||
do_sleep();
|
||||
|
||||
|
||||
sleep_cycles_remaining = sleep_cycles_per_transmission;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Pong back role. Receive each packet, dump it out, and send it back
|
||||
//
|
||||
// This is untouched from the pingpair example.
|
||||
//
|
||||
|
||||
|
||||
if ( role == role_pong_back )
|
||||
{
|
||||
// if there is data ready
|
||||
@@ -232,19 +232,19 @@ void loop(void)
|
||||
{
|
||||
// Fetch the payload, and see if this was the last one.
|
||||
done = radio.read( &got_time, sizeof(unsigned long) );
|
||||
|
||||
|
||||
// Spew it. Include our time, because the ping_out millis counter is unreliable
|
||||
// due to it sleeping
|
||||
// due to it sleeping
|
||||
printf("Got payload %lu @ %lu...",got_time,millis());
|
||||
}
|
||||
|
||||
|
||||
// First, stop listening so we can talk
|
||||
radio.stopListening();
|
||||
|
||||
|
||||
// Send the final one back.
|
||||
radio.write( &got_time, sizeof(unsigned long) );
|
||||
radio.write( &got_time, sizeof(unsigned long) );
|
||||
printf("Sent response.\n\r");
|
||||
|
||||
|
||||
// Now, resume listening so we catch the next packets.
|
||||
radio.startListening();
|
||||
}
|
||||
@@ -252,36 +252,37 @@ void loop(void)
|
||||
}
|
||||
|
||||
//
|
||||
// Sleep helpers
|
||||
// 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
|
||||
|
||||
void setup_watchdog(uint8_t prescalar)
|
||||
void setup_watchdog(uint8_t prescalar)
|
||||
{
|
||||
prescalar = min(9,prescalar);
|
||||
uint8_t wdtcsr = prescalar & 7;
|
||||
if ( prescalar & 8 )
|
||||
wdtcsr |= _BV(WDP3);
|
||||
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);
|
||||
MCUSR &= ~_BV(WDRF);
|
||||
WDTCSR = _BV(WDCE) | _BV(WDE);
|
||||
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE);
|
||||
}
|
||||
|
||||
ISR(WDT_vect) {
|
||||
ISR(WDT_vect)
|
||||
{
|
||||
--sleep_cycles_remaining;
|
||||
}
|
||||
|
||||
void do_sleep(void)
|
||||
{
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
|
||||
sleep_enable();
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
|
||||
sleep_enable();
|
||||
|
||||
sleep_mode(); // System sleeps here
|
||||
sleep_mode(); // System sleeps here
|
||||
|
||||
sleep_disable(); // System continues execution here when watchdog timed out
|
||||
sleep_disable(); // System continues execution here when watchdog timed out
|
||||
}
|
||||
|
||||
// vim:ai:cin:sts=2 sw=2 ft=cpp
|
||||
|
||||
Reference in New Issue
Block a user