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.

463 lines
17 KiB

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