-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
121 lines (95 loc) · 3.96 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @file main.cpp
* Example use of nRF24L01+ library by Owen Edwards
* https://os.mbed.com/users/Owen/code/nRF24L01P_Hello_World/
*
* @author Isabella Bologna
* @date Aug 2020
*
* @section LICENSE
*
* @copyright mbed Microcontroller Library
* Copyright (c) 2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* @copyright (c) 2010 Owen Edwards
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @copydoc main.cpp https://os.mbed.com/users/Owen/code/nRF24L01P_Hello_World/
* @copydoc nRF24L01P.cpp
*/
#include "mbed.h"
#include "nRF24L01P.h"
// The nRF24L01+ supports transfers from 1 to 32 bytes (some boards may not handle many bytes)
#define TRANSFER_SIZE 1
#define WAIT_TIME_MS 500 // macro for generic delay time, in milisseconds
/*** Pins definitions***/
#define MOSI PTD2 //D11
#define MISO PTD3 //D12
#define SCK PTC5
#define CS PTD0 //D10
#define CE PTD5 //D9
#define IRQ PTA13 //D8
DigitalOut red(LED_RED);
DigitalOut blue(LED_BLUE);
nRF24L01P my_nrf24l01p(MOSI, MISO, SCK, CS, CE, IRQ); // module instance - FRDM KL25Z
// nRF24L01P my_nrf24l01p(D11, D12, D4, D10, D9, D8); // module instance (Arduino pinout - compatible to other boards)
/* Objeto Serial */
static BufferedSerial pc(USBTX, USBRX, 9600);
int main() {
char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
int txDataCnt = 0;
int rxDataCnt = 0;
my_nrf24l01p.powerUp();
// Display the (default) setup of the nRF24L01+ chip
printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
my_nrf24l01p.setReceiveMode();
my_nrf24l01p.enable();
while (true)
{
// If we've received anything over the host serial link...
if ( pc.readable() ) {
// ...add it to the transmit buffer
// txData[txDataCnt++] = getc();
pc.read(txData,TRANSFER_SIZE);
txDataCnt++;
// If the transmit buffer is full
if ( txDataCnt >= sizeof( txData ) ) {
// Send the transmitbuffer via the nRF24L01+
my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
txDataCnt = 0;
}
// Toggle LED1 (to help debug Host -> nRF24L01+ communication)
red = !red;
}
// If we've received anything in the nRF24L01+...
if ( my_nrf24l01p.readable() ) {
// ...read the data into the receive buffer
rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
// Display the receive buffer contents via the host serial link
for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
printf("%c",rxData[i]); // putc( rxData[i] );
}
// Toggle LED2 (to help debug nRF24L01+ -> Host communication)
blue = !blue;
}
}
}