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.

443 lines
16 KiB

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
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
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
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
  1. #include <stdbool.h> //定义布尔
  2. #include <string.h>
  3. #include "board.h"
  4. //
  5. #include "zes8p5066lib/basic.h"
  6. #include "zes8p5066lib/gpio.h"
  7. #include "zes8p5066lib/key.h"
  8. #include "zes8p5066lib/systicket.h"
  9. #include "zes8p5066lib/uart0.h"
  10. //
  11. #include "service/human_computer_interaction_service.h"
  12. #include "service/ozone_control_service.h"
  13. #include "service/thisdevice.h"
  14. #include "test.h"
  15. #include "zsimple_timer/zsimple_timer.h"
  16. /***********************************************************************************************************************
  17. * =========================================================================================================== *
  18. ***********************************************************************************************************************/
  19. void onkey(zkey_t* key, zkey_state_t key_state);
  20. /***********************************************************************************************************************
  21. * =========================================================================================================== *
  22. ***********************************************************************************************************************/
  23. static zkey_t s_keys[] = {
  24. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  25. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  26. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  27. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  28. };
  29. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  30. /***********************************************************************************************************************
  31. * =============================================================================================================== *
  32. ***********************************************************************************************************************/
  33. /***********************************************************************************************************************
  34. * ===================================================COUNT_COMPUTE=================================================== *
  35. ***********************************************************************************************************************/
  36. static uint32_t compute_countdown_num(int countdowns) { return countdowns / kconst_countdown_step_s + !!(countdowns % kconst_countdown_step_s); }
  37. static void increase_and_assign_countdonwnum() {
  38. if (thisDevice.countdonwnum == 4) {
  39. thisDevice.countdonwnum = 0;
  40. thisDevice.countdonwnum_s = 0;
  41. thisDevice.countdonw_setting_num = 0;
  42. } else {
  43. thisDevice.countdonwnum_s = (thisDevice.countdonwnum + 1) * kconst_countdown_step_s;
  44. thisDevice.countdonwnum = thisDevice.countdonwnum + 1;
  45. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  46. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  47. }
  48. }
  49. static void mf_set_countdown(int countdownnum) {
  50. thisDevice.countdonwnum = countdownnum;
  51. thisDevice.countdonwnum_s = thisDevice.countdonwnum * kconst_countdown_step_s;
  52. thisDevice.countdonw_setting_num = thisDevice.countdonwnum;
  53. thisDevice.countdonw_start_ticket = systicket_get_now_ms();
  54. }
  55. /***********************************************************************************************************************
  56. * ===================================================POWER_CONTROL=================================================== *
  57. ***********************************************************************************************************************/
  58. //设备开始工作
  59. static void startwork() {
  60. port_fan_set(true);
  61. ozone_control_start();
  62. thisDevice.working = true;
  63. }
  64. //停止设备工作
  65. static void stopwork() {
  66. ozone_control_stop();
  67. port_fan_set(false);
  68. thisDevice.working = false;
  69. }
  70. //关机
  71. static void shutdwon() {
  72. printf("power off\n");
  73. thisDevice.poweron = false;
  74. stopwork();
  75. }
  76. //开机
  77. static void poweron() {
  78. printf("power on\n");
  79. thisDevice.poweron = true;
  80. thisDevice.level = klevel2;
  81. thisDevice.mode = knormal;
  82. thisDevice.error = knoneException;
  83. startwork();
  84. }
  85. /***********************************************************************************************************************
  86. * =================================================KEY_EVENT_PROCESS================================================= *
  87. ***********************************************************************************************************************/
  88. //电源按键处理
  89. static void mf_process_poweron_key(zkey_t* key) {
  90. printf("on %s \n", key->name);
  91. if (!thisDevice.poweron) {
  92. poweron();
  93. } else {
  94. shutdwon();
  95. }
  96. return;
  97. }
  98. //等级按键处理
  99. static void mf_process_level_key(zkey_t* key) {
  100. if (!thisDevice.poweron) return;
  101. if (!thisDevice.error != knoneException) return;
  102. printf("on %s \n", key->name);
  103. /**
  104. * @brief
  105. */
  106. if (thisDevice.level == klevel1) {
  107. printf("changet level to level2\n");
  108. thisDevice.level = klevel2;
  109. //更改臭氧状态
  110. } else if (thisDevice.level == klevel2) {
  111. printf("changet level to level1\n");
  112. thisDevice.level = klevel1;
  113. //更改臭氧状态
  114. }
  115. hcis_active_input(kchange_level_input);
  116. return;
  117. }
  118. //定时按键处理
  119. static void mf_process_timer_key(zkey_t* key) {
  120. if (!thisDevice.poweron) return;
  121. if (!thisDevice.error != knoneException) return;
  122. printf("on %s \n", key->name);
  123. if (!thisDevice.working) {
  124. startwork();
  125. }
  126. if (thisDevice.mode != ktimingMode) {
  127. mf_set_countdown(0);
  128. }
  129. increase_and_assign_countdonwnum();
  130. if (thisDevice.countdonwnum_s == 0) {
  131. hcis_active_input(knone_active);
  132. thisDevice.mode = knormal;
  133. } else {
  134. hcis_active_input(kchange_countdonw_time_input);
  135. thisDevice.mode = ktimingMode;
  136. }
  137. }
  138. //间隔按键处理
  139. static void mf_process_interval_key(zkey_t* key) {
  140. if (!thisDevice.poweron) return;
  141. if (!thisDevice.error != knoneException) return;
  142. printf("on %s \n", key->name);
  143. if (!thisDevice.working) {
  144. startwork();
  145. }
  146. if (thisDevice.mode != kintermittentMode) {
  147. mf_set_countdown(0);
  148. }
  149. increase_and_assign_countdonwnum();
  150. if (thisDevice.countdonwnum_s == 0) {
  151. hcis_active_input(knone_active);
  152. thisDevice.mode = knormal;
  153. } else {
  154. hcis_active_input(kchange_intermittentmode_time_input);
  155. thisDevice.mode = kintermittentMode;
  156. }
  157. }
  158. static void onkey(zkey_t* key, zkey_state_t key_state) {
  159. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  160. mf_process_poweron_key(key);
  161. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  162. mf_process_level_key(key);
  163. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  164. mf_process_timer_key(key);
  165. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  166. mf_process_interval_key(key);
  167. }
  168. }
  169. /***********************************************************************************************************************
  170. * ===============================================THIS_MODULE_SCHEDULE================================================ *
  171. ***********************************************************************************************************************/
  172. static void mf_try_autoshutdown() {
  173. if (thisDevice.poweron) {
  174. if (thisDevice.countdonwnum_s == 0) {
  175. shutdwon();
  176. }
  177. }
  178. }
  179. static void mf_try_autostop() {
  180. //间歇模式下,定时到达定时时间,停止工作
  181. if (thisDevice.countdonwnum_s == 0) stopwork();
  182. }
  183. static void mf_try_auto_restart() {
  184. //间歇模式下,设备工作一定时间后,休息一定时间
  185. if (systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000 > //
  186. (/*设备工作时间:*/ thisDevice.countdonw_setting_num * kconst_countdown_step_s + //
  187. /*设备休息时间*/ thisDevice.countdonw_setting_num * kconst_countdown_step_s)) {
  188. mf_set_countdown(thisDevice.countdonw_setting_num);
  189. startwork();
  190. }
  191. }
  192. static void mf_update_countdown() {
  193. if /* */ (thisDevice.mode == knormal) {
  194. thisDevice.countdonwnum = 0;
  195. thisDevice.countdonwnum_s = 0;
  196. } else if (thisDevice.mode == ktimingMode || thisDevice.mode == kintermittentMode) {
  197. if (thisDevice.countdonwnum_s != 0) {
  198. thisDevice.countdonwnum_s = //
  199. thisDevice.countdonw_setting_num * kconst_countdown_step_s - systicket_haspassedms(thisDevice.countdonw_start_ticket) / 1000;
  200. thisDevice.countdonwnum = compute_countdown_num(thisDevice.countdonwnum_s);
  201. }
  202. }
  203. }
  204. void this_module_schedule() {
  205. if (thisDevice.error != knoneException) {
  206. return;
  207. }
  208. /**
  209. * @brief
  210. */
  211. if (thisDevice.mode == ktimingMode) {
  212. //定时自动关机
  213. mf_update_countdown();
  214. mf_try_autoshutdown();
  215. }
  216. /**
  217. * @brief ,
  218. */
  219. if (thisDevice.mode == kintermittentMode) {
  220. mf_update_countdown();
  221. if (thisDevice.working) {
  222. mf_try_autostop();
  223. } else {
  224. mf_try_auto_restart();
  225. }
  226. }
  227. };
  228. /***********************************************************************************************************************
  229. * =======================================================MAIN======================================================== *
  230. ***********************************************************************************************************************/
  231. static void mf_do_debug_light_state() {
  232. static uint8_t debug_led_state = 1;
  233. debug_led_state = !debug_led_state;
  234. port_debug_set(debug_led_state);
  235. }
  236. static void mf_init_all_subdevice_state() {
  237. port_debug_set(false);
  238. port_fan_set(false);
  239. port_led0_set(false);
  240. port_led1_set(false);
  241. port_led2_set(false);
  242. port_led3_set(false);
  243. port_led_r_set(false);
  244. port_led_g_set(false);
  245. port_led_b_set(false);
  246. }
  247. void WDTInit(void) {
  248. IWDT_InitStruType x;
  249. x.WDT_Tms = 10000;
  250. x.WDT_IE = Enable; /* IWDT中断使能 */
  251. x.WDT_Rst = Enable; /* IWDT复位使能 */
  252. x.WDT_Clock = IWDT_CLOCK_WDT; /* LRC */
  253. IWDT_Init(&x);
  254. /* 使能IWDT */
  255. IWDT_Enable();
  256. }
  257. // static float mf_get_ozone_generator_power() {
  258. // float fanpower = 0;
  259. // for (size_t i = 0; i < 20; i++) {
  260. // fanpower += port_adc_get_ozone_generator_power();
  261. // }
  262. // return fanpower / 20;
  263. // }
  264. static const char* errorToStr(error_t exception) {
  265. if (exception == knoneException) {
  266. return "kNoError";
  267. }
  268. if (exception == kOzonePrimaryCircuitAnomaly) {
  269. return "kOzonePrimaryCircuitAnomaly";
  270. }
  271. if (exception == kOzoneSecondaryCircuitAnomaly) {
  272. return "kOzoneSecondaryCircuitAnomaly";
  273. }
  274. if (exception == kfanIsBroken) {
  275. return "kfanIsBroken";
  276. }
  277. if (exception == kPowerOutOfControl) {
  278. return "kPowerOutOfControl";
  279. }
  280. return "kUnknownException";
  281. }
  282. void trigger_exception(error_t exception) {
  283. thisDevice.error = exception;
  284. printf("======================DeviceSnapshot==========================\n");
  285. /*Exception*/
  286. printf("= Exception :\n");
  287. printf("=\terror :%d\n", errorToStr(exception));
  288. /*Power*/
  289. printf("= Power :\n");
  290. printf("=\tfanpower :%f\n", ozone_control_get_fan_power());
  291. printf("=\tozonepower :%f\n", ozone_control_get_ozone_power());
  292. /*ThisDevice*/
  293. printf("= ThisDevice :\n");
  294. printf("=\tworking :%d\n", thisDevice.working);
  295. printf("=\tmode :%d\n", thisDevice.mode);
  296. printf("=\tlevel :%d\n", thisDevice.level);
  297. printf("=\tpoweron :%d\n", thisDevice.poweron);
  298. printf("=\tcountdonwnum :%d\n", thisDevice.countdonwnum);
  299. printf("=\tcountdonwnum_s :%d\n", thisDevice.countdonwnum_s);
  300. printf("=\tcountdonw_setting_num :%d\n", thisDevice.countdonw_setting_num);
  301. printf("=\tcountdonw_start_ticket :%d\n", thisDevice.countdonw_start_ticket);
  302. printf("=\tactive_input :%d\n", thisDevice.active_input);
  303. printf("=\tactive_start_ticket :%d\n", thisDevice.active_start_ticket);
  304. /*ozone_control*/
  305. printf("= OZONE CONTROL :\n");
  306. printf("=\tworking_state_id :%d\n", ozone_control_get_working_state_id());
  307. printf("=\tnowfreq :%d\n", ozone_control_get_working_state()->nowfreq);
  308. printf("=\tchangefreqdirection :%d\n", ozone_control_get_working_state()->changefreqdirection);
  309. printf("=\tadjustedToTheProperPower:%d\n", ozone_control_get_working_state()->adjustedToTheProperPower);
  310. printf("=\tresonant_frequency :%d\n", ozone_control_get_working_state()->resonant_frequency);
  311. printf("=\tslope_when_freq40k :%f\n", ozone_control_get_working_state()->slope_when_freq40k);
  312. printf("=\tavarage_power :%f\n", ozone_control_get_working_state()->avarage_power);
  313. printf("\n");
  314. // reset device
  315. thisDevice.mode = knormal;
  316. stopwork();
  317. }
  318. void exception_monitor_schedule() {
  319. if (!thisDevice.poweron) {
  320. return;
  321. }
  322. if (thisDevice.error != knoneException) {
  323. return;
  324. }
  325. // printf("FanPower:%f,OzonePower:%f\n", fan_get_power(), mf_get_ozone_generator_power());
  326. /*****************************************************************************************************************
  327. * ================================================================================================= *
  328. *****************************************************************************************************************/
  329. if (port_fan_get()) {
  330. float fanpower = ozone_control_get_fan_power();
  331. if (fanpower > 3 || fanpower < 0.2) {
  332. printf("fanpower error: %f\n", fanpower);
  333. trigger_exception(kfanIsBroken);
  334. return;
  335. }
  336. }
  337. /*****************************************************************************************************************
  338. * ========================================================================================= *
  339. *****************************************************************************************************************/
  340. if (ozone_control_get_working_state_id() == kWorkingState) {
  341. /**
  342. * @brief
  343. */
  344. if (ozone_control_get_working_state()->avarage_power < 0.2) {
  345. trigger_exception(kOzonePrimaryCircuitAnomaly);
  346. return;
  347. }
  348. /**
  349. * @brief
  350. */
  351. if (ozone_control_get_working_state()->slope_when_freq40k < -0.00200) {
  352. trigger_exception(kOzoneSecondaryCircuitAnomaly);
  353. return;
  354. }
  355. /**
  356. * @brief
  357. * ,,
  358. */
  359. if (ozone_control_get_ozone_power() > (ozone_control_get_expect_power() + const_expect_power_windonws) && //
  360. ozone_control_get_working_state()->nowfreq == ozone_control_get_working_state()->resonant_frequency) {
  361. trigger_exception(kPowerOutOfControl);
  362. return;
  363. }
  364. }
  365. }
  366. int main(void) {
  367. SystemInit(); //配置系统时钟
  368. DeviceClockAllEnable(); //打开所有外设时钟
  369. systicket_init();
  370. /*系统初始化*/
  371. zgpio_init_all_gpio(); //
  372. port_init(); //
  373. mf_init_all_subdevice_state(); //
  374. printf("==========OZONE_GENERATOR==========\n"); //
  375. printf("= manufactor: iflytop\n"); //
  376. printf("= version : %s\n", VERSION); //
  377. printf("=\n"); //
  378. // WDTInit();
  379. /*组件初始化*/
  380. zkey_init(&key_module); //按键初始化
  381. ozone_control_init();
  382. /**
  383. * @brief
  384. * 20k起步50hz100ms计算功率
  385. */
  386. while (true) {
  387. //按键扫描逻辑
  388. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  389. END();
  390. //调试指示灯
  391. DO_IT_EACH_MS(150) { mf_do_debug_light_state(); }
  392. END();
  393. //臭氧控制逻辑Schedule
  394. ozone_control_schedule();
  395. //人机交互逻辑Schedule
  396. hcis_shcedule();
  397. //当前模块逻辑Schedule
  398. this_module_schedule();
  399. DO_IT_EACH_MS(3000) { exception_monitor_schedule(); }
  400. END();
  401. //喂狗
  402. // if (0x01 == IWDT_GetFlagStatus()) IWDT_Clear();
  403. }
  404. }