Bare Metal Programming Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sr04.h
1 //***************************************************************************
2 //
3 // file : bmptk/chips/sr04.h
4 //
5 // LICENSE (MIT expat license, copy of license.txt)
6 //
7 // Copyright (c) 2013 Wouter van Ooijen (wouter@voti.nl)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE..
27 //
28 //***************************************************************************
29 
30 #pragma once
31 #ifndef BMPTK_CHIPS_H
32 #define BMPTK_CHIPS_H
33 
34  namespace bmptk {
35 
37  //
64  //
65  template< typename trigger, typename pulse >
66  class sr04 {
67  public:
68 
70  static void init(){
71  trigger::init();
72  trigger::set( 0 );
73  pulse::init();
74  }
75 
77  //
85  bmptk::time error_value = -1 * bmptk::us
86  ){
87 
88  // when the echo line is not silent something is wrong
89  if( pulse::get() ){
90  return error_value;
91  }
92 
93  // trigger pulse to the sr04
94  trigger::set( 1 );
95  bmptk::wait( 10 * bmptk::us );
96  trigger::set( 0 );
97 
98  // wait for start of echo pulse from sr04
100  while( !pulse::get() ){
101  if( ( bmptk::current_time() - t1 ) > ( 1 * bmptk::ms ) ){
102  return error_value;
103  }
104  }
105 
106  // wait for end of echo pulse from sr04
107  t1 = bmptk::current_time();
108  while( pulse::get() ){
109  if( ( bmptk::current_time() - t1 ) > ( 20 * bmptk::ms ) ){
110  return error_value;
111  }
112  }
113 
114  // get end time
116 
117  // debug
118  // std::cout << t2 - t1 << " " << bmptk::us << "\n" ;
119 
120  // return length of the pulse
121  return t2 - t1;
122  }
123 
125  //
137  static int distance_mm_get(
138  int error_value = -1,
139  int speed_of_sound = 3430
140  ){
142  if( t == ( -1 * bmptk::us )){
143  return error_value;
144  }
145 
146  // calculate the distance in mm
147  return ( t * speed_of_sound ) / ( 2 * 10000 * bmptk::us );
148  }
149 
150  };
151 
152 
153 
154  }; // namespace bmptk;
155 
156 #endif // #ifndef BMPTK_CHIPS_H