Interface class for the Max Botix ultrasonic range finder model 1210. It includes input methods for PWM, Analog, and Serial. A PwmIn class was created to allow the PWM input to be read. Now includes automatic range update via interrupts.

Dependencies:   mbed

Revision:
4:a615b75d4126
Parent:
3:05183e50a923
--- a/MB1210.cpp	Thu Aug 26 18:25:19 2010 +0000
+++ b/MB1210.cpp	Sat Aug 28 07:59:29 2010 +0000
@@ -13,6 +13,7 @@
         SerialInput = new Serial(NC, rx);
         SerialInput->baud(9600);
         SerialInput->format(8, Serial::None, 1);
+        SerialInput->attach(NULL, Serial::RxIrq);
         OperatingMode = 0x02;
     }
     if (an != NC)
@@ -66,14 +67,34 @@
 
 void MB1210::Mode(char Selection)
 {
+    if (SerialInput)
+    {
+        if (Selection & 0x08)
+        {
+            SerialInput->attach(this, &MB1210::Interrupt, Serial::RxIrq);
+        }
+        else
+        {
+            SerialInput->attach(NULL, Serial::RxIrq);
+        }
+            //attach or detach the interrupt function
+    }
+        //interrupts can only be generated if rx pin is connected
     if (SerialOutput)
     {
         SerialOutput->write(Selection & 0x04);
     }
+        //synchronous modes can only be set if tx pin is connected
     OperatingMode = Selection & 0x03;
 }
     //change the operating mode; SerialOutput controls synchronicity
 
+void MB1210::AttachInterruptBuffer(float* Buffer)
+{
+    InterruptBuffer = Buffer;
+}
+    //the user changes the pointer to their own storage area so they can use the interrupt
+
 void MB1210::RequestSyncRead()
 {
     if (SerialOutput)
@@ -120,12 +141,13 @@
             if (SerialInput)
             {
                 unsigned char i = 0;
-                while (SerialInput->readable() && !SerialInput->scanf("R%3d", &Range) && (i < 32))
+                while (SerialInput->readable() && !SerialInput->scanf("R%3f", &Range) && (i < 32))
                 {
                     SerialInput->getc();
                     i++;
                 }
-                return (float)Range * UnitFactor;
+                    //find R and parse the range out
+                return Range * UnitFactor;
             }
             else
             {
@@ -137,20 +159,15 @@
 }
     //OperatingMode switches to desired output method;
     //the result is scaled according to voltage, the speed of sound, and desired unit
-void MB1210::Read(char* Buffer)
+
+void MB1210::Interrupt()
 {
-    if (SerialInput)
-    {
-        unsigned char i = 0;
-        while (SerialInput->readable() && !SerialInput->scanf("R%3s", Buffer) && (i < 32))
-        {
-            SerialInput->getc();
-            i++;
-        }
-    }
+    *InterruptBuffer = Read();
+    DiscardSerialBuffer();
 }
-    //this overload is for serial only, regardless of selected mode;
-    //reads 3 ASCII character result into the given buffer
+    //this is called whenever an interrupt mode is
+    //set and a serial rx interrupt is generated;
+    //it writes to the user's data storage area
 
 MB1210::operator float()
 {