Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 5:7891519b8809
- Parent:
- 4:ae1520241a90
- Child:
- 6:c3d9f6a28df6
--- a/main.cpp Tue Dec 04 13:36:13 2018 +0000 +++ b/main.cpp Mon Dec 10 13:26:08 2018 +0000 @@ -7,10 +7,12 @@ nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7); // mosi, miso, sck, csn, ce, irq -#define TRANSFER_SIZE 4 +#define TRANSFER_SIZE 2 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; int txDataCnt = 0; int rxDataCnt = 0; + +int led = 0; void init() { cs = 0; @@ -51,48 +53,57 @@ cs = 1; } -int readButton() { - cs = 0; - spi.write(0x47); - spi.write(0x09); - int misoOutput = spi.write(0x00); - cs = 1; +void sendNumber(int number) { + printf("Sending led: %d\n\r", number); + // ...add it to the transmit buffer + txData[txDataCnt++] = 0; + txData[txDataCnt++] = (number); - misoOutput = misoOutput & 192; - printf("output miso: %d \n\r", misoOutput); - if (misoOutput == 128) { // S1 pressed - return 1; - } else if (misoOutput == 64) { // S2 pressed - return 5; - } - return 0; -} - -int sendNumber(int number) { - // ...add it to the transmit buffer - txData[txDataCnt++] = number; + bool ackReceived = false; // If the transmit buffer is full if ( txDataCnt >= sizeof( txData ) ) { + for(int i = 0; i <= txDataCnt; i++){ + printf("%c\n\r", txData[i]); + } // Send the transmitbuffer via the nRF24L01+ my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); txDataCnt = 0; } - - // If we've received anything in the nRF24L01+... +} + +void readNRF() { +// 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(rxData); -// pc.putc( rxData[i] ); - } + printf("Received %d for: %d\n\r", rxData[0], rxData[1]); + } +} + +void readButton() { + cs = 0; + spi.write(0x47); + spi.write(0x09); + int misoOutput = spi.write(0x00); + cs = 1; + misoOutput = misoOutput & 192; + + //printf("output miso: %d \n\r", misoOutput); + if (misoOutput == 128) { // S1 pressed + led = (led + 1) % 6; + sendNumber(led); + } else if (misoOutput == 64) { // S2 pressed + led = (led + 5) % 6; + sendNumber(led); } + + setLed(led); } int main() { @@ -102,12 +113,10 @@ printf("Setting Led. \n\r"); // Select the device by seting chip select low - int led = 0; printf("Starting \n\r"); while (true) { - led = (led + readButton()) % 6; - setLed(led); - sendNumber(led); + readButton(); + readNRF(); wait(0.1); } } \ No newline at end of file