You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "lwip/opt.h"
  6. #include "lwip/sys.h"
  7. #include "lwip/api.h"
  8. #include "main.h"
  9. #include "zport.h"
  10. #include "zboard.h"
  11. #include "encoder.h"
  12. #include "udpclient.h"
  13. #include "zflash.h"
  14. #include "config.h"
  15. #include "zkey.h"
  16. #include "atcmd.h"
  17. #define KEY_SCAN_PERIOD 20
  18. #define CHEAR_ENCODER_1_KEY_NAME "Clear_Encoder_1_Key"
  19. #define CHEAR_ENCODER_2_KEY_NAME "Clear_Encoder_2_Key"
  20. static uint16_t s_key_long_press_time_ms = 3000;
  21. static inline bool clear_encoder_1_key_get_status() { return HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin); }
  22. static inline bool clear_encoder_2_key_get_status() { return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin); }
  23. static void onkey(zkey_t *key, zkey_state_t key_state);
  24. static zkey_t s_keys[] = {
  25. ZKEY_INIT(CHEAR_ENCODER_1_KEY_NAME, clear_encoder_1_key_get_status), //
  26. ZKEY_INIT(CHEAR_ENCODER_2_KEY_NAME, clear_encoder_2_key_get_status), //
  27. };
  28. static zkey_module_t s_key_module = ZMODULE_INIT(s_keys, onkey);
  29. static uint16_t encoder_get_temp;
  30. static void zkey_schedule()
  31. {
  32. static uint32_t lastprocess_key_ticket;
  33. if (sys_haspassedms(lastprocess_key_ticket) >= KEY_SCAN_PERIOD)
  34. {
  35. zkey_do_loop_in_each_period(NULL);
  36. lastprocess_key_ticket = HAL_GetTick();
  37. }
  38. }
  39. static void onkey(zkey_t *key, zkey_state_t key_state)
  40. {
  41. if (key_state == zks_rising_edge)
  42. {
  43. if (strcmp(key->name, CHEAR_ENCODER_1_KEY_NAME) == 0)
  44. {
  45. encoder_switch_clear_counter(CAMERA_ENCODER);
  46. }
  47. else if (strcmp(key->name, CHEAR_ENCODER_2_KEY_NAME) == 0)
  48. {
  49. encoder_switch_clear_counter(DRIVEN_ENCODER_GEAR);
  50. }
  51. }
  52. if (key_state == zks_keep)
  53. {
  54. if (key->keep_state_count == s_key_long_press_time_ms / KEY_SCAN_PERIOD)
  55. {
  56. // 触发长按事件
  57. key->hasProcessed = true;
  58. }
  59. }
  60. else if (key_state == zks_falling_edge)
  61. {
  62. /**
  63. * @brief
  64. */
  65. if (!key->hasProcessed)
  66. {
  67. }
  68. }
  69. }
  70. /**
  71. * @brief
  72. */
  73. void port_mock_on_uart_rx(uart_t *uart)
  74. {
  75. // 处理指令串口接收到的数据
  76. if (uart->uarthandler == &DEBUG_UART)
  77. {
  78. at_cmd_processer_push_data(uart->rxbuf);
  79. }
  80. }
  81. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  82. {
  83. if (GPIO_Pin == GPIO_PIN_8)
  84. {
  85. *udp_client_genlock_and_esync_active_flag_ret() = true;
  86. }
  87. }
  88. int32_t compute_uint16_t_minus_the_numbers(uint16_t now, uint16_t last)
  89. {
  90. int32_t diff1 = now - last;
  91. int32_t diff2 = UINT16_MAX - abs(diff1);
  92. int32_t diffreal = 0;
  93. if (now > last)
  94. {
  95. diff2 = -diff2;
  96. }
  97. diffreal = abs(diff1) < abs(diff2) ? diff1 : diff2;
  98. return diffreal;
  99. }
  100. void netif_link_up_user_func(void)
  101. {
  102. encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_GREEN);
  103. encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_GREEN);
  104. }
  105. void netif_link_down_user_func(void)
  106. {
  107. encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_RED);
  108. encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_RED);
  109. }
  110. static void encoder_get_and_calculation(void)
  111. {
  112. encoder_get_temp = __HAL_TIM_GET_COUNTER(encoder_get_camera_encoder_structer()->tim_handler);
  113. if (encoder_get_camera_encoder_structer()->last_count != encoder_get_temp)
  114. {
  115. encoder_get_camera_encoder_structer()->count += compute_uint16_t_minus_the_numbers(encoder_get_temp, encoder_get_camera_encoder_structer()->last_count);
  116. encoder_get_camera_encoder_structer()->last_count = encoder_get_temp;
  117. }
  118. encoder_get_temp = __HAL_TIM_GET_COUNTER(encoder_get_driven_encoder_gear_structer()->tim_handler);
  119. if (encoder_get_driven_encoder_gear_structer()->last_count != encoder_get_temp)
  120. {
  121. encoder_get_driven_encoder_gear_structer()->count += compute_uint16_t_minus_the_numbers(encoder_get_temp, encoder_get_driven_encoder_gear_structer()->last_count);
  122. encoder_get_driven_encoder_gear_structer()->last_count = encoder_get_temp;
  123. }
  124. }
  125. /* 重写了该函数,需要添加编码器采集部分的逻辑 */
  126. void HAL_IncTick(void)
  127. {
  128. uwTick += uwTickFreq;
  129. encoder_get_and_calculation();
  130. }
  131. void user_main()
  132. {
  133. printf("==============ethernet_sound_acquisition_card=============\r\n");
  134. printf("version %d.%d", VERSION_MAIN_ID, VERSION_SUB_ID);
  135. encoder_all_start();
  136. udp_client_init();
  137. zkey_init(&s_key_module);
  138. port_uart_start_all_uart_receive();
  139. while (1)
  140. {
  141. udp_client_genlock_and_esync_active();
  142. udp_client_active();
  143. zkey_schedule();
  144. udp_client_recv();
  145. at_cmd_processer_try_process_data();
  146. encoder_light_schedule();
  147. port_do_debug_light_state();
  148. osDelay(1);
  149. }
  150. }