A01_DOOR_example.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "DOOR.h"
  19. #include "cmsis_os2.h"
  20. #include "ohos_init.h"
  21. #include "iot_gpio.h"
  22. #include "iot_gpio_ex.h"
  23. #include "hi_time.h"
  24. #define TASK_STACK_SIZE 1024 * 8
  25. #define TASK_PRIO 25
  26. #define GPIO_0 10
  27. #define GPIO_1 12
  28. float GetDistance (void) {
  29. static unsigned long start_time = 0, time = 0;
  30. float distance = 0.0;
  31. IotGpioValue value = IOT_GPIO_VALUE0;
  32. unsigned int flag = 0;
  33. IoTWatchDogDisable();
  34. // hi_io_set_func(GPIO_8, GPIO_FUNC);
  35. IoTGpioInit(GPIO_0);
  36. IoTGpioSetFunc(GPIO_0, IOT_GPIO_FUNC_GPIO_10_GPIO);
  37. IoTGpioSetDir(GPIO_0, IOT_GPIO_DIR_IN);
  38. IoTGpioInit(GPIO_1);
  39. IoTGpioSetFunc(GPIO_1, IOT_GPIO_FUNC_GPIO_12_GPIO);
  40. IoTGpioSetDir(GPIO_1, IOT_GPIO_DIR_OUT);
  41. IoTGpioSetOutputVal(GPIO_1, IOT_GPIO_VALUE1);
  42. hi_udelay(20);
  43. IoTGpioSetOutputVal(GPIO_1, IOT_GPIO_VALUE0);
  44. while (1) {
  45. IoTGpioGetInputVal(GPIO_0, &value);
  46. if ( value == IOT_GPIO_VALUE1 && flag == 0) {
  47. start_time = hi_get_us();
  48. flag = 1;
  49. }
  50. if (value == IOT_GPIO_VALUE0 && flag == 1) {
  51. time = hi_get_us() - start_time;
  52. start_time = 0;
  53. break;
  54. }
  55. }
  56. distance = time * 0.034 / 2;
  57. return distance;
  58. }
  59. static void ExampleTask(void)
  60. {
  61. IotGpioValue value = IOT_GPIO_VALUE0;
  62. // IoTGpioInit(11);
  63. // IoTGpioSetFunc(11, IOT_GPIO_FUNC_GPIO_11_GPIO);
  64. // IoTGpioSetPull(11,IOT_GPIO_PULL_UP);
  65. // IoTGpioSetDir(11, IOT_GPIO_DIR_IN);
  66. printf("start test hcsr04\r\n");
  67. while (1) {
  68. // IoTGpioGetInputVal(11, &value);
  69. // if (value == IOT_GPIO_VALUE0) {
  70. float distance = GetDistance();
  71. printf("distance is %f\r\n", distance);
  72. //
  73. // }
  74. osDelay(100);
  75. }
  76. }
  77. static void ExampleEntry(void)
  78. {
  79. osThreadAttr_t attr;
  80. attr.name = "ExampleTask";
  81. attr.attr_bits = 0U;
  82. attr.cb_mem = NULL;
  83. attr.cb_size = 0U;
  84. attr.stack_mem = NULL;
  85. attr.stack_size = TASK_STACK_SIZE;
  86. attr.priority = TASK_PRIO;
  87. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  88. printf("Failed to create ExampleTask!\n");
  89. }
  90. }
  91. APP_FEATURE_INIT(ExampleEntry);