OutputTask.c 8.9 KB

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