Alex Allen / UM12

Dependents:   Balloon

Committer:
AlexAllen
Date:
Fri Jul 22 10:57:14 2011 +0000
Revision:
2:f2cf3a42e690
Parent:
1:84430ccc5662
Child:
4:c947442469dd
Now added bools

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 2:f2cf3a42e690 45 void UM12::send(bool msg)
AlexAllen 2:f2cf3a42e690 46 {
AlexAllen 2:f2cf3a42e690 47 putc((int) msg);
AlexAllen 2:f2cf3a42e690 48 }
AlexAllen 2:f2cf3a42e690 49
AlexAllen 0:18297993986b 50 void UM12::send(int msg)
AlexAllen 0:18297993986b 51 {
AlexAllen 0:18297993986b 52 char *ch;
AlexAllen 0:18297993986b 53 ch = (char*) &msg;
AlexAllen 0:18297993986b 54 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 55 }
AlexAllen 0:18297993986b 56
AlexAllen 0:18297993986b 57 void UM12::send(float msg)
AlexAllen 0:18297993986b 58 {
AlexAllen 0:18297993986b 59 char *ch;
AlexAllen 0:18297993986b 60 ch = (char*) &msg;
AlexAllen 0:18297993986b 61 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 62 }
AlexAllen 0:18297993986b 63
AlexAllen 0:18297993986b 64 char UM12::receive(char &msg)
AlexAllen 0:18297993986b 65 {
AlexAllen 0:18297993986b 66 msg = getc();
AlexAllen 0:18297993986b 67 return msg;
AlexAllen 0:18297993986b 68 }
AlexAllen 0:18297993986b 69
AlexAllen 2:f2cf3a42e690 70 bool UM12::receive(bool &msg)
AlexAllen 2:f2cf3a42e690 71 {
AlexAllen 2:f2cf3a42e690 72 msg = getc();
AlexAllen 2:f2cf3a42e690 73 return msg;
AlexAllen 2:f2cf3a42e690 74 }
AlexAllen 2:f2cf3a42e690 75
AlexAllen 0:18297993986b 76 int UM12::receive(int &msg)
AlexAllen 0:18297993986b 77 {
AlexAllen 0:18297993986b 78 int i, intsize = sizeof(int);
AlexAllen 0:18297993986b 79 char bytes[intsize];
AlexAllen 0:18297993986b 80
AlexAllen 0:18297993986b 81 for(i=0; i<intsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 82
AlexAllen 0:18297993986b 83 int *rec;
AlexAllen 0:18297993986b 84 rec = (int*) bytes;
AlexAllen 0:18297993986b 85 msg = *rec;
AlexAllen 0:18297993986b 86
AlexAllen 0:18297993986b 87 return msg;
AlexAllen 0:18297993986b 88 }
AlexAllen 0:18297993986b 89
AlexAllen 0:18297993986b 90
AlexAllen 0:18297993986b 91 float UM12::receive(float &msg)
AlexAllen 0:18297993986b 92 {
AlexAllen 0:18297993986b 93 int i, flsize = sizeof(float);
AlexAllen 0:18297993986b 94 char bytes[flsize];
AlexAllen 0:18297993986b 95
AlexAllen 0:18297993986b 96 for(i=0; i<flsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 97
AlexAllen 0:18297993986b 98 float *rec;
AlexAllen 0:18297993986b 99 rec = (float*) bytes;
AlexAllen 0:18297993986b 100 msg = *rec;
AlexAllen 0:18297993986b 101
AlexAllen 0:18297993986b 102 return msg;
AlexAllen 0:18297993986b 103 }