Alex Allen / UM12

Dependents:   Balloon

Committer:
AlexAllen
Date:
Tue Jul 19 17:09:58 2011 +0000
Revision:
0:18297993986b
Child:
1:84430ccc5662
A library for the UM12 433MHz long range radio modules from HAC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexAllen 0:18297993986b 1 #include "UM12.h"
AlexAllen 0:18297993986b 2 #include "mbed.h"
AlexAllen 0:18297993986b 3
AlexAllen 0:18297993986b 4 UM12::UM12( PinName tx, PinName rx, PinName slp, PinName rst) : Serial::Serial(tx, rx)
AlexAllen 0:18297993986b 5 {
AlexAllen 0:18297993986b 6 if(slp != LED1) sleepPin = new DigitalOut(slp);
AlexAllen 0:18297993986b 7 else sleepPin = 0;
AlexAllen 0:18297993986b 8 if(rst != LED2) resetPin = new DigitalOut(rst);
AlexAllen 0:18297993986b 9 else resetPin = 0;
AlexAllen 0:18297993986b 10 baud(1200);
AlexAllen 0:18297993986b 11 }
AlexAllen 0:18297993986b 12
AlexAllen 0:18297993986b 13 UM12::~UM12()
AlexAllen 0:18297993986b 14 {
AlexAllen 0:18297993986b 15 if(sleepPin) delete sleepPin;
AlexAllen 0:18297993986b 16 if(resetPin) delete resetPin;
AlexAllen 0:18297993986b 17 }
AlexAllen 0:18297993986b 18
AlexAllen 0:18297993986b 19 void UM12::sleep()
AlexAllen 0:18297993986b 20 {
AlexAllen 0:18297993986b 21 if(sleepPin) *sleepPin = 1;
AlexAllen 0:18297993986b 22 }
AlexAllen 0:18297993986b 23
AlexAllen 0:18297993986b 24 void UM12::wake()
AlexAllen 0:18297993986b 25 {
AlexAllen 0:18297993986b 26 if(sleepPin) *sleepPin = 0;
AlexAllen 0:18297993986b 27 }
AlexAllen 0:18297993986b 28
AlexAllen 0:18297993986b 29 void UM12::reset()
AlexAllen 0:18297993986b 30 {
AlexAllen 0:18297993986b 31 if(resetPin)
AlexAllen 0:18297993986b 32 {
AlexAllen 0:18297993986b 33 *resetPin = 0;
AlexAllen 0:18297993986b 34 wait(0.15);
AlexAllen 0:18297993986b 35 *resetPin = 1;
AlexAllen 0:18297993986b 36 }
AlexAllen 0:18297993986b 37 }
AlexAllen 0:18297993986b 38
AlexAllen 0:18297993986b 39 void UM12::send(char msg)
AlexAllen 0:18297993986b 40 {
AlexAllen 0:18297993986b 41 putc(msg);
AlexAllen 0:18297993986b 42 }
AlexAllen 0:18297993986b 43
AlexAllen 0:18297993986b 44 void UM12::send(int msg)
AlexAllen 0:18297993986b 45 {
AlexAllen 0:18297993986b 46 char *ch;
AlexAllen 0:18297993986b 47 ch = (char*) &msg;
AlexAllen 0:18297993986b 48 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 49 }
AlexAllen 0:18297993986b 50
AlexAllen 0:18297993986b 51 void UM12::send(float msg)
AlexAllen 0:18297993986b 52 {
AlexAllen 0:18297993986b 53 char *ch;
AlexAllen 0:18297993986b 54 ch = (char*) &msg;
AlexAllen 0:18297993986b 55 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 56 }
AlexAllen 0:18297993986b 57
AlexAllen 0:18297993986b 58 char UM12::receive(char &msg)
AlexAllen 0:18297993986b 59 {
AlexAllen 0:18297993986b 60 msg = getc();
AlexAllen 0:18297993986b 61 return msg;
AlexAllen 0:18297993986b 62 }
AlexAllen 0:18297993986b 63
AlexAllen 0:18297993986b 64 int UM12::receive(int &msg)
AlexAllen 0:18297993986b 65 {
AlexAllen 0:18297993986b 66 int i, intsize = sizeof(int);
AlexAllen 0:18297993986b 67 char bytes[intsize];
AlexAllen 0:18297993986b 68
AlexAllen 0:18297993986b 69 for(i=0; i<intsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 70
AlexAllen 0:18297993986b 71 int *rec;
AlexAllen 0:18297993986b 72 rec = (int*) bytes;
AlexAllen 0:18297993986b 73 msg = *rec;
AlexAllen 0:18297993986b 74
AlexAllen 0:18297993986b 75 return msg;
AlexAllen 0:18297993986b 76 }
AlexAllen 0:18297993986b 77
AlexAllen 0:18297993986b 78
AlexAllen 0:18297993986b 79 float UM12::receive(float &msg)
AlexAllen 0:18297993986b 80 {
AlexAllen 0:18297993986b 81 int i, flsize = sizeof(float);
AlexAllen 0:18297993986b 82 char bytes[flsize];
AlexAllen 0:18297993986b 83
AlexAllen 0:18297993986b 84 for(i=0; i<flsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 85
AlexAllen 0:18297993986b 86 float *rec;
AlexAllen 0:18297993986b 87 rec = (float*) bytes;
AlexAllen 0:18297993986b 88 msg = *rec;
AlexAllen 0:18297993986b 89
AlexAllen 0:18297993986b 90 return msg;
AlexAllen 0:18297993986b 91 }