pagerlib-arduino.cpp 2.08 KB
Newer Older
yids's avatar
yids committed
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
/* GPL blabla free burger pls
agerlib-arduino, library for sending and receving message with arduino using pagerlin and RadioHead
*/

#include <RadioHead.h>
#include <RH_ASK.h>
#include <RHDatagram.h>
#include <SPI.h> // Not actually used but needed to compile
#include "../pagerlib/pagerlib.h"
#include "../pagerlib/packets.h"
#include <pagerlib-arduino.h>

RH_ASK driver(BITRATE);
RHDatagram manager(driver, ADDRESS);

/* pl_radio_init() 
   initialize the RadioHead lib
*/
uint8_t pl_radio_init()
{
#ifdef ARDUINO
  if (!manager.init())
    return 1; //manager init failed
#else
  printf("I dont know what to do here were is the radio?\n");
  return 1;
#endif
  return 0;
}

/* pl_send_to_radio(struct pl_pagermessage *message_ptr)
   Send a message out via the RadioHead lib
*/
uint8_t pl_send_to_radio(struct pl_pagermessage *message_ptr)
{
#ifdef ARDUINO
  Serial.print("sending - ");
  manager.setHeaderId(52);
  manager.sendto((uint8_t *)message_ptr, sizeof(struct pl_pagermessage), 2); //FIXME hardcoded address which we wont use
  manager.waitPacketSent();    
  Serial.println("OK"); 
#else
  printf("I dont know what to do here were is the radio?\n");
  return 1;
#endif
  return 0;
}

/* pl_receive_from_radio(struct pl_pagermessage *message_ptr) 
   receive a message from radio lib and put it in  *message_ptr for 
*/
uint8_t pl_receive_from_radio(struct pl_pagermessage *message_ptr)                                                                                                                   
{
#ifdef ARDUINO
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  uint8_t from;
  uint8_t to;
  uint8_t id;   
  Serial.print("Waiting for message - ");
  manager.waitAvailable();
  if (manager.recvfrom(buf, &buflen, &from, &to ,&id))
  {
    Serial.print("got message from : ");
    Serial.print(from, DEC);
    Serial.print(" : ");
    Serial.print(id);
    Serial.println(" : ");
   
    memcpy(message_ptr, &buf, sizeof(struct pl_pagermessage)); // copy the message to the struct

  }
#else
  printf("I dont know what to do here were is the radio?\n");
  return 1;
#endif
  return 0;
}