Driver for HC-SR04 connected to a Microbit

MicrobitUltrasound.cpp

Committer:
isaeldiaz@developer.mbed.org
Date:
2016-08-22
Revision:
2:826744569821
Parent:
1:17a477201275
Child:
3:8a5d2e5f64fb

File content as of revision 2:826744569821:

/*
The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/**
  * Class definition for MicroBitPin.
  *
  * 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);