Alex Allen / UM12

Dependents:   Balloon

Committer:
AlexAllen
Date:
Sat Oct 13 11:59:13 2012 +0000
Revision:
4:c947442469dd
Parent:
3:22a375fbcb3a
Parent:
2:f2cf3a42e690
Tried a merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexAllen 3:22a375fbcb3a 1 /* Copyright (c) 2011 Alex Allen, MIT License
AlexAllen 3:22a375fbcb3a 2 *
AlexAllen 3:22a375fbcb3a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
AlexAllen 3:22a375fbcb3a 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
AlexAllen 3:22a375fbcb3a 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
AlexAllen 3:22a375fbcb3a 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
AlexAllen 3:22a375fbcb3a 7 * furnished to do so, subject to the following conditions:
AlexAllen 3:22a375fbcb3a 8 *
AlexAllen 3:22a375fbcb3a 9 * The above copyright notice and this permission notice shall be included in all copies or
AlexAllen 3:22a375fbcb3a 10 * substantial portions of the Software.
AlexAllen 3:22a375fbcb3a 11 *
AlexAllen 3:22a375fbcb3a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
AlexAllen 3:22a375fbcb3a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
AlexAllen 3:22a375fbcb3a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
AlexAllen 3:22a375fbcb3a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AlexAllen 3:22a375fbcb3a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AlexAllen 3:22a375fbcb3a 17 */
AlexAllen 3:22a375fbcb3a 18
AlexAllen 0:18297993986b 19 #include "UM12.h"
AlexAllen 0:18297993986b 20 #include "mbed.h"
AlexAllen 0:18297993986b 21
AlexAllen 0:18297993986b 22 UM12::UM12( PinName tx, PinName rx, PinName slp, PinName rst) : Serial::Serial(tx, rx)
AlexAllen 0:18297993986b 23 {
AlexAllen 0:18297993986b 24 if(slp != LED1) sleepPin = new DigitalOut(slp);
AlexAllen 0:18297993986b 25 else sleepPin = 0;
AlexAllen 0:18297993986b 26 if(rst != LED2) resetPin = new DigitalOut(rst);
AlexAllen 0:18297993986b 27 else resetPin = 0;
AlexAllen 0:18297993986b 28 baud(1200);
AlexAllen 1:84430ccc5662 29 format(8, Serial::Even, 1);
AlexAllen 0:18297993986b 30 }
AlexAllen 0:18297993986b 31
AlexAllen 0:18297993986b 32 UM12::~UM12()
AlexAllen 0:18297993986b 33 {
AlexAllen 0:18297993986b 34 if(sleepPin) delete sleepPin;
AlexAllen 0:18297993986b 35 if(resetPin) delete resetPin;
AlexAllen 0:18297993986b 36 }
AlexAllen 0:18297993986b 37
AlexAllen 0:18297993986b 38 void UM12::sleep()
AlexAllen 0:18297993986b 39 {
AlexAllen 0:18297993986b 40 if(sleepPin) *sleepPin = 1;
AlexAllen 0:18297993986b 41 }
AlexAllen 0:18297993986b 42
AlexAllen 0:18297993986b 43 void UM12::wake()
AlexAllen 0:18297993986b 44 {
AlexAllen 0:18297993986b 45 if(sleepPin) *sleepPin = 0;
AlexAllen 0:18297993986b 46 }
AlexAllen 0:18297993986b 47
AlexAllen 0:18297993986b 48 void UM12::reset()
AlexAllen 0:18297993986b 49 {
AlexAllen 0:18297993986b 50 if(resetPin)
AlexAllen 0:18297993986b 51 {
AlexAllen 0:18297993986b 52 *resetPin = 0;
AlexAllen 0:18297993986b 53 wait(0.15);
AlexAllen 0:18297993986b 54 *resetPin = 1;
AlexAllen 0:18297993986b 55 }
AlexAllen 0:18297993986b 56 }
AlexAllen 0:18297993986b 57
AlexAllen 0:18297993986b 58 void UM12::send(char msg)
AlexAllen 0:18297993986b 59 {
AlexAllen 0:18297993986b 60 putc(msg);
AlexAllen 0:18297993986b 61 }
AlexAllen 0:18297993986b 62
AlexAllen 2:f2cf3a42e690 63 void UM12::send(bool msg)
AlexAllen 2:f2cf3a42e690 64 {
AlexAllen 2:f2cf3a42e690 65 putc((int) msg);
AlexAllen 2:f2cf3a42e690 66 }
AlexAllen 2:f2cf3a42e690 67
AlexAllen 0:18297993986b 68 void UM12::send(int msg)
AlexAllen 0:18297993986b 69 {
AlexAllen 0:18297993986b 70 char *ch;
AlexAllen 0:18297993986b 71 ch = (char*) &msg;
AlexAllen 0:18297993986b 72 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 73 }
AlexAllen 0:18297993986b 74
AlexAllen 0:18297993986b 75 void UM12::send(float msg)
AlexAllen 0:18297993986b 76 {
AlexAllen 0:18297993986b 77 char *ch;
AlexAllen 0:18297993986b 78 ch = (char*) &msg;
AlexAllen 0:18297993986b 79 for (int i=0; i<sizeof(float);i++) putc(ch[i]);
AlexAllen 0:18297993986b 80 }
AlexAllen 0:18297993986b 81
AlexAllen 0:18297993986b 82 char UM12::receive(char &msg)
AlexAllen 0:18297993986b 83 {
AlexAllen 0:18297993986b 84 msg = getc();
AlexAllen 0:18297993986b 85 return msg;
AlexAllen 0:18297993986b 86 }
AlexAllen 0:18297993986b 87
AlexAllen 2:f2cf3a42e690 88 bool UM12::receive(bool &msg)
AlexAllen 2:f2cf3a42e690 89 {
AlexAllen 2:f2cf3a42e690 90 msg = getc();
AlexAllen 2:f2cf3a42e690 91 return msg;
AlexAllen 2:f2cf3a42e690 92 }
AlexAllen 2:f2cf3a42e690 93
AlexAllen 0:18297993986b 94 int UM12::receive(int &msg)
AlexAllen 0:18297993986b 95 {
AlexAllen 0:18297993986b 96 int i, intsize = sizeof(int);
AlexAllen 0:18297993986b 97 char bytes[intsize];
AlexAllen 0:18297993986b 98
AlexAllen 0:18297993986b 99 for(i=0; i<intsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 100
AlexAllen 0:18297993986b 101 int *rec;
AlexAllen 0:18297993986b 102 rec = (int*) bytes;
AlexAllen 0:18297993986b 103 msg = *rec;
AlexAllen 0:18297993986b 104
AlexAllen 0:18297993986b 105 return msg;
AlexAllen 0:18297993986b 106 }
AlexAllen 0:18297993986b 107
AlexAllen 0:18297993986b 108
AlexAllen 0:18297993986b 109 float UM12::receive(float &msg)
AlexAllen 0:18297993986b 110 {
AlexAllen 0:18297993986b 111 int i, flsize = sizeof(float);
AlexAllen 0:18297993986b 112 char bytes[flsize];
AlexAllen 0:18297993986b 113
AlexAllen 0:18297993986b 114 for(i=0; i<flsize; i++) bytes[i] = getc();
AlexAllen 0:18297993986b 115
AlexAllen 0:18297993986b 116 float *rec;
AlexAllen 0:18297993986b 117 rec = (float*) bytes;
AlexAllen 0:18297993986b 118 msg = *rec;
AlexAllen 0:18297993986b 119
AlexAllen 0:18297993986b 120 return msg;
AlexAllen 0:18297993986b 121 }