summaryrefslogtreecommitdiff
path: root/vibrator
diff options
context:
space:
mode:
authorchasewu <chasewu@google.com>2020-01-15 11:28:53 +0800
committerchasewu <chasewu@google.com>2020-03-25 13:50:28 +0800
commit12f4d55c9bd3b2b2816995ce11df788a9204aeee (patch)
tree0ef734a5e46fbe7e0615bb520b3c282d37954457 /vibrator
parent3042243c1936befc52b82246117948862cbdaa89 (diff)
downloadbramble-12f4d55c9bd3b2b2816995ce11df788a9204aeee.tar.gz
vibrator: Add motion awareness mechanism
1. Get 20 gravity sensor data in 100ms 2. Set sensing sensor data period in 2s 3. Use new tuning result for motion and non-motion 4. Temporarily remove test and benchmark Bug: 150737103 Bug: 150916170 Bug: 151276021 Test: UT test Change-Id: Iedbf3771d1b7e0b7a066998ab9a4b7aef7f83c32 Signed-off-by: chasewu <chasewu@google.com>
Diffstat (limited to 'vibrator')
-rw-r--r--vibrator/Android.bp2
-rw-r--r--vibrator/drv2624/Android.bp9
-rw-r--r--vibrator/drv2624/Vibrator.cpp102
-rw-r--r--vibrator/drv2624/bench/Android.bp17
-rw-r--r--vibrator/drv2624/service.cpp4
-rw-r--r--vibrator/drv2624/tests/Android.bp16
6 files changed, 106 insertions, 44 deletions
diff --git a/vibrator/Android.bp b/vibrator/Android.bp
index d4b5abd..178edb9 100644
--- a/vibrator/Android.bp
+++ b/vibrator/Android.bp
@@ -27,5 +27,7 @@ cc_defaults {
"liblog",
"libutils",
"libhardware",
+ "libsensorndkbridge",
],
+ proprietary: true,
}
diff --git a/vibrator/drv2624/Android.bp b/vibrator/drv2624/Android.bp
index 94576fd..8c1dcd6 100644
--- a/vibrator/drv2624/Android.bp
+++ b/vibrator/drv2624/Android.bp
@@ -28,20 +28,11 @@ cc_defaults {
],
}
-cc_defaults {
- name: "VibratorHalDrv2624TestDefaultsBramble",
- defaults: ["android.hardware.vibrator@1.3-defaults.bramble"],
- static_libs: ["android.hardware.vibrator@1.3-impl.bramble"],
- test_suites: ["device-tests"],
- require_root: true,
-}
-
cc_library {
name: "android.hardware.vibrator@1.3-impl.bramble",
defaults: ["android.hardware.vibrator@1.3-defaults.bramble"],
srcs: ["Vibrator.cpp"],
export_include_dirs: ["."],
- vendor_available: true,
}
cc_binary {
diff --git a/vibrator/drv2624/Vibrator.cpp b/vibrator/drv2624/Vibrator.cpp
index 793b2b1..a537bb5 100644
--- a/vibrator/drv2624/Vibrator.cpp
+++ b/vibrator/drv2624/Vibrator.cpp
@@ -16,6 +16,8 @@
#include "Vibrator.h"
+#include <android/looper.h>
+#include <android/sensor.h>
#include <cutils/properties.h>
#include <hardware/hardware.h>
#include <hardware/vibrator.h>
@@ -26,6 +28,7 @@
#include <cmath>
#include <fstream>
#include <iostream>
+#include <numeric>
#include "utils.h"
@@ -55,9 +58,79 @@ static constexpr char WAVEFORM_HEAVY_CLICK_EFFECT_SEQ[] = "4 0";
// UT team design those target G values
static constexpr std::array<float, 5> EFFECT_TARGET_G = {0.175, 0.325, 0.37, 0.475, 0.6};
-static constexpr std::array<float, 3> STEADY_TARGET_G = {1.38, 1.145, 0.905};
+static constexpr std::array<float, 3> STEADY_TARGET_G = {1.6, 1.145, 0.4};
+
+struct SensorContext {
+ ASensorEventQueue *queue;
+};
+static std::vector<float> sXAxleData;
+static std::vector<float> sYAxleData;
+static uint64_t sEndTime = 0;
+static struct timespec sGetTime;
#define FLOAT_EPS 1e-7
+#define SENSOR_DATA_NUM 20
+// Set sensing period to 2s
+#define SENSING_PERIOD 2000000000
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+int GSensorCallback(__attribute__((unused)) int fd, __attribute__((unused)) int events,
+ void *data) {
+ ASensorEvent event;
+ int event_count = 0;
+ SensorContext *context = reinterpret_cast<SensorContext *>(data);
+ event_count = ASensorEventQueue_getEvents(context->queue, &event, 1);
+ ALOGI("%s: event data: %f %f\n", __func__, event.data[0], event.data[1]);
+ sXAxleData.push_back(event.data[0]);
+ sYAxleData.push_back(event.data[1]);
+ return 1;
+}
+// TODO: b/152305970
+int32_t PollGSensor() {
+ int err = NO_ERROR, counter = 0;
+ ASensorManager *sensorManager = nullptr;
+ ASensorRef GSensor;
+ ALooper *looper;
+ struct SensorContext context = {nullptr};
+
+ // Get proximity sensor events from the NDK
+ sensorManager = ASensorManager_getInstanceForPackage("");
+ if (!sensorManager) {
+ ALOGI("Chase %s: Sensor manager is NULL.\n", __FUNCTION__);
+ err = UNEXPECTED_NULL;
+ return 0;
+ }
+ GSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GRAVITY);
+ if (GSensor == nullptr) {
+ ALOGE("%s:Chase Unable to get g sensor\n", __func__);
+ } else {
+ looper = ALooper_forThread();
+ if (looper == nullptr) {
+ looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
+ }
+ context.queue =
+ ASensorManager_createEventQueue(sensorManager, looper, 0, GSensorCallback, &context);
+
+ err = ASensorEventQueue_registerSensor(context.queue, GSensor, 0, 0);
+ if (err != NO_ERROR) {
+ ALOGE("Chase %s: Error %d registering G sensor with event queue.\n", __FUNCTION__, err);
+ return 0;
+ }
+ if (err < 0) {
+ ALOGE("%s:Chase Unable to register for G sensor events\n", __func__);
+ } else {
+ for (counter = 0; counter < SENSOR_DATA_NUM; counter++) {
+ ALooper_pollOnce(5, nullptr, nullptr, nullptr);
+ }
+ }
+ }
+ if (sensorManager != nullptr && context.queue != nullptr) {
+ ASensorEventQueue_disableSensor(context.queue, GSensor);
+ ASensorManager_destroyEventQueue(sensorManager, context.queue);
+ }
+
+ return 0;
+}
// Temperature protection upper bound 10°C and lower bound 5°C
static constexpr int32_t TEMP_UPPER_BOUND = 10000;
@@ -173,6 +246,30 @@ static float targetGToVlevelsUnderCubicEquation(std::array<float, 4> inputCoeffs
}
}
+static bool motionAwareness() {
+ float avgX = 0.0, avgY = 0.0;
+ uint64_t current_time = 0;
+ clock_gettime(CLOCK_MONOTONIC, &sGetTime);
+ current_time = ((uint64_t)sGetTime.tv_sec * 1000 * 1000 * 1000) + sGetTime.tv_nsec;
+
+ if ((current_time - sEndTime) > SENSING_PERIOD) {
+ sXAxleData.clear();
+ sYAxleData.clear();
+ PollGSensor();
+ avgX = std::accumulate(sXAxleData.begin(), sXAxleData.end(), 0.0) / sXAxleData.size();
+ avgY = std::accumulate(sYAxleData.begin(), sYAxleData.end(), 0.0) / sYAxleData.size();
+ clock_gettime(CLOCK_MONOTONIC, &sGetTime);
+
+ sEndTime = ((uint64_t)sGetTime.tv_sec * 1000 * 1000 * 1000) + sGetTime.tv_nsec;
+ }
+
+ if ((avgX > -1.3) && (avgX < 1.3) && (avgY > -0.8) && (avgY < 0.8)) {
+ return false;
+ } else {
+ return true;
+ }
+}
+
using utils::toUnderlying;
using Status = ::android::hardware::vibrator::V1_0::Status;
@@ -329,6 +426,9 @@ Return<Status> Vibrator::on(uint32_t timeoutMs) {
if (temperature > TEMP_UPPER_BOUND) {
mSteadyConfig->odClamp = &mSteadyTargetOdClamp[0];
mSteadyConfig->olLraPeriod = mSteadyOlLraPeriod;
+ if (!motionAwareness()) {
+ return on(timeoutMs, RTP_MODE, mSteadyConfig, 2);
+ }
} else if (temperature < TEMP_LOWER_BOUND) {
mSteadyConfig->odClamp = &STEADY_VOLTAGE_LOWER_BOUND;
mSteadyConfig->olLraPeriod = mSteadyOlLraPeriodShift;
diff --git a/vibrator/drv2624/bench/Android.bp b/vibrator/drv2624/bench/Android.bp
index bab4656..0db978c 100644
--- a/vibrator/drv2624/bench/Android.bp
+++ b/vibrator/drv2624/bench/Android.bp
@@ -13,20 +13,3 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-cc_benchmark {
- name: "VibratorHalDrv2624BenchmarkBramble",
- defaults: ["VibratorHalDrv2624TestDefaultsBramble"],
- srcs: [
- "benchmark.cpp",
- ],
- static_libs: [
- "libc++fs",
- ],
- shared_libs: [
- "libbase",
- ],
- // TODO(b/135767253): Remove when fixed.
- test_suites: ["device-tests"],
- // TODO(b/142024316): Remove when fixed.
- require_root: true,
-}
diff --git a/vibrator/drv2624/service.cpp b/vibrator/drv2624/service.cpp
index c0447a7..fdf52f6 100644
--- a/vibrator/drv2624/service.cpp
+++ b/vibrator/drv2624/service.cpp
@@ -46,7 +46,9 @@ status_t registerVibratorService() {
}
int main() {
- configureRpcThreadpool(1, true);
+ // One thread for vibrator APIs and one for sensor callback
+ // WARN: there could be an issue if two vibrator APIs are called simultaneously
+ configureRpcThreadpool(2, true);
status_t status = registerVibratorService();
if (status != OK) {
diff --git a/vibrator/drv2624/tests/Android.bp b/vibrator/drv2624/tests/Android.bp
index 0179950..0db978c 100644
--- a/vibrator/drv2624/tests/Android.bp
+++ b/vibrator/drv2624/tests/Android.bp
@@ -13,19 +13,3 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-cc_test {
- name: "VibratorHalDrv2624TestSuiteBramble",
- defaults: ["VibratorHalDrv2624TestDefaultsBramble"],
- srcs: [
- "test-hwapi.cpp",
- "test-hwcal.cpp",
- "test-vibrator.cpp",
- ],
- static_libs: [
- "libc++fs",
- "libgmock",
- ],
- shared_libs: [
- "libbase",
- ],
-}