summaryrefslogtreecommitdiff
path: root/libsensors_iio/src/StepCounter.cpp
blob: 7eecd81389e5c3498fbbb55b5cae800541d7e52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * STMicroelectronics Step Counter Sensor Class
 *
 * Copyright 2013-2015 STMicroelectronics Inc.
 * Author: Denis Ciocca - <denis.ciocca@st.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 */

#include <fcntl.h>
#include <assert.h>
#include <signal.h>

#include "StepCounter.h"

StepCounter::StepCounter(HWSensorBaseCommonData *data, const char *name,
		int handle, unsigned int hw_fifo_len, int pipe_data_fd,
		float power_consumption, bool wakeup) :
			HWSensorBase(data, name, handle,
			SENSOR_TYPE_STEP_COUNTER, hw_fifo_len, pipe_data_fd, power_consumption)
{
	sensor_t_data.stringType = SENSOR_STRING_TYPE_STEP_COUNTER;
	sensor_t_data.flags = SENSOR_FLAG_ON_CHANGE_MODE;

	if (wakeup)
		sensor_t_data.flags |= SENSOR_FLAG_WAKE_UP;

	sensor_t_data.resolution = 1.0f;
	sensor_t_data.maxRange = pow(2.0, (double)data->channels[0].bits_used) - 1;

	num_data_axis = SENSOR_BASE_1AXIS;
}

StepCounter::~StepCounter()
{

}

int StepCounter::Enable(int handle, bool enable)
{
	int err;

	err = HWSensorBase::Enable(handle, enable);
	if (err < 0)
		return err;

	if (!GetStatus())
		last_data_timestamp = 0;

	return 0;
}

int StepCounter::SetDelay(int handle, int64_t period_ns, int64_t timeout)
{
	int err;
	int64_t min_pollrate_ns;

	err = SensorBase::SetDelay(handle, period_ns, timeout);
	if (err < 0)
		return err;

	min_pollrate_ns = GetMinPeriod();

	err = write_sysfs_int((char *)FILENAME_MAX_DELIVERY_RATE,
			common_data.iio_sysfs_path, (int)NS_TO_MS(min_pollrate_ns));
	if (err < 0) {
		ALOGE("%s: Failed to write max rate delivery \"%s/%s\".",
				common_data.device_name, common_data.iio_sysfs_path,
				FILENAME_MAX_DELIVERY_RATE);
		return err;
	}

	return 0;
}

void StepCounter::ProcessData(SensorBaseData *data)
{
#define	STEP_COUNTER_DATA_MASK	0xFFFF
	sensor_event.u64.step_counter = (uint64_t)data->raw[0] & STEP_COUNTER_DATA_MASK;
	sensor_event.timestamp = data->timestamp;

	HWSensorBase::WriteDataToPipe();
	HWSensorBase::ProcessData(data);
}