加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mythread_heart_rate.cpp 5.88 KB
一键复制 编辑 原始数据 按行查看 历史
姜玮 提交于 2024-08-23 11:57 . 心率数据入库
#include "mythread_heart_rate.h"
#include <QFile>
int Heart_rate;
int SpO2_;
int Fd;
Mythread_heart_rate::Mythread_heart_rate()
{
}
// 定义数据掩码和位移量
#define DATA_MASK ((1 << 18) - 1)
#define DATA_SHIFT 8
#define DATA_BUF_SIZE 400
char device_name[128]={"iio:device2"};
unsigned int aun_red_buf[DATA_BUF_SIZE];
unsigned int aun_ir_buf[DATA_BUF_SIZE];
// 声明计算心率和血氧饱和度的外部函数
extern void maxim_heart_rate_and_oxygen_saturation(
unsigned int *pun_ir_buffer,
int n_ir_buffer_length,
unsigned int *pun_red_buffer,
int *pn_spo2,
int *pch_spo2_valid,
int *pn_heart_rate,
int *pch_hr_valid);
// 定义向系统文件写入整数的函数
int write_sys_int(char *filename, int data) {
int ret = 0;
FILE *filp;
filp = fopen(filename, "w");
if (!filp) {
ret = -errno;
fprintf(stderr, "failed to open %s\n", filename);
goto error;
}
ret = fprintf(filp, "%d", data);
if (ret < 0) {
if (fclose(filp))
perror("_write_sysfs_int(): Failed to close dir");
goto error;
}
if (fclose(filp)) {
ret = -errno;
goto error;
}
return 0;
error:
return ret;
}
// 定义启用或禁用所有通道的函数
int enable_disable_all_channels(int enable) {
int ret = 0;
char red_enable[128] = {0};
char ir_enable[128] = {0};
sprintf(red_enable, "/sys/bus/iio/devices/%s/scan_elements/in_intensity_red_en", device_name);
sprintf(ir_enable, "/sys/bus/iio/devices/%s/scan_elements/in_intensity_ir_en", device_name);
ret = write_sys_int(red_enable, enable);
if (ret < 0) {
fprintf(stderr, "Failed to enable/disable %s", red_enable);
return ret;
}
ret = write_sys_int(ir_enable, enable);
if (ret < 0) {
fprintf(stderr, "Failed to enable/disable %s", ir_enable);
return ret;
}
return 0;
}
// 定义启用或禁用缓冲区的函数
int enable_disable_buffer(int enable) {
int ret = 0;
char buffer_enable[128] = {0};
sprintf(buffer_enable, "/sys/bus/iio/devices/%s/buffer/enable", device_name);
ret = write_sys_int(buffer_enable, enable);
if (ret < 0) {
fprintf(stderr, "Failed to enable/disable %s", buffer_enable);
return ret;
}
return 0;
}
// 定义设置缓冲区长度的函数
int set_buffer_len(int len) {
int ret = 0;
char buffer_len[128] = {0};
sprintf(buffer_len, "/sys/bus/iio/devices/%s/buffer/length", device_name);
ret = write_sys_int(buffer_len, len);
if (ret < 0) {
fprintf(stderr, "Failed to enable/disable %s", buffer_len);
return ret;
}
return 0;
}
// 定义清理资源的函数
void cleanup() {
enable_disable_buffer(0);
enable_disable_all_channels(0);
}
// 定义信号处理函数
void sig_handler(int signum) {
fprintf(stderr, "Caught signal %d\n", signum);
cleanup();
exit(-signum);
}
// 定义注册清理函数的函数
void register_cleanup(void) {
struct sigaction sa = { sa.sa_handler = sig_handler };
const int signums[] = { SIGINT, SIGTERM, SIGABRT };
int ret, i;
for (i = 0; i < 3; ++i) {
ret = sigaction(signums[i], &sa, NULL);
if (ret) {
perror("Failed to register signal handler");
exit(-1);
}
}
}
// 主函数
int main_heart_rate() {
int ret = 0;
int data[2];
char device_path[128] = {0};
int fd = 0;
int i, j;
int read_size;
int SpO2;
int heart_rate;
int hr_valid;
int spo2_valid;
int tmp_val = 0;
int flag;
flag=0;
// if (argc != 2) {
// printf("usage: %s <iio:device[n]>\n", argv[0]);
// return 1;
// }
register_cleanup();
//sprintf(device_name, "%s", argv[1]);
enable_disable_all_channels(1);
set_buffer_len(4);
enable_disable_buffer(1);
sprintf(device_path, "/dev/%s", device_name);
fd = open(device_path, O_RDONLY | O_NONBLOCK);
Fd=fd;
if (fd < 0) { /* TODO: If it isn't there make the node */
fprintf(stderr, "Failed open device: %s", strerror(errno));
goto error;
}
// 持续读取新数据并更新心率和血氧饱和度
while (1) {
for (i = 100; i < DATA_BUF_SIZE; i++) {
aun_red_buf[i - 100] = aun_red_buf[i];
aun_ir_buf[i - 100] = aun_ir_buf[i];
}
int count = 0;
for (j = DATA_BUF_SIZE - 100; j < DATA_BUF_SIZE; j++) {
struct pollfd pfd = {
.fd = fd,
.events = POLLIN,
};
ret = poll(&pfd, 1, -1);
if (ret < 0) {
ret = -errno;
goto error;
} else if (ret == 0) {
printf("continue\n");
continue;
}
read_size = read(fd, data, 8);
if (read_size < 0) {
if (errno == EAGAIN) {
fprintf(stderr, "nothing available\n");
continue;
} else {
break;
}
}
tmp_val = be32toh(data[0]);
aun_red_buf[j] = (tmp_val >> DATA_SHIFT) & DATA_MASK;
tmp_val = be32toh(data[1]);
aun_ir_buf[j] = (tmp_val >> DATA_SHIFT) & DATA_MASK;
count++;
}
maxim_heart_rate_and_oxygen_saturation(aun_ir_buf, DATA_BUF_SIZE,
aun_red_buf, &SpO2, &spo2_valid, &heart_rate, &hr_valid);
if (spo2_valid)
printf("222SpO2: %d\t", SpO2);
SpO2_=SpO2;
if (hr_valid)
printf("222heart rate: %d\n", heart_rate);
Heart_rate=heart_rate;
flag++;
if(flag==20)
{
qDebug()<<"break";
break;
}
}
error:
cleanup();
close(fd);
return 0;
}
void Mythread_heart_rate::run()
{
main_heart_rate();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化