Driver for HC-SR04 connected to a Microbit
Diff: MicrobitUltrasound.cpp
- Revision:
- 8:12e6c5d3dc5f
- Parent:
- 7:13ba98b16e74
- Child:
- 9:d153bcba081c
diff -r 13ba98b16e74 -r 12e6c5d3dc5f MicrobitUltrasound.cpp --- a/MicrobitUltrasound.cpp Mon Aug 29 20:32:40 2016 +0200 +++ b/MicrobitUltrasound.cpp Mon Aug 29 22:03:36 2016 +0200 @@ -49,12 +49,13 @@ */ MicrobitUltrasound(PinName triggerPinName, PinName echoPinName, uint16_t id) { - this->id = id; - this->status = 0; - this->sampleTime = 0; - this->echoDuration = 0; - this->triggerPeriod = MICROBIT_ULTRASOUND_PERIOD_DEFAULT_MS; - this->triggerDuration = MICROBIT_ULTRASOUND_TRIGGER_DURATION_DEFAULT_US; + this->id = id; + this->status = 0; + this->sampleTime = 0; + this->echoDuration = 0; + this->triggerPeriod_ms = MICROBIT_ULTRASOUND_PERIOD_DEFAULT_MS; + this->triggerDuration_us = MICROBIT_ULTRASOUND_TRIGGER_DURATION_DEFAULT_US; + this->triggerActiveValue = TRIGGER_ACTIVE_VALUE; trigger = new DigitalOut(triggerPinName); echo = new TimedInterruptIn(echoPinName); @@ -98,7 +99,7 @@ MicroBitEvent(id, MICROBIT_ULTRASOUND_EVT_ECHO_RISE); if(status & ULTRASOUND_STATUS_EVENTON_EDGE) - MicroBitEvent(id, MICROBIT_PIN_EVT_EDGE); + MicroBitEvent(id, MICROBIT_ULTRASOUND_EVT_ECHO_EDGE); } /** @@ -113,7 +114,7 @@ MicroBitEvent(id, MICROBIT_ULTRASOUND_EVT_ECHO_FALL); if(status & ULTRASOUND_STATUS_EVENTON_EDGE) - MicroBitEvent(id, MICROBIT_PIN_EVT_EDGE); + MicroBitEvent(id, MICROBIT_ULTRASOUND_EVT_ECHO_EDGE); } /** @@ -127,7 +128,7 @@ */ void MicrobitUltrasound::setPeriod(int period_ms) { - this->triggerPeriod = period_ms; + this->triggerPeriod_ms = period_ms; } /** @@ -137,7 +138,7 @@ */ void MicrobitUltrasound::getPeriod() { - return triggerPeriod; + return triggerPeriod_ms; } /** @@ -172,7 +173,7 @@ result = trigger.write(actVal); if (result =! 0) return result; - wait_us(MICROBIT_ULTRASOUND_TRIGGER_DURATION_DEFAULT_US); + wait_us(triggerDuration_us); result = trigger.write(pasVal); return result; } @@ -237,7 +238,7 @@ // Schedule our next trigger. - sampleTime = system_timer_current_time() + triggerPeriod; + sampleTime = system_timer_current_time() + triggerPeriod_ms; return MICROBIT_OK } @@ -270,6 +271,10 @@ { switch(eventType) { + case MICROBIT_ULTRASOUND_EVT_ECHO_PULSE_HI : status |= ULTRASOUND_STATUS_EVENTON_PULSE; + break; + case MICROBIT_ULTRASOUND_EVT_ECHO_PULSE_LO : status |= ULTRASOUND_STATUS_EVENTON_PULSE; + break; case MICROBIT_ULTRASOUND_EVT_TRIGGER : status |= ULTRASOUND_STATUS_EVENTON_TRIGGER; break; case MICROBIT_ULTRASOUND_EVT_ECHO_RISE : status |= ULTRASOUND_STATUS_EVENTON_RISE; @@ -278,8 +283,6 @@ break; case MICROBIT_ULTRASOUND_EVT_ECHO_EDGE : status |= ULTRASOUND_STATUS_EVENTON_EDGE; break; - case MICROBIT_ULTRASOUND_EVT_ECHO_PULSE : status |= ULTRASOUND_STATUS_EVENTON_PULSE; - break; default : return MICROBIT_INVALID_PARAMETER; break; }