1 // *********************************************************************
    2 //   DPA peer-to-peer transmitter example                              *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: Peer-to-Peer-Transmitter.c,v $
    7 // Version: $Revision: 1.21 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2019/01/10  Release for DPA 4.00
   12 //   2017/03/13  Release for DPA 3.00
   13 //   2015/08/05  Release for DPA 2.20
   14 //
   15 // *********************************************************************
   16 
   17 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   18 
   19 // Default IQRF include (modify the path according to your setup)
   20 #include "IQRF.h"
   21 
   22 // Default DPA header (modify the path according to your setup)
   23 #include "DPA.h"
   24 
   25 // This example transmits peer-to-peer packets to CustomDpaHandler-Peer-to-Peer.c example
   26 // If the RF channel is set to the secondary channel of the main network, then example shows using peer-to-peer packets without build-in DPA support
   27 // !!! It is highly recommended to use additional security techniques (e.g. encryption, rolling code, checksum, CRC) against packet sniffing, spoofing and eavesdropping.
   28 // !!!  It is also recommended to use listen-before-talk technique to minimize the risk of RF collision that might cause the main network RF traffic to fail.
   29 
   30 // Address of the node to receive the peer-to-peer packet (modify according to your setup)
   31 #define RECEIVER_ADDRESS  1
   32 
   33 //############################################################################################
   34 void APPLICATION()
   35 //############################################################################################
   36 {
   37   // Set RF mode to STD TX, use _TX_LP when used at LP mode!
   38   setRFmode( _TX_STD | _STDL );
   39   // setRFmode( _TX_LP );
   40 
   41   // Use the very low RF power
   42   setRFpower( 0 );
   43 
   44   // Infinite loop
   45   for ( ;; )
   46   {
   47     // Sleep till button is pressed
   48     sleepWOC();
   49     if ( buttonPressed )
   50     {
   51       // Check if the button is pressed for more than 0.5s
   52       uns8 loop = 500 / 10;
   53       do
   54       {
   55         // 10 ms wait
   56         waitDelay( 1 );
   57       } while ( --loop != 0 && buttonPressed );
   58 
   59       // Prepare default PIN
   60       PIN = 0;
   61 
   62       // Was just a short (< 0.5s) button press?
   63       if ( loop != 0 )
   64       {
   65         // Indicate green LED
   66         pulseLEDG();
   67 
   68         // Prepare "DPA" peer-to-peer packet
   69 
   70         // DPA packet fields will be used
   71 #pragma warning Remove this branch
   72         _DPAF = TRUE;
   73 
   74         // Fill in PNUM and PCMD
   75         _PNUM = PNUM_LEDG;
   76         _PCMD = CMD_LED_PULSE;
   77         // No DPA Data
   78         _DpaDataLength = 0;
   79         // DPaParams DPA field will be misused for addressing as the TX and RX field are not available at non-networking packets
   80         _DpaParams = RECEIVER_ADDRESS;
   81       }
   82       else
   83       {
   84         // Indicate red LED
   85         pulseLEDR();
   86 
   87         // Prepare pure peer-to-peer packet
   88 
   89         // Prepare packet content
   90         // Header
   91         bufferRF[0] = 'P';
   92         // Address
   93         bufferRF[1] = RECEIVER_ADDRESS;
   94         // Packet length
   95         DLEN = 2;
   96       }
   97 
   98       // Transmit the prepared packet
   99       RFTXpacket();
  100 
  101       // Wait to allow LED pulse and to debounce button
  102       waitMS( 40 );
  103       // Wait till the button is released
  104       while ( buttonPressed );
  105     }
  106   }
  107 }
  108 //############################################################################################
  109