Update examples, minimize code size

- Updated some example files and tests
- Small changes to minimize code size
This commit is contained in:
TMRh20
2014-04-15 09:40:36 -06:00
parent 5fabf2608a
commit e83a107bdf
12 changed files with 97 additions and 146 deletions
+4 -5
View File
@@ -61,12 +61,11 @@ void loop(void){
radio.stopListening(); // First, stop listening so we can talk.
unsigned long time;
printf("Now sending ",time);
printf("Now sending \n\r");
time = micros(); // Take the time, and send it. This will block until complete
boolean ok = radio.write( &time, sizeof(unsigned long) ); // Send the current time
if (!ok){ printf("failed.\n\r"); }
unsigned long time = micros(); // Take the time, and send it. This will block until complete
if (!radio.write( &time, sizeof(unsigned long) )){ printf("failed.\n\r"); }
radio.startListening(); // Now, continue listening
@@ -1,7 +1,7 @@
/*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com>
March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/**
* Example for efficient call-response using ack-payloads
*
+4 -4
View File
@@ -1,11 +1,11 @@
/*
Copyright (C) 2011 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.
*/
/**
* @file printf.h
*
@@ -18,12 +18,12 @@
#ifdef ARDUINO
int serial_putc( char c, FILE * )
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
}
void printf_begin(void)
{
+1 -1
View File
@@ -1,7 +1,7 @@
/*
// March 2014 - TMRh20 - Updated along with High Speed RF24 Library fork
// Parts derived from examples by J. Coliz <maniacbug@ymail.com>
*/
/**
* Example for efficient call-response using ack-payloads
*
+4 -4
View File
@@ -15,7 +15,7 @@
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
//#include "printf.h"
#include "printf.h"
//
// Hardware configuration
@@ -23,7 +23,7 @@
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
RF24 radio(7,8);
// sets the role of this unit in hardware. Connect to GND to be the 'pong' receiver
// Leave open to be the 'ping' transmitter
@@ -74,7 +74,7 @@ void setup(void)
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,LOW);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
@@ -88,7 +88,7 @@ void setup(void)
//
Serial.begin(57600);
//printf_begin();
printf_begin();
printf("\n\rRF24/examples/pingpair_dyn/\n\r");
printf("ROLE: %s\n\r",role_friendly_name[role]);
+6 -13
View File
@@ -67,13 +67,15 @@ void setup(){
radio.enableAckPayload(); // We will be using the Ack Payload feature, so please enable it
// Open pipes to other node for communication
if ( role == role_sender ) { // This simple sketch opens a pipe on a single address for these two nodes to
radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it.
radio.openWritingPipe(address[0]); // communicate back and forth. One listens on it, the other talks to it.
radio.openReadingPipe(1,address[1]);
}else{
radio.openWritingPipe(address[1]);
radio.openReadingPipe(1,address[0]);
radio.startListening();
}
radio.printDetails(); // Dump the configuration of the rf unit for debugging
radio.powerUp(); // Power up the radio
delay(50);
attachInterrupt(0, check_radio, FALLING); // Attach interrupt handler to interrupt #0 (using pin 2) on BOTH the sender and receiver
}
@@ -87,20 +89,12 @@ void loop() {
if (role == role_sender) { // Sender role. Repeatedly send the current time
unsigned long time = millis(); // Take the time, and send it.
printf("Now sending %lu\n\r",time);
radio.startWrite( &time, sizeof(unsigned long) );
radio.startWrite( &time, sizeof(unsigned long) ,0);
delay(2000); // Try again soon
}
if(role == role_receiver){ // Receiver does nothing but sleep between payloads
if(!radio.available() && gotMsg == 1 ){ // Display the uptime in millis(). (Counter does not increment during sleep)
gotMsg = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Put the Arduino to sleep between payloads
sleep_enable();
sleep_mode(); // Arduino goes to sleep here
//ZZZ... sleeping
sleep_disable(); // This happens when the interrupt wakes Arduino up
}
if(role == role_receiver){ // Receiver does nothing except in IRQ
}
}
@@ -141,4 +135,3 @@ void check_radio(void) // Receiver role: Does not
}
}
}
+6 -7
View File
@@ -71,7 +71,7 @@ void setup(){
// set up the role pin
pinMode(role_pin, INPUT);
digitalWrite(role_pin,LOW);
digitalWrite(role_pin,HIGH);
delay(20); // Just to get a solid reading on the role pin
// read the address pin, establish our role
@@ -113,7 +113,7 @@ void setup(){
radio.startListening();
// Dump the configuration of the rf unit for debugging
radio.printDetails();
//radio.printDetails();
}
void loop(){
@@ -123,8 +123,9 @@ void loop(){
radio.powerUp(); // Power up the radio after sleeping
radio.stopListening(); // First, stop listening so we can talk.
printf("Now sending... ");
unsigned long time = millis(); // Take the time, and send it.
unsigned long time = millis(); // Take the time, and send it.
printf("Now sending... %lu \n\r",time);
radio.write( &time, sizeof(unsigned long) );
radio.startListening(); // Now, continue listening
@@ -177,7 +178,7 @@ void loop(){
radio.startListening(); // Now, resume listening so we catch the next packets.
}else{
Serial.println("Sleeping");
delay(200); // Delay so the serial data can print out
delay(50); // Delay so the serial data can print out
do_sleep();
}
@@ -199,7 +200,6 @@ void setup_watchdog(uint8_t prescalar){
uint8_t wdtcsr = prescalar & 7;
if ( prescalar & 8 )
wdtcsr |= _BV(WDP3);
Serial.println(wdtcsr);
MCUSR &= ~_BV(WDRF); // Clear the WD System Reset Flag
WDTCSR = _BV(WDCE) | _BV(WDE); // Write the WD Change enable bit to enable changing the prescaler and enable system reset
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE); // Write the prescalar bits (how long to sleep, enable the interrupt to wake the MCU
@@ -223,4 +223,3 @@ void do_sleep(void)
detachInterrupt(0);
WDTCSR &= ~_BV(WDIE);
}
@@ -102,12 +102,14 @@ void loop(void)
// Listen for a little
radio.startListening();
delayMicroseconds(128);
radio.stopListening();
delayMicroseconds(225);
// Did we get a carrier?
if ( radio.testCarrier() )
if ( radio.testCarrier() ){
++values[i];
}
radio.stopListening();
}
}
@@ -121,4 +123,4 @@ void loop(void)
printf("\n\r");
}
// vim:ai:cin:sts=2 sw=2 ft=cpp
// vim:ai:cin:sts=2 sw=2 ft=cpp