Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: MicrobitUltrasound.cpp
- Revision:
- 2:826744569821
- Parent:
- 1:17a477201275
- Child:
- 3:8a5d2e5f64fb
--- a/MicrobitUltrasound.cpp Sat Aug 20 23:09:04 2016 +0200
+++ b/MicrobitUltrasound.cpp Mon Aug 22 22:13:17 2016 +0200
@@ -28,5 +28,92 @@
*
* Commonly represents an I/O pin on the edge connector.
*/
+#include "MicrobitUltrasound.h"
#include "MicroBitPin.h"
+#include "MicroBitConfig.h"
+#include "MicroBitSystemTimer.h"
+#include "MicroBitFiber.h"
+/**
+ * Constructor.
+ * Create new MicrobitUltrasound that reports distance to closest object
+ *
+ * @param _trigPinName Name of the MicroBitPin to be used as trigger.
+ *
+ * @param _echoPinName Name of the MicroBitPin to be used as response from sensor.
+ *
+ * @code
+ * MicroBitPin P1_PING = MicroBitPin(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_DIGITAL);
+ * MicroBitPin P2_ECHO = MicroBitPin(MICROBIT_ID_IO_P2, MICROBIT_PIN_P2, PIN_CAPABILITY_DIGITAL);
+ * MicrobitUltrasound uSoundSensor(P1_PING, P2_ECHO);
+ * @endcode
+ */
+MicrobitUltrasound(MicroBitPin& pingPin, MicroBitPin& echoPin, uint16_t id):
+ ping(&pingPin) : echo(&echoPin)
+{
+ this->id = id;
+
+
+/**
+ * Constructor.
+ * Create new MicrobitUltrasound that measures distance to nearest object.
+ *
+ * @param id the unique EventModel id of this component. Defaults to MICROBIT_ID_ULTRASOUND.
+ *
+ *
+ * @code
+ * MicrobitUltrasound ultrasound
+ * @endcode
+ */
+MicrobitUltrasound(uint16_t id = MICROBIT_ID_ULTRASOUND);
+
+/**
+ * Set the sample rate at which the micro:bit triggers sensor (in ms).
+ *
+ * The default sample period is 1 second.
+ *
+ * @param period the requested time between pings, in milliseconds.
+ *
+ * @note the micro:bit sends pings in the background.
+ */
+void setPeriod(int period_ms);
+
+/**
+ * Get the sample rate at which the micro:bit triggers sensor (in ms).
+ *
+ * @return period the requested time between pings, in milliseconds.
+ */
+void getPeriod();
+
+/**
+ * Get the pulse's duration from sensor, last updated sample
+ *
+ * @return the latest updated pulse duration.
+ *
+ */
+int getPulseDuration();
+
+/**
+ * Send ping to start measurement in sensor
+ *
+ * This call also will add the class to fiber components to receive
+ * one callback only.
+ *
+ * @return MICROBIT_OK on success.
+ */
+int ping(void);
+
+/**
+ * Start periodic ping to sensor. This call also will add the class
+ * to fiber components for periodic callbacks.
+ *
+ * @return MICROBIT_OK on success.
+ */
+int start(void);
+
+/**
+ * Stop periodic ping to sensor.
+ *
+ * @return MICROBIT_OK on success.
+ */
+int stop(void);