
/*
  EasyJoystick - NRF24L01 Receiver Example

  This example receives joystick direction data wirelessly using an NRF24L01
  transceiver module and prints the received state to the Serial Monitor.

  It is designed to work with the matching transmitter example in this library.

  Requirements:
    - 2x Arduino boards (Nano or similar)
    - 2x NRF24L01 modules
    - 2x 10µF–100µF capacitors (recommended for stability)
    - RF24 library by TMRh20 (install via Library Manager)
    - Wires + breadboards

  Important:
    - NRF24L01 must be powered from 3.3V ONLY (not 5V)
    - Incorrect power wiring may permanently damage the module

  Wiring reference:
    See the docs folder in this library for diagrams.

  RF24 library:
    https://github.com/tmrh20/RF24

  More info on NRF24L01 modules:
    https://www.instructables.com/NRF24L01-Tutorial-Arduino-Wireless-Communication/
*/

#include <EasyJoystick.h>

// RF24 library
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>

RF24 radio(9, 8); // CE, CSN
const byte address[10] = "ADDRESS01";
int8_t state = EasyJoystick::NONE;

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setPayloadSize(sizeof(state));
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&state, sizeof(state));
    Serial.print(state);
    Serial.print(" = ");
    if (state == EasyJoystick::NONE) {
      Serial.println("none");
    } else if (state == EasyJoystick::MIDDLE) {
      Serial.println("middle");
    } else if (state == EasyJoystick::UP) {
      Serial.println("up");
    } else if (state == EasyJoystick::DOWN) {
      Serial.println("down");
    } else if (state == EasyJoystick::LEFT) {
      Serial.println("left");
    } else if (state == EasyJoystick::RIGHT) {
      Serial.println("right");
    }
  }
}
