ae5483240c0b45fc8cf8430c4f7bcded6a4e2346
[contrib/upm.git] / src / pulsensor / pulsensor.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Credits to Adafruit.
6  * Based on Adafruit BMP085 library.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 #pragma once
28
29 #include <string>
30 #include <math.h>
31 #include <maa/pwm.h>
32 #include <maa/aio.h>
33 #include <maa/gpio.h>
34 #include <pthread.h>
35
36 #define HIGH               1
37 #define LOW                0
38
39 #define TRUE               HIGH
40 #define FALSE              LOW
41
42 struct clbk_data {
43     int is_heart_beat;
44 };
45
46 typedef void (* callback_handler) (clbk_data);
47
48 struct pulsensor_context {
49     pthread_t        sample_thread;
50     uint32_t         sample_counter;
51     uint32_t         last_beat_time;
52     int              threshold;
53     int              ibi_rate[10];
54     int              ibi;
55     int              trough;
56     int              peak;
57     int              bpm;
58     int              apmlitude;
59     uint8_t          qs;
60     uint8_t          is_pulse;
61     uint8_t          first_beat;
62     uint8_t          second_beat;
63     uint8_t          pin;
64     uint8_t          ret;
65     maa_aio_context  pin_ctx;
66     
67     callback_handler callback;
68 };
69
70 static volatile uint16_t ctx_counter = 0;
71
72 void init_pulsensor (pulsensor_context * ctx, callback_handler handler);
73 void start_sampler (pulsensor_context * ctx);
74 void stop_sampler (pulsensor_context * ctx);
75
76 void * do_sample (void * arg);