123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #include "OutputTask.h"
- bool isOpen;
- int InitComPort()
- {
- int fd;
- struct termios tios;
- fd = open(priPortName, O_RDWR);
- if(fd<=0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("open 407 Communication port NG \n");
- #endif
- return -1;
- }
- ioctl (fd, TCGETS, &tios);
- tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
- tios.c_lflag = 0;
- tios.c_iflag = 0;
- tios.c_oflag = 0;
- tios.c_cc[VMIN]=0;
- tios.c_cc[VTIME]=(unsigned char)1;
- tios.c_lflag=0;
- tcflush(fd, TCIFLUSH);
- ioctl (fd, TCSETS, &tios);
- return fd;
- }
- unsigned long GetTimeoutValue(struct timeval _sour_time)
- {
- struct timeval _end_time;
- gettimeofday(&_end_time, NULL);
- return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
- }
- void ShowMainMsg()
- {
- printf("Max Vol : %f, Max Cur : %d, POW : %d \n", UnSafeDataInfo->PSU_VOLTAGE,
- UnSafeDataInfo->PSU_CURRENT, UnSafeDataInfo->PSU_POWER);
- printf("=> ");
- }
- void ChkButtonStatus()
- {
- if (Button1 == PRESS && !leftBtnPush)
- {
- if(!leftBtnPush)
- {
- leftBtnPush = true;
- if (_charging_mode == CHARGING_MODE_STOP)
- {
- _charging_mode = CHARGING_MODE_START;
- printf("****************** Switch to Charging Mode ******************\n");
- }
- }
- else if (Button1 == RELEASE)
- {
- if(leftBtnPush)
- {
- leftBtnPush = false;
- }
- }
- }
- if (Button2 == PRESS && !rightBtnPush)
- {
- if(!rightBtnPush)
- {
- rightBtnPush = true;
- if (_charging_mode == CHARGING_MODE_START)
- {
- _charging_mode = CHARGING_MODE_TERMINATING;
- printf("****************** Switch to Stop Mode ******************\n");
- }
- }
- else if (Button2 == RELEASE)
- {
- if(rightBtnPush)
- {
- rightBtnPush = false;
- }
- }
- }
- }
- void GetModuleCountCallback(byte group, byte count)
- {
-
- if (group == SYSTEM_CMD)
- UnSafeDataInfo->PSU_COUNT = count;
- }
- void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
- {
- int _groupPower = 0, _groupCurrent = 0;
- UnSafeDataInfo->PsuModule[address].PSU_VOLTAGE_INFO = maxVol;
- UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO = maxCur;
- UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO = totalPow;
- for (byte index = 0; index < UnSafeDataInfo->PSU_COUNT; index++)
- {
- _groupCurrent += UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO;
- _groupPower += UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO;
- }
- UnSafeDataInfo->PSU_VOLTAGE = maxVol;
- UnSafeDataInfo->PSU_CURRENT = _groupCurrent;
- UnSafeDataInfo->PSU_POWER = _groupPower;
- }
- void GetStatusCallback(byte group, byte address, byte temp, int alarm)
- {
-
- }
- void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
- {
-
- }
- int CreateShareMemory()
- {
- int MeterSMId;
- if ((MeterSMId = shmget(ShmTestKey, sizeof(struct UnSafeData), IPC_CREAT | 0777)) < 0)
- {
- return 0;
- }
- else if ((UnSafeDataInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- return 0;
- }
- memset(UnSafeDataInfo, 0, sizeof(struct UnSafeData));
- return 1;
- }
- static void get_char(char *word)
- {
- fd_set rfds;
- struct timeval tv;
- FD_ZERO(&rfds);
- FD_SET(0, &rfds);
- tv.tv_sec = 0;
- tv.tv_usec = 10;
-
- if (select(1, &rfds, NULL, NULL, &tv) > 0)
- {
- fgets(word, 128, stdin);
- }
- }
- void GetInputString()
- {
- char word[128];
- char newString[7][10];
- int i, j, ctr;
- get_char(word);
- if (strlen(word) == 0)
- return;
-
- j = 0;
- ctr = 0;
- for (i = 0; i <= (strlen(word)); i++) {
- if (word[i] == ' ' || word[i] == '\0' || word[i] == 10) {
- newString[ctr][j] = '\0';
- ctr++;
- j = 0;
- } else {
- newString[ctr][j] = word[i];
- j++;
- }
- }
- VOLTAGE = atof(newString[0]);
- CURRENT = atof(newString[1]);
- if (VOLTAGE <= UnSafeDataInfo->PSU_VOLTAGE && CURRENT <= UnSafeDataInfo->PSU_CURRENT)
- {
-
- }
- else
- {
- ShowMainMsg();
- }
- }
- void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
- {
-
- }
- void GetOutputAndTempCallback(byte address, unsigned short outputVol,
- unsigned short outputCur, unsigned short outputPower, unsigned char Temperature)
- {
-
-
- }
- void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
- unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
- {
-
-
-
-
-
- }
- void GetModuleInputCallback(byte address, unsigned short inputR,
- unsigned short inputS, unsigned short inputT)
- {
- }
- int main(void)
- {
- isOpen =false;
- if(CreateShareMemory() == 0)
- {
- printf("CreateShareMemory fail. \n");
- return 0;
- }
- RefreshModuleCount(&GetModuleCountCallback);
- RefreshAvailableCap(&GetAvailableCapCallback);
- RefreshStatus(&GetStatusCallback);
- RefreshInputVol(&GetInputVoltageCallback);
- RefreshIavailable(&GetIavailableCallback);
- AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
- AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
- AutoMode_RefreshModuleInput(&GetModuleInputCallback);
- Uart1Fd = InitComPort();
- libInitialize = InitialCommunication();
- if (Uart1Fd < 0 || !libInitialize)
- {
- printf("Initial port fail. \n");
- return 0;
- }
- sleep(5);
- gettimeofday(&_cmdSubPriority_time, NULL);
- VOLTAGE = 0.0;
- CURRENT = 0.0;
- SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
- while (1)
- {
- GetInputGpioStatus();
-
-
- SetWalkInConfig(SYSTEM_CMD, NO, 0);
- int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
- while(isGetCount == YES)
- {
- if (_charging_mode == CHARGING_MODE_START)
- {
-
- GetModuleIavailable(0);
- }
- GetInputString();
- if (VOLTAGE > 150 && CURRENT >= 0)
- _charging_mode = CHARGING_MODE_START;
- else
- _charging_mode = CHARGING_MODE_TERMINATING;
-
- switch(_charging_mode)
- {
- case CHARGING_MODE_START:
- {
- if (!isOpen)
- {
- SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
- FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
- isOpen = true;
- }
- PresentOutputVol(SYSTEM_CMD, VOLTAGE * 10, CURRENT * 10);
- }
- break;
- case CHARGING_MODE_TERMINATING:
- {
- if (isOpen)
- {
- SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
- FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
- isOpen = false;
- }
- }
- break;
- }
-
-
- sleep(1);
- }
- if (UnSafeDataInfo->PSU_COUNT <= 0)
- {
- if (time > 1000)
- {
- printf("Step 1 : GetModuleCount...... \n");
- GetModuleCount(SYSTEM_CMD);
- gettimeofday(&_cmdSubPriority_time, NULL);
- }
- }
- else if (time < 5000)
- {
- printf("Step 2 : GetModuleCap...... \n");
- GetModuleCap(0);
- SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
- FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
- }
- else
- {
- ShowMainMsg();
- isGetCount = YES;
- }
- sleep(1);
- }
- return 0;
- }
|