OutputTask.c 10.0 KB

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