2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "pulsensor.h"
29 void init_pulsensor (pulsensor_context * ctx, callback_handler handler) {
30 ctx->callback = handler;
32 ctx->pin_ctx = mraa_aio_init(0);
34 ctx->sample_counter = 0;
35 ctx->last_beat_time = 0;
40 ctx->is_pulse = FALSE;
47 void start_sampler (pulsensor_context * ctx) {
51 error = pthread_create (&(ctx->sample_thread), NULL, do_sample, (void *) ctx);
53 printf ("ERROR : Cannot created sampler thread.\n");
57 void stop_sampler (pulsensor_context * ctx) {
61 void * do_sample (void * arg) {
63 clbk_data callback_data;
65 pulsensor_context * ctx = (pulsensor_context *) arg;
66 mraa_aio_context pin = ctx->pin_ctx;
67 data_from_sensor = mraa_aio_read (pin);
70 ctx->sample_counter += 2;
71 int N = ctx->sample_counter - ctx->last_beat_time;
73 if (data_from_sensor < ctx->threshold && N > ( ctx->ibi / 5)* 3) {
74 if (data_from_sensor < ctx->trough) {
75 ctx->trough = data_from_sensor;
79 if (data_from_sensor > ctx->threshold && data_from_sensor > ctx->peak) {
80 ctx->peak = data_from_sensor;
84 // printf ("(NO_GDB) DEBUG\n");
85 if ( (data_from_sensor > ctx->threshold) &&
86 (ctx->is_pulse == FALSE) &&
87 (N > (ctx->ibi / 5)* 3) ) {
88 ctx->is_pulse = callback_data.is_heart_beat = TRUE;
89 ((pulsensor_context *) arg)->callback(callback_data);
91 ctx->ibi = ctx->sample_counter - ctx->last_beat_time;
92 ctx->last_beat_time = ctx->sample_counter;
95 if (ctx->second_beat) {
96 ctx->second_beat = FALSE;
97 for (int i = 0; i <= 9; i++) {
98 ctx->ibi_rate[i] = ctx->ibi;
103 if (ctx->first_beat) {
104 ctx->first_beat = FALSE;
105 ctx->second_beat = TRUE;
108 uint32_t running_total = 0;
109 for(int i = 0; i <= 8; i++){
110 ctx->ibi_rate[i] = ctx->ibi_rate[i+1];
111 running_total += ctx->ibi_rate[i];
114 ctx->ibi_rate[9] = ctx->ibi;
115 running_total += ctx->ibi_rate[9];
117 ctx->bpm = 60000 / running_total;
123 if (ctx->ret == FALSE) {
124 if (data_from_sensor < ctx->threshold && ctx->is_pulse == TRUE) {
125 ctx->is_pulse = callback_data.is_heart_beat = FALSE;
126 ((pulsensor_context *) arg)->callback(callback_data);
128 ctx->is_pulse = FALSE;
129 ctx->apmlitude = ctx->peak - ctx->trough;
130 ctx->threshold = ctx->apmlitude / 2 + ctx->trough;
131 ctx->peak = ctx->threshold;
132 ctx->trough = ctx->threshold;
136 ctx->threshold = 512;
139 ctx->last_beat_time = ctx->sample_counter;
140 ctx->first_beat = TRUE;
141 ctx->second_beat = FALSE;