Code required to drive the microcontroller of the NMF Atlantis

Dependencies:   mbed WakeUp DHT

Committer:
nmf_atlantis
Date:
Mon Jan 20 15:47:07 2020 +0000
Revision:
0:dbaef09f6d82
NMF Atlantis final version

Who changed what in which revision?

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