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.
Fork of Pulse1 by
Pulse1.cpp@2:a91e76505e52, 2015-11-07 (annotated)
- Committer:
- PabloViana
- Date:
- Sat Nov 07 22:07:03 2015 +0000
- Revision:
- 2:a91e76505e52
- Parent:
- 1:48651f86a80c
v1;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tony63 | 1:48651f86a80c | 1 | |
| tony63 | 1:48651f86a80c | 2 | #include "Pulse1.h" |
| tony63 | 1:48651f86a80c | 3 | |
| tony63 | 1:48651f86a80c | 4 | PulseInOut::PulseInOut(PinName pin): |
| tony63 | 1:48651f86a80c | 5 | startval(0), pulsetime(), runtime(), io(pin) { |
| tony63 | 1:48651f86a80c | 6 | } |
| tony63 | 1:48651f86a80c | 7 | |
| tony63 | 1:48651f86a80c | 8 | |
| tony63 | 1:48651f86a80c | 9 | PulseInOut::~PulseInOut() { |
| tony63 | 1:48651f86a80c | 10 | } |
| tony63 | 1:48651f86a80c | 11 | |
| tony63 | 1:48651f86a80c | 12 | void PulseInOut::write(int val) { |
| tony63 | 1:48651f86a80c | 13 | io.output(); |
| tony63 | 1:48651f86a80c | 14 | io = val; |
| tony63 | 1:48651f86a80c | 15 | } |
| tony63 | 1:48651f86a80c | 16 | |
| tony63 | 1:48651f86a80c | 17 | void PulseInOut::write_us(int val, int time) { |
| tony63 | 1:48651f86a80c | 18 | io.output(); |
| tony63 | 1:48651f86a80c | 19 | io = val; |
| tony63 | 1:48651f86a80c | 20 | wait_us(time); |
| tony63 | 1:48651f86a80c | 21 | io = !val; |
| tony63 | 1:48651f86a80c | 22 | } |
| tony63 | 1:48651f86a80c | 23 | |
| tony63 | 1:48651f86a80c | 24 | int PulseInOut::read_high_us() { |
| tony63 | 1:48651f86a80c | 25 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 26 | io.input(); |
| tony63 | 1:48651f86a80c | 27 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 28 | } |
| tony63 | 1:48651f86a80c | 29 | while (io == 0) { |
| tony63 | 1:48651f86a80c | 30 | } |
| tony63 | 1:48651f86a80c | 31 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 32 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 33 | } |
| tony63 | 1:48651f86a80c | 34 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 35 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 36 | } |
| tony63 | 1:48651f86a80c | 37 | |
| tony63 | 1:48651f86a80c | 38 | int PulseInOut::read_high_us(int timeout) { |
| tony63 | 1:48651f86a80c | 39 | runtime.reset(); |
| tony63 | 1:48651f86a80c | 40 | runtime.start(); |
| tony63 | 1:48651f86a80c | 41 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 42 | io.input(); |
| tony63 | 1:48651f86a80c | 43 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 44 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 45 | } |
| tony63 | 1:48651f86a80c | 46 | while (io == 0) { |
| tony63 | 1:48651f86a80c | 47 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 48 | } |
| tony63 | 1:48651f86a80c | 49 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 50 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 51 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 52 | } |
| tony63 | 1:48651f86a80c | 53 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 54 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 55 | } |
| tony63 | 1:48651f86a80c | 56 | |
| tony63 | 1:48651f86a80c | 57 | int PulseInOut::read_low_us() { |
| tony63 | 1:48651f86a80c | 58 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 59 | io.input(); |
| PabloViana | 2:a91e76505e52 | 60 | Temp1.start(); |
| PabloViana | 2:a91e76505e52 | 61 | while (io == 0 ) { |
| tony63 | 1:48651f86a80c | 62 | } |
| PabloViana | 2:a91e76505e52 | 63 | |
| tony63 | 1:48651f86a80c | 64 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 65 | } |
| PabloViana | 2:a91e76505e52 | 66 | |
| tony63 | 1:48651f86a80c | 67 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 68 | while (io == 0) { |
| tony63 | 1:48651f86a80c | 69 | } |
| tony63 | 1:48651f86a80c | 70 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 71 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 72 | } |
| tony63 | 1:48651f86a80c | 73 | |
| tony63 | 1:48651f86a80c | 74 | int PulseInOut::read_low_us(int timeout) { |
| tony63 | 1:48651f86a80c | 75 | runtime.reset(); |
| tony63 | 1:48651f86a80c | 76 | runtime.start(); |
| tony63 | 1:48651f86a80c | 77 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 78 | io.input(); |
| tony63 | 1:48651f86a80c | 79 | while (io == 0) { |
| tony63 | 1:48651f86a80c | 80 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 81 | } |
| tony63 | 1:48651f86a80c | 82 | while (io == 1) { |
| tony63 | 1:48651f86a80c | 83 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 84 | } |
| tony63 | 1:48651f86a80c | 85 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 86 | while (io == 0) { |
| tony63 | 1:48651f86a80c | 87 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 88 | } |
| tony63 | 1:48651f86a80c | 89 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 90 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 91 | } |
| tony63 | 1:48651f86a80c | 92 | |
| tony63 | 1:48651f86a80c | 93 | int PulseInOut::read_us() { |
| tony63 | 1:48651f86a80c | 94 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 95 | io.input(); |
| tony63 | 1:48651f86a80c | 96 | startval = io; |
| tony63 | 1:48651f86a80c | 97 | while (io == startval) { |
| tony63 | 1:48651f86a80c | 98 | } |
| tony63 | 1:48651f86a80c | 99 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 100 | while (io != startval) { |
| tony63 | 1:48651f86a80c | 101 | } |
| tony63 | 1:48651f86a80c | 102 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 103 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 104 | } |
| tony63 | 1:48651f86a80c | 105 | |
| tony63 | 1:48651f86a80c | 106 | int PulseInOut::read_us(int timeout) { |
| tony63 | 1:48651f86a80c | 107 | runtime.reset(); |
| tony63 | 1:48651f86a80c | 108 | runtime.start(); |
| tony63 | 1:48651f86a80c | 109 | pulsetime.reset(); |
| tony63 | 1:48651f86a80c | 110 | io.input(); |
| tony63 | 1:48651f86a80c | 111 | startval = io; |
| tony63 | 1:48651f86a80c | 112 | while (io == startval) { |
| tony63 | 1:48651f86a80c | 113 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 114 | } |
| tony63 | 1:48651f86a80c | 115 | pulsetime.start(); |
| tony63 | 1:48651f86a80c | 116 | while (io != startval) { |
| tony63 | 1:48651f86a80c | 117 | if (runtime.read_us() > timeout) return -1; |
| tony63 | 1:48651f86a80c | 118 | } |
| tony63 | 1:48651f86a80c | 119 | pulsetime.stop(); |
| tony63 | 1:48651f86a80c | 120 | return pulsetime.read_us(); |
| tony63 | 1:48651f86a80c | 121 | } |
