button.c 27 KB

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