1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/logging/log.h>
10 #include <zephyr/sys_clock.h>
11 #include <zephyr/sensing/sensing.h>
12 
13 LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
14 
acc_data_event_callback(sensing_sensor_handle_t handle,const void * buf,void * context)15 static void acc_data_event_callback(sensing_sensor_handle_t handle, const void *buf,
16 				    void *context)
17 {
18 	const struct sensing_sensor_info *info = sensing_get_sensor_info(handle);
19 	struct sensing_sensor_value_3d_q31 *sample = (struct sensing_sensor_value_3d_q31 *)buf;
20 
21 	ARG_UNUSED(context);
22 
23 	LOG_INF("%s(%d), handle:%p, Sensor:%s data:(x:%d, y:%d, z:%d)",
24 		__func__, __LINE__, handle, info->name,
25 		sample->readings[0].x,
26 		sample->readings[0].y,
27 		sample->readings[0].z);
28 }
29 
hinge_angle_data_event_callback(sensing_sensor_handle_t handle,const void * buf,void * context)30 static void hinge_angle_data_event_callback(sensing_sensor_handle_t handle, const void *buf,
31 				    void *context)
32 {
33 	const struct sensing_sensor_info *info = sensing_get_sensor_info(handle);
34 	struct sensing_sensor_value_q31 *sample = (struct sensing_sensor_value_q31 *)buf;
35 
36 	ARG_UNUSED(context);
37 
38 	LOG_INF("handle:%p, Sensor:%s data:(v:%d)", handle, info->name, sample->readings[0].v);
39 }
40 
41 static struct sensing_callback_list base_acc_cb_list = {
42 	.on_data_event = &acc_data_event_callback,
43 };
44 
45 static struct sensing_callback_list hinge_angle_cb_list = {
46 	.on_data_event = &hinge_angle_data_event_callback,
47 };
48 
main(void)49 int main(void)
50 {
51 	const struct sensing_sensor_info *info;
52 	sensing_sensor_handle_t base_acc;
53 	sensing_sensor_handle_t hinge_angle;
54 	struct sensing_sensor_config base_acc_config;
55 	struct sensing_sensor_config hinge_angle_config;
56 	const struct sensing_sensor_info *tmp_sensor_info = NULL;
57 	const struct sensing_sensor_info *accle_info = NULL;
58 	const struct sensing_sensor_info *hinge_info = NULL;
59 	int ret, i, num = 0;
60 
61 	ret = sensing_get_sensors(&num, &info);
62 	if (ret) {
63 		LOG_ERR("sensing_get_sensors error");
64 		return 0;
65 	}
66 
67 	for (i = 0; i < num; ++i) {
68 		LOG_INF("Sensor %d: name: %s friendly_name: %s type: %d",
69 			 i,
70 			 info[i].name,
71 			 info[i].friendly_name,
72 			 info[i].type);
73 		switch (info[i].type) {
74 		case SENSING_SENSOR_TYPE_MOTION_ACCELEROMETER_3D:
75 			accle_info = &info[i];
76 			break;
77 		case SENSING_SENSOR_TYPE_MOTION_HINGE_ANGLE:
78 			hinge_info = &info[i];
79 			break;
80 		default:
81 			break;
82 		}
83 	}
84 
85 	LOG_INF("sensing subsystem run successfully");
86 
87 	ret = sensing_open_sensor(accle_info, &base_acc_cb_list, &base_acc);
88 	if (ret) {
89 		LOG_ERR("sensing_open_sensor, type:0x%x index:0 error:%d",
90 			SENSING_SENSOR_TYPE_MOTION_ACCELEROMETER_3D, ret);
91 	}
92 
93 	ret = sensing_open_sensor(hinge_info, &hinge_angle_cb_list,
94 					&hinge_angle);
95 	if (ret) {
96 		LOG_ERR("sensing_open_sensor_by_type, type:0x%x index:0 error:%d",
97 			SENSING_SENSOR_TYPE_MOTION_HINGE_ANGLE, ret);
98 	}
99 
100 	/* set base acc, lid acc, hinge sensor interval */
101 	base_acc_config.attri = SENSING_SENSOR_ATTRIBUTE_INTERVAL;
102 	base_acc_config.interval = 100 * USEC_PER_MSEC;
103 	ret = sensing_set_config(base_acc, &base_acc_config, 1);
104 	if (ret) {
105 		LOG_ERR("base_acc sensing_set_interval error:%d\n", ret);
106 	}
107 
108 	tmp_sensor_info = sensing_get_sensor_info(hinge_angle);
109 	hinge_angle_config.attri = SENSING_SENSOR_ATTRIBUTE_INTERVAL;
110 	hinge_angle_config.interval = tmp_sensor_info->minimal_interval;
111 	ret = sensing_set_config(hinge_angle, &hinge_angle_config, 1);
112 	if (ret) {
113 		LOG_ERR("hinge_angle sensing_set_interval error:%d\n", ret);
114 	}
115 
116 	memset(&base_acc_config, 0x00, sizeof(struct sensing_sensor_config));
117 	memset(&hinge_angle_config, 0x00, sizeof(struct sensing_sensor_config));
118 
119 	/* get base acc, lid acc, hinge sensor interval */
120 	base_acc_config.attri = SENSING_SENSOR_ATTRIBUTE_INTERVAL;
121 	ret = sensing_get_config(base_acc, &base_acc_config, 1);
122 	if (ret) {
123 		LOG_ERR("base_acc sensing_get_interval error:%d\n", ret);
124 	}
125 
126 	hinge_angle_config.attri = SENSING_SENSOR_ATTRIBUTE_INTERVAL;
127 	ret = sensing_get_config(hinge_angle, &hinge_angle_config, 1);
128 	if (ret) {
129 		LOG_ERR("hinge_angle sensing_get_interval error:%d\n", ret);
130 	}
131 
132 	/* set base acc, lid acc, hinge sensor sensitivity */
133 	base_acc_config.attri = SENSING_SENSOR_ATTRIBUTE_SENSITIVITY;
134 	base_acc_config.data_field = SENSING_SENSITIVITY_INDEX_ALL;
135 	base_acc_config.sensitivity = 0;
136 	ret = sensing_set_config(base_acc, &base_acc_config, 1);
137 	if (ret) {
138 		LOG_ERR("base_acc sensing_set_sensitivity error:%d\n", ret);
139 	}
140 
141 	hinge_angle_config.attri = SENSING_SENSOR_ATTRIBUTE_SENSITIVITY;
142 	hinge_angle_config.data_field = SENSING_SENSITIVITY_INDEX_ALL;
143 	hinge_angle_config.sensitivity = 1;
144 	ret = sensing_set_config(hinge_angle, &hinge_angle_config, 1);
145 	if (ret) {
146 		LOG_ERR("hinge_angle sensing_set_sensitivity error:%d\n", ret);
147 	}
148 
149 	memset(&base_acc_config, 0x00, sizeof(struct sensing_sensor_config));
150 	memset(&hinge_angle_config, 0x00, sizeof(struct sensing_sensor_config));
151 
152 	/* get base acc, lid acc, hinge sensor sensitivity */
153 	base_acc_config.attri = SENSING_SENSOR_ATTRIBUTE_SENSITIVITY;
154 	base_acc_config.data_field = SENSING_SENSITIVITY_INDEX_ALL;
155 	ret = sensing_get_config(base_acc, &base_acc_config, 1);
156 	if (ret) {
157 		LOG_ERR("base_acc sensing_get_sensitivity error:%d\n", ret);
158 	}
159 
160 	hinge_angle_config.attri = SENSING_SENSOR_ATTRIBUTE_SENSITIVITY;
161 	hinge_angle_config.data_field = SENSING_SENSITIVITY_INDEX_ALL;
162 	ret = sensing_get_config(hinge_angle, &hinge_angle_config, 1);
163 	if (ret) {
164 		LOG_ERR("hinge_angle sensing_get_sensitivity error:%d\n", ret);
165 	}
166 
167 	return 0;
168 }
169