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.

305 lines
12 KiB

4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "main.h"
  2. #include <stdbool.h> //���岼��
  3. #include "board.h"
  4. /***********************************************************************************************************************
  5. * =========================================================================================================== *
  6. ***********************************************************************************************************************/
  7. void onkey(zkey_t *key, zkey_state_t key_state);
  8. /***********************************************************************************************************************
  9. * =====================================================ȫֱ====================================================== *
  10. ***********************************************************************************************************************/
  11. static zkey_t s_keys[] = {
  12. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state),
  13. ZKEY_INIT("gearskey", port_gpio_get_gears_key_state),
  14. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state),
  15. ZKEY_INIT("switchkey", port_gpio_get_switch_key_state),
  16. };
  17. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  18. /***********************************************************************************************************************
  19. * =====================================================ȫ״̬====================================================== *
  20. ***********************************************************************************************************************/
  21. static bool error_even_trigger_after_stop_ozone_work_state; //�����¼�������ֹͣ����������־λ
  22. // ����
  23. bool g_setting_interval_work_flag = false; //���ڵĵ�λ
  24. WORK_LEVEL_T g_setting_level = WORK_LEVEL_LOW; //�ػ�ǰ�ĵ�λ
  25. //��־λ
  26. bool g_power_flag = false; //���ػ���־λ
  27. static int g_error_num;
  28. bool g_auto_shutdown_flag = false; //��ʱ����ʹ�ܱ�־λ
  29. static uint32_t g_auto_shutdown_countdown_s; //��ʱʱ��
  30. //����
  31. static bool pwm_modble_enable_falg; //Ӳ��pwmʹ�ܱ�־λ
  32. /***********************************************************************************************************************
  33. * =======================================================HOOK======================================================== *
  34. ***********************************************************************************************************************/
  35. void HOOK_pwm_module_set_pwm_duty(uint32_t frequency, uint32_t duty) {
  36. set_pwm_modbul_freq_duty(frequency, duty);
  37. port_fan_set(true); //�򿪷���
  38. pwm_modble_enable_falg = true;
  39. printf("work\r\n");
  40. }
  41. void HOOK_pwm_stop(void) {
  42. set_pwm_modbul_freq_duty(1, 0); //�ر�PWM����
  43. port_fan_set(false); //���Ƚ�������
  44. pwm_modble_enable_falg = false;
  45. printf("rest\r\n");
  46. }
  47. bool HOOK_pwm_is_enable(void) { return pwm_modble_enable_falg; }
  48. int hook_get_autoshutdown_timecount() { return g_auto_shutdown_countdown_s; }
  49. /***********************************************************************************************************************
  50. * =================================================INTERNAL_FUNCTION================================================= *
  51. ***********************************************************************************************************************/
  52. /**
  53. * @brief ݵλpwmrgbɫ
  54. *
  55. * @param gears
  56. */
  57. void update_ozone_work_level(bool interval_work, WORK_LEVEL_T level) {
  58. printf("update_ozone_work_level(interval_work:%d,level:%d)\n", interval_work, level);
  59. rgb_light_mode_t lightmode;
  60. int duty = 0;
  61. if (level == WORK_LEVEL_CLOSE) {
  62. duty = 0;
  63. lightmode = RGB_CLOSE;
  64. } else if (level == WORK_LEVEL_LOW) {
  65. duty = 25;
  66. lightmode = RGB_COLOR_GERRN;
  67. } else if (level == WORK_LEVEL_MIDDLE) {
  68. duty = 50;
  69. lightmode = RGB_COLOR_BLUE;
  70. } else if (level == WORK_LEVEL_HIGHT) {
  71. duty = 100;
  72. lightmode = RGB_COLOR_RED;
  73. }
  74. if (interval_work) {
  75. ozone_pwm_control_module_set_pwm_output_2(1, 0, INTERVAL_WORK_PERIOD_MS,INTERVAL_WORK_DUTY);
  76. } else {
  77. set_pwm_modbul_freq_duty(1, duty);
  78. }
  79. light_module_set_rgb_mode(lightmode);
  80. light_module_set_rgb_flicker_mode(interval_work);
  81. }
  82. /**
  83. * @brief ػ
  84. */
  85. void shutdown(void) {
  86. printf("shutdown\n");
  87. g_power_flag = false;
  88. g_error_num = 0;
  89. g_auto_shutdown_flag = false;
  90. g_auto_shutdown_countdown_s = 0;
  91. update_ozone_work_level(false, WORK_LEVEL_CLOSE);
  92. port_fan_set(false); //�رշ���
  93. light_module_close_all_light();
  94. }
  95. /***********************************************************************************************************************
  96. * ======================================================Ź======================================================= *
  97. ***********************************************************************************************************************/
  98. void iwdt_init(void) {
  99. /**
  100. * @brief Źܣ0ʱڼ+1
  101. * ڼΪ2ʱ򣬲ж
  102. * ڼΪ4֮ǰûнιλ
  103. */
  104. IWDT_InitStruType x;
  105. x.WDT_Tms = 4000;
  106. x.WDT_IE = Enable; /* IWDT�ж�ʹ�� */
  107. x.WDT_Rst = Enable; /* IWDT��λʹ�� */
  108. x.WDT_Clock = IWDT_CLOCK_WDT; /* LRC */
  109. IWDT_Init(&x);
  110. /* ʹ��IWDT */
  111. IWDT_Enable();
  112. }
  113. void feed_iwdt(void) {
  114. //�
  115. if (0x01 == IWDT_GetFlagStatus()) {
  116. IWDT_Clear();
  117. // printf("�\r\n");
  118. }
  119. }
  120. void onkey(zkey_t *key, zkey_state_t key_state) {
  121. if (key->hasProcessed) return;
  122. /**
  123. * @brief ػʱֻԴ
  124. */
  125. if (!g_power_flag) {
  126. if (key == &s_keys[3] && key->cur_state == zks_falling_edge) {
  127. key->hasProcessed = true;
  128. printf("key zks_rising_edge\r\n");
  129. /**
  130. * @brief
  131. * 1.ñ־λ
  132. * 2.򿪷
  133. * 3.򿪳Ӧָʾ
  134. */
  135. g_power_flag = true; //���ػ���־λ
  136. g_error_num = 0;
  137. g_auto_shutdown_flag = false; //��ʱ����ʹ�ܱ�־λ
  138. g_auto_shutdown_countdown_s = 0; //��ʱʱ��
  139. port_fan_set(true); //����������
  140. update_ozone_work_level(g_setting_interval_work_flag, g_setting_level);
  141. }
  142. return;
  143. }
  144. /**
  145. * @brief ʱ˵ԴаʧЧ
  146. */
  147. if (g_error_num != 0) { //�豸�����쳣
  148. if (key == &s_keys[3] && key->cur_state == zks_falling_edge) {
  149. key->hasProcessed = true;
  150. printf("process key[2] event\r\n");
  151. /**
  152. * @brief ػ
  153. */
  154. shutdown();
  155. }
  156. return;
  157. }
  158. if (key == &s_keys[0] && key->cur_state == zks_falling_edge) //��3s
  159. {
  160. printf("process key[0] event\r\n");
  161. /**
  162. * @brief ʱ¼
  163. */
  164. if (!g_auto_shutdown_flag) {
  165. g_auto_shutdown_flag = true;
  166. g_auto_shutdown_countdown_s = 0;
  167. }
  168. g_auto_shutdown_countdown_s += AUTO_SHUTDOWN_ONE_LIGHT_EQ_TIME_S;
  169. } else if (key == &s_keys[1] && key->cur_state == zks_falling_edge) {
  170. /**
  171. * @brief ȼ
  172. */
  173. printf("process key[1] event\r\n");
  174. if (g_setting_level == WORK_LEVEL_HIGHT) {
  175. update_ozone_work_level(g_setting_interval_work_flag, WORK_LEVEL_LOW);
  176. } else if (g_setting_level == WORK_LEVEL_MIDDLE) {
  177. update_ozone_work_level(g_setting_interval_work_flag, WORK_LEVEL_HIGHT);
  178. } else if (g_setting_level == WORK_LEVEL_LOW) {
  179. update_ozone_work_level(g_setting_interval_work_flag, WORK_LEVEL_MIDDLE);
  180. }
  181. } else if (key == &s_keys[2] && key->cur_state == zks_falling_edge) {
  182. printf("process key[2] event\r\n");
  183. /**
  184. * @brief Ъ¼
  185. */
  186. g_setting_interval_work_flag = !g_setting_interval_work_flag;
  187. update_ozone_work_level(g_setting_interval_work_flag, g_setting_level);
  188. } else if (key == &s_keys[3] && key->cur_state == zks_falling_edge) {
  189. printf("process key[2] event\r\n");
  190. /**
  191. * @brief ػ
  192. */
  193. shutdown();
  194. }
  195. }
  196. /***********************************************************************************************************************
  197. * =============================================================================================================
  198. ***********************************************************************************************************************/
  199. int main(void) {
  200. HRC_Config(Enable, SCU_HRC_48M, Enable); //ʱ��ԴSCU_CLK_HRC
  201. SystemInit();
  202. DeviceClockAllEnable(); //������������ʱ��
  203. User_SysTickInit(); //�δ���ʱ����ʼ��Ϊ(����Ϊ1ms�ж�)
  204. SysTick_Enable();
  205. //Ӳ����ʼ��
  206. unused_gpio_init();
  207. gpio_init();
  208. uart0_init();
  209. t16_pa4_init();
  210. //ģ����ʼ��
  211. zkey_init(&key_module);
  212. //�ϵ�Ĭ�Ϲػ�
  213. shutdown();
  214. //��������
  215. printf("Initialization completed\r\n");
  216. while (true) {
  217. /*******************************************************************************************************************
  218. * ==================================================ָʾ=================================================== *
  219. *******************************************************************************************************************/
  220. port_do_debug_light_state();
  221. /***********************************************************************************************************************
  222. * =================================================ģȴ================================================== *
  223. ***********************************************************************************************************************/
  224. {
  225. static uint32_t keylastprocess = 0;
  226. if (port_haspassedms(keylastprocess) > KEY_PERIOD) {
  227. keylastprocess = get_sys_ticket();
  228. zkey_do_loop_in_each_period(NULL);
  229. }
  230. }
  231. /***********************************************************************************************************************
  232. * ===============================================ƹģȴ================================================ *
  233. ***********************************************************************************************************************/
  234. light_module_schedule();
  235. /***********************************************************************************************************************
  236. * ===============================================ģȴ================================================ *
  237. ***********************************************************************************************************************/
  238. ozone_pwm_control_module_loop();
  239. /***********************************************************************************************************************
  240. * ===================================================Զػ߼==================================================== *
  241. ***********************************************************************************************************************/
  242. //
  243. if (g_power_flag && g_auto_shutdown_flag) {
  244. static uint32_t ticket = 0;
  245. ticket = get_sys_ticket();
  246. //
  247. if (port_haspassedms(ticket) > 1000) {
  248. ticket = get_sys_ticket();
  249. if (g_auto_shutdown_countdown_s > 0) {
  250. g_auto_shutdown_countdown_s -= 1;
  251. } else {
  252. g_auto_shutdown_flag = false;
  253. /**
  254. * @brief ʱػ
  255. */
  256. shutdown();
  257. }
  258. }
  259. //
  260. }
  261. /***********************************************************************************************************************
  262. * ===================================================߼====================================================
  263. ***********************************************************************************************************************/
  264. {
  265. static uint32_t ticket = 0;
  266. if (port_haspassedms(ticket) > EXCEPTION_CHECK_PERIOD) {
  267. ticket = get_sys_ticket();
  268. /**
  269. * @brief
  270. * ADCֵǷ񳬳Χ
  271. */
  272. if (false) {
  273. }
  274. }
  275. }
  276. // End..................................
  277. }
  278. }