1 /*
2  * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
3  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
4  * Copyright (c) 2015 Intel Corporation.
5  *
6  * Credits to Adafruit.
7  * Based on Adafruit BMP085 library.
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
18  * included 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
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 #pragma once
29 
30 #include <string>
31 #include <math.h>
32 #include <mraa/pwm.hpp>
33 #include <mraa/aio.hpp>
34 #include <mraa/gpio.hpp>
35 #include <pthread.h>
36 
37 #define HIGH               1
38 #define LOW                0
39 
40 #define TRUE               HIGH
41 #define FALSE              LOW
42 
43 /**
44  * @brief Pulsensor Pulse Sensor library
45  * @defgroup pulsensor libupm-pulsensor
46  * @ingroup seeed analog medical
47  */
48 /**
49  * @library pulsensor
50  * @sensor pulsensor
51  * @comname Pulse Sensor
52  * @type medical
53  * @man seeed
54  * @web http://www.adafruit.com/products/1093
55  * @con analog
56  *
57  * @brief C++ API for the 3-Wire Pulse Sensor
58  *
59  * This is a library for a 3-wire pulse sensor sold by several manufacturers.
60  * Usually, you can identify the sensor based on the round breakout and the
61  * distinctive heart symbol.
62  *
63  * @image html pulsensor.jpg
64  * @snippet pulsensor.cxx Interesting
65  */
66 
67 /*!
68  * @struct clbk_data
69  * @brief callback data
70  */
71 struct clbk_data {
72     int is_heart_beat; /**< heartbeat check */
73 };
74 
75 
76 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
77 #include "Callback.h"
78 #else
79 typedef void (* callback_handler) (clbk_data);
80 #endif
81 
82 /*!
83  * @class Pulsensor
84  * @brief The context for the heartbeat pulse sensor
85  */
86 class Pulsensor {
87 
88 public:
89 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
90     Pulsensor(Callback *callback);
91 #else
92     Pulsensor(callback_handler handler);
93 #endif
94     void start_sampler();
95     void stop_sampler();
96 
97 private:
98     static void      *do_sample(void *arg);
99     pthread_t        sample_thread; /**< Thread for the code sample */
100     uint32_t         sample_counter; /**< Counter for the code sample */
101     uint32_t         last_beat_time; /**< Last heartbeat time */
102     int              threshold; /**< Threshold */
103     int              ibi_rate[10]; /**< ibi rate */
104     int              ibi; /**< ibi */
105     int              trough; /**< Trough */
106     int              peak; /**< Peak */
107     int              bpm; /**< Bits per minute */
108     int              apmlitude; /**< Amplitude */
109     uint8_t          qs; /**< qs */
110     uint8_t          is_pulse; /**< Is pulse check */
111     uint8_t          first_beat; /**< First heartbeat */
112     uint8_t          second_beat; /**< Second heartbeat */
113     uint8_t          pin; /**< Pin */
114     uint8_t          ret; /**< Return value */
115     mraa::Aio        pin_ctx; /**< The pin context */
116 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
117     Callback *obj_callback; /**< The callback object */
118 #else
119     callback_handler callback; /**< The callback function */
120 #endif
121     volatile uint16_t ctx_counter;
122 };
123 
124