Nagano kosen robocon

Dependencies:   mbed QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pulse.cpp Source File

Pulse.cpp

00001 #include "Pulse.h"
00002 
00003 PulseInOut::PulseInOut(PinName pin):
00004         startval(0), pulsetime(), runtime(), io(pin)    {
00005 }
00006 
00007 
00008 PulseInOut::~PulseInOut() {
00009 }
00010 
00011 void PulseInOut::write(int val) {
00012     io.output();
00013     io = val;
00014 }
00015 
00016 void PulseInOut::write_us(int val, int time)   {
00017     io.output();
00018     io = val;
00019     wait_us(time);
00020     io = !val;
00021 }
00022 
00023 int PulseInOut::read_high_us()  {
00024     pulsetime.reset();
00025     io.input();
00026     while (io == 1) {
00027     }
00028     while (io == 0) {
00029     }
00030     pulsetime.start();
00031     while (io == 1) {
00032     }
00033     pulsetime.stop();
00034     return pulsetime.read_us();
00035 }
00036 
00037 int PulseInOut::read_high_us(int timeout)  {
00038     runtime.reset();
00039     runtime.start();
00040     pulsetime.reset();
00041     io.input();
00042     while (io == 1) {
00043         if (runtime.read_us() > timeout)   return -1;
00044     }
00045     while (io == 0) {
00046         if (runtime.read_us() > timeout)   return -1;
00047     }
00048     pulsetime.start();
00049     while (io == 1) {
00050         if (runtime.read_us() > timeout)   return -1;
00051     }
00052     pulsetime.stop();
00053     return pulsetime.read_us();
00054 }
00055 
00056 int PulseInOut::read_low_us()   {
00057     pulsetime.reset();
00058     io.input();
00059     while (io == 0) {
00060     }
00061     while (io == 1) {
00062     }
00063     pulsetime.start();
00064     while (io == 0) {
00065     }
00066     pulsetime.stop();
00067     return pulsetime.read_us();
00068 }
00069 
00070 int PulseInOut::read_low_us(int timeout)   {
00071     runtime.reset();
00072     runtime.start();
00073     pulsetime.reset();
00074     io.input();
00075     while (io == 0) {
00076         if (runtime.read_us() > timeout)   return -1;
00077     }
00078     while (io == 1) {
00079         if (runtime.read_us() > timeout)   return -1;
00080     }
00081     pulsetime.start();
00082     while (io == 0) {
00083         if (runtime.read_us() > timeout)   return -1;
00084     }
00085     pulsetime.stop();
00086     return pulsetime.read_us();
00087 }
00088 
00089 int PulseInOut::read_us()  {
00090     pulsetime.reset();
00091     io.input();
00092     startval = io;
00093     while (io == startval)   {
00094     }
00095     pulsetime.start();
00096     while (io != startval)  {
00097     }
00098     pulsetime.stop();
00099     return pulsetime.read_us();
00100 }
00101 
00102 int PulseInOut::read_us(int timeout)   {
00103     runtime.reset();
00104     runtime.start();
00105     pulsetime.reset();
00106     io.input();
00107     startval = io;
00108     while (io == startval)  {
00109         if (runtime.read_us() > timeout)   return -1;
00110     }
00111     pulsetime.start();
00112     while (io != startval)   {
00113         if (runtime.read_us() > timeout)   return -1;
00114     }
00115     pulsetime.stop();
00116     return pulsetime.read_us();
00117 }