Alex Allen / UM12

Dependents:   Balloon

Committer:
AlexAllen
Date:
Tue Jul 19 19:29:21 2011 +0000
Revision:
1:84430ccc5662
Parent:
0:18297993986b
Child:
2:f2cf3a42e690
Child:
3:22a375fbcb3a
Fixed the constructor

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 1:84430ccc5662 11 format(8, Serial::Even, 1);
AlexAllen 0:18297993986b 12 }
AlexAllen 0:18297993986b 13
AlexAllen 0:18297993986b 14 UM12::~UM12()
AlexAllen 0:18297993986b 15 {
AlexAllen 0:18297993986b 16 if(sleepPin) delete sleepPin;
AlexAllen 0:18297993986b 17 if(resetPin) delete resetPin;
AlexAllen 0:18297993986b 18 }
AlexAllen 0:18297993986b 19
AlexAllen 0:18297993986b 20 void UM12::sleep()
AlexAllen 0:18297993986b 21 {
AlexAllen 0:18297993986b 22 if(sleepPin) *sleepPin = 1;
AlexAllen 0:18297993986b 23 }
AlexAllen 0:18297993986b 24
AlexAllen 0:18297993986b 25 void UM12::wake()
AlexAllen 0:18297993986b 26 {
AlexAllen 0:18297993986b 27 if(sleepPin) *sleepPin = 0;
AlexAllen 0:18297993986b 28 }
AlexAllen 0:18297993986b 29
AlexAllen 0:18297993986b 30 void UM12::reset()
AlexAllen 0:18297993986b 31 {
AlexAllen 0:18297993986b 32 if(resetPin)
AlexAllen 0:18297993986b 33 {
AlexAllen 0:18297993986b 34 *resetPin = 0;
AlexAllen 0:18297993986b 35 wait(0.15);
AlexAllen 0:18297993986b 36 *resetPin = 1;
AlexAllen 0:18297993986b 37 }
AlexAllen 0:18297993986b 38 }
AlexAllen 0:18297993986b 39
AlexAllen 0:18297993986b 40 void UM12::send(char msg)
AlexAllen 0:18297993986b 41 {
AlexAllen 0:18297993986b 42 putc(msg);
AlexAllen 0:18297993986b 43 }
AlexAllen 0:18297993986b 44
AlexAllen 0:18297993986b 45 void UM12::send(int msg)
AlexAllen 0:18297993986b 46 {
AlexAllen 0:18297993986b 47 char *ch;
AlexAllen 0:18297993986b 48 ch = (char*) &msg;
AlexAllen 0:18297993986b 49 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 50 }
AlexAllen 0:18297993986b 51
AlexAllen 0:18297993986b 52 void UM12::send(float msg)
AlexAllen 0:18297993986b 53 {
AlexAllen 0:18297993986b 54 char *ch;
AlexAllen 0:18297993986b 55 ch = (char*) &msg;
AlexAllen 0:18297993986b 56 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 57 }
AlexAllen 0:18297993986b 58
AlexAllen 0:18297993986b 59 char UM12::receive(char &msg)
AlexAllen 0:18297993986b 60 {
AlexAllen 0:18297993986b 61 msg = getc();
AlexAllen 0:18297993986b 62 return msg;
AlexAllen 0:18297993986b 63 }
AlexAllen 0:18297993986b 64
AlexAllen 0:18297993986b 65 int UM12::receive(int &msg)
AlexAllen 0:18297993986b 66 {
AlexAllen 0:18297993986b 67 int i, intsize = sizeof(int);
AlexAllen 0:18297993986b 68 char bytes[intsize];
AlexAllen 0:18297993986b 69
AlexAllen 0:18297993986b 70 for(i=0; i<intsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 71
AlexAllen 0:18297993986b 72 int *rec;
AlexAllen 0:18297993986b 73 rec = (int*) bytes;
AlexAllen 0:18297993986b 74 msg = *rec;
AlexAllen 0:18297993986b 75
AlexAllen 0:18297993986b 76 return msg;
AlexAllen 0:18297993986b 77 }
AlexAllen 0:18297993986b 78
AlexAllen 0:18297993986b 79
AlexAllen 0:18297993986b 80 float UM12::receive(float &msg)
AlexAllen 0:18297993986b 81 {
AlexAllen 0:18297993986b 82 int i, flsize = sizeof(float);
AlexAllen 0:18297993986b 83 char bytes[flsize];
AlexAllen 0:18297993986b 84
AlexAllen 0:18297993986b 85 for(i=0; i<flsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 86
AlexAllen 0:18297993986b 87 float *rec;
AlexAllen 0:18297993986b 88 rec = (float*) bytes;
AlexAllen 0:18297993986b 89 msg = *rec;
AlexAllen 0:18297993986b 90
AlexAllen 0:18297993986b 91 return msg;
AlexAllen 0:18297993986b 92 }