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.

182 lines
5.0 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 bool netif_link_status;
  31. static bool camera_encoder_raw_count_update_flag;
  32. static bool driven_encoder_gear_raw_count_update_flag;
  33. static void zkey_schedule()
  34. {
  35. static uint32_t lastprocess_key_ticket;
  36. if (sys_haspassedms(lastprocess_key_ticket) >= KEY_SCAN_PERIOD)
  37. {
  38. zkey_do_loop_in_each_period(NULL);
  39. lastprocess_key_ticket = HAL_GetTick();
  40. }
  41. }
  42. static void onkey(zkey_t *key, zkey_state_t key_state)
  43. {
  44. if (key_state == zks_rising_edge)
  45. {
  46. if (strcmp(key->name, CHEAR_ENCODER_1_KEY_NAME) == 0)
  47. {
  48. encoder_switch_clear_counter(CAMERA_ENCODER);
  49. }
  50. else if (strcmp(key->name, CHEAR_ENCODER_2_KEY_NAME) == 0)
  51. {
  52. encoder_switch_clear_counter(DRIVEN_ENCODER_GEAR);
  53. }
  54. }
  55. if (key_state == zks_keep)
  56. {
  57. if (key->keep_state_count == s_key_long_press_time_ms / KEY_SCAN_PERIOD)
  58. {
  59. // 触发长按事件
  60. key->hasProcessed = true;
  61. }
  62. }
  63. else if (key_state == zks_falling_edge)
  64. {
  65. /**
  66. * @brief
  67. */
  68. if (!key->hasProcessed)
  69. {
  70. }
  71. }
  72. }
  73. /**
  74. * @brief
  75. */
  76. void port_mock_on_uart_rx(uart_t *uart)
  77. {
  78. // 处理指令串口接收到的数据
  79. if (uart->uarthandler == &DEBUG_UART)
  80. {
  81. at_cmd_processer_push_data(uart->rxbuf);
  82. }
  83. }
  84. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  85. {
  86. if (GPIO_Pin == GPIO_PIN_8)
  87. {
  88. *udp_client_genlock_and_esync_active_flag_ret() = true;
  89. }
  90. }
  91. int32_t compute_uint16_t_minus_the_numbers(uint16_t now, uint16_t last)
  92. {
  93. int32_t diff1 = now - last;
  94. int32_t diff2 = UINT16_MAX - abs(diff1);
  95. int32_t diffreal = 0;
  96. if (now > last)
  97. {
  98. diff2 = -diff2;
  99. }
  100. diffreal = abs(diff1) < abs(diff2) ? diff1 : diff2;
  101. return diffreal;
  102. }
  103. void netif_link_up_user_func(void)
  104. {
  105. encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_GREEN);
  106. encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_GREEN);
  107. netif_link_status = true;
  108. }
  109. void netif_link_down_user_func(void)
  110. {
  111. encoder_light_switch_set_color(CAMERA_ENCODER, ENCODER_LIGHT_COLOR_RED);
  112. encoder_light_switch_set_color(DRIVEN_ENCODER_GEAR, ENCODER_LIGHT_COLOR_RED);
  113. netif_link_status = false;
  114. }
  115. static void encoder_get_and_calculation(void)
  116. {
  117. encoder_get_temp = __HAL_TIM_GET_COUNTER(encoder_get_camera_encoder_structer()->tim_handler);
  118. if (encoder_get_camera_encoder_structer()->last_count != encoder_get_temp)
  119. {
  120. encoder_get_camera_encoder_structer()->count += compute_uint16_t_minus_the_numbers(encoder_get_temp, encoder_get_camera_encoder_structer()->last_count);
  121. encoder_get_camera_encoder_structer()->last_count = encoder_get_temp;
  122. camera_encoder_raw_count_update_flag = true;
  123. }
  124. encoder_get_temp = __HAL_TIM_GET_COUNTER(encoder_get_driven_encoder_gear_structer()->tim_handler);
  125. if (encoder_get_driven_encoder_gear_structer()->last_count != encoder_get_temp)
  126. {
  127. encoder_get_driven_encoder_gear_structer()->count += compute_uint16_t_minus_the_numbers(encoder_get_temp, encoder_get_driven_encoder_gear_structer()->last_count);
  128. encoder_get_driven_encoder_gear_structer()->last_count = encoder_get_temp;
  129. driven_encoder_gear_raw_count_update_flag = true;
  130. }
  131. }
  132. /* 重写了该函数,需要添加编码器采集部分的逻辑 */
  133. void HAL_IncTick(void)
  134. {
  135. uwTick += uwTickFreq;
  136. encoder_get_and_calculation();
  137. }
  138. void user_main()
  139. {
  140. printf("==============ethernet_sound_acquisition_card=============\r\n");
  141. printf("version %d.%d", VERSION_MAIN_ID, VERSION_SUB_ID);
  142. encoder_all_start();
  143. udp_client_init();
  144. zkey_init(&s_key_module);
  145. port_uart_start_all_uart_receive();
  146. while (1)
  147. {
  148. udp_client_genlock_and_esync_active();
  149. udp_client_active();
  150. zkey_schedule();
  151. udp_client_recv();
  152. at_cmd_processer_try_process_data();
  153. encoder_light_schedule(netif_link_status, &camera_encoder_raw_count_update_flag, &driven_encoder_gear_raw_count_update_flag);
  154. port_do_debug_light_state();
  155. osDelay(1);
  156. }
  157. }