Chad Amonn / Mbed 2 deprecated ClassFile

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USRF_MB1310.cpp Source File

USRF_MB1310.cpp

00001 #include "USRF_MB1310.h"
00002 
00003 USRF_MB1310::USRF_MB1310(PinName an, PinName tx, PinName rx)
00004 {
00005     if (an != NC)
00006     {
00007         AnalogInput = new AnalogIn(an);
00008     }
00009     if (tx != NC)
00010     {
00011         SerialOutput = new DigitalOut(tx);
00012     }
00013     if (rx != NC)
00014     {
00015         SerialInput = new Serial(NC, rx);
00016     }
00017     operatingvoltage = 3.3;
00018     unitfactor = 1;
00019     scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;//range, millivolts, device, units
00020         //set defaults
00021 }
00022 
00023 void USRF_MB1310::setoperatingvoltage(float input)
00024 {
00025     operatingvoltage = input;
00026     scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
00027 }
00028 
00029 void USRF_MB1310::selectunit(int unit)
00030 {
00031     switch(unit)
00032     {
00033         case 0:
00034             unitfactor = 1;
00035             scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
00036             break;
00037         case 1:
00038             unitfactor = 1 / 100;
00039             scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
00040             break;
00041         case 2:
00042             unitfactor = 1 / 2.54;
00043             scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
00044             break;
00045         case 3:
00046             unitfactor = 1 / 2.54 / 12;
00047             scalingfactor = 3.3 / 1000 * 1024 / operatingvoltage * unitfactor;
00048             break;
00049     }
00050 }
00051 
00052 void USRF_MB1310::requestserialquery()
00053 {
00054     if (SerialOutput != 0)
00055     {
00056         SerialOutput->write(1);
00057         wait_us(50);
00058         SerialOutput->write(0);
00059     }
00060 }
00061     //hold pin high for at least 20 us to signal a serial query
00062 
00063 float USRF_MB1310::query()
00064 {
00065     if (AnalogInput != 0)
00066     {
00067         distance = AnalogInput->read() * scalingfactor;
00068     }
00069     else if ((SerialInput != 0) && (SerialInput->readable()))
00070     {
00071         SerialInput->scanf("R%f\r", &distance);
00072         distance = distance * unitfactor;
00073     }
00074     else
00075     {
00076         distance = 0;
00077     }
00078     return distance;
00079 }
00080 float USRF_MB1310::query(int mode)
00081 {
00082     switch (mode)
00083     {
00084         case 0:
00085             if (AnalogInput != 0)
00086             {
00087                 distance = AnalogInput->read() * scalingfactor;
00088             }
00089             else
00090             {
00091                 distance = 0;
00092             }
00093             break;
00094         case 1:
00095             if ((SerialInput != 0) && (SerialInput->readable()))
00096             {
00097                 SerialInput->scanf("R%f\r", &distance);
00098                 distance = distance * unitfactor;
00099             }
00100             else
00101             {
00102                 distance = 0;
00103             }
00104             break;
00105     }
00106     return distance;
00107 }
00108 
00109 USRF_MB1310& USRF_MB1310::operator =(float assignment)
00110 {
00111     unitfactor = assignment;
00112     *this = assignment;
00113     return *this;
00114 }
00115 
00116 USRF_MB1310::operator float()
00117 {
00118     if (AnalogInput != 0)
00119     {
00120         distance = AnalogInput->read() * scalingfactor;
00121     }
00122     else if ((SerialInput != 0) && (SerialInput->readable()))
00123     {
00124         SerialInput->scanf("R%f\r", &distance);
00125         distance = distance * unitfactor;
00126     }
00127     else
00128     {
00129         distance = 0;
00130     }
00131     return distance;
00132 }