Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
UM12.cpp@3:22a375fbcb3a, 2012-10-13 (annotated)
- Committer:
- AlexAllen
- Date:
- Sat Oct 13 11:51:43 2012 +0000
- Revision:
- 3:22a375fbcb3a
- Parent:
- 1:84430ccc5662
- Child:
- 4:c947442469dd
Added licence
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:18297993986b | 63 | void UM12::send(int msg) |
AlexAllen | 0:18297993986b | 64 | { |
AlexAllen | 0:18297993986b | 65 | char *ch; |
AlexAllen | 0:18297993986b | 66 | ch = (char*) &msg; |
AlexAllen | 0:18297993986b | 67 | for (int i=0; i<sizeof(float);i++) putc(ch[i]); |
AlexAllen | 0:18297993986b | 68 | } |
AlexAllen | 0:18297993986b | 69 | |
AlexAllen | 0:18297993986b | 70 | void UM12::send(float msg) |
AlexAllen | 0:18297993986b | 71 | { |
AlexAllen | 0:18297993986b | 72 | char *ch; |
AlexAllen | 0:18297993986b | 73 | ch = (char*) &msg; |
AlexAllen | 0:18297993986b | 74 | for (int i=0; i<sizeof(float);i++) putc(ch[i]); |
AlexAllen | 0:18297993986b | 75 | } |
AlexAllen | 0:18297993986b | 76 | |
AlexAllen | 0:18297993986b | 77 | char UM12::receive(char &msg) |
AlexAllen | 0:18297993986b | 78 | { |
AlexAllen | 0:18297993986b | 79 | msg = getc(); |
AlexAllen | 0:18297993986b | 80 | return msg; |
AlexAllen | 0:18297993986b | 81 | } |
AlexAllen | 0:18297993986b | 82 | |
AlexAllen | 0:18297993986b | 83 | int UM12::receive(int &msg) |
AlexAllen | 0:18297993986b | 84 | { |
AlexAllen | 0:18297993986b | 85 | int i, intsize = sizeof(int); |
AlexAllen | 0:18297993986b | 86 | char bytes[intsize]; |
AlexAllen | 0:18297993986b | 87 | |
AlexAllen | 0:18297993986b | 88 | for(i=0; i<intsize; i++) bytes[i] = getc(); |
AlexAllen | 0:18297993986b | 89 | |
AlexAllen | 0:18297993986b | 90 | int *rec; |
AlexAllen | 0:18297993986b | 91 | rec = (int*) bytes; |
AlexAllen | 0:18297993986b | 92 | msg = *rec; |
AlexAllen | 0:18297993986b | 93 | |
AlexAllen | 0:18297993986b | 94 | return msg; |
AlexAllen | 0:18297993986b | 95 | } |
AlexAllen | 0:18297993986b | 96 | |
AlexAllen | 0:18297993986b | 97 | |
AlexAllen | 0:18297993986b | 98 | float UM12::receive(float &msg) |
AlexAllen | 0:18297993986b | 99 | { |
AlexAllen | 0:18297993986b | 100 | int i, flsize = sizeof(float); |
AlexAllen | 0:18297993986b | 101 | char bytes[flsize]; |
AlexAllen | 0:18297993986b | 102 | |
AlexAllen | 0:18297993986b | 103 | for(i=0; i<flsize; i++) bytes[i] = getc(); |
AlexAllen | 0:18297993986b | 104 | |
AlexAllen | 0:18297993986b | 105 | float *rec; |
AlexAllen | 0:18297993986b | 106 | rec = (float*) bytes; |
AlexAllen | 0:18297993986b | 107 | msg = *rec; |
AlexAllen | 0:18297993986b | 108 | |
AlexAllen | 0:18297993986b | 109 | return msg; |
AlexAllen | 0:18297993986b | 110 | } |