button.c 27 KB

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