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.

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