button.c 25 KB

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