A25_MPU6050_example.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2020  Jinan Bosai Network 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. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "MPU6050.h"
  19. #include "cmsis_os2.h"
  20. #include "ohos_init.h"
  21. #define TASK_STACK_SIZE 1024 * 8
  22. #define TASK_PRIO 25
  23. static void ExampleTask(void)
  24. {
  25. uint8_t ret;
  26. MPU6050Data data;
  27. int X = 0, Y = 0, Z = 0;
  28. ret = BoardInit();
  29. if (ret != 0) {
  30. printf("BoardInit failed!\r\n");
  31. return;
  32. }
  33. while (1) {
  34. printf("=======================================\r\n");
  35. printf("*************A25_MPU6050_example***********\r\n");
  36. printf("=======================================\r\n");
  37. ret = MPU6050ReadData(&data);
  38. if (ret != 0) {
  39. printf("MPU6050 Read Data failed!\r\n");
  40. return;
  41. }
  42. printf("\r\n**************Temperature is %d\r\n", (int)data.Temperature);
  43. printf("\r\n**************Accel[0] is %d\r\n", (int)data.Accel[0]);
  44. printf("\r\n**************Accel[1] is %d\r\n", (int)data.Accel[1]);
  45. printf("\r\n**************Accel[2] is %d\r\n", (int)data.Accel[2]);
  46. if (X == 0 && Y == 0 && Z == 0) {
  47. X = (int)data.Accel[0];
  48. Y = (int)data.Accel[1];
  49. Z = (int)data.Accel[2];
  50. } else {
  51. if (X + 100 < data.Accel[0] || X - 100 > data.Accel[0] || Y + 100 < data.Accel[1] ||
  52. Y - 100 > data.Accel[1] || Z + 100 < data.Accel[2] || Z - 100 > data.Accel[2]) {
  53. LedD1StatusSet(ON);
  54. LedD2StatusSet(OFF);
  55. } else {
  56. LedD1StatusSet(OFF);
  57. LedD2StatusSet(ON);
  58. }
  59. }
  60. usleep(1000000);
  61. }
  62. }
  63. static void ExampleEntry(void)
  64. {
  65. osThreadAttr_t attr;
  66. attr.name = "ExampleTask";
  67. attr.attr_bits = 0U;
  68. attr.cb_mem = NULL;
  69. attr.cb_size = 0U;
  70. attr.stack_mem = NULL;
  71. attr.stack_size = TASK_STACK_SIZE;
  72. attr.priority = TASK_PRIO;
  73. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  74. printf("Failed to create ExampleTask!\n");
  75. }
  76. }
  77. APP_FEATURE_INIT(ExampleEntry);