/******************************************************************************* * TmsLib.h TMS Client library functions * T.Barnaby, BEAM Ltd, 2007-03-14 ******************************************************************************* */ #include <stdlib.h> #include <stdint.h> #include <unistd.h> #include <math.h> #include <TmsLib.h> #include <BDir.h> #include <BEntry.h> #include <BFile.h> namespace Tms { TmsCycleParams::TmsCycleParams(BString baseDir){ obaseDir = baseDir; } BError TmsCycleParams::getCycleTypes(BList<BString>& typeList){ BError err; BDir dir; BIter i; BString n; BEntryFile f; typeList.clear(); if(err = dir.open(obaseDir)){ return err; } dir.setWild("*_0.spt"); dir.setSort(1); for(dir.start(i); !dir.isEnd(i); dir.next(i)){ if(!f.open(obaseDir + "/" + dir.entryName(i))){ f.read(); n = f.findValue("cycleType:"); typeList.append(n); } } return err; } BError TmsCycleParams::getFileNames(BList<BString>& fileList){ BError err; BDir dir; BIter i; fileList.clear(); if(err = dir.open(obaseDir)){ return err; } dir.setWild("*.spt"); dir.setSort(1); for(dir.start(i); !dir.isEnd(i); dir.next(i)){ fileList.append(dir.entryName(i)); } return err; } BError TmsCycleParams::getCycleParams(BString fileName, Tms::CycleParam& param){ BString pathName; pathName = obaseDir + "/" + fileName; return readCycleParams(pathName, param); } BError TmsCycleParams::setCycleParams(Tms::CycleParam param){ BString fileName; fileName = obaseDir + "/" + param.cycleType + "_" + param.channel + ".spt"; return writeCycleParams(fileName, param); } BError TmsCycleParams::deleteCycleParams(BString cycleType, UInt32 puChannel){ BError err; BDir dir; BIter i; BString n; BEntryFile f; BString pn; if(err = dir.open(obaseDir)){ return err; } if(puChannel) dir.setWild(BString("*_") + puChannel + ".spt"); else dir.setWild("*_*.spt"); for(dir.start(i); !dir.isEnd(i); dir.next(i)){ pn = obaseDir + "/" + dir.entryName(i); if(!f.open(pn)){ f.read(); n = f.findValue("cycleType:"); if(n == cycleType){ unlink(pn); } } } #ifdef ZAP err.set(ErrorNotImplemented, "Not Implemented"); #endif return err; } BError TmsCycleParams::readCycleParams(BString fileName, Tms::CycleParam& param){ BError err; BIter i; BEntryFile file; int n; UInt32 p; BString str; BList<BString> strList; if(err = file.open(fileName)){ err.set(err.getErrorNo(), BString("Unable to open file: ") + fileName); return err; } if(err = file.read()){ err.set(err.getErrorNo(), BString("Unable to read file: ") + fileName); return err; } param.cycleType = file.findValue("cycleType:"); param.info = file.findValue("info:"); param.channel = file.findValue("channel:").retInt(); param.pllInitialFrequency = file.findValue("pllInitialFrequency:").retInt(); param.pllInitialFrequencyDelay = file.findValue("pllInitialFrequencyDelay:").retInt(); param.pllFrefGain = file.findValue("pllFrefGain:").retInt(); param.pllGain = file.findValue("pllGain:").retInt(); param.pllDdsMinimum = file.findValue("pllDdsMinimum:").retInt(); param.pllDdsMaximum = file.findValue("pllDdsMaximum:").retInt(); param.frefPhaseDelay.resize(40); for(n = 0; n < 40; n++){ param.frefPhaseDelay[n] = file.findValue(BString("frefPhaseDelay") + n + ":").retInt(); } param.stateTable.resize(0); for(n = 0; n < 14; n++){ if(!file.find(BString("stateTable") + n + ".state:")) break; param.stateTable.resize(n + 1); param.stateTable[n].state = file.findValue(BString("stateTable") + n + ".state:").retInt(); param.stateTable[n].numBunches = file.findValue(BString("stateTable") + n + ".numBunches:").retInt(); str = file.findValue(BString("stateTable") + n + ".phaseTable:"); strList = str.getTokenList(","); param.stateTable[n].phaseTable.resize(tmsPhaseTableSize); for(p = 0, strList.start(i); (p < tmsPhaseTableSize) && (p < strList.number()); p++, strList.next(i)){ param.stateTable[n].phaseTable[p] = strList[i].retInt(); } } return err; } BError TmsCycleParams::writeCycleParams(BString fileName, Tms::CycleParam param){ BError err; unsigned int n; unsigned int p; BString s; BString str; BFile file; if(err = file.open(fileName, "w")){ err.set(err.getErrorNo(), BString("Unable to open file: ") + fileName); return err; } file.printf("cycleType: %s\n", param.cycleType.retStr()); file.printf("info: %s\n", param.info.retStr()); file.printf("channel: %u\n", param.channel); file.printf("pllInitialFrequency: %u\n", param.pllInitialFrequency); file.printf("pllInitialFrequencyDelay: %u\n", param.pllInitialFrequencyDelay); file.printf("pllFrefGain: %u\n", param.pllFrefGain); file.printf("pllGain: %u\n", param.pllGain); file.printf("pllDdsMinimum: %u\n", param.pllDdsMinimum); file.printf("pllDdsMaximum: %u\n", param.pllDdsMaximum); for(n = 0; n < param.frefPhaseDelay.size(); n++){ file.printf("frefPhaseDelay%d: %u\n", n, param.frefPhaseDelay[n]); } for(n = 0; n < param.stateTable.size(); n++){ file.printf("stateTable%d.state: 0x%x\n", n, param.stateTable[n].state); file.printf("stateTable%d.numBunches: %u\n", n, param.stateTable[n].numBunches); str = ""; for(p = 0; p < param.stateTable[n].phaseTable.size(); p++){ if(str.len()) str = str + ","; s.printf("0x%x", param.stateTable[n].phaseTable[p]); str = str + s; } file.printf("stateTable%d.phaseTable: %s\n", n, str.retStr()); } file.close(); return err; } BError TmsCycleParams::writeToString(Tms::CycleParam param, BString& string){ BError err; unsigned int n; unsigned int p; BString s; BString str; str.printf("cycleType: %s\n", param.cycleType.retStr()); string += str; str.printf("info: %s\n", param.info.retStr()); string += str; str.printf("channel: %u\n", param.channel); string += str; str.printf("pllInitialFrequency: %u\n", param.pllInitialFrequency); string += str; str.printf("pllInitialFrequencyDelay: %u\n", param.pllInitialFrequencyDelay); string += str; str.printf("pllFrefGain: %u\n", param.pllFrefGain); string += str; str.printf("pllGain: %u\n", param.pllGain); string += str; str.printf("pllDdsMinimum: %u\n", param.pllDdsMinimum); string += str; str.printf("pllDdsMaximum: %u\n", param.pllDdsMaximum); string += str; for(n = 0; n < param.frefPhaseDelay.size(); n++){ str.printf("frefPhaseDelay%d: %u\n", n, param.frefPhaseDelay[n]); string += str; } for(n = 0; n < param.stateTable.size(); n++){ str.printf("stateTable%d.state: 0x%x\n", n, param.stateTable[n].state); string += str; str.printf("stateTable%d.numBunches: %u\n", n, param.stateTable[n].numBunches); string += str; str = ""; for(p = 0; p < param.stateTable[n].phaseTable.size(); p++){ if(str.len()) str = str + ","; s.printf("0x%x", param.stateTable[n].phaseTable[p]); str = str + s; } s.printf("stateTable%d.phaseTable: %s\n", n, str.retStr()); string += s; } return err; } /// Calculates the base pickup phase values for the PS ring void tmsPickupPositions(BArray<int>& pos){ unsigned int p = 43; // The first pickup in the PS ring after injection unsigned int n; pos.clear(); pos.resize(tmsNumPickups); for(n = 0; n < tmsNumPickups; n++){ pos[n] = int(round(((p - 43) * 512.0) / 100)); if((p % 10) == 0){ p += 3; } else if((p % 10) == 3){ p += 2; } else if((p % 10) == 5){ p += 2; } else if((p % 10) == 7){ p += 3; } } } }