OutputTask.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * OutputTask.c
  3. *
  4. * Created on: 2020年2月25日
  5. * Author: 7564
  6. */
  7. #include "OutputTask.h"
  8. bool isOpen;
  9. int InitComPort()
  10. {
  11. int fd;
  12. struct termios tios;
  13. fd = open(priPortName, O_RDWR);
  14. if(fd<=0)
  15. {
  16. #ifdef SystemLogMessage
  17. DEBUG_ERROR("open 407 Communication port NG \n");
  18. #endif
  19. return -1;
  20. }
  21. ioctl (fd, TCGETS, &tios);
  22. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  23. tios.c_lflag = 0;
  24. tios.c_iflag = 0;
  25. tios.c_oflag = 0;
  26. tios.c_cc[VMIN]=0;
  27. tios.c_cc[VTIME]=(unsigned char)1;
  28. tios.c_lflag=0;
  29. tcflush(fd, TCIFLUSH);
  30. ioctl (fd, TCSETS, &tios);
  31. return fd;
  32. }
  33. unsigned long GetTimeoutValue(struct timeval _sour_time)
  34. {
  35. struct timeval _end_time;
  36. gettimeofday(&_end_time, NULL);
  37. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  38. }
  39. void ShowMainMsg()
  40. {
  41. printf("Max Vol : %f, Max Cur : %d, POW : %d \n", UnSafeDataInfo->PSU_VOLTAGE,
  42. UnSafeDataInfo->PSU_CURRENT, UnSafeDataInfo->PSU_POWER);
  43. printf("=> ");
  44. }
  45. void ChkButtonStatus()
  46. {
  47. if (Button1 == PRESS && !leftBtnPush)
  48. {
  49. if(!leftBtnPush)
  50. {
  51. leftBtnPush = true;
  52. if (_charging_mode == CHARGING_MODE_STOP)
  53. {
  54. _charging_mode = CHARGING_MODE_START;
  55. printf("****************** Switch to Charging Mode ******************\n");
  56. }
  57. }
  58. else if (Button1 == RELEASE)
  59. {
  60. if(leftBtnPush)
  61. {
  62. leftBtnPush = false;
  63. }
  64. }
  65. }
  66. if (Button2 == PRESS && !rightBtnPush)
  67. {
  68. if(!rightBtnPush)
  69. {
  70. rightBtnPush = true;
  71. if (_charging_mode == CHARGING_MODE_START)
  72. {
  73. _charging_mode = CHARGING_MODE_TERMINATING;
  74. printf("****************** Switch to Stop Mode ******************\n");
  75. }
  76. }
  77. else if (Button2 == RELEASE)
  78. {
  79. if(rightBtnPush)
  80. {
  81. rightBtnPush = false;
  82. }
  83. }
  84. }
  85. }
  86. void GetModuleCountCallback(byte group, byte count)
  87. {
  88. printf("group = %d, count = %d \n", group, count);
  89. if (group == SYSTEM_CMD)
  90. UnSafeDataInfo->PSU_COUNT = count;
  91. }
  92. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  93. {
  94. int _groupPower = 0, _groupCurrent = 0;
  95. UnSafeDataInfo->PsuModule[address].PSU_VOLTAGE_INFO = maxVol;
  96. UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO = maxCur;
  97. UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO = totalPow;
  98. for (byte index = 0; index < UnSafeDataInfo->PSU_COUNT; index++)
  99. {
  100. _groupCurrent += UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO;
  101. _groupPower += UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO;
  102. }
  103. UnSafeDataInfo->PSU_VOLTAGE = maxVol;
  104. UnSafeDataInfo->PSU_CURRENT = _groupCurrent;
  105. UnSafeDataInfo->PSU_POWER = _groupPower;
  106. }
  107. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  108. {
  109. printf("alarm = %d \n", alarm);
  110. }
  111. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  112. {
  113. printf("vol1 = %d, vol2 = %d, vol3 = %d \n", vol1, vol2, vol3);
  114. }
  115. int CreateShareMemory()
  116. {
  117. int MeterSMId;
  118. if ((MeterSMId = shmget(ShmTestKey, sizeof(struct UnSafeData), IPC_CREAT | 0777)) < 0)
  119. {
  120. return 0;
  121. }
  122. else if ((UnSafeDataInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  123. {
  124. return 0;
  125. }
  126. memset(UnSafeDataInfo, 0, sizeof(struct UnSafeData));
  127. return 1;
  128. }
  129. static void get_char(char *word)
  130. {
  131. fd_set rfds;
  132. struct timeval tv;
  133. FD_ZERO(&rfds);
  134. FD_SET(0, &rfds);
  135. tv.tv_sec = 0;
  136. tv.tv_usec = 10; //wait input timout time
  137. //if input
  138. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  139. {
  140. fgets(word, 128, stdin);
  141. }
  142. }
  143. void GetInputString()
  144. {
  145. char word[128];
  146. char newString[7][10];
  147. int i, j, ctr;
  148. get_char(word);
  149. if (strlen(word) == 0)
  150. return;
  151. //fgets(word, sizeof(word), stdin);
  152. j = 0;
  153. ctr = 0;
  154. for (i = 0; i <= (strlen(word)); i++) {
  155. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10) {
  156. newString[ctr][j] = '\0';
  157. ctr++;
  158. j = 0;
  159. } else {
  160. newString[ctr][j] = word[i];
  161. j++;
  162. }
  163. }
  164. VOLTAGE = atof(newString[0]);
  165. CURRENT = atof(newString[1]);
  166. if (VOLTAGE <= UnSafeDataInfo->PSU_VOLTAGE && CURRENT <= UnSafeDataInfo->PSU_CURRENT)
  167. {
  168. //printf("OutputVol = %f, OutputCur = %f \n", VOLTAGE, CURRENT);
  169. }
  170. else
  171. {
  172. ShowMainMsg();
  173. }
  174. }
  175. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  176. {
  177. //printf("address = %d, Iavail = %d, Vext = %d \n", address, Iavail, Vext);
  178. }
  179. void GetOutputAndTempCallback(byte address, unsigned short outputVol,
  180. unsigned short outputCur, unsigned short outputPower, unsigned char Temperature)
  181. {
  182. //printf("***Output Value and Temp*** address = %d, Vol = %d, Cur = %d, Pow = %d, Temp = %d \n",
  183. // address, outputVol, outputCur, outputPower, Temperature);
  184. }
  185. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  186. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  187. {
  188. //int alarm = (err2 << 24) | (err3 << 16) | (err4 << 8);
  189. // err2 == state 2
  190. // err3 == state 1
  191. // err4 == state 0
  192. //printf("***Status*** address = %d, alarm = %d \n", address, alarm);
  193. // printf("***Status*** address = %d, err1 = %d, err2 = %d, err3 = %d, err4 = %d \n",
  194. // address, err1,err2,err3,err4);
  195. }
  196. void GetModuleInputCallback(byte address, unsigned short inputR,
  197. unsigned short inputS, unsigned short inputT)
  198. {
  199. }
  200. int main(void)
  201. {
  202. isOpen =false;
  203. if(CreateShareMemory() == 0)
  204. {
  205. printf("CreateShareMemory fail. \n");
  206. return 0;
  207. }
  208. RefreshModuleCount(&GetModuleCountCallback);
  209. RefreshAvailableCap(&GetAvailableCapCallback);
  210. RefreshStatus(&GetStatusCallback);
  211. RefreshInputVol(&GetInputVoltageCallback);
  212. RefreshIavailable(&GetIavailableCallback);
  213. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  214. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  215. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  216. Uart1Fd = InitComPort();
  217. libInitialize = InitialCommunication();
  218. if (Uart1Fd < 0 || !libInitialize)
  219. {
  220. printf("Initial port fail. \n");
  221. return 0;
  222. }
  223. sleep(5);
  224. gettimeofday(&_cmdSubPriority_time, NULL);
  225. VOLTAGE = 0.0;
  226. CURRENT = 0.0;
  227. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  228. // while (1)
  229. // {
  230. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  231. // SetWalkInConfig(0, YES, 0);
  232. // SetWalkInConfig(1, NO, 0);
  233. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  234. // sleep(1);
  235. // }
  236. //
  237. // sleep(1);
  238. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  239. // SetWalkInConfig(SYSTEM_CMD, NO, 0);
  240. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  241. // return 0;
  242. while (1)
  243. {
  244. GetInputGpioStatus();
  245. //ChkButtonStatus();
  246. // 切換 Walk-in mode (default 5s -> 2s)
  247. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  248. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  249. while(isGetCount == YES)
  250. {
  251. if (_charging_mode == CHARGING_MODE_START)
  252. {
  253. // 取得模塊輸出額定電流能力
  254. GetModuleIavailable(0);
  255. }
  256. GetInputString();
  257. if (VOLTAGE > 150 && CURRENT >= 0)
  258. _charging_mode = CHARGING_MODE_START;
  259. else
  260. _charging_mode = CHARGING_MODE_TERMINATING;
  261. //printf("_charging_mode = %d \n", _charging_mode);
  262. switch(_charging_mode)
  263. {
  264. case CHARGING_MODE_START:
  265. {
  266. //if (!isOpen)
  267. {
  268. //SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  269. //FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  270. SetDirModulePresentOutput(0,
  271. VOLTAGE * 10,
  272. CURRENT * 10,
  273. 0x01,
  274. 0x01);
  275. }
  276. //PresentOutputVol(SYSTEM_CMD, VOLTAGE * 10, CURRENT * 10);
  277. }
  278. break;
  279. case CHARGING_MODE_TERMINATING:
  280. {
  281. //if (isOpen)
  282. {
  283. SetDirModulePresentOutput(0,
  284. VOLTAGE * 10,
  285. CURRENT * 10,
  286. 0x00,
  287. 0x01);
  288. //SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  289. //FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  290. }
  291. }
  292. break;
  293. }
  294. //GetStatus(0);
  295. //GetModuleInput(0);
  296. sleep(1);
  297. }
  298. if (UnSafeDataInfo->PSU_COUNT <= 0)
  299. {
  300. if (time > 1000)
  301. {
  302. printf("Step 1 : GetModuleCount...... \n");
  303. GetModuleCount(SYSTEM_CMD);
  304. gettimeofday(&_cmdSubPriority_time, NULL);
  305. }
  306. }
  307. else if (time < 5000)
  308. {
  309. printf("Step 2 : GetModuleCap...... \n");
  310. GetModuleCap(0);
  311. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  312. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  313. }
  314. else
  315. {
  316. ShowMainMsg();
  317. isGetCount = YES;
  318. }
  319. sleep(1);
  320. }
  321. return 0;
  322. }