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.
Revision 7:a254dc462f4c, committed 2016-01-26
- Comitter:
- Jan Tetour
- Date:
- Tue Jan 26 13:59:23 2016 +0100
- Parent:
- 6:5beda7c318d5
- Child:
- 9:af77e61b4845
- Commit message:
- Application of coding rules.
Changed in this revision
| Distance_HC_SR04.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Distance_HC_SR04.cpp Tue Dec 22 12:00:15 2015 +0000
+++ b/Distance_HC_SR04.cpp Tue Jan 26 13:59:23 2016 +0100
@@ -1,11 +1,16 @@
#include "Distance_HC_SR04.h"
+/**
+ * Aplikována pravidla: 0, 1,
+ */
+
/** Create Distance_HC_SR04 instance. Set FSM to IDLE state.
*
*/
Distance_HC_SR04::Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us, float coeff,
uint32_t tmin_us, uint32_t tmax_us) :
- _trig(trig), _echo(echo), _tout_us(tout_us), _coeff(coeff), _tmin_us(tmin_us), _tmax_us(tmax_us) {
+ _trig(trig), _echo(echo), _tout_us(tout_us), _coeff(coeff), _tmin_us(tmin_us), _tmax_us(tmax_us)
+{
_trig = 0;
_state = IDLE;
}
@@ -18,20 +23,25 @@
* @returns
* uint32_t value of distance > 0 in case of a success, 0 in case of an error
*/
-uint32_t Distance_HC_SR04::measureTicks(void) {
+uint32_t Distance_HC_SR04::measureTicks(void)
+{
reset();
trigger();
- while (_state == STARTED)
+ while (STARTED == _state)
+ {
;
+ }
_echo.rise(NULL);
_echo.fall(NULL);
_timeout.detach();
- switch (_state) {
+ switch (_state)
+ {
case COMPLETED:
break;
+
default:
_ticks_us = 0;
break;
@@ -48,7 +58,8 @@
* @returns
* float value of distance > 0.0f in case of a success, 0.0f in case of an error
*/
-float Distance_HC_SR04::measureDistance(void) {
+float Distance_HC_SR04::measureDistance(void)
+{
return measureTicks()*_coeff;
}
@@ -57,7 +68,8 @@
* FSM set to IDLE state.
*
*/
-void Distance_HC_SR04::reset(void) {
+void Distance_HC_SR04::reset(void)
+{
_state = IDLE;
_echo.rise(NULL);
_echo.fall(NULL);
@@ -74,12 +86,15 @@
* In other case FSM sate is set to "ERROR_SIG".
*
*/
-void Distance_HC_SR04::trigger(void) {
- if (_state == IDLE && _echo == 0) {
+void Distance_HC_SR04::trigger(void)
+{
+ if (IDLE == _state && 0 == _echo)
+ {
_trig = 1;
wait_us(TRIG_PULSE_US);
_trig = 0;
- if (_echo == 0) {
+ if (0 == _echo)
+ {
_state = STARTED;
_timeout.attach_us(this, &Distance_HC_SR04::_tout, TIMEOUT_DELAY_US);
_echo.rise(this, &Distance_HC_SR04::_rising);
@@ -89,7 +104,8 @@
}
}
- if (_state == IDLE) {
+ if (IDLE == _state)
+ {
_state = ERROR_SIG;
_ticks_us = 0;
}
@@ -104,7 +120,8 @@
* @returns
* Distance_HC_SR04_state FSM state value.
*/
-Distance_HC_SR04_state Distance_HC_SR04::getState(void) {
+Distance_HC_SR04_state Distance_HC_SR04::getState(void)
+{
return _state;
}
@@ -115,7 +132,8 @@
* @returns
* uint32_t Measured duration of "echo" pulse in microseconds.
*/
-uint32_t Distance_HC_SR04::getTicks(void) {
+uint32_t Distance_HC_SR04::getTicks(void)
+{
return _ticks_us;
}
@@ -126,7 +144,8 @@
* @returns
* float Distance in milimiters calculated from measured duration of "echo" pulse.
*/
-float Distance_HC_SR04::getDistance(void) {
+float Distance_HC_SR04::getDistance(void)
+{
return _ticks_us*_coeff;
}
@@ -135,7 +154,8 @@
* @returns
* float Value of the coefficient used to multiply time in microseconds to get distance in mm.
*/
-float Distance_HC_SR04::getCoeff(void) {
+float Distance_HC_SR04::getCoeff(void)
+{
return _coeff;
}
@@ -145,7 +165,8 @@
* @returns
* void -
*/
-void Distance_HC_SR04::setCoeff(float coeff) {
+void Distance_HC_SR04::setCoeff(float coeff)
+{
_coeff = coeff;
}
@@ -155,8 +176,9 @@
* @returns
* void -
*/
-void Distance_HC_SR04::_tout(void) {
- if (_state == STARTED)
+void Distance_HC_SR04::_tout(void)
+{
+ if (STARTED == _state)
_state = TIMEOUT;
}
@@ -166,8 +188,10 @@
* @returns
* void -
*/
-void Distance_HC_SR04::_rising(void) {
- if (_state == STARTED) {
+void Distance_HC_SR04::_rising(void)
+{
+ if (STARTED == _state)
+ {
_timer.start();
}
}
@@ -178,18 +202,23 @@
* @returns
* void -
*/
-void Distance_HC_SR04::_falling(void) {
- if (_state == STARTED) {
+void Distance_HC_SR04::_falling(void)
+{
+ if (STARTED == _state)
+ {
_timer.stop();
_ticks_us = _timer.read_us();
- if (_ticks_us < _tmin_us) {
+ if (_ticks_us < _tmin_us)
+ {
_ticks_us = 0;
_state = OUT_OF_RANGE_MIN;
- } else if (_ticks_us > _tmax_us) {
+ } else if (_ticks_us > _tmax_us)
+ {
_ticks_us = 0;
_state = OUT_OF_RANGE_MAX;
- } else {
+ } else
+ {
_state = COMPLETED;
}
}