Output digital pulses of specified length and read the length of input pulses.

Dependents:   SeeedUltrasoundRangeFinder RangeFinder senplussd Sprint3_Sonar ... more

Committer:
NickRyder
Date:
Wed Jul 04 15:56:06 2012 +0000
Revision:
0:fb79a4637a64
Released under MIT license.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NickRyder 0:fb79a4637a64 1 /* Copyright (c) 2012 Nick Ryder, University of Oxford
NickRyder 0:fb79a4637a64 2 * nick.ryder@physics.ox.ac.uk
NickRyder 0:fb79a4637a64 3 *
NickRyder 0:fb79a4637a64 4 * MIT License
NickRyder 0:fb79a4637a64 5 *
NickRyder 0:fb79a4637a64 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
NickRyder 0:fb79a4637a64 7 * and associated documentation files (the "Software"), to deal in the Software without restriction,
NickRyder 0:fb79a4637a64 8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
NickRyder 0:fb79a4637a64 9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
NickRyder 0:fb79a4637a64 10 * furnished to do so, subject to the following conditions:
NickRyder 0:fb79a4637a64 11 *
NickRyder 0:fb79a4637a64 12 * The above copyright notice and this permission notice shall be included in all copies or
NickRyder 0:fb79a4637a64 13 * substantial portions of the Software.
NickRyder 0:fb79a4637a64 14 *
NickRyder 0:fb79a4637a64 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
NickRyder 0:fb79a4637a64 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NickRyder 0:fb79a4637a64 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
NickRyder 0:fb79a4637a64 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
NickRyder 0:fb79a4637a64 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
NickRyder 0:fb79a4637a64 20 */
NickRyder 0:fb79a4637a64 21
NickRyder 0:fb79a4637a64 22
NickRyder 0:fb79a4637a64 23 #include "Pulse.h"
NickRyder 0:fb79a4637a64 24
NickRyder 0:fb79a4637a64 25 PulseInOut::PulseInOut(PinName pin):
NickRyder 0:fb79a4637a64 26 startval(0), pulsetime(), runtime(), io(pin) {
NickRyder 0:fb79a4637a64 27 }
NickRyder 0:fb79a4637a64 28
NickRyder 0:fb79a4637a64 29
NickRyder 0:fb79a4637a64 30 PulseInOut::~PulseInOut() {
NickRyder 0:fb79a4637a64 31 }
NickRyder 0:fb79a4637a64 32
NickRyder 0:fb79a4637a64 33 void PulseInOut::write(int val) {
NickRyder 0:fb79a4637a64 34 io.output();
NickRyder 0:fb79a4637a64 35 io = val;
NickRyder 0:fb79a4637a64 36 }
NickRyder 0:fb79a4637a64 37
NickRyder 0:fb79a4637a64 38 void PulseInOut::write_us(int val, int time) {
NickRyder 0:fb79a4637a64 39 io.output();
NickRyder 0:fb79a4637a64 40 io = val;
NickRyder 0:fb79a4637a64 41 wait_us(time);
NickRyder 0:fb79a4637a64 42 io = !val;
NickRyder 0:fb79a4637a64 43 }
NickRyder 0:fb79a4637a64 44
NickRyder 0:fb79a4637a64 45 int PulseInOut::read_high_us() {
NickRyder 0:fb79a4637a64 46 pulsetime.reset();
NickRyder 0:fb79a4637a64 47 io.input();
NickRyder 0:fb79a4637a64 48 while (io == 1) {
NickRyder 0:fb79a4637a64 49 }
NickRyder 0:fb79a4637a64 50 while (io == 0) {
NickRyder 0:fb79a4637a64 51 }
NickRyder 0:fb79a4637a64 52 pulsetime.start();
NickRyder 0:fb79a4637a64 53 while (io == 1) {
NickRyder 0:fb79a4637a64 54 }
NickRyder 0:fb79a4637a64 55 pulsetime.stop();
NickRyder 0:fb79a4637a64 56 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 57 }
NickRyder 0:fb79a4637a64 58
NickRyder 0:fb79a4637a64 59 int PulseInOut::read_high_us(int timeout) {
NickRyder 0:fb79a4637a64 60 runtime.reset();
NickRyder 0:fb79a4637a64 61 runtime.start();
NickRyder 0:fb79a4637a64 62 pulsetime.reset();
NickRyder 0:fb79a4637a64 63 io.input();
NickRyder 0:fb79a4637a64 64 while (io == 1) {
NickRyder 0:fb79a4637a64 65 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 66 }
NickRyder 0:fb79a4637a64 67 while (io == 0) {
NickRyder 0:fb79a4637a64 68 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 69 }
NickRyder 0:fb79a4637a64 70 pulsetime.start();
NickRyder 0:fb79a4637a64 71 while (io == 1) {
NickRyder 0:fb79a4637a64 72 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 73 }
NickRyder 0:fb79a4637a64 74 pulsetime.stop();
NickRyder 0:fb79a4637a64 75 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 76 }
NickRyder 0:fb79a4637a64 77
NickRyder 0:fb79a4637a64 78 int PulseInOut::read_low_us() {
NickRyder 0:fb79a4637a64 79 pulsetime.reset();
NickRyder 0:fb79a4637a64 80 io.input();
NickRyder 0:fb79a4637a64 81 while (io == 0) {
NickRyder 0:fb79a4637a64 82 }
NickRyder 0:fb79a4637a64 83 while (io == 1) {
NickRyder 0:fb79a4637a64 84 }
NickRyder 0:fb79a4637a64 85 pulsetime.start();
NickRyder 0:fb79a4637a64 86 while (io == 0) {
NickRyder 0:fb79a4637a64 87 }
NickRyder 0:fb79a4637a64 88 pulsetime.stop();
NickRyder 0:fb79a4637a64 89 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 90 }
NickRyder 0:fb79a4637a64 91
NickRyder 0:fb79a4637a64 92 int PulseInOut::read_low_us(int timeout) {
NickRyder 0:fb79a4637a64 93 runtime.reset();
NickRyder 0:fb79a4637a64 94 runtime.start();
NickRyder 0:fb79a4637a64 95 pulsetime.reset();
NickRyder 0:fb79a4637a64 96 io.input();
NickRyder 0:fb79a4637a64 97 while (io == 0) {
NickRyder 0:fb79a4637a64 98 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 99 }
NickRyder 0:fb79a4637a64 100 while (io == 1) {
NickRyder 0:fb79a4637a64 101 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 102 }
NickRyder 0:fb79a4637a64 103 pulsetime.start();
NickRyder 0:fb79a4637a64 104 while (io == 0) {
NickRyder 0:fb79a4637a64 105 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 106 }
NickRyder 0:fb79a4637a64 107 pulsetime.stop();
NickRyder 0:fb79a4637a64 108 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 109 }
NickRyder 0:fb79a4637a64 110
NickRyder 0:fb79a4637a64 111 int PulseInOut::read_us() {
NickRyder 0:fb79a4637a64 112 pulsetime.reset();
NickRyder 0:fb79a4637a64 113 io.input();
NickRyder 0:fb79a4637a64 114 startval = io;
NickRyder 0:fb79a4637a64 115 while (io == startval) {
NickRyder 0:fb79a4637a64 116 }
NickRyder 0:fb79a4637a64 117 pulsetime.start();
NickRyder 0:fb79a4637a64 118 while (io != startval) {
NickRyder 0:fb79a4637a64 119 }
NickRyder 0:fb79a4637a64 120 pulsetime.stop();
NickRyder 0:fb79a4637a64 121 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 122 }
NickRyder 0:fb79a4637a64 123
NickRyder 0:fb79a4637a64 124 int PulseInOut::read_us(int timeout) {
NickRyder 0:fb79a4637a64 125 runtime.reset();
NickRyder 0:fb79a4637a64 126 runtime.start();
NickRyder 0:fb79a4637a64 127 pulsetime.reset();
NickRyder 0:fb79a4637a64 128 io.input();
NickRyder 0:fb79a4637a64 129 startval = io;
NickRyder 0:fb79a4637a64 130 while (io == startval) {
NickRyder 0:fb79a4637a64 131 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 132 }
NickRyder 0:fb79a4637a64 133 pulsetime.start();
NickRyder 0:fb79a4637a64 134 while (io != startval) {
NickRyder 0:fb79a4637a64 135 if (runtime.read_us() > timeout) return -1;
NickRyder 0:fb79a4637a64 136 }
NickRyder 0:fb79a4637a64 137 pulsetime.stop();
NickRyder 0:fb79a4637a64 138 return pulsetime.read_us();
NickRyder 0:fb79a4637a64 139 }