/*
RF02 TX duckweed collector

in arduino version 022

tested with MLAB modules with ATmega 328 at 16MHz
see RF02 library for a Band and Frequency
*/

//connections RF02:  (TX)
// SDI,  -> RF02   Atmega PB0 Arduino 8  cannot be changed
// SCK,  -> RF02   Atmega PB1 Arduino 9  cannot be changed
// nSEL, -> RF02   Atmega PB2 Arduino 10 cannot be changed
// nIRQ, <- RF02   Atmega PB4 Arduino 12 cannot be changed
// FSK: Pullupto VCC

#include <RF02.h>

unsigned int MotorOffset;
unsigned int RudderOffset;

void setup() 
{
  Serial.begin(9600);
  Serial.println("\nSTM02 init" );

  MotorOffset = analogRead(A0);            
  RudderOffset = analogRead(A1);            
 
  rf02_prepAll();

  Serial.println("done" );
}

void loop() 
{
  unsigned char _tx_buffer[]="aab\0";
  unsigned int Motor;
  unsigned int Rudder;
  unsigned int Check;
  signed int ble;
  
  Motor = analogRead(A0);            
  Rudder = analogRead(A1);            

  Motor = MotorOffset - Motor;            
  Rudder = 512 - ((RudderOffset - Rudder)<<2);
  Motor>>=4;
  Rudder>>=5;  

  if (Motor>32) Motor=0;
  if (Rudder>32) Rudder=0;
  Check=Motor+Rudder;
  if (Check>32) Check-=32;

  Motor+=(Motor>9)?('a'-10):'0';
  Rudder+=(Rudder>9)?('a'-10):'0';
  Check+=(Check>9)?('a'-10):'0';
  
  _tx_buffer[0]=Motor;
  _tx_buffer[1]=Rudder;
  _tx_buffer[2]=Check;
  
  rf02_txdata( _tx_buffer, sizeof(_tx_buffer));
}