summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Beare <bbeare1@gmail.com>2015-12-19 08:46:43 -0800
committerBruce Beare <bruce.j.beare@intel.com>2015-12-30 13:23:22 -0800
commit19825459f79869ed8517b7ac4a6c2e2784c6da8a (patch)
tree6979557b61450b67bff9114a487318b845bc064f
parent5620d101aee8efa3509782569509c4ea04292ce2 (diff)
downloadcommon-19825459f79869ed8517b7ac4a6c2e2784c6da8a.tar.gz
Add different sensor types to the the sensor HAL test
The sensor HAL test is extended to allow the selection of a particular sensor type. Additional sensor types are easy to add. The default (for compatibility) continues to be the accelerometer. Orientation and Light sensors are added with this commit. BUG=none Change-Id: If17624a610d9de1c71e437c0d680de48308b82e3 Signed-off-by: Bruce Beare <bruce.j.beare@intel.com>
-rw-r--r--sensors_example/hal-example-app.cpp192
1 files changed, 155 insertions, 37 deletions
diff --git a/sensors_example/hal-example-app.cpp b/sensors_example/hal-example-app.cpp
index 2a7cf83..dfec435 100644
--- a/sensors_example/hal-example-app.cpp
+++ b/sensors_example/hal-example-app.cpp
@@ -14,34 +14,155 @@
* limitations under the License.
*/
-// This file constains an example app that uses sensors HAL.
+/* This file contains an example app that uses sensors HAL.
+ * It accepts a sensor type as an input argument and reads data from a
+ * sensor of that type.
+ *
+ * For any specified sensor type, there may be multiple such sensors defined
+ * in the HAL. Some or all of the defined sensors will actually be equipped.
+ * This test program will scan the HAL for the first equipped sensor of
+ * the specified sensor type and dump data from that sensor.
+ */
+#include <getopt.h>
+#include <math.h>
#include <stdio.h>
#include <string.h>
-#include <sys/types.h>
#include <unistd.h>
#include <hardware/hardware.h>
#include <hardware/sensors.h>
+#include <sys/types.h>
+
+#define DEFAULT_SENSOR_TYPE SENSOR_TYPE_ACCELEROMETER
+
+// Structure to hold the decoded command line options
+struct pgm_options {
+ int sensor_type;
+};
+
+// Be sure to keep the options for longopts and shortopts in the same order
+// so that Usage() is correct.
+static struct option longopts[] = {
+ {"help", no_argument, NULL, '?'},
+ {"accel", no_argument, NULL, 'a'},
+ {"temp", no_argument, NULL, 't'},
+ {"light", no_argument, NULL, 'l'},
+ {"orient", no_argument, NULL, 'o'},
+ {"prox", no_argument, NULL, 'p'},
+ {"motion", no_argument, NULL, 'm'},
+ {NULL, 0, NULL, 0}
+};
+static char shortopts[] = "?atlopm";
+
+// Describes the options for this program.
+void Usage(char *pgm_name) {
+ printf("Usage: %s [options...]\n", pgm_name);
+ printf("Exercises the sensors HAL by direct calling.\n");
+ printf("Options:\n");
+ for (int i = 0; longopts[i].name; i++) {
+ printf(" --%-6s or -%c\n", longopts[i].name, shortopts[i]);
+ }
+}
+
+// Processes all command line options.
+// sets the options members for commnd line options
+// returns (0) on success, -1 otherwise.
+int ReadOpts(int argc, char **argv, struct pgm_options *options) {
+ int ch = 0;
+
+ if (!options) {
+ fprintf(stderr, "Invalid options pointer\n");
+ return 1;
+ }
+
+ while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
+ switch (ch) {
+ case 'a':
+ options->sensor_type = SENSOR_TYPE_ACCELEROMETER;
+ break;
+ case 't':
+ options->sensor_type = SENSOR_TYPE_TEMPERATURE;
+ break;
+ case 'l':
+ options->sensor_type = SENSOR_TYPE_LIGHT;
+ break;
+ case 'o':
+ options->sensor_type = SENSOR_TYPE_ORIENTATION;
+ break;
+ case 'p':
+ options->sensor_type = SENSOR_TYPE_PROXIMITY;
+ break;
+ case 'm':
+ options->sensor_type = SENSOR_TYPE_SIGNIFICANT_MOTION;
+ break;
+ default:
+ Usage(argv[0]);
+ return -1;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ return 0;
+}
+
+// Prints data associated with each supported sensor type.
+// Be sure to provide a case for each sensor type supported in
+// the ReadOpts() function.
+void DisplaySensorData(int sensor_type, const sensors_event_t *data) {
+ switch (sensor_type) {
+ case SENSOR_TYPE_PROXIMITY:
+ printf("Proximity distance: %f\n", data->distance);
+ break;
+ case SENSOR_TYPE_SIGNIFICANT_MOTION:
+ if (data->data[0] == 1) {
+ printf("Significant motion detected\n");
+ }
+ break;
+ case SENSOR_TYPE_ACCELEROMETER:
+ printf("Acceleration: x = %f, y = %f, z = %f\n",
+ data->acceleration.x, data->acceleration.y, data->acceleration.z);
+ break;
+ case SENSOR_TYPE_TEMPERATURE:
+ printf("Temperature: %f\n", data->temperature);
+ break;
+ case SENSOR_TYPE_LIGHT:
+ printf("Light: %f\n", data->light);
+ break;
+ case SENSOR_TYPE_ORIENTATION: {
+ float heading =
+ atan2(static_cast<double>(data->orientation.y),
+ static_cast<double>(data->orientation.x)) * 180.0 / M_PI;
+ if (heading < 0.0)
+ heading += 360.0;
+ printf("Heading: %f, Orientation: x = %f, y = %f, z = %f\n",
+ heading, data->orientation.x, data->orientation.y,
+ data->orientation.z);
+ }
+ break;
+ }
+}
+
int main(int argc, char* argv[]) {
- sensors_module_t* sensor_module = nullptr;
+ pgm_options options = {DEFAULT_SENSOR_TYPE};
+ if (ReadOpts(argc, argv, &options) < 0)
+ return 1;
- int ret = hw_get_module(
- SENSORS_HARDWARE_MODULE_ID,
+ sensors_module_t* sensor_module = nullptr;
+ int ret = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
const_cast<hw_module_t const**>(
- reinterpret_cast<hw_module_t**>(&sensor_module)));
+ reinterpret_cast<hw_module_t**>(&sensor_module)));
if (ret || !sensor_module) {
fprintf(stderr, "Failed to load %s module: %s\n",
SENSORS_HARDWARE_MODULE_ID, strerror(-ret));
return 1;
}
- sensor_t const* sensor_list = nullptr;
-
- int sensor_count = sensor_module->get_sensors_list(sensor_module, &sensor_list);
+ const sensor_t *sensor_list = nullptr;
+ int sensor_count =
+ sensor_module->get_sensors_list(sensor_module, &sensor_list);
printf("Found %d supported sensors\n", sensor_count);
-
for (int i = 0; i < sensor_count; i++) {
printf("HAL supports sensor %s\n", sensor_list[i].name);
}
@@ -54,66 +175,63 @@ int main(int argc, char* argv[]) {
return 1;
}
- // Find the first accellerometer that can be opened
- int accelerometer_index = -1;
+ // Find the first sensor of the specified type that can be opened
+ bool sensor_found = false;
+ const sensor_t *sensor = nullptr;
for (int i = 0; i < sensor_count; i++) {
- if (sensor_list[i].type != SENSOR_TYPE_ACCELEROMETER)
+ sensor = &sensor_list[i];
+ if (sensor->type != options.sensor_type)
continue;
- ret = sensor_device->activate(
- reinterpret_cast<sensors_poll_device_t*>(sensor_device),
- sensor_list[i].handle, 1 /* enabled */);
- if (ret) {
- fprintf(stderr, "Unable to activate %s\n", sensor_list[i].name);
+ if (sensor_device->activate(
+ reinterpret_cast<sensors_poll_device_t*>(sensor_device),
+ sensor->handle, 1 /* enabled */) < 0) {
continue;
}
- // found an equipped accelerometer.
- accelerometer_index = i;
+ // Found an equipped sensor of the specified type.
+ sensor_found = true;
break;
}
- if (ret) {
- fprintf(stderr, "No accelerometer found\n");
+ if (!sensor_found) {
+ fprintf(stderr, "No sensor of the specified type found\n");
ret = sensors_close_1(sensor_device);
if (ret)
- fprintf(stderr, "Failed to close the accelerometer device\n");
+ fprintf(stderr, "Failed to close the sensor device\n");
return 1;
}
+ printf("\nSensor %s activated\n", sensor->name);
- printf("\nSensor %s activated\n", sensor_list[accelerometer_index].name);
-
- const int kNumSamples = 10;
const int kNumEvents = 1;
+ const int kNumSamples = 10;
const int kWaitTimeSecs = 1;
-
for (int i = 0; i < kNumSamples; i++) {
- sensors_event_t data;
+ sensors_event_t data[kNumEvents];
+ memset(data, 0, sizeof(data));
int event_count = sensor_device->poll(
reinterpret_cast<sensors_poll_device_t*>(sensor_device),
- &data, kNumEvents);
+ data, kNumEvents);
if (!event_count) {
- fprintf(stderr, "Failed to read data from the accelerometer\n");
- break;
- } else {
- printf("Acceleration: x = %f, y = %f, z = %f\n",
- data.acceleration.x, data.acceleration.y, data.acceleration.z);
+ fprintf(stderr, "Failed to read data from the sensor.\n");
+ continue;
}
+ DisplaySensorData(options.sensor_type, data);
sleep(kWaitTimeSecs);
}
ret = sensor_device->activate(
reinterpret_cast<sensors_poll_device_t*>(sensor_device),
- sensor_list[accelerometer_index].handle, 0 /* disabled */);
+ sensor->handle, 0 /* disabled */);
if (ret) {
- fprintf(stderr, "Failed to disable the accelerometer\n");
+ fprintf(stderr, "Failed to disable %s: %s\n", sensor->name, strerror(ret));
return 1;
}
// sensors_close_1 is used in HAL versions >= 1.0.
ret = sensors_close_1(sensor_device);
if (ret) {
- fprintf(stderr, "Failed to close the accelerometer device\n");
+ fprintf(stderr, "Failed to close the sensor device\n");
return 1;
}