ultimaact

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 13:05:02 2021 +0000
Revision:
14:21cb6b2ac16b
Parent:
0:8a1a447db446
eee

Who changed what in which revision?

UserRevisionLine numberNew contents of line
javiervicente 0:8a1a447db446 1 /* Copyright (c) 2013 Prabhu Desai
javiervicente 0:8a1a447db446 2 * pdtechworld@gmail.com
javiervicente 0:8a1a447db446 3 *
javiervicente 0:8a1a447db446 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
javiervicente 0:8a1a447db446 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
javiervicente 0:8a1a447db446 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
javiervicente 0:8a1a447db446 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
javiervicente 0:8a1a447db446 8 * furnished to do so, subject to the following conditions:
javiervicente 0:8a1a447db446 9 *
javiervicente 0:8a1a447db446 10 * The above copyright notice and this permission notice shall be included in all copies or
javiervicente 0:8a1a447db446 11 * substantial portions of the Software.
javiervicente 0:8a1a447db446 12 *
javiervicente 0:8a1a447db446 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
javiervicente 0:8a1a447db446 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
javiervicente 0:8a1a447db446 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
javiervicente 0:8a1a447db446 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
javiervicente 0:8a1a447db446 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
javiervicente 0:8a1a447db446 18 */
javiervicente 0:8a1a447db446 19
javiervicente 0:8a1a447db446 20
javiervicente 0:8a1a447db446 21 #include "hcsr04.h"
javiervicente 0:8a1a447db446 22
javiervicente 0:8a1a447db446 23
javiervicente 0:8a1a447db446 24 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
javiervicente 0:8a1a447db446 25 trigger(TrigPin), echo(EchoPin)
javiervicente 0:8a1a447db446 26 {
javiervicente 0:8a1a447db446 27 pulsetime.stop();
javiervicente 0:8a1a447db446 28 pulsetime.reset();
javiervicente 0:8a1a447db446 29 echo.rise(this,&HCSR04::isr_rise);
javiervicente 0:8a1a447db446 30 echo.fall(this,&HCSR04::isr_fall);
javiervicente 0:8a1a447db446 31 trigger=0;
javiervicente 0:8a1a447db446 32 }
javiervicente 0:8a1a447db446 33
javiervicente 0:8a1a447db446 34 HCSR04::~HCSR04()
javiervicente 0:8a1a447db446 35 {
javiervicente 0:8a1a447db446 36 }
javiervicente 0:8a1a447db446 37
javiervicente 0:8a1a447db446 38 void HCSR04::isr_rise(void)
javiervicente 0:8a1a447db446 39 {
javiervicente 0:8a1a447db446 40 pulsetime.start();
javiervicente 0:8a1a447db446 41 }
javiervicente 0:8a1a447db446 42 void HCSR04::start(void)
javiervicente 0:8a1a447db446 43 {
javiervicente 0:8a1a447db446 44 trigger=1;
javiervicente 0:8a1a447db446 45 wait_us(10);
javiervicente 0:8a1a447db446 46 trigger=0;
javiervicente 0:8a1a447db446 47 }
javiervicente 0:8a1a447db446 48
javiervicente 0:8a1a447db446 49 void HCSR04::isr_fall(void)
javiervicente 0:8a1a447db446 50 {
javiervicente 0:8a1a447db446 51 pulsetime.stop();
javiervicente 0:8a1a447db446 52 pulsedur = pulsetime.read_us();
javiervicente 0:8a1a447db446 53 distance= (pulsedur*343)/20000;
javiervicente 0:8a1a447db446 54 pulsetime.reset();
javiervicente 0:8a1a447db446 55 }
javiervicente 0:8a1a447db446 56
javiervicente 0:8a1a447db446 57 void HCSR04::rise (void (*fptr)(void))
javiervicente 0:8a1a447db446 58 {
javiervicente 0:8a1a447db446 59 echo.rise(fptr);
javiervicente 0:8a1a447db446 60 }
javiervicente 0:8a1a447db446 61 void HCSR04::fall (void (*fptr)(void))
javiervicente 0:8a1a447db446 62 {
javiervicente 0:8a1a447db446 63 echo.fall(fptr);
javiervicente 0:8a1a447db446 64 }
javiervicente 0:8a1a447db446 65
javiervicente 0:8a1a447db446 66 unsigned int HCSR04::get_dist_cm()
javiervicente 0:8a1a447db446 67 {
javiervicente 0:8a1a447db446 68 return distance;
javiervicente 0:8a1a447db446 69 }
javiervicente 0:8a1a447db446 70 unsigned int HCSR04::get_pulse_us()
javiervicente 0:8a1a447db446 71 {
javiervicente 0:8a1a447db446 72 return pulsedur;
javiervicente 0:8a1a447db446 73 }
javiervicente 0:8a1a447db446 74
javiervicente 0:8a1a447db446 75
javiervicente 0:8a1a447db446 76
javiervicente 0:8a1a447db446 77 /*******************************************************
javiervicente 0:8a1a447db446 78 Here is a sample code usage
javiervicente 0:8a1a447db446 79 *********************************************************
javiervicente 0:8a1a447db446 80 #include "hcsr04.h"
javiervicente 0:8a1a447db446 81 HCSR04 usensor(p25,p6);
javiervicente 0:8a1a447db446 82 int main()
javiervicente 0:8a1a447db446 83 {
javiervicente 0:8a1a447db446 84 unsigned char count=0;
javiervicente 0:8a1a447db446 85 while(1) {
javiervicente 0:8a1a447db446 86 usensor.start();
javiervicente 0:8a1a447db446 87 wait_ms(500);
javiervicente 0:8a1a447db446 88 dist=usensor.get_dist_cm();
javiervicente 0:8a1a447db446 89 lcd.cls();
javiervicente 0:8a1a447db446 90 lcd.locate(0,0);
javiervicente 0:8a1a447db446 91 lcd.printf("cm:%ld",dist );
javiervicente 0:8a1a447db446 92
javiervicente 0:8a1a447db446 93 count++;
javiervicente 0:8a1a447db446 94 lcd.locate(0,1);
javiervicente 0:8a1a447db446 95 lcd.printf("Distance =%d",count);
javiervicente 0:8a1a447db446 96
javiervicente 0:8a1a447db446 97 }
javiervicente 0:8a1a447db446 98 */