Lab_interaccio/2016/nrf-USB-TX/nrf-USB-TX.ino

151 lines
2.7 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#define CE_PIN 12
#define CSN_PIN 13
#define DEBUG 1
#define TEST 0
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//uint8_t cmd[ 16 ];
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
uint8_t CMD[ 8 ]; // CMD + Row + 9 rgb
int counter = 0;
int inc = 1;
int flag = 0;
int i = 0;
char c;
void setup() {
SerialUSB.begin(115200);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
delay(2000);
radio.begin();
printf_begin();
radio.setDataRate( RF24_250KBPS );
radio.setPALevel( RF24_PA_MAX );
radio.setAutoAck(0);
radio.setPayloadSize(8);
radio.openWritingPipe(pipe);
radio.printDetails();
}
void loop()
{
if (TEST)
{
if (flag)
flag = 0;
else
flag = 1;
CMD[0] = 1;
for (int i = 1; i < 4; i++)
CMD[i] = 255 * flag;
radio.writeFast( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
if (DEBUG)
{
for (i = 0 ; i < 8 ; i++)
{
SerialUSB.print(CMD[i]);
SerialUSB.print(",");
}
SerialUSB.println();
}
/*
//MODO ACK NO FUNCIONA!!!! IDEALMENTE HA DE SER CON ESTE MODO
bool ok = radio.write( CMD, sizeof(CMD) );
if (ok)
printf("ok\n\r");
else
printf("failed\n\r");
*/
delay(100);
}
else
{
if ( SerialUSB.available() > 0 )
{
c = SerialUSB.read();
/*
CMD[0] = 'r';
CMD[1] = c;
*/
if( c =='r' ) // Reset
{
CMD[0] = 'r';
CMD[1] = SerialUSB.read(); // id
radio.writeFast( CMD, sizeof(CMD) );
// if(!radio.txStandBy())
// SerialUSB.println("Fail");
// else
// SerialUSB.println("OK");
}
else if( c =='p' ) // play / ON
{
CMD[0] = 'p';
CMD[1] = SerialUSB.read(); // id
//radio.startWrite( CMD, sizeof(CMD), 0 );
radio.writeFast( CMD, sizeof(CMD) );
// if(!radio.txStandBy())
// SerialUSB.println("Fail");
// else
// SerialUSB.println("OK");
}
else if( c =='s' ) // stop / OFF
{
CMD[0] = 's';
CMD[1] = SerialUSB.read(); // id
//radio.startWrite( CMD, sizeof(CMD), 0 );
radio.writeFast( CMD, sizeof(CMD) );
// if(!radio.txStandBy())
// SerialUSB.println("Fail");
// else
// SerialUSB.println("OK");
}
//delay(10);
//SerialUSB.println(c);
}
}
}