Driver for HC-SR04 connected to a Microbit

Revision:
3:8a5d2e5f64fb
Parent:
2:826744569821
Child:
4:c67cff7b9733
--- a/MicrobitUltrasound.cpp	Mon Aug 22 22:13:17 2016 +0200
+++ b/MicrobitUltrasound.cpp	Tue Aug 23 22:14:21 2016 +0200
@@ -49,23 +49,17 @@
  * @endcode
  */
 MicrobitUltrasound(MicroBitPin& pingPin, MicroBitPin& echoPin, uint16_t id): 
-  ping(&pingPin) : echo(&echoPin)
+  ping(&pingPin), echo(&echoPin)
 {
-  this->id = id;
-  
+  this->id            = id;
+  this->status        = 0;
+  this->sampleTime    = 0;
+  this->echoDuration  = 0;
+  this->pingPeriod    = MICROBIT_ULTRASOUND_PERIOD_DEFAULT_MS;
+  this->pingDuration  = MICROBIT_ULTRASOUND_PING_DURATION_DEFAULT_US;
+  this->sampleTimeOut = MICROBIT_ULTRASOUND_TIMEOUT_DEFAULT_US;
+}
 
-/**
- * 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).
@@ -76,14 +70,20 @@
  *
  * @note the micro:bit sends pings in the background.
  */
-void setPeriod(int period_ms);
+void setPeriod(int period_ms)
+{
+  this->pingPeriod    = 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();
+void getPeriod()
+{
+  return pingPeriod;
+}
 
 /**
  * Get the pulse's duration from sensor, last updated sample
@@ -91,7 +91,10 @@
  * @return the latest updated pulse duration.
  *
  */
-int getPulseDuration();
+int getEchoDuration()
+{
+  return echoDuration;
+}
 
 /**
  * Send ping to start measurement in sensor
@@ -101,7 +104,17 @@
  *
  * @return MICROBIT_OK on success.
  */
-int ping(void);
+int ping(void)
+{
+  int errorCode;
+  error = trigger.setDigitalValue(TRIGGER_IDLE_VALUE);
+  wait_us(MICROBIT_ULTRASOUND_IDLE_DURATION_DEFAULT_US);
+  error = trigger.setDigitalValue(TRIGGER_ACTIVE_VALUE);
+  wait_us(MICROBIT_ULTRASOUND_PING_DURATION_DEFAULT_US);
+  error = trigger.setDigitalValue(TRIGGER_IDLE_VALUE);
+  //TODO Implement callback?
+  return errorCode;
+}
 
 /**
  * Start periodic ping to sensor. This call also will add the class