summaryrefslogtreecommitdiff
path: root/include/motion_sense.h
diff options
context:
space:
mode:
authorIkjoon Jang <ikjn@chromium.org>2020-07-29 18:49:50 +0800
committerCommit Bot <commit-bot@chromium.org>2020-09-02 14:16:36 +0000
commita30ccbaefe30d16ff6327cf4e3e687077b7ef041 (patch)
tree381b78cfbf81e8b3531b8c9c0c6ce96bc9f62cdb /include/motion_sense.h
parent4e2524427a59435987ba8dc7bb8ee79329234024 (diff)
downloadchrome-ec-a30ccbaefe30d16ff6327cf4e3e687077b7ef041.tar.gz
common: motion_sense: Add helper functions clamping 32bit into ec protocol
This helper functions are for clamping 32bit values into ec motion sensor protocol 16 data representations. BUG=b:162396219 TEST=build BRANCH=none Signed-off-by: Ikjoon Jang <ikjn@chromium.org> Change-Id: I9c2a6e824570abde916de8cf513d1be79e345b56 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2328949 Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Diffstat (limited to 'include/motion_sense.h')
-rw-r--r--include/motion_sense.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/include/motion_sense.h b/include/motion_sense.h
index 515d569d9e..7e059794c1 100644
--- a/include/motion_sense.h
+++ b/include/motion_sense.h
@@ -16,6 +16,7 @@
#include "math_util.h"
#include "queue.h"
#include "timer.h"
+#include "util.h"
enum sensor_state {
SENSOR_NOT_INITIALIZED = 0,
@@ -319,4 +320,39 @@ enum motionsensor_orientation motion_sense_remap_orientation(
#endif
#endif
+/*
+ * helper functions for clamping raw i32 values,
+ * each sensor driver should take care of overflow condition.
+ */
+static inline uint16_t ec_motion_sensor_clamp_u16(const int32_t value)
+{
+ return (uint16_t)MIN(MAX(value, 0), UINT16_MAX);
+}
+static inline void ec_motion_sensor_clamp_u16s(uint16_t *arr, const int32_t *v)
+{
+ arr[0] = ec_motion_sensor_clamp_u16(v[0]);
+ arr[1] = ec_motion_sensor_clamp_u16(v[1]);
+ arr[2] = ec_motion_sensor_clamp_u16(v[2]);
+}
+
+static inline int16_t ec_motion_sensor_clamp_i16(const int32_t value)
+{
+ return MIN(MAX(value, INT16_MIN), INT16_MAX);
+}
+static inline void ec_motion_sensor_clamp_i16s(int16_t *arr, const int32_t *v)
+{
+ arr[0] = ec_motion_sensor_clamp_i16(v[0]);
+ arr[1] = ec_motion_sensor_clamp_i16(v[1]);
+ arr[2] = ec_motion_sensor_clamp_i16(v[2]);
+}
+
+/* direct assignment */
+static inline void ec_motion_sensor_fill_values(
+ struct ec_response_motion_sensor_data *dst, const int32_t *v)
+{
+ dst->data[0] = v[0];
+ dst->data[1] = v[1];
+ dst->data[2] = v[2];
+}
+
#endif /* __CROS_EC_MOTION_SENSE_H */