-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sonar.h
73 lines (55 loc) · 1.38 KB
/
Sonar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Name: Sonar.h
Author: Rithvik Nishad (github.com/rithviknishad)
License: GNU General Public License v3.0
Description:
Sonar library allows users to handle multiple sonar sensors with minimal
code and also supports up to 65,536 sonar sensors in just two IO pins.
*/
#ifndef __ULTRASONIC_H
#define __ULTRASONIC_H
#include "Arduino.h"
#define units_cm 28
#define units_inch 71
#ifndef DEFAULT_ULTRASONIC_REPORTING_UNIT
#define DEFAULT_ULTRASONIC_REPORTING_UNIT units_cm
#endif
class Sonar
{
protected:
volatile uint8_t* Trigger;
const uint8_t TriggerBit, Echo;
public:
float Offset, MaxTimeout;
public:
Sonar(int trigPin, int echoPin, float offset = 0, float MaxDistance = -1);
protected:
void performTrigger() const;
float getDistance() const;
public:
float Scan() const;
operator float() const { return Scan(); }
operator int() const { return Scan(); }
};
template<size_t S>
class SonarArray : protected Sonar
{
public:
float Distances[S];
SonarArray(uint8_t trigPin, uint8_t echoPin);
float Scan();
};
template<size_t S>
inline SonarArray<S>::SonarArray(uint8_t trigPin, uint8_t echoPin)
: Sonar(trigPin, echoPin, 0, -1)
{
}
template<size_t S>
inline float SonarArray<S>::Scan()
{
performTrigger();
for (size_t i = 0; i < S; ++i)
Distances[i] = getDistance();
return Distances[0];
}
#endif