mirror of
				https://github.com/digistump/DigistumpArduino.git
				synced 2025-11-03 21:14:46 -08:00 
			
		
		
		
	switch to setup for Arduino Boards Manager
This commit is contained in:
		@@ -0,0 +1,40 @@
 | 
			
		||||
#############################################################################
 | 
			
		||||
#
 | 
			
		||||
# Makefile for librf24 examples on Raspberry Pi
 | 
			
		||||
#
 | 
			
		||||
# License: GPL (General Public License)
 | 
			
		||||
# Author:  gnulnulf <arco@appeltaart.mine.nu>
 | 
			
		||||
# Date:    2013/02/07 (version 1.0)
 | 
			
		||||
#
 | 
			
		||||
# Description:
 | 
			
		||||
# ------------
 | 
			
		||||
# use make all and make install to install the examples
 | 
			
		||||
# You can change the install directory by editing the prefix line
 | 
			
		||||
#
 | 
			
		||||
prefix := /usr/local
 | 
			
		||||
 | 
			
		||||
# The recommended compiler flags for the Raspberry Pi
 | 
			
		||||
CCFLAGS=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s
 | 
			
		||||
#CCFLAGS=
 | 
			
		||||
 | 
			
		||||
# define all programs
 | 
			
		||||
#PROGRAMS = scanner pingtest pongtest
 | 
			
		||||
PROGRAMS = rpi-hub scanner pingtest pongtest
 | 
			
		||||
SOURCES = ${PROGRAMS:=.cpp}
 | 
			
		||||
 | 
			
		||||
all: ${PROGRAMS}
 | 
			
		||||
 | 
			
		||||
${PROGRAMS}: ${SOURCES}
 | 
			
		||||
	g++ ${CCFLAGS} -Wall -I../ -lrf24-bcm $@.cpp -o $@
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm -rf $(PROGRAMS)
 | 
			
		||||
 | 
			
		||||
install: all
 | 
			
		||||
	test -d $(prefix) || mkdir $(prefix)
 | 
			
		||||
	test -d $(prefix)/bin || mkdir $(prefix)/bin
 | 
			
		||||
	for prog in $(PROGRAMS); do \
 | 
			
		||||
	  install -m 0755 $$prog $(prefix)/bin; \
 | 
			
		||||
	done
 | 
			
		||||
 | 
			
		||||
.PHONY: install
 | 
			
		||||
@@ -0,0 +1,230 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 
 | 
			
		||||
 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
 | 
			
		||||
              Modified to use with Arduipi board http://hallard.me/arduipi
 | 
			
		||||
						  Changed to use modified bcm2835 and RF24 library 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Example RF Radio Ping Pair
 | 
			
		||||
 *
 | 
			
		||||
 * This is an example of how to use the RF24 class.  Write this sketch to two different nodes,
 | 
			
		||||
 * 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 <cstdlib>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "./RF24.h"
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Hardware configuration
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// CE Pin, CSN Pin, SPI Speed
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ); 
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
 | 
			
		||||
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_8MHZ);  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
 | 
			
		||||
// 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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
  //
 | 
			
		||||
  // Role
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  // set up the role 
 | 
			
		||||
  role = role_ping_out;
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble:
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  printf("RF24/examples/pingtest/\n");
 | 
			
		||||
  printf("ROLE: %s\n",role_friendly_name[role]);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Setup and configure rf radio
 | 
			
		||||
  //
 | 
			
		||||
  radio.begin();
 | 
			
		||||
 | 
			
		||||
  // optionally, increase the delay between retries & # of retries
 | 
			
		||||
  radio.setRetries(15,15);
 | 
			
		||||
 | 
			
		||||
  // optionally, reduce the payload size.  seems to
 | 
			
		||||
  // improve reliability
 | 
			
		||||
	//  radio.setPayloadSize(8);
 | 
			
		||||
	radio.setChannel(0x4c);
 | 
			
		||||
  radio.setPALevel(RF24_PA_LOW);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // 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]);
 | 
			
		||||
    radio.openReadingPipe(1,pipes[1]);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    radio.openWritingPipe(pipes[1]);
 | 
			
		||||
    radio.openReadingPipe(1,pipes[0]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Start listening
 | 
			
		||||
  //
 | 
			
		||||
  radio.startListening();
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Dump the configuration of the rf unit for debugging
 | 
			
		||||
  //
 | 
			
		||||
  radio.printDetails();
 | 
			
		||||
  //
 | 
			
		||||
  // Ping out role.  Repeatedly send the current time
 | 
			
		||||
  //
 | 
			
		||||
	
 | 
			
		||||
	// forever loop
 | 
			
		||||
	while (1)
 | 
			
		||||
	{
 | 
			
		||||
		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);
 | 
			
		||||
			bool ok = radio.write( &time, sizeof(unsigned long) );
 | 
			
		||||
			
 | 
			
		||||
			if (ok)
 | 
			
		||||
				printf("ok...");
 | 
			
		||||
			else
 | 
			
		||||
				printf("failed.\n");
 | 
			
		||||
 | 
			
		||||
			// 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 ) {
 | 
			
		||||
					// by bcatalin » Thu Feb 14, 2013 11:26 am
 | 
			
		||||
					delay(5); //add a small delay to let radio.available to check payload
 | 
			
		||||
				if (millis() - started_waiting_at > 200 )
 | 
			
		||||
					timeout = true;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			// Describe the results
 | 
			
		||||
			if ( timeout )
 | 
			
		||||
			{
 | 
			
		||||
				printf("Failed, response timed out.\n");
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				// 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",got_time,millis()-got_time);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Try again 1s later
 | 
			
		||||
			//    delay(1000);
 | 
			
		||||
			sleep(1);
 | 
			
		||||
			
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//
 | 
			
		||||
		// Pong back role.  Receive each packet, dump it out, and send it back
 | 
			
		||||
		//
 | 
			
		||||
 | 
			
		||||
		if ( role == role_pong_back )
 | 
			
		||||
		{
 | 
			
		||||
			// if there is data ready
 | 
			
		||||
			//printf("Check available...\n");
 | 
			
		||||
			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) );
 | 
			
		||||
 | 
			
		||||
					// Spew it
 | 
			
		||||
					printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
 | 
			
		||||
 | 
			
		||||
					// Delay just a little bit to let the other unit
 | 
			
		||||
					// make the transition to receiver
 | 
			
		||||
					delay(20);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// First, stop listening so we can talk
 | 
			
		||||
				radio.stopListening();
 | 
			
		||||
 | 
			
		||||
				// Send the final one back.
 | 
			
		||||
				radio.write( &got_time, sizeof(unsigned long) );
 | 
			
		||||
 | 
			
		||||
				// Now, resume listening so we catch the next packets.
 | 
			
		||||
				radio.startListening();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	} // forever loop
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// vim:cin:ai:sts=2 sw=2 ft=cpp
 | 
			
		||||
@@ -0,0 +1,229 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 
 | 
			
		||||
 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
 | 
			
		||||
              Modified to use with Arduipi board http://hallard.me/arduipi
 | 
			
		||||
						  Changed to use modified bcm2835 and RF24 library 
 | 
			
		||||
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Example RF Radio Ping Pair
 | 
			
		||||
 *
 | 
			
		||||
 * This is an example of how to use the RF24 class.  Write this sketch to two different nodes,
 | 
			
		||||
 * 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 <cstdlib>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "./RF24.h"
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Hardware configuration
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// CE Pin, CSN Pin, SPI Speed
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ); 
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
 | 
			
		||||
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_8MHZ);  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
 | 
			
		||||
// 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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
  // Role
 | 
			
		||||
  role = role_pong_back;
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble:
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  //Serial.begin(115200);
 | 
			
		||||
  //printf_begin();
 | 
			
		||||
  printf("RF24/examples/pongtest/\n");
 | 
			
		||||
  printf("ROLE: %s\n",role_friendly_name[role]);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Setup and configure rf radio
 | 
			
		||||
  //
 | 
			
		||||
  radio.begin();
 | 
			
		||||
 | 
			
		||||
  // optionally, increase the delay between retries & # of retries
 | 
			
		||||
  radio.setRetries(15,15);
 | 
			
		||||
 | 
			
		||||
  // optionally, reduce the payload size.  seems to
 | 
			
		||||
  // improve reliability
 | 
			
		||||
	//  radio.setPayloadSize(8);
 | 
			
		||||
	radio.setChannel(0x4c);
 | 
			
		||||
  radio.setPALevel(RF24_PA_LOW);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // 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]);
 | 
			
		||||
    radio.openReadingPipe(1,pipes[1]);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    radio.openWritingPipe(pipes[1]);
 | 
			
		||||
    radio.openReadingPipe(1,pipes[0]);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Start listening
 | 
			
		||||
  //
 | 
			
		||||
  radio.startListening();
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Dump the configuration of the rf unit for debugging
 | 
			
		||||
  //
 | 
			
		||||
  radio.printDetails();
 | 
			
		||||
  //
 | 
			
		||||
  // Ping out role.  Repeatedly send the current time
 | 
			
		||||
  //
 | 
			
		||||
	
 | 
			
		||||
	// forever loop
 | 
			
		||||
	while (1)
 | 
			
		||||
	{
 | 
			
		||||
		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);
 | 
			
		||||
			bool ok = radio.write( &time, sizeof(unsigned long) );
 | 
			
		||||
			
 | 
			
		||||
			if (ok)
 | 
			
		||||
				printf("ok...");
 | 
			
		||||
			else
 | 
			
		||||
				printf("failed.\n");
 | 
			
		||||
 | 
			
		||||
			// 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 ) {
 | 
			
		||||
					// by bcatalin » Thu Feb 14, 2013 11:26 am
 | 
			
		||||
					delay(5); //add a small delay to let radio.available to check payload
 | 
			
		||||
				if (millis() - started_waiting_at > 200 )
 | 
			
		||||
					timeout = true;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			// Describe the results
 | 
			
		||||
			if ( timeout )
 | 
			
		||||
			{
 | 
			
		||||
				printf("Failed, response timed out.\n");
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				// 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",got_time,millis()-got_time);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Try again 1s later
 | 
			
		||||
			//    delay(1000);
 | 
			
		||||
			sleep(1);
 | 
			
		||||
			
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//
 | 
			
		||||
		// Pong back role.  Receive each packet, dump it out, and send it back
 | 
			
		||||
		//
 | 
			
		||||
 | 
			
		||||
		if ( role == role_pong_back )
 | 
			
		||||
		{
 | 
			
		||||
			// if there is data ready
 | 
			
		||||
			//printf("Check available...\n");
 | 
			
		||||
			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) );
 | 
			
		||||
 | 
			
		||||
					// Spew it
 | 
			
		||||
					printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
 | 
			
		||||
 | 
			
		||||
					// Delay just a little bit to let the other unit
 | 
			
		||||
					// make the transition to receiver
 | 
			
		||||
					delay(20);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// First, stop listening so we can talk
 | 
			
		||||
				radio.stopListening();
 | 
			
		||||
 | 
			
		||||
				// Send the final one back.
 | 
			
		||||
				radio.write( &got_time, sizeof(unsigned long) );
 | 
			
		||||
 | 
			
		||||
				// Now, resume listening so we catch the next packets.
 | 
			
		||||
				radio.startListening();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	} // forever loop
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// vim:cin:ai:sts=2 sw=2 ft=cpp
 | 
			
		||||
@@ -0,0 +1,134 @@
 | 
			
		||||
/* 
 | 
			
		||||
 *
 | 
			
		||||
 *  Filename : rpi-hub.cpp
 | 
			
		||||
 *
 | 
			
		||||
 *  This program makes the RPi as a hub listening to all six pipes from the remote sensor nodes ( usually Arduino )
 | 
			
		||||
 *  and will return the packet back to the sensor on pipe0 so that the sender can calculate the round trip delays
 | 
			
		||||
 *  when the payload matches.
 | 
			
		||||
 *  
 | 
			
		||||
 *  I encounter that at times, it also receive from pipe7 ( or pipe0 ) with content of FFFFFFFFF that I will not sent
 | 
			
		||||
 *  back to the sender
 | 
			
		||||
 *
 | 
			
		||||
 *  Refer to RF24/examples/rpi_hub_arduino/ for the corresponding Arduino sketches to work with this code.
 | 
			
		||||
 * 
 | 
			
		||||
 *  
 | 
			
		||||
 *  CE is not used and CSN is GPIO25 (not pinout)
 | 
			
		||||
 *
 | 
			
		||||
 *  Refer to RPi docs for GPIO numbers
 | 
			
		||||
 *
 | 
			
		||||
 *  Author : Stanley Seow
 | 
			
		||||
 *  e-mail : stanleyseow@gmail.com
 | 
			
		||||
 *  date   : 6th Mar 2013
 | 
			
		||||
 *
 | 
			
		||||
 * 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
 | 
			
		||||
 *              Modified to use with Arduipi board http://hallard.me/arduipi
 | 
			
		||||
 *						  Changed to use modified bcm2835 and RF24 library 
 | 
			
		||||
 *
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "./RF24.h"
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
// Radio pipe addresses for the 2 nodes to communicate.
 | 
			
		||||
// First pipe is for writing, 2nd, 3rd, 4th, 5th & 6th is for reading...
 | 
			
		||||
const uint64_t pipes[6] = 
 | 
			
		||||
					{ 0xF0F0F0F0D2LL, 0xF0F0F0F0E1LL, 
 | 
			
		||||
						0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL, 
 | 
			
		||||
						0xF0F0F0F0F1, 0xF0F0F0F0F2 
 | 
			
		||||
					};
 | 
			
		||||
 | 
			
		||||
// CE Pin, CSN Pin, SPI Speed
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ); 
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
 | 
			
		||||
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_8MHZ);  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) 
 | 
			
		||||
{
 | 
			
		||||
	uint8_t len;
 | 
			
		||||
 | 
			
		||||
	// Refer to RF24.h or nRF24L01 DS for settings
 | 
			
		||||
	radio.begin();
 | 
			
		||||
	radio.enableDynamicPayloads();
 | 
			
		||||
	radio.setAutoAck(1);
 | 
			
		||||
	radio.setRetries(15,15);
 | 
			
		||||
	radio.setDataRate(RF24_1MBPS);
 | 
			
		||||
	radio.setPALevel(RF24_PA_MAX);
 | 
			
		||||
	radio.setChannel(76);
 | 
			
		||||
	radio.setCRCLength(RF24_CRC_16);
 | 
			
		||||
 | 
			
		||||
	// Open 6 pipes for readings ( 5 plus pipe0, also can be used for reading )
 | 
			
		||||
	radio.openWritingPipe(pipes[0]);
 | 
			
		||||
	radio.openReadingPipe(1,pipes[1]);
 | 
			
		||||
	radio.openReadingPipe(2,pipes[2]);
 | 
			
		||||
	radio.openReadingPipe(3,pipes[3]);
 | 
			
		||||
	radio.openReadingPipe(4,pipes[4]);
 | 
			
		||||
	radio.openReadingPipe(5,pipes[5]);
 | 
			
		||||
 | 
			
		||||
	//
 | 
			
		||||
	// Start listening
 | 
			
		||||
	//
 | 
			
		||||
	radio.startListening();
 | 
			
		||||
 | 
			
		||||
	//
 | 
			
		||||
	// Dump the configuration of the rf unit for debugging
 | 
			
		||||
	//
 | 
			
		||||
	radio.printDetails();
 | 
			
		||||
	
 | 
			
		||||
	printf("Output below : \n");
 | 
			
		||||
	delay(1);
 | 
			
		||||
	
 | 
			
		||||
	while(1)
 | 
			
		||||
	{
 | 
			
		||||
		char receivePayload[32];
 | 
			
		||||
		uint8_t pipe = 1;
 | 
			
		||||
		
 | 
			
		||||
		// Start listening
 | 
			
		||||
		radio.startListening();
 | 
			
		||||
					
 | 
			
		||||
		while ( radio.available(&pipe) ) 
 | 
			
		||||
		{
 | 
			
		||||
			len = radio.getDynamicPayloadSize();
 | 
			
		||||
			radio.read( receivePayload, len );
 | 
			
		||||
 | 
			
		||||
			// Display it on screen
 | 
			
		||||
			printf("Recv: size=%i payload=%s pipe=%i",len,receivePayload,pipe);
 | 
			
		||||
 | 
			
		||||
			// Send back payload to sender
 | 
			
		||||
			radio.stopListening();
 | 
			
		||||
 | 
			
		||||
			// if pipe is 7, do not send it back
 | 
			
		||||
			if ( pipe != 7 ) 
 | 
			
		||||
			{
 | 
			
		||||
				radio.write(receivePayload,len);
 | 
			
		||||
				receivePayload[len]=0;
 | 
			
		||||
				printf("\t Send: size=%i payload=%s pipe:%i\n",len,receivePayload,pipe);
 | 
			
		||||
			}
 | 
			
		||||
			else 
 | 
			
		||||
			{
 | 
			
		||||
				printf("\n");
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
			pipe++;
 | 
			
		||||
			
 | 
			
		||||
			// reset pipe to 0
 | 
			
		||||
			if ( pipe > 6 ) 
 | 
			
		||||
				pipe = 0;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		delayMicroseconds(20);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,146 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 
 | 
			
		||||
 
 | 
			
		||||
 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
 | 
			
		||||
              Modified to use with Arduipi board http://hallard.me/arduipi
 | 
			
		||||
						  Changed to use modified bcm2835 and RF24 library 
 | 
			
		||||
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Channel scanner
 | 
			
		||||
 *
 | 
			
		||||
 * Example to detect interference on the various channels available.
 | 
			
		||||
 * This is a good diagnostic tool to check whether you're picking a
 | 
			
		||||
 * good channel for your application.
 | 
			
		||||
 *
 | 
			
		||||
 * Inspired by cpixip.
 | 
			
		||||
 * See http://arduino.cc/forum/index.php/topic,54795.0.html
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <cstdlib>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "./RF24.h"
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Hardware configuration
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// CE Pin, CSN Pin, SPI Speed
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ);
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
 | 
			
		||||
//RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ); 
 | 
			
		||||
 | 
			
		||||
// Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
 | 
			
		||||
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_8MHZ);  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Channel info
 | 
			
		||||
//
 | 
			
		||||
//const uint8_t num_channels = 128;
 | 
			
		||||
const uint8_t num_channels = 120;
 | 
			
		||||
uint8_t values[num_channels];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const int num_reps = 100;
 | 
			
		||||
int reset_array=0;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  //Serial.begin(57600);
 | 
			
		||||
  //printf_begin();
 | 
			
		||||
  printf("RF24/examples/scanner/\n");
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Setup and configure rf radio
 | 
			
		||||
  //
 | 
			
		||||
  radio.begin();
 | 
			
		||||
	
 | 
			
		||||
  radio.setAutoAck(false);
 | 
			
		||||
 | 
			
		||||
  // Get into standby mode
 | 
			
		||||
  radio.startListening();
 | 
			
		||||
  radio.stopListening();
 | 
			
		||||
 | 
			
		||||
  radio.printDetails();
 | 
			
		||||
 | 
			
		||||
  // Print out header, high then low digit
 | 
			
		||||
  int i = 0;
 | 
			
		||||
	
 | 
			
		||||
  while ( i < num_channels )
 | 
			
		||||
  {
 | 
			
		||||
    printf("%x",i>>4);
 | 
			
		||||
    ++i;
 | 
			
		||||
  }
 | 
			
		||||
  printf("\n");
 | 
			
		||||
	
 | 
			
		||||
  i = 0;
 | 
			
		||||
  while ( i < num_channels )
 | 
			
		||||
  {
 | 
			
		||||
    printf("%x",i&0xf);
 | 
			
		||||
    ++i;
 | 
			
		||||
  }
 | 
			
		||||
  printf("\n");       
 | 
			
		||||
	
 | 
			
		||||
	// forever loop
 | 
			
		||||
  while(1)
 | 
			
		||||
	{
 | 
			
		||||
		if ( reset_array == 1 )
 | 
			
		||||
		{	
 | 
			
		||||
			// Clear measurement values
 | 
			
		||||
			memset(values,0,sizeof(values));
 | 
			
		||||
			printf("\n");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Scan all channels num_reps times
 | 
			
		||||
		int i = num_channels;
 | 
			
		||||
		while (i--)
 | 
			
		||||
		{
 | 
			
		||||
			// Select this channel
 | 
			
		||||
			radio.setChannel(i);
 | 
			
		||||
 | 
			
		||||
			// Listen for a little
 | 
			
		||||
			radio.startListening();
 | 
			
		||||
			delayMicroseconds(128);
 | 
			
		||||
			radio.stopListening();
 | 
			
		||||
 | 
			
		||||
			// Did we get a carrier?
 | 
			
		||||
			if ( radio.testCarrier() )
 | 
			
		||||
					++values[i];
 | 
			
		||||
			if ( values[i] == 0xf ) 
 | 
			
		||||
			{
 | 
			
		||||
				reset_array = 2;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Print out channel measurements, clamped to a single hex digit
 | 
			
		||||
		i = 0;
 | 
			
		||||
		while ( i < num_channels )
 | 
			
		||||
		{
 | 
			
		||||
			printf("%x",min(0xf,(values[i]&0xf)));
 | 
			
		||||
			++i;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		printf("\n");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// vim:ai:cin:sts=2 sw=2 ft=cpp
 | 
			
		||||
		Reference in New Issue
	
	Block a user