comparison include/PulseIn.h @ 193:3c3d14654b7f

Added PulseIn.
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 04 Feb 2016 18:02:52 +0000
parents
children e4392164b458
comparison
equal deleted inserted replaced
192:1402f22fc99a 193:3c3d14654b7f
1 /*
2 * PulseIn.h
3 *
4 * Created on: 4 Feb 2016
5 * Author: giulio
6 */
7
8 #ifndef PULSEIN_H_
9 #define PULSEIN_H_
10
11 #include <BeagleRT.h>
12 #include <vector>
13 class PulseIn {
14 private:
15 std::vector<int> _array;
16 int _pulseOnState;
17 int _digitalInput;
18 bool _pulseIsOn;
19 uint64_t _pulseStart;
20 uint64_t _lastContext;
21 public:
22 PulseIn(){
23 _digitalInput = -1;
24 };
25
26 PulseIn(BeagleRTContext* context, unsigned int digitalInput, int direction=1){
27 init(context, digitalInput, direction);
28 };
29 /**
30 * Initializes the PulseIn object. Also takes care of initializing the digital pin as input.
31 *
32 * If the object has been created with the default constructor, the user will
33 * need to call init() before calling check() or hasPulsed().
34 * @param digitalInput the digital input where to activate a pulse detector
35 * @param direction the direction of the pulse,
36 * can be 1 to detect positive pulses, e.g.:( 0 0 0 0 1 1 0 0 0 0 0)
37 * or -1 to detect negative pulses, e.g.: ( 1 1 1 1 0 0 1 1 1 1)
38 */
39 void init(BeagleRTContext* context, unsigned int digitalInput, int direction=1);
40
41 /**
42 * Detects pulses.
43 *
44 * The user does not need to call this method as long as they call hasPulsed() at least once per context.
45 * The rationale why we check() for pulses in a different method
46 * than hasPulsed() is because user might not query for hasPulsed() every sample,
47 * so we are safe so long as they call hasPulsed() or check() at least once per buffer.
48 * Also, results are cached (i.e.: we do not check() for pulses twice for the same context.
49 * context->audioSampleCount is used as an identifier.
50 */
51 void check(BeagleRTContext* context);
52
53 /**
54 * Looks for the end of a pulse.
55 *
56 * @param context the current BeagleRTContext
57 * @param frame the frame at which to check if a pulse was detected.
58 * @return the length of the pulse if a pulse ending was detected at sample n, zero otherwise.
59 */
60 int hasPulsed(BeagleRTContext* context, int frame){//let's leave this in PulseIn.h to allow the compiler to optimize out the call.
61 if(_lastContext != context->audioSampleCount){ // check for pulses in the whole context and cache the result
62 check(context);
63 }
64 return _array[frame];
65 }
66 virtual ~PulseIn();
67 };
68
69 #endif /* PULSEIN_H_ */