summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIkjoon Jang <ikjn@chromium.org>2020-07-29 18:49:50 +0800
committerCommit Bot <commit-bot@chromium.org>2021-02-26 03:33:40 +0000
commite374a626d87bd11c90ece29e9356850e685db8df (patch)
tree64e930d8dbfbb4e0979d0b4b0c2843f94145dd59
parenta4dd3719bc582aee57cb7de8c4b61080d738d91b (diff)
downloadchrome-ec-e374a626d87bd11c90ece29e9356850e685db8df.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> (cherry picked from commit a30ccbaefe30d16ff6327cf4e3e687077b7ef041) Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2721095 Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org> Commit-Queue: Tim Wawrzynczak <twawrzynczak@chromium.org> Tested-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
-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 8e0ade7492..08e78bc75c 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,
@@ -294,4 +295,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 */