Code required to drive the microcontroller of the NMF Atlantis

Dependencies:   mbed WakeUp DHT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pulse.cpp Source File

Pulse.cpp

00001 /* Copyright (c) 2012 Nick Ryder, University of Oxford
00002  * nick.ryder@physics.ox.ac.uk
00003  *
00004  *  MIT License
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00007  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00008  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00009  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in all copies or 
00013  * substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00016  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00017  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00018  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020  */
00021  
00022  
00023 #include "Pulse.h"
00024 
00025 PulseInOut::PulseInOut(PinName pin):
00026         startval(0), pulsetime(), runtime(), io(pin)    {
00027 }
00028 
00029 
00030 PulseInOut::~PulseInOut() {
00031 }
00032 
00033 void PulseInOut::write(int val) {
00034     io.output();
00035     io = val;
00036 }
00037 
00038 void PulseInOut::write_us(int val, int time)   {
00039     io.output();
00040     io = val;
00041     wait_us(time);
00042     io = !val;
00043 }
00044 
00045 int PulseInOut::read_high_us()  {
00046     pulsetime.reset();
00047     io.input();
00048     while (io == 1) {
00049     }
00050     while (io == 0) {
00051     }
00052     pulsetime.start();
00053     while (io == 1) {
00054     }
00055     pulsetime.stop();
00056     return pulsetime.read_us();
00057 }
00058 
00059 int PulseInOut::read_high_us(int timeout)  {
00060     runtime.reset();
00061     runtime.start();
00062     pulsetime.reset();
00063     io.input();
00064     while (io == 1) {
00065         if (runtime.read_us() > timeout)   return -1;
00066     }
00067     while (io == 0) {
00068         if (runtime.read_us() > timeout)   return -1;
00069     }
00070     pulsetime.start();
00071     while (io == 1) {
00072         if (runtime.read_us() > timeout)   return -1;
00073     }
00074     pulsetime.stop();
00075     return pulsetime.read_us();
00076 }
00077 
00078 int PulseInOut::read_low_us()   {
00079     pulsetime.reset();
00080     io.input();
00081     while (io == 0) {
00082     }
00083     while (io == 1) {
00084     }
00085     pulsetime.start();
00086     while (io == 0) {
00087     }
00088     pulsetime.stop();
00089     return pulsetime.read_us();
00090 }
00091 
00092 int PulseInOut::read_low_us(int timeout)   {
00093     runtime.reset();
00094     runtime.start();
00095     pulsetime.reset();
00096     io.input();
00097     while (io == 0) {
00098         if (runtime.read_us() > timeout)   return -1;
00099     }
00100     while (io == 1) {
00101         if (runtime.read_us() > timeout)   return -1;
00102     }
00103     pulsetime.start();
00104     while (io == 0) {
00105         if (runtime.read_us() > timeout)   return -1;
00106     }
00107     pulsetime.stop();
00108     return pulsetime.read_us();
00109 }
00110 
00111 int PulseInOut::read_us()  {
00112     pulsetime.reset();
00113     io.input();
00114     startval = io;
00115     while (io == startval)   {
00116     }
00117     pulsetime.start();
00118     while (io != startval)  {
00119     }
00120     pulsetime.stop();
00121     return pulsetime.read_us();
00122 }
00123 
00124 int PulseInOut::read_us(int timeout)   {
00125     runtime.reset();
00126     runtime.start();
00127     pulsetime.reset();
00128     io.input();
00129     startval = io;
00130     while (io == startval)  {
00131         if (runtime.read_us() > timeout)   return -1;
00132     }
00133     pulsetime.start();
00134     while (io != startval)   {
00135         if (runtime.read_us() > timeout)   return -1;
00136     }
00137     pulsetime.stop();
00138     return pulsetime.read_us();
00139 }