button.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. #include <rtthread.h>
  2. #include "rtc.h"
  3. #include "flash.h"
  4. #include "main.h"
  5. #include "ad.h"
  6. #include "plan.h"
  7. #include "button.h"
  8. #include "buffer.h"
  9. #include "display.h"
  10. #include "xBeeAppEscapingMode.h"
  11. rt_sem_t thread_beep_sem = RT_NULL;
  12. int WeightConst_JZ = 0;
  13. uint8_t WeightConst_JZ_ = 0;
  14. uint32_t WeightConst_JZZ = 0; //校正称量的当前位
  15. uint8_t WeightConst_JZZ_ = 8; //校正称量的当前位
  16. uint32_t WeightConst_ZD = 3000; //最大称量值
  17. uint8_t WeightConst_ZD_ = 8; //最大称量的当前位
  18. uint8_t CanButton=0;
  19. struct button_state_t button_state = {1,1,1,1,1,1,1};
  20. //static rt_uint8_t MenuItem__=0; //当前菜单值序号
  21. extern unsigned char iscowweight;
  22. void sendshowCC(void)
  23. {
  24. XbeeConst_FRemoteTxBuf[0] = 7;
  25. XbeeConst_FRemoteTxBuf[1] = 0x7E;
  26. XbeeConst_FRemoteTxBuf[2] = 0x05;
  27. XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress;
  28. XbeeConst_FRemoteTxBuf[4] = 0xB2;
  29. XbeeConst_FRemoteTxBuf[5] = XbeeConst_UseAPI;
  30. XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;
  31. HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR3, XbeeConst_FRemoteTxBuf[6]);
  32. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
  33. for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
  34. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
  35. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
  36. }
  37. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];
  38. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  39. setSendFrame_t(0x01,0x01,0xC1);
  40. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
  41. for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
  42. usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];
  43. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  44. rt_sem_release(uart2_lock);
  45. }
  46. static void CheckMenuButton_entry(void *parameter) {
  47. extern uint8_t isBLE;
  48. while(1)
  49. {
  50. if (button_state.b1>0) button_state.b1 ++;
  51. if (button_state.b2>0) button_state.b2 ++;
  52. if (button_state.b3>0) button_state.b3 ++;
  53. if (button_state.b4>0) button_state.b4 ++;
  54. if (button_state.b5>0) button_state.b5 ++;
  55. if (button_state.b7>0) button_state.b7 ++;
  56. if (button_state.b1>=5) button_state.b1 = 0;
  57. if (button_state.b2>=5) button_state.b2 = 0;
  58. if (button_state.b3>=5) button_state.b3 = 0;
  59. if (button_state.b4>=5) button_state.b4 = 0;
  60. if (button_state.b5>=5) button_state.b5 = 0;
  61. if (button_state.b7>=15) button_state.b7 = 0;
  62. // if (HAL_GPIO_ReadPin(pwr_GPIO_Port, pwr_Pin) == 0) {
  63. // rt_thread_mdelay(2000);
  64. // if ((HAL_GPIO_ReadPin(pwr_GPIO_Port, pwr_Pin) == 0) ) {
  65. // beep();
  66. // pwr_lock_off();
  67. // }
  68. // }
  69. // if (WeightConst_TQ==14) ad_sample=Ad_Sample1();//get_weight();
  70. if (WeightConst_SBType<5)
  71. {
  72. if ((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) > 0) && button_state.b1 == 0) {
  73. beep();
  74. if (MenuItem==0)
  75. {
  76. if (XbeeConst_DeviceAddress>1) XbeeConst_DeviceAddress--; else XbeeConst_DeviceAddress = 30;
  77. write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress));
  78. WeightConst_Weight_display[6] = XbeeConst_DeviceAddress + 0x30;
  79. }
  80. else if (MenuItem==1)
  81. {
  82. if (WeightConst_TQ>1) WeightConst_TQ--; else WeightConst_TQ = 14;
  83. write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ));
  84. }
  85. else if (MenuItem==2)
  86. {
  87. if (XbeeConst_NetAddress>0) XbeeConst_NetAddress--;
  88. else
  89. {
  90. if (WeightConst_SBType==4) XbeeConst_NetAddress=2; else XbeeConst_NetAddress=13;
  91. }
  92. setNetAddress();
  93. write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress));
  94. }
  95. else if (MenuItem==3 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
  96. {
  97. if (WeightConst_SBType>1) WeightConst_SBType--; else WeightConst_SBType=2;
  98. write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType));
  99. }
  100. else if (MenuItem==4)
  101. {
  102. if (WeightConst_ChanCheShu>1) WeightConst_ChanCheShu--; else WeightConst_ChanCheShu=30;
  103. write_Flash("CC", &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu));
  104. }
  105. else if (MenuItem==5)
  106. {
  107. if (XbeeConst_UseAPI>0) XbeeConst_UseAPI--; else {XbeeConst_UseAPI=3; if(isBLE>0) XbeeConst_UseAPI=5;}
  108. setAPI();
  109. write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));
  110. }
  111. else if (MenuItem==6)
  112. {
  113. if (WeightConst_JZZ_ == 0 && WeightConst_JZZ>0) WeightConst_JZZ--;
  114. else if (WeightConst_JZZ_==1&& WeightConst_JZZ>10) WeightConst_JZZ = WeightConst_JZZ - 10;
  115. else if (WeightConst_JZZ_==2&& WeightConst_JZZ>100) WeightConst_JZZ = WeightConst_JZZ - 100;
  116. else if (WeightConst_JZZ_==3&& WeightConst_JZZ>1000) WeightConst_JZZ = WeightConst_JZZ - 1000;
  117. else if (WeightConst_JZZ_==4 && WeightConst_JZZ>10000) WeightConst_JZZ = WeightConst_JZZ - 10000;
  118. }
  119. else if (MenuItem==7)
  120. {
  121. if (WeightConst_ZD_ == 0 & Maxval>0) { Maxval--; }
  122. else if (WeightConst_ZD_==1 & Maxval>10) { Maxval = Maxval - 10; }
  123. else if (WeightConst_ZD_==2 & Maxval>100) { Maxval = Maxval - 100; }
  124. else if (WeightConst_ZD_==3 & Maxval>1000) { Maxval = Maxval - 1000; }
  125. else if (WeightConst_ZD_==4 & Maxval>10000) { Maxval = Maxval - 10000; }
  126. }
  127. else if (MenuItem==8)
  128. {
  129. if (ad_FS>0) ad_FS--; else ad_FS=8;
  130. write_Flash("ad_FS", &ad_FS, sizeof(ad_FS));
  131. }
  132. else if (MenuItem==9)
  133. {
  134. if (ad_Point>0) ad_Point--; else ad_Point=3;
  135. write_Flash("ad_Point", &ad_Point, sizeof(ad_Point));
  136. }
  137. else if (MenuItem==10)
  138. {
  139. if (ad_Inteval== 1) ad_Inteval=5; else if (ad_Inteval==2) ad_Inteval=1;
  140. else if (ad_Inteval==5) ad_Inteval=2; else ad_Inteval=1;
  141. write_Flash("ad_Inteval", &ad_Inteval, sizeof(ad_Inteval));
  142. }
  143. else if (MenuItem==11)
  144. {
  145. if (WeightConst_BLE>0) WeightConst_BLE --;
  146. else WeightConst_BLE = 3;
  147. write_Flash("BLE", &WeightConst_BLE, sizeof(WeightConst_BLE));
  148. //----------------------------setBT();
  149. }
  150. button_state.b1 = 1;
  151. button_state.b2 = 0;
  152. button_state.b3 = 0;
  153. button_state.b4 = 0;
  154. button_state.b5 = 0;
  155. }
  156. else if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)) && (WeightConst_SBType != 4&&WeightConst_SBType != 6) && button_state.b2 == 0) {
  157. beep();
  158. if (MenuItem==0)
  159. {
  160. if (XbeeConst_DeviceAddress<30) XbeeConst_DeviceAddress++; else XbeeConst_DeviceAddress = 1 ;
  161. write_Flash("CH", &XbeeConst_DeviceAddress, sizeof(XbeeConst_DeviceAddress));
  162. }
  163. else if (MenuItem==1)
  164. {
  165. if (WeightConst_TQ<14) WeightConst_TQ++; else WeightConst_TQ = 1 ;
  166. write_Flash("TQ", &WeightConst_TQ, sizeof(WeightConst_TQ));
  167. }
  168. else if (MenuItem==2)
  169. {
  170. if (XbeeConst_NetAddress<13 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
  171. XbeeConst_NetAddress++;
  172. else if (XbeeConst_NetAddress<2 && (WeightConst_SBType==4))
  173. XbeeConst_NetAddress++;
  174. else
  175. XbeeConst_NetAddress=0;
  176. setNetAddress();
  177. write_Flash("WL", &XbeeConst_NetAddress, sizeof(XbeeConst_NetAddress));
  178. }
  179. else if (MenuItem==3)
  180. {
  181. if (WeightConst_SBType<2 && (WeightConst_SBType!=4&&WeightConst_SBType!=6))
  182. WeightConst_SBType++;
  183. write_Flash("PM", &WeightConst_SBType, sizeof(WeightConst_SBType));
  184. }
  185. else if (MenuItem==4)
  186. {
  187. if (WeightConst_ChanCheShu<30) WeightConst_ChanCheShu++;
  188. else WeightConst_ChanCheShu=1;
  189. write_Flash("CC", &WeightConst_ChanCheShu, sizeof(WeightConst_ChanCheShu));
  190. WeightConst_Weight_display[6] = WeightConst_ChanCheShu + 0x30;
  191. }
  192. else if (MenuItem==5)
  193. {
  194. if ((XbeeConst_UseAPI<3 && isBLE==0) || (XbeeConst_UseAPI<5 && isBLE>0)) XbeeConst_UseAPI++; else XbeeConst_UseAPI=0;
  195. setAPI();
  196. write_Flash("API", &XbeeConst_UseAPI, sizeof(XbeeConst_UseAPI));
  197. }
  198. else if (MenuItem==6)
  199. {
  200. if (WeightConst_JZZ_ == 0) WeightConst_JZZ++;
  201. else if (WeightConst_JZZ_==1) WeightConst_JZZ = WeightConst_JZZ + 10;
  202. else if (WeightConst_JZZ_==2) WeightConst_JZZ = WeightConst_JZZ + 100;
  203. else if (WeightConst_JZZ_==3) WeightConst_JZZ = WeightConst_JZZ + 1000;
  204. else if (WeightConst_JZZ_==4) WeightConst_JZZ = WeightConst_JZZ + 10000;
  205. }
  206. else if (MenuItem==7)
  207. {
  208. if (WeightConst_ZD_ == 0) Maxval++;
  209. else if (WeightConst_ZD_==1) Maxval = Maxval + 10;
  210. else if (WeightConst_ZD_==2) Maxval = Maxval + 100;
  211. else if (WeightConst_ZD_==3) Maxval = Maxval + 1000;
  212. else if (WeightConst_ZD_==4) Maxval = Maxval + 10000;
  213. }
  214. else if (MenuItem==8)
  215. {
  216. if (ad_FS<8) ad_FS++; else ad_FS=0;
  217. write_Flash("ad_FS", &ad_FS, sizeof(ad_FS));
  218. }
  219. else if (MenuItem==9)
  220. {
  221. if (ad_Point<3) ad_Point++; else ad_Point=0;
  222. write_Flash("ad_Point", &ad_Point, sizeof(ad_Point));
  223. }
  224. else if (MenuItem==10)
  225. {
  226. if (ad_Inteval== 1) ad_Inteval=2; else if (ad_Inteval==2) ad_Inteval=5;
  227. else if (ad_Inteval==5) ad_Inteval=1; else ad_Inteval=1;
  228. write_Flash("ad_Inteval", &ad_Inteval, sizeof(ad_Inteval));
  229. }
  230. else if (MenuItem==11)
  231. {
  232. if (WeightConst_BLE<3) WeightConst_BLE ++;
  233. else WeightConst_BLE = 0;
  234. write_Flash("BLE", &WeightConst_BLE, sizeof(WeightConst_BLE));
  235. }
  236. button_state.b1 = 0;
  237. button_state.b2 = 1;
  238. button_state.b3 = 0;
  239. button_state.b4 = 0;
  240. button_state.b5 = 0;
  241. }
  242. else if ((HAL_GPIO_ReadPin(B3_GPIO_Port, B3_Pin) > 0) && button_state.b3 == 0) {
  243. if (MenuItem==6)
  244. {
  245. if (WeightConst_JZZ_==4) WeightConst_JZZ_ = 0;
  246. else WeightConst_JZZ_++;
  247. beep();
  248. }
  249. else if (MenuItem==7)
  250. {
  251. if (WeightConst_ZD_==4) WeightConst_ZD_ = 0;
  252. else WeightConst_ZD_++;
  253. beep();
  254. }
  255. button_state.b1 = 0;
  256. button_state.b2 = 0;
  257. button_state.b3 = 1;
  258. button_state.b4 = 0;
  259. button_state.b5 = 0;
  260. }
  261. else if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0) && (WeightConst_SBType == 4) && button_state.b4 == 0) {
  262. if (WeightConst_TQ<14)
  263. if (MenuItem==5) MenuItem = 11;
  264. else if (MenuItem==11) MenuItem = 0;
  265. else MenuItem++;
  266. else if (MenuItem==11) MenuItem = 0;
  267. else MenuItem++;
  268. if (MenuItem==6) WeightConst_JZZ_ = 0; //切换菜单后重置
  269. else if (MenuItem==7) WeightConst_ZD_ = 0;
  270. if (MenuItem==7)
  271. {
  272. if (WeightConst_JZZ>0) setFullCal(WeightConst_JZZ);
  273. }
  274. else if (MenuItem==8) ArgSave();
  275. beep();
  276. button_state.b1 = 0;
  277. button_state.b2 = 0;
  278. button_state.b3 = 0;
  279. button_state.b4 = 1;
  280. button_state.b5 = 0;
  281. }
  282. else if ((HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0) && (WeightConst_SBType != 4 && WeightConst_SBType != 6) && button_state.b4 == 0) {
  283. if (WeightConst_TQ < 14)
  284. {
  285. if (MenuItem == 5) MenuItem = 11;
  286. else if (MenuItem == 11) MenuItem = 0;
  287. else MenuItem++;
  288. }
  289. else if (MenuItem==11) MenuItem = 0;
  290. else MenuItem++;
  291. if (MenuItem == 6) WeightConst_JZZ_ = 0; //切换菜单后重置
  292. else if (MenuItem == 7) WeightConst_ZD_ = 0;
  293. if (MenuItem==7) setFullCal(WeightConst_JZZ);
  294. else if (MenuItem==8) ArgSave();
  295. beep();
  296. button_state.b1 = 0;
  297. button_state.b2 = 0;
  298. button_state.b3 = 0;
  299. button_state.b4 = 1;
  300. button_state.b5 = 0;
  301. }
  302. }
  303. rt_thread_mdelay(200);
  304. }
  305. }
  306. #define THREAD_PRIORITY 3
  307. #define THREAD_TIMESLICE 5
  308. static rt_thread_t tid1 = RT_NULL;
  309. void MenuButtonthread(void)
  310. {
  311. tid1 = rt_thread_create("CheckMB",
  312. CheckMenuButton_entry, RT_NULL,
  313. 0x400,
  314. THREAD_PRIORITY, THREAD_TIMESLICE);
  315. /* 如果获得线程控制块,启动这个线程 */
  316. if (tid1 != RT_NULL)
  317. rt_thread_startup(tid1);
  318. }
  319. static void beep_off(void)
  320. {
  321. HAL_GPIO_WritePin(beep_GPIO_Port, beep_Pin, GPIO_PIN_RESET);
  322. }
  323. static void beep_on(void)
  324. {
  325. HAL_GPIO_WritePin(beep_GPIO_Port, beep_Pin, GPIO_PIN_SET);
  326. }
  327. void alarm_off(void)
  328. {
  329. HAL_GPIO_WritePin(alarm_GPIO_Port, alarm_Pin, GPIO_PIN_RESET);
  330. }
  331. void alarm_on(void)
  332. {
  333. HAL_GPIO_WritePin(alarm_GPIO_Port, alarm_Pin, GPIO_PIN_SET);
  334. }
  335. void pwr_lock_off(void)
  336. {
  337. HAL_GPIO_WritePin(GPIOC, pwr_lock_Pin, GPIO_PIN_RESET);
  338. }
  339. void pwr_lock_on(void)
  340. {
  341. HAL_GPIO_WritePin(GPIOC, pwr_lock_Pin, GPIO_PIN_SET);
  342. }
  343. void output1_off(void)
  344. {
  345. HAL_GPIO_WritePin(output1_GPIO_Port, output1_Pin, GPIO_PIN_SET);
  346. }
  347. void output1_on(void)
  348. {
  349. HAL_GPIO_WritePin(output1_GPIO_Port, output1_Pin, GPIO_PIN_RESET);
  350. }
  351. void output2_off(void)
  352. {
  353. HAL_GPIO_WritePin(output2_GPIO_Port, output2_Pin, GPIO_PIN_SET);
  354. }
  355. void output2_on(void)
  356. {
  357. HAL_GPIO_WritePin(output2_GPIO_Port, output2_Pin, GPIO_PIN_RESET);
  358. }
  359. void AT_off(void)
  360. {
  361. HAL_GPIO_WritePin(ble_c_GPIO_Port, ble_c_Pin, GPIO_PIN_SET);
  362. }
  363. void AT_on(void)
  364. {
  365. HAL_GPIO_WritePin(ble_c_GPIO_Port, ble_c_Pin, GPIO_PIN_RESET);
  366. }
  367. void beep(void)
  368. {
  369. rt_sem_release(thread_beep_sem);
  370. }
  371. static void thread_beep_entry(void *parameter)
  372. {
  373. while(1)
  374. {
  375. rt_sem_take(thread_beep_sem, RT_WAITING_FOREVER); //等待串口消息
  376. beep_on();
  377. rt_thread_mdelay(400);
  378. beep_off();
  379. }
  380. }
  381. static rt_thread_t tid_beep = RT_NULL;
  382. void thread_beep_init(void)
  383. {
  384. thread_beep_sem = rt_sem_create("thread_beep_sem", 0, RT_IPC_FLAG_FIFO);
  385. tid_beep = rt_thread_create("beep",
  386. thread_beep_entry, RT_NULL,
  387. 0x100,
  388. THREAD_PRIORITY, THREAD_TIMESLICE);
  389. /* 如果获得线程控制块,启动这个线程 */
  390. if (tid_beep != RT_NULL)
  391. rt_thread_startup(tid_beep);
  392. }
  393. extern uint8_t isnewBan;
  394. static unsigned int button4_press=0;
  395. static unsigned int button4_press_=0;
  396. static void CheckButton_entry(void *parameter) {
  397. while (1) {
  398. button4_press_ = 200;
  399. if (WeightConst_SBType == 2 || WeightConst_SBType == 7)
  400. {
  401. uart4_send_data[18]=0;
  402. for (int i = 2; i <18; i++) //校验和
  403. uart4_send_data[18]=uart4_send_data[18]+uart4_send_data[i];
  404. rt_sem_take(uart4_lock, RT_WAITING_FOREVER);
  405. HAL_UART_Transmit_DMA(&huart4, uart4_send_data, 19);
  406. rt_sem_release(uart4_lock);
  407. }
  408. if (button_state.b1>0) button_state.b1 ++;
  409. if (button_state.b2>0) button_state.b2 ++;
  410. if (button_state.b3>0) button_state.b3 ++;
  411. if (button_state.b4>0) button_state.b4 ++;
  412. if (button_state.b7>0) button_state.b7 ++;
  413. if (button_state.b1>=15) button_state.b1 = 0;
  414. if (button_state.b2>=15) button_state.b2 = 0;
  415. if (button_state.b3>=5) button_state.b3 = 0;
  416. if (button_state.b4>=5) button_state.b4 = 0;
  417. if (button_state.b7>=15) button_state.b7 = 0;
  418. if (WeightConst_SBType == 3 && isnewBan==1)
  419. {
  420. if ((HAL_GPIO_ReadPin(pwr_GPIO_Port, pwr_Pin) == 0) ) {
  421. rt_thread_mdelay(2000);
  422. if ((HAL_GPIO_ReadPin(pwr_GPIO_Port, pwr_Pin) == 0) ) {
  423. beep();
  424. pwr_lock_off();
  425. }
  426. }
  427. }
  428. if ((HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) > 0) && button_state.b1 == 0 && CanButton==1 && iscowweight==0) {
  429. if (WeightConst_SBType != 2 && WeightConst_SBType != 7) {
  430. if (pf_comp.data.pfsort!=0xFE) {
  431. if (WeightConst_DELAYON>0)
  432. {
  433. // beep();
  434. // WeightConst_DELAYTIME = (pf_comp.data.delaytime*60)+1; //允许按键跳过搅拌延时
  435. }
  436. else if (pf_comp.data.needBegin==1 && WeightConst_WeightHaveBegin==0)
  437. {
  438. beep();
  439. rt_sem_take(weight_lock, RT_WAITING_FOREVER);
  440. WeightConst_WeightBegin = WeightConst_WeightCur;
  441. rt_sem_release(weight_lock);
  442. CompleteTarget_order = pf_comp.data.pfsort;
  443. write_Flash("completeorder", &CompleteTarget_order, sizeof(CompleteTarget_order));
  444. rt_sem_take(setTIME_lock, RT_WAITING_FOREVER);
  445. write_Flash("begintime", &TimeConst, sizeof(TimeConst));
  446. rt_sem_release(setTIME_lock);
  447. write_Flash("beginweight", &WeightConst_WeightBegin, sizeof(WeightConst_WeightBegin));
  448. getTargetWeight();
  449. }
  450. else if (tsdb_recordcount('p')>0 && delayKeying == 0 && WeightConst_Product_Run)
  451. {
  452. rt_sem_release(delay_key_sem);
  453. sendkey1(0xF2, 0);
  454. }
  455. }
  456. }
  457. else
  458. {
  459. beep();
  460. ChancheShow();
  461. XbeeConst_FRemoteTxBuf[0] = 7;
  462. XbeeConst_FRemoteTxBuf[1] = 0x7E;
  463. XbeeConst_FRemoteTxBuf[2] = 0x05;
  464. if (isCC)
  465. XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_DeviceAddress&0x0F) << 5 ) |(XbeeConst_RemoteAddress&0x1f);
  466. else
  467. XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress&0x1f;
  468. if (XbeeConst_UseAPI==1||XbeeConst_UseAPI==3)
  469. XbeeConst_FRemoteTxBuf[4] = 0xA2;
  470. else
  471. XbeeConst_FRemoteTxBuf[4] = 0x14;
  472. XbeeConst_FRemoteTxBuf[5] = 0x01;
  473. XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;
  474. HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR3, XbeeConst_FRemoteTxBuf[6]);
  475. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
  476. for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
  477. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
  478. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
  479. }
  480. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];
  481. if (XbeeConst_UseAPI>0) {
  482. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  483. setSendFrame_t(0x01,0x01,0xC1);
  484. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
  485. for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
  486. usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];
  487. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  488. rt_sem_release(uart2_lock);
  489. }
  490. else {
  491. if (iscowweight<1)
  492. {
  493. rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
  494. for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
  495. usart3_send_data[i-1] = XbeeConst_FRemoteTxBuf[i];
  496. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
  497. rt_sem_release(uart3_lock);
  498. }
  499. }
  500. }
  501. button_state.b1 = 1;
  502. button_state.b2 = 0;
  503. button_state.b3 = 0;
  504. button_state.b4 = 0;
  505. button_state.b7 = 0;
  506. }
  507. if ((HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin) > 0) && button_state.b2 == 0 && CanButton==1 && iscowweight==0) {
  508. if (((WeightConst_SBType != 2 && WeightConst_SBType != 7)))
  509. {
  510. if(WeightConst_DELAYON==0)
  511. {
  512. if (tsdb_recordcount('k')==0)
  513. sendkey2(0xF4);
  514. }
  515. }
  516. else {
  517. beep();
  518. ChancheShow();
  519. XbeeConst_FRemoteTxBuf[0] = 7;
  520. XbeeConst_FRemoteTxBuf[1] = 0x7E;
  521. XbeeConst_FRemoteTxBuf[2] = 0x05;
  522. if (isCC)
  523. XbeeConst_FRemoteTxBuf[3] = ((XbeeConst_DeviceAddress&0x0F) << 5 ) | (XbeeConst_RemoteAddress&0x1f);
  524. else
  525. XbeeConst_FRemoteTxBuf[3] = XbeeConst_RemoteAddress&0x1f;
  526. if (XbeeConst_UseAPI==1||XbeeConst_UseAPI==3)
  527. XbeeConst_FRemoteTxBuf[4] = 0xA2;
  528. else
  529. XbeeConst_FRemoteTxBuf[4] = 0x14;
  530. XbeeConst_FRemoteTxBuf[5] = 0x02;
  531. XbeeConst_FRemoteTxBuf[6] = XbeeConst_FrameNum++;
  532. HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR3, XbeeConst_FRemoteTxBuf[6]);
  533. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0;
  534. for (int i = 1; i < XbeeConst_FRemoteTxBuf[0]; i++) {
  535. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] =
  536. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]]+XbeeConst_FRemoteTxBuf[i];
  537. }
  538. XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]] = 0xFF - XbeeConst_FRemoteTxBuf[XbeeConst_FRemoteTxBuf[0]];
  539. if (XbeeConst_UseAPI>0) {
  540. rt_sem_take(uart2_lock, RT_WAITING_FOREVER);
  541. setSendFrame_t(0x01,0x01,0xC1);
  542. XbeeConst_PTxBuf0 = xBeeApp2MakeFrame(&userSendFrame_t, XbeeConst_FRemoteTxBuf);
  543. for (int i = 1; i <= XbeeConst_PTxBuf0[0]; i++) //将缓冲区发出
  544. usart2_send_data[i-1] = XbeeConst_PTxBuf0[i];
  545. HAL_UART_Transmit_DMA(&huart2, usart2_send_data, XbeeConst_PTxBuf0[0]);
  546. rt_sem_release(uart2_lock);
  547. }
  548. else
  549. if (iscowweight<1 )
  550. {
  551. rt_sem_take(uart3_lock, RT_WAITING_FOREVER);
  552. for (int i = 1; i <= XbeeConst_FRemoteTxBuf[0]; i++) //将缓冲区发出
  553. usart3_send_data[i-1] = XbeeConst_FRemoteTxBuf[i];
  554. HAL_UART_Transmit_DMA(&huart3, usart3_send_data, XbeeConst_FRemoteTxBuf[0]);
  555. rt_sem_release(uart3_lock);
  556. }
  557. }
  558. button_state.b1 = 0;
  559. button_state.b2 = 1;
  560. button_state.b3 = 0;
  561. button_state.b4 = 0;
  562. button_state.b7 = 0;
  563. }
  564. if ((HAL_GPIO_ReadPin(B3_GPIO_Port, B3_Pin) > 0) && button_state.b3 == 0) {
  565. if (WeightConst_SBType == 2 || WeightConst_SBType == 7)
  566. {
  567. if (XbeeConst_RemoteAddress == 1) XbeeConst_RemoteAddress = WeightConst_ChanCheShu;
  568. else XbeeConst_RemoteAddress--;
  569. // if ((WeightConst_ChanCheShu==7) && XbeeConst_RemoteAddress>2 && XbeeConst_RemoteAddress<5)
  570. // setNetAddress_byapi(1);
  571. // else setNetAddress_byapi(0);
  572. ChancheShow();
  573. if (XbeeConst_UseAPI==3) sendshowCC();
  574. beep();
  575. }
  576. else {
  577. if (HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin) > 0)
  578. {
  579. if(WeightConst_TQ==14)
  580. {
  581. if (button_state.b4>0) button4_press = button4_press + button4_press_;
  582. else button4_press=1;
  583. }
  584. if((button4_press>1500) || (iscowweight==1 && WeightConst_TQ==14)){
  585. beep();
  586. button4_press = 0;
  587. setZero();
  588. setZeroCal();
  589. }
  590. }
  591. else if (iscowweight==0)
  592. {
  593. if (WeightConst_SBType!=3)
  594. beep();
  595. rt_thread_mdelay(1000);
  596. if (HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) > 0 && WeightConst_SBType == 1 && WeightConst_Product_Run==1){
  597. rt_sem_take(display_lock, RT_WAITING_FOREVER);
  598. for (int i = 0; i <40; i++)
  599. WeightConst_feed[i] = WeightConst_product[i]; //显示一秒牛舍
  600. rt_sem_release(display_lock);
  601. while(HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin) > 0){
  602. rt_thread_mdelay(200);
  603. }
  604. rt_sem_take(display_lock, RT_WAITING_FOREVER);
  605. rt_memset(WeightConst_feed,0x20,40);
  606. for (int i = 0; i <= WeightConst_PlantempList[1]-11; i++) {
  607. WeightConst_feed[i] = WeightConst_PlantempList[i+4]; //饲料名称
  608. }
  609. rt_sem_release(display_lock);
  610. }
  611. else if (WeightConst_SBType!=3)
  612. {
  613. if (WeightConst_displayTotal == 1) WeightConst_displayTotal = 0;
  614. else WeightConst_displayTotal = 1;
  615. }
  616. }
  617. }
  618. button_state.b1 = 0;
  619. button_state.b2 = 0;
  620. button_state.b3 = 1;
  621. button_state.b4 = 0;
  622. button_state.b7 = 0;
  623. }
  624. if ((HAL_GPIO_ReadPin(B4_GPIO_Port, B4_Pin) > 0) && button_state.b4 == 0 && iscowweight==0) {
  625. WriteTag();
  626. if (WeightConst_SBType != 2 && WeightConst_SBType != 7) {
  627. if (isTag==1)
  628. {
  629. //if (WriteTag_state <2 && pf_comp.data.needBegin == 1 && WeightConst_WeightHaveBegin==0)
  630. {
  631. beep();
  632. WriteTag();
  633. }
  634. }
  635. if (WeightConst_SBType==6)
  636. {
  637. if (WeightConst_remoteDisplay==0)
  638. WeightConst_remoteDisplay = 1;
  639. else
  640. WeightConst_remoteDisplay = 0;
  641. beep();
  642. }
  643. else {
  644. Planback();
  645. }
  646. }
  647. else
  648. {
  649. if (XbeeConst_RemoteAddress==WeightConst_ChanCheShu)
  650. XbeeConst_RemoteAddress = 1;
  651. else
  652. XbeeConst_RemoteAddress++;
  653. // if ((WeightConst_ChanCheShu==7) && XbeeConst_RemoteAddress>2 && XbeeConst_RemoteAddress<5)
  654. // setNetAddress_byapi(1);
  655. // else setNetAddress_byapi(0);
  656. ChancheShow();
  657. if (XbeeConst_UseAPI==3) sendshowCC();
  658. beep();
  659. }
  660. button_state.b1 = 0;
  661. button_state.b2 = 0;
  662. button_state.b3 = 0;
  663. button_state.b4 = 1;
  664. button_state.b7 = 0;
  665. }
  666. else if (button_state.b4==0)
  667. button4_press = 0;
  668. rt_thread_mdelay(200);
  669. }
  670. }
  671. void Button_thread(void)
  672. {
  673. tid1 = rt_thread_create("Button",
  674. CheckButton_entry, RT_NULL,
  675. 0x400,
  676. 3, 5);
  677. /* 如果获得线程控制块,启动这个线程 */
  678. if (tid1 != RT_NULL)
  679. rt_thread_startup(tid1);
  680. }