Easyuino  1.2.0
DistanceMeterNonBlock.h
1 /*
2 MIT License
3 
4 Copyright (c) 2017 André Pires
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 /*
25 DistanceMeterNonBlock.h
26 */
27 #ifndef _EASYUINO_DISTANCE_METER_NON_BLOCK_h
28 #define _EASYUINO_DISTANCE_METER_NON_BLOCK_h
29 
30 #include "Utilities.h"
31 #include "DistanceMeter.h"
32 
35 #define MAXIMUM_NUMBER_OF_DM_NON_BLOCK 2
36 
37 namespace Easyuino {
38 
48 
49  private:
51  static DistanceMeterNonBlock* INSTANCES[MAXIMUM_NUMBER_OF_DM_NON_BLOCK];
52 
54  bool _blockingMeasure;
55 
58  bool _isDirtyDistance;
59 
62  volatile bool _echoSent;
63 
65  volatile unsigned long _lastTimeTrigger;
67  volatile unsigned long _lastTimeEcho;
68 
69  public:
74  DistanceMeterNonBlock(IN uint8_t triggerPin, IN uint8_t echoPin);
75 
79  DistanceMeterNonBlock(IN uint8_t triggerEchoPin);
80 
83 
84  bool begin();
85 
86  void end();
87 
88  float getDistanceCentimeters();
89 
90  void updateDistance();
91 
97  virtual void updateDistanceNonBlock();
98 
99  protected:
100 
103 
108 
113  float calculateDistance(IN float soundSpeedCmPerSec);
114 
115  private:
116  #pragma region Interrupt Management/Handling Methods
117 
118  void interruptHandler(IN unsigned long interruptCallTimeMicros);
119 
121  static void assignInstance(IN DistanceMeterNonBlock* dm);
122 
124  static void deleteInstance(IN DistanceMeterNonBlock* dm);
125 
127  static void attachInterruptToInstance(IN DistanceMeterNonBlock* dm);
128 
130  static void interruptCaller0();
131  static void interruptCaller1();
132 
133  #pragma endregion
134 
135  };
136 
137 };
138 
139 #endif
void executeUpdateDistanceNonBlock()
Execute a non-block distance measurement.
Used to represent the at least supported Ultrasonic Module models enum UltrasonicModule : uint8_t { H...
Definition: DistanceMeter.h:59
virtual void updateDistanceNonBlock()
Updates the distance of the Ultrasonic Module to the objects in a non-blocking way.
float calculateDistance(IN float soundSpeedCmPerSec)
Calculates the distance to the object based on the sound speed in air using the last distance value m...
void updateDistance()
Updates the distance of the Ultrasonic Module to the objects in a blocking way.
DistanceMeterNonBlock(IN uint8_t triggerPin, IN uint8_t echoPin)
Constructor.
Definition: Button.h:38
void end()
Used to stop the device API.
DistanceMeterNonBlock offers an API to interact with an Ultrasonic Module to measure distance in a no...
Definition: DistanceMeterNonBlock.h:47
bool begin()
Used to put the device ready to receive requests.
float getDistanceCentimeters()
Gets the last value that the API measured using the Ultrasonic Module.
bool isUpdateDistanceNonBlockTimeout()
Verify if there is a non-block measuremenet on going that has already timeouted.