mirror of
				https://github.com/digistump/DigistumpArduino.git
				synced 2025-11-03 13:04:48 -08:00 
			
		
		
		
	switch to setup for Arduino Boards Manager
This commit is contained in:
		@@ -0,0 +1,41 @@
 | 
			
		||||
#############################################################################
 | 
			
		||||
#
 | 
			
		||||
# 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 := /opt/librf24-examples
 | 
			
		||||
 | 
			
		||||
# The recommended compiler flags for the Raspberry Pi
 | 
			
		||||
CCFLAGS=-Wall -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 -L../librf24/  -lrf24 $@.cpp -o $@
 | 
			
		||||
	g++ ${CCFLAGS} -L../librf24/  -lrf24 $@.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,239 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
 | 
			
		||||
 | 
			
		||||
//RF24 radio(9,10);
 | 
			
		||||
RF24 radio("/dev/spidev0.0",8000000 , 25);  //spi device, speed and CSN,only CSN is NEEDED in RPI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// 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;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
  //else
 | 
			
		||||
  //  role = role_pong_back;
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble:
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  //Serial.begin(115200);
 | 
			
		||||
  //printf_begin();
 | 
			
		||||
  printf("\n\rRF24/examples/pingpair/\n\r");
 | 
			
		||||
  printf("ROLE: %s\n\r",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_MAX);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // 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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
    bool ok = radio.write( &time, sizeof(unsigned long) );
 | 
			
		||||
    
 | 
			
		||||
    if (ok)
 | 
			
		||||
      printf("ok...");
 | 
			
		||||
    else
 | 
			
		||||
      printf("failed.\n\r");
 | 
			
		||||
 | 
			
		||||
    // 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 
 | 
			
		||||
	__msleep(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\r");
 | 
			
		||||
    }
 | 
			
		||||
    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\r",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
 | 
			
		||||
    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 %lu...",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.
 | 
			
		||||
      printf("Sent response.\n\r");
 | 
			
		||||
      radio.write( &got_time, sizeof(unsigned long) );
 | 
			
		||||
 | 
			
		||||
      // Now, resume listening so we catch the next packets.
 | 
			
		||||
      radio.startListening();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
        setup();
 | 
			
		||||
        while(1)
 | 
			
		||||
                loop();
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// vim:cin:ai:sts=2 sw=2 ft=cpp
 | 
			
		||||
@@ -0,0 +1,240 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
 | 
			
		||||
 | 
			
		||||
//RF24 radio(9,10);
 | 
			
		||||
RF24 radio("/dev/spidev0.0",2000000 , 25);  //spi device, speed and CE,only CE is NEEDED in RPI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// 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;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
  //else
 | 
			
		||||
    role = role_pong_back;
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble:
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  //Serial.begin(115200);
 | 
			
		||||
  //printf_begin();
 | 
			
		||||
  printf("\n\rRF24/examples/pingpair/\n\r");
 | 
			
		||||
  printf("ROLE: %s\n\r",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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
    bool ok = radio.write( &time, sizeof(unsigned long) );
 | 
			
		||||
    
 | 
			
		||||
    if (ok)
 | 
			
		||||
      printf("ok...");
 | 
			
		||||
    else
 | 
			
		||||
      printf("failed.\n\r");
 | 
			
		||||
 | 
			
		||||
    // 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
 | 
			
		||||
        __msleep(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\r");
 | 
			
		||||
    }
 | 
			
		||||
    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\r",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
 | 
			
		||||
    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 %lu...",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.
 | 
			
		||||
      printf("Sent response.\n\r");
 | 
			
		||||
      radio.write( &got_time, sizeof(unsigned long) );
 | 
			
		||||
 | 
			
		||||
      // Now, resume listening so we catch the next packets.
 | 
			
		||||
      radio.startListening();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
        setup();
 | 
			
		||||
        while(1)
 | 
			
		||||
                loop();
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// vim:cin:ai:sts=2 sw=2 ft=cpp
 | 
			
		||||
@@ -0,0 +1,119 @@
 | 
			
		||||
/* 
 | 
			
		||||
 *
 | 
			
		||||
 *  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
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#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 and CSN pins On header using GPIO numbering (not pin numbers)
 | 
			
		||||
RF24 radio("/dev/spidev0.0",8000000,25);  // Setup for GPIO 25 CSN
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void setup(void)
 | 
			
		||||
{
 | 
			
		||||
	//
 | 
			
		||||
	// 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("\n\rOutput below : \n\r");
 | 
			
		||||
	usleep(1000);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop(void)
 | 
			
		||||
{
 | 
			
		||||
	char receivePayload[32];
 | 
			
		||||
	uint8_t pipe = 1;
 | 
			
		||||
	
 | 
			
		||||
	// Start listening
 | 
			
		||||
	radio.startListening();
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
	 while ( radio.available(&pipe) ) {
 | 
			
		||||
 | 
			
		||||
		uint8_t 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\r",len,receivePayload,pipe);
 | 
			
		||||
		} else {
 | 
			
		||||
			printf("\n\r");
 | 
			
		||||
                }
 | 
			
		||||
		pipe++;
 | 
			
		||||
		// reset pipe to 0
 | 
			
		||||
		if ( pipe > 6 ) pipe = 0;
 | 
			
		||||
	}
 | 
			
		||||
	usleep(20);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) 
 | 
			
		||||
{
 | 
			
		||||
	setup();
 | 
			
		||||
	while(1)
 | 
			
		||||
		loop();
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,189 @@
 | 
			
		||||
/*
 | 
			
		||||
 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
 | 
			
		||||
 | 
			
		||||
// CE and CSN pins 
 | 
			
		||||
//RF24 radio(8, 25);  //only CSN is NEEDED in RPI
 | 
			
		||||
RF24 radio("/dev/spidev0.0",8000000 , 25);  //spi device, speed and CSN,only CSN is NEEDED in RPI
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Channel info
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
//const uint8_t num_channels = 128;
 | 
			
		||||
const uint8_t num_channels = 120;
 | 
			
		||||
uint8_t values[num_channels];
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Setup
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
void setup(void)
 | 
			
		||||
{
 | 
			
		||||
  //
 | 
			
		||||
  // Print preamble
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  //Serial.begin(57600);
 | 
			
		||||
  //printf_begin();
 | 
			
		||||
  printf("\n\rRF24/examples/scanner/\n\r");
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // 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\r");
 | 
			
		||||
  i = 0;
 | 
			
		||||
  while ( i < num_channels )
 | 
			
		||||
  {
 | 
			
		||||
    printf("%x",i&0xf);
 | 
			
		||||
    ++i;
 | 
			
		||||
  }
 | 
			
		||||
  printf("\n\r");
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
// Loop
 | 
			
		||||
//
 | 
			
		||||
/*
 | 
			
		||||
const int num_reps = 100;
 | 
			
		||||
 | 
			
		||||
void loop2(void)
 | 
			
		||||
{
 | 
			
		||||
  // Clear measurement values
 | 
			
		||||
  memset(values,0,sizeof(values));
 | 
			
		||||
 | 
			
		||||
  // Scan all channels num_reps times
 | 
			
		||||
  int rep_counter = num_reps;
 | 
			
		||||
  while (rep_counter--)
 | 
			
		||||
  {
 | 
			
		||||
    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];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Print out channel measurements, clamped to a single hex digit
 | 
			
		||||
  int i = 0;
 | 
			
		||||
  while ( i < num_channels )
 | 
			
		||||
  {
 | 
			
		||||
    printf("%x",min(0xf,values[i]&0xf));
 | 
			
		||||
    ++i;
 | 
			
		||||
  }
 | 
			
		||||
  printf("\n\r");
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
//
 | 
			
		||||
// Loop
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
const int num_reps = 100;
 | 
			
		||||
 | 
			
		||||
int reset_array=0;
 | 
			
		||||
 | 
			
		||||
void loop(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
if ( reset_array == 1 ) {	
 | 
			
		||||
  // Clear measurement values
 | 
			
		||||
  memset(values,0,sizeof(values));
 | 
			
		||||
  printf("\n\r");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
  // 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\r");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
        setup();
 | 
			
		||||
        while(1)
 | 
			
		||||
                loop();
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// vim:ai:cin:sts=2 sw=2 ft=cpp
 | 
			
		||||
		Reference in New Issue
	
	Block a user