1 /*
2 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "E53_SC2.h"
21 #include "cmsis_os2.h"
22 #include "ohos_init.h"
23
24 #define TASK_STACK_SIZE (1024 * 8)
25 #define TASK_PRIO 25
26 #define ACCEL_THRESHOLD 100
27 #define TASK_DELAY_1S 1000000
28
ExampleTask(void)29 static void ExampleTask(void)
30 {
31 uint8_t ret;
32 E53SC2Data data;
33 int X = 0, Y = 0, Z = 0;
34
35 ret = E53SC2Init();
36 if (ret != 0) {
37 printf("E53_SC2 Init failed!\r\n");
38 return;
39 }
40 while (1) {
41 printf("=======================================\r\n");
42 printf("*************E53_SC2_example***********\r\n");
43 printf("=======================================\r\n");
44 ret = E53SC2ReadData(&data);
45 if (ret != 0) {
46 printf("E53_SC2 Read Data!\r\n");
47 return;
48 }
49 printf("\r\n**************Temperature is %d\r\n", (int)data.Temperature);
50 printf("\r\n**************Accel[0] is %d\r\n", (int)data.Accel[ACCEL_X_AXIS]);
51 printf("\r\n**************Accel[1] is %d\r\n", (int)data.Accel[ACCEL_Y_AXIS]);
52 printf("\r\n**************Accel[2] is %d\r\n", (int)data.Accel[ACCEL_Z_AXIS]);
53 if (X == 0 && Y == 0 && Z == 0) {
54 X = (int)data.Accel[ACCEL_X_AXIS];
55 Y = (int)data.Accel[ACCEL_Y_AXIS];
56 Z = (int)data.Accel[ACCEL_Z_AXIS];
57 } else {
58 if (X + ACCEL_THRESHOLD < data.Accel[ACCEL_X_AXIS] || X - ACCEL_THRESHOLD > data.Accel[ACCEL_X_AXIS]
59 || Y + ACCEL_THRESHOLD < data.Accel[ACCEL_Y_AXIS] || Y - ACCEL_THRESHOLD > data.Accel[ACCEL_Y_AXIS]
60 || Z + ACCEL_THRESHOLD < data.Accel[ACCEL_Z_AXIS] || Z - ACCEL_THRESHOLD > data.Accel[ACCEL_Z_AXIS]) {
61 LedD1StatusSet(OFF);
62 LedD2StatusSet(ON);
63 } else {
64 LedD1StatusSet(ON);
65 LedD2StatusSet(OFF);
66 }
67 }
68 usleep(TASK_DELAY_1S);
69 }
70 }
71
ExampleEntry(void)72 static void ExampleEntry(void)
73 {
74 osThreadAttr_t attr;
75
76 attr.name = "ExampleTask";
77 attr.attr_bits = 0U;
78 attr.cb_mem = NULL;
79 attr.cb_size = 0U;
80 attr.stack_mem = NULL;
81 attr.stack_size = TASK_STACK_SIZE;
82 attr.priority = TASK_PRIO;
83
84 if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
85 printf("Failed to create ExampleTask!\n");
86 }
87 }
88
89 APP_FEATURE_INIT(ExampleEntry);