mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +03:00
java/pulsensor: Changed to C++ types and added callback functionality for Java
Signed-off-by: Andrei Vasiliu <andrei.vasiliu@intel.com> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
parent
6719168a20
commit
701a25261f
15
src/pulsensor/Callback.h
Normal file
15
src/pulsensor/Callback.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||||
|
class Callback
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Callback()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
virtual void run(clbk_data arg)
|
||||||
|
{ /* empty, overloaded in Java*/
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -2,11 +2,14 @@
|
|||||||
%include "../upm.i"
|
%include "../upm.i"
|
||||||
%include "arrays_java.i"
|
%include "arrays_java.i"
|
||||||
|
|
||||||
|
%feature("director") Callback;
|
||||||
|
|
||||||
%ignore sample_thread;
|
%ignore sample_thread;
|
||||||
%ignore pin_ctx;
|
%ignore pin_ctx;
|
||||||
%ignore do_sample;
|
%ignore do_sample;
|
||||||
%ignore callback;
|
%ignore callback;
|
||||||
|
|
||||||
|
%include "Callback.h"
|
||||||
%{
|
%{
|
||||||
#include "pulsensor.h"
|
#include "pulsensor.h"
|
||||||
%}
|
%}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
|
* Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
|
||||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Copyright (c) 2015 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -26,126 +27,153 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "pulsensor.h"
|
#include "pulsensor.h"
|
||||||
|
|
||||||
void init_pulsensor (pulsensor_context * ctx, callback_handler handler) {
|
#if defined(JAVACALLBACK)
|
||||||
ctx->callback = handler;
|
Pulsensor::Pulsensor (Callback *obj_call) : pin_ctx(0)
|
||||||
|
{
|
||||||
|
obj_callback = obj_call;
|
||||||
|
|
||||||
if ( !(ctx->pin_ctx = mraa_aio_init(0)) )
|
sample_counter = 0;
|
||||||
{
|
last_beat_time = 0;
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
threshold = 512;
|
||||||
": mraa_aio_init() failed, invalid pin?");
|
ibi = 600;
|
||||||
return;
|
trough = 512;
|
||||||
}
|
peak = 512;
|
||||||
|
is_pulse = FALSE;
|
||||||
|
ret = FALSE;
|
||||||
|
bpm = 0;
|
||||||
|
qs = FALSE;
|
||||||
|
apmlitude = 100;
|
||||||
|
|
||||||
ctx->sample_counter = 0;
|
|
||||||
ctx->last_beat_time = 0;
|
|
||||||
ctx->threshold = 512;
|
|
||||||
ctx->ibi = 600;
|
|
||||||
ctx->trough = 512;
|
|
||||||
ctx->peak = 512;
|
|
||||||
ctx->is_pulse = FALSE;
|
|
||||||
ctx->ret = FALSE;
|
|
||||||
ctx->bpm = 0;
|
|
||||||
ctx->qs = FALSE;
|
|
||||||
ctx->apmlitude = 100;
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
Pulsensor::Pulsensor (callback_handler handler) : pin_ctx(0)
|
||||||
|
{
|
||||||
|
callback = handler;
|
||||||
|
|
||||||
void start_sampler (pulsensor_context * ctx) {
|
sample_counter = 0;
|
||||||
|
last_beat_time = 0;
|
||||||
|
threshold = 512;
|
||||||
|
ibi = 600;
|
||||||
|
trough = 512;
|
||||||
|
peak = 512;
|
||||||
|
is_pulse = FALSE;
|
||||||
|
ret = FALSE;
|
||||||
|
bpm = 0;
|
||||||
|
qs = FALSE;
|
||||||
|
apmlitude = 100;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void Pulsensor::start_sampler ()
|
||||||
|
{
|
||||||
int error;
|
int error;
|
||||||
ctx_counter++;
|
ctx_counter++;
|
||||||
usleep (100000);
|
usleep (100000);
|
||||||
error = pthread_create (&(ctx->sample_thread), NULL, &do_sample, (void *) ctx);
|
error = pthread_create (&(sample_thread), NULL, &Pulsensor::do_sample, this);
|
||||||
if (error != 0) {
|
if (error != 0) {
|
||||||
printf ("ERROR : Cannot created sampler thread.\n");
|
printf ("ERROR : Cannot created sampler thread.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop_sampler (pulsensor_context * ctx) {
|
void Pulsensor::stop_sampler () {
|
||||||
ctx_counter--;
|
Pulsensor::ctx_counter--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * do_sample (void * arg) {
|
void *Pulsensor::do_sample (void *arg) {
|
||||||
int data_from_sensor;
|
int data_from_sensor;
|
||||||
clbk_data callback_data;
|
clbk_data callback_data;
|
||||||
while (ctx_counter) {
|
|
||||||
pulsensor_context * ctx = (pulsensor_context *) arg;
|
|
||||||
mraa_aio_context pin = ctx->pin_ctx;
|
|
||||||
data_from_sensor = mraa_aio_read (pin);
|
|
||||||
ctx->ret = FALSE;
|
|
||||||
|
|
||||||
ctx->sample_counter += 2;
|
Pulsensor *pulsensor = static_cast<Pulsensor *>(arg);
|
||||||
int N = ctx->sample_counter - ctx->last_beat_time;
|
|
||||||
|
|
||||||
if (data_from_sensor < ctx->threshold && N > ( ctx->ibi / 5)* 3) {
|
while (Pulsensor::ctx_counter) {
|
||||||
if (data_from_sensor < ctx->trough) {
|
data_from_sensor = pulsensor->pin_ctx.read ();
|
||||||
ctx->trough = data_from_sensor;
|
pulsensor->ret = FALSE;
|
||||||
|
|
||||||
|
pulsensor->sample_counter += 2;
|
||||||
|
int N = pulsensor->sample_counter - pulsensor->last_beat_time;
|
||||||
|
|
||||||
|
if (data_from_sensor < pulsensor->threshold &&
|
||||||
|
N > ( pulsensor->ibi / 5)* 3) {
|
||||||
|
if (data_from_sensor < pulsensor->trough) {
|
||||||
|
pulsensor->trough = data_from_sensor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data_from_sensor > ctx->threshold && data_from_sensor > ctx->peak) {
|
if (data_from_sensor > pulsensor->threshold &&
|
||||||
ctx->peak = data_from_sensor;
|
data_from_sensor > pulsensor->peak) {
|
||||||
|
pulsensor->peak = data_from_sensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (N > 250) {
|
if (N > 250) {
|
||||||
// printf ("(NO_GDB) DEBUG\n");
|
// printf ("(NO_GDB) DEBUG\n");
|
||||||
if ( (data_from_sensor > ctx->threshold) &&
|
if ( (data_from_sensor > pulsensor->threshold) &&
|
||||||
(ctx->is_pulse == FALSE) &&
|
(pulsensor->is_pulse == FALSE) &&
|
||||||
(N > (ctx->ibi / 5)* 3) ) {
|
(N > (pulsensor->ibi / 5)* 3) ) {
|
||||||
ctx->is_pulse = callback_data.is_heart_beat = TRUE;
|
pulsensor->is_pulse = callback_data.is_heart_beat = TRUE;
|
||||||
((pulsensor_context *) arg)->callback(callback_data);
|
#if defined(JAVACALLBACK)
|
||||||
|
pulsensor->obj_callback->run(callback_data);
|
||||||
|
#else
|
||||||
|
pulsensor->callback(callback_data);
|
||||||
|
#endif
|
||||||
|
|
||||||
ctx->ibi = ctx->sample_counter - ctx->last_beat_time;
|
pulsensor->ibi = pulsensor->sample_counter - pulsensor->last_beat_time;
|
||||||
ctx->last_beat_time = ctx->sample_counter;
|
pulsensor->last_beat_time = pulsensor->sample_counter;
|
||||||
|
|
||||||
// second beat
|
// second beat
|
||||||
if (ctx->second_beat) {
|
if (pulsensor->second_beat) {
|
||||||
ctx->second_beat = FALSE;
|
pulsensor->second_beat = FALSE;
|
||||||
for (int i = 0; i <= 9; i++) {
|
for (int i = 0; i <= 9; i++) {
|
||||||
ctx->ibi_rate[i] = ctx->ibi;
|
pulsensor->ibi_rate[i] = pulsensor->ibi;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// first beat
|
// first beat
|
||||||
if (ctx->first_beat) {
|
if (pulsensor->first_beat) {
|
||||||
ctx->first_beat = FALSE;
|
pulsensor->first_beat = FALSE;
|
||||||
ctx->second_beat = TRUE;
|
pulsensor->second_beat = TRUE;
|
||||||
ctx->ret = TRUE;
|
pulsensor->ret = TRUE;
|
||||||
} else {
|
} else {
|
||||||
uint32_t running_total = 0;
|
uint32_t running_total = 0;
|
||||||
for(int i = 0; i <= 8; i++){
|
for(int i = 0; i <= 8; i++){
|
||||||
ctx->ibi_rate[i] = ctx->ibi_rate[i+1];
|
pulsensor->ibi_rate[i] = pulsensor->ibi_rate[i+1];
|
||||||
running_total += ctx->ibi_rate[i];
|
running_total += pulsensor->ibi_rate[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->ibi_rate[9] = ctx->ibi;
|
pulsensor->ibi_rate[9] = pulsensor->ibi;
|
||||||
running_total += ctx->ibi_rate[9];
|
running_total += pulsensor->ibi_rate[9];
|
||||||
running_total /= 10;
|
running_total /= 10;
|
||||||
ctx->bpm = 60000 / running_total;
|
pulsensor->bpm = 60000 / running_total;
|
||||||
ctx->qs = TRUE;
|
pulsensor->qs = TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->ret == FALSE) {
|
if (pulsensor->ret == FALSE) {
|
||||||
if (data_from_sensor < ctx->threshold && ctx->is_pulse == TRUE) {
|
if (data_from_sensor < pulsensor->threshold &&
|
||||||
ctx->is_pulse = callback_data.is_heart_beat = FALSE;
|
pulsensor->is_pulse == TRUE) {
|
||||||
((pulsensor_context *) arg)->callback(callback_data);
|
pulsensor->is_pulse = callback_data.is_heart_beat = FALSE;
|
||||||
|
#if defined(JAVACALLBACK)
|
||||||
ctx->is_pulse = FALSE;
|
pulsensor->obj_callback->run(callback_data);
|
||||||
ctx->apmlitude = ctx->peak - ctx->trough;
|
#else
|
||||||
ctx->threshold = ctx->apmlitude / 2 + ctx->trough;
|
pulsensor->callback(callback_data);
|
||||||
ctx->peak = ctx->threshold;
|
#endif
|
||||||
ctx->trough = ctx->threshold;
|
pulsensor->is_pulse = FALSE;
|
||||||
|
pulsensor->apmlitude = pulsensor->peak - pulsensor->trough;
|
||||||
|
pulsensor->threshold = pulsensor->apmlitude / 2 + pulsensor->trough;
|
||||||
|
pulsensor->peak = pulsensor->threshold;
|
||||||
|
pulsensor->trough = pulsensor->threshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (N > 2500) {
|
if (N > 2500) {
|
||||||
ctx->threshold = 512;
|
pulsensor->threshold = 512;
|
||||||
ctx->peak = 512;
|
pulsensor->peak = 512;
|
||||||
ctx->trough = 512;
|
pulsensor->trough = 512;
|
||||||
ctx->last_beat_time = ctx->sample_counter;
|
pulsensor->last_beat_time = pulsensor->sample_counter;
|
||||||
ctx->first_beat = TRUE;
|
pulsensor->first_beat = TRUE;
|
||||||
ctx->second_beat = FALSE;
|
pulsensor->second_beat = FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
|
* Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
|
||||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Copyright (c) 2015 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Credits to Adafruit.
|
* Credits to Adafruit.
|
||||||
* Based on Adafruit BMP085 library.
|
* Based on Adafruit BMP085 library.
|
||||||
@ -28,9 +29,9 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <mraa/pwm.h>
|
#include <mraa/pwm.hpp>
|
||||||
#include <mraa/aio.h>
|
#include <mraa/aio.hpp>
|
||||||
#include <mraa/gpio.h>
|
#include <mraa/gpio.hpp>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#define HIGH 1
|
#define HIGH 1
|
||||||
@ -71,13 +72,30 @@ struct clbk_data {
|
|||||||
int is_heart_beat; /**< heartbeat check */
|
int is_heart_beat; /**< heartbeat check */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||||
|
#include "Callback.h"
|
||||||
|
#else
|
||||||
typedef void (* callback_handler) (clbk_data);
|
typedef void (* callback_handler) (clbk_data);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @struct pulsensor_context
|
* @class Pulsensor
|
||||||
* @brief The context for the heartbeat pulse sensor
|
* @brief The context for the heartbeat pulse sensor
|
||||||
*/
|
*/
|
||||||
struct pulsensor_context {
|
class Pulsensor {
|
||||||
|
|
||||||
|
public:
|
||||||
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||||
|
Pulsensor(Callback *callback);
|
||||||
|
#else
|
||||||
|
Pulsensor(callback_handler handler);
|
||||||
|
#endif
|
||||||
|
void start_sampler();
|
||||||
|
void stop_sampler();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void *do_sample(void *arg);
|
||||||
pthread_t sample_thread; /**< Thread for the code sample */
|
pthread_t sample_thread; /**< Thread for the code sample */
|
||||||
uint32_t sample_counter; /**< Counter for the code sample */
|
uint32_t sample_counter; /**< Counter for the code sample */
|
||||||
uint32_t last_beat_time; /**< Last heartbeat time */
|
uint32_t last_beat_time; /**< Last heartbeat time */
|
||||||
@ -94,15 +112,12 @@ struct pulsensor_context {
|
|||||||
uint8_t second_beat; /**< Second heartbeat */
|
uint8_t second_beat; /**< Second heartbeat */
|
||||||
uint8_t pin; /**< Pin */
|
uint8_t pin; /**< Pin */
|
||||||
uint8_t ret; /**< Return value */
|
uint8_t ret; /**< Return value */
|
||||||
mraa_aio_context pin_ctx; /**< The pin context */
|
mraa::Aio pin_ctx; /**< The pin context */
|
||||||
|
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
|
||||||
|
Callback *obj_callback; /**< The callback object */
|
||||||
|
#else
|
||||||
callback_handler callback; /**< The callback function */
|
callback_handler callback; /**< The callback function */
|
||||||
|
#endif
|
||||||
|
static volatile uint16_t ctx_counter;
|
||||||
};
|
};
|
||||||
|
|
||||||
static volatile uint16_t ctx_counter = 0;
|
|
||||||
|
|
||||||
void init_pulsensor (pulsensor_context * ctx, callback_handler handler);
|
|
||||||
void start_sampler (pulsensor_context * ctx);
|
|
||||||
void stop_sampler (pulsensor_context * ctx);
|
|
||||||
|
|
||||||
void * do_sample (void * arg);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user