summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sensors_example/hal-example-app.cpp49
1 files changed, 28 insertions, 21 deletions
diff --git a/sensors_example/hal-example-app.cpp b/sensors_example/hal-example-app.cpp
index 428636e..2a7cf83 100644
--- a/sensors_example/hal-example-app.cpp
+++ b/sensors_example/hal-example-app.cpp
@@ -39,42 +39,49 @@ int main(int argc, char* argv[]) {
sensor_t const* sensor_list = nullptr;
- int sensor_count = sensor_module->get_sensors_list(sensor_module,
- &sensor_list);
- printf("Found %d sensors\n", sensor_count);
-
- int accelerometer_index = -1;
+ 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("Found %s\n", sensor_list[i].name);
- if (sensor_list[i].type == SENSOR_TYPE_ACCELEROMETER) {
- accelerometer_index = i;
- }
+ printf("HAL supports sensor %s\n", sensor_list[i].name);
}
- if (accelerometer_index == -1) {
- fprintf(stderr, "No accelerometer found\n");
- return 1;
- }
-
- // sensors_poll_device_1_t is used in HAL versions >= 1.0.
- sensors_poll_device_1_t* sensor_device = nullptr;
// sensors_open_1 is used in HAL versions >= 1.0.
+ sensors_poll_device_1_t* sensor_device = nullptr;
ret = sensors_open_1(&sensor_module->common, &sensor_device);
if (ret || !sensor_device) {
- fprintf(stderr, "Failed to open the accelerometer device\n");
+ fprintf(stderr, "Failed to open the sensor HAL\n");
return 1;
}
- ret = sensor_device->activate(
+ // Find the first accellerometer that can be opened
+ int accelerometer_index = -1;
+ for (int i = 0; i < sensor_count; i++) {
+ if (sensor_list[i].type != SENSOR_TYPE_ACCELEROMETER)
+ continue;
+ ret = sensor_device->activate(
reinterpret_cast<sensors_poll_device_t*>(sensor_device),
- sensor_list[accelerometer_index].handle, 1 /* enabled */);
+ sensor_list[i].handle, 1 /* enabled */);
+ if (ret) {
+ fprintf(stderr, "Unable to activate %s\n", sensor_list[i].name);
+ continue;
+ }
+
+ // found an equipped accelerometer.
+ accelerometer_index = i;
+ break;
+ }
+
if (ret) {
- fprintf(stderr, "Failed to enable the accelerometer\n");
- sensors_close_1(sensor_device);
+ fprintf(stderr, "No accelerometer found\n");
+ ret = sensors_close_1(sensor_device);
+ if (ret)
+ fprintf(stderr, "Failed to close the accelerometer device\n");
return 1;
}
+ printf("\nSensor %s activated\n", sensor_list[accelerometer_index].name);
+
const int kNumSamples = 10;
const int kNumEvents = 1;
const int kWaitTimeSecs = 1;