summaryrefslogtreecommitdiff
path: root/peripheral/sensors/edison_arduino/Sensor.hpp
blob: d28a3989bb7abc14b7e09f9fa7c3ed71f3d21d55 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SENSOR_HPP
#define SENSOR_HPP

#include "AcquisitionThread.hpp"

struct sensors_event_t;
class AcquisitionThread;

/**
 * Sensor representation class
 *
 * It supports sensor enabling/disabling, changing the sensor's parameters
 * and event reading.
 */
class Sensor {
  public:
    /**
     * Enum for type specification
     */
    enum Type {
      kMMA7660Accelerometer,
      kMPU9150Accelerometer,
      kGroveLight,
      /* NEW sensors: index here above kNumTypes */
      kNumTypes
    };

    /**
     * Sensor constructor
     */
    Sensor();

    /**
     * Sensor destructor
     */
    virtual ~Sensor();

    /**
     * Activate the sensor
     * @param handle sensor identifier
     * @param enabled 1 for enabling and 0 for disabling
     * @return 0 on success and a negative error number otherwise
     */
    virtual int activate(int handle, int enabled);

    /**
     * Set delay
     * @param handle sensor identifier
     * @param ns the sampling period at which the sensor should run, in nanoseconds
     * @return 0 on success and a negative error number otherwise
     */
    virtual int setDelay(int handle, int64_t ns);

    /**
     * Poll for events
     * @param data where to store the events
     * @param count the number of events returned must be <= to the count
     * @return 0 on success and a negative error number otherwise
     */
    virtual int pollEvents(sensors_event_t* data, int count) = 0;

    /**
     * Sets a sensor’s parameters, including sampling frequency and maximum report latency
     * @param handle sensor identifier
     * @param flags currently unused
     * @param period_ns the sampling period at which the sensor should run, in nanoseconds
     * @param timeout the maximum time by which events can be delayed before being reported
     *          through the HAL, in nanoseconds
     * @return 0 on success and a negative error number otherwise
     */
    virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);

    /**
     * Add a flush complete event to the end of the hardware FIFO for the specified sensor and flushes the FIFO
     * @param handle sensor identifier
     * @return 0 on success and a negative error number otherwise
     */
    virtual int flush(int handle);

    /**
     * Read and store an event
     * @param event where to store the event
     * @return true on success and a false otherwise
     */
    virtual bool readOneEvent(sensors_event_t *event);

    /**
     * Get sensor identifier
     * @return sensor handle
     */
    int getHandle() { return handle; }

    /**
     * Get sensor type
     * @return sensor type
     */
    int getType() { return type; }

    /**
     * Get sensor delay in nanoseconds
     * @return sensor delay
     */
    int64_t getDelay() { return delay; }

    /**
     * Gravitational acceleration constant in m/s^2
     */
    static const float kGravitationalAcceleration;

  protected:

    /**
     * Enable or disable the associated acquisition thread
     * @param handle sensor identifier
     * @param enabled 1 for enabling and 0 for disabling
     * @return 0 on success and a negative error number otherwise
     */
    virtual int activateAcquisitionThread(int handle, int enabled);

    AcquisitionThread *acquisitionThread;
    int handle, type;
    int64_t delay;
};

#endif  // SENSOR_HPP