| 1 |
|
|---|
| 2 | /********************************************************************\
|
|---|
| 3 |
|
|---|
| 4 | Name: HVConfig.cc
|
|---|
| 5 |
|
|---|
| 6 | Created by: Sebastian Commichau, November 2008
|
|---|
| 7 | commichau@phys.ethz.ch
|
|---|
| 8 |
|
|---|
| 9 | Contents: Class reading the HV utility configuration file
|
|---|
| 10 |
|
|---|
| 11 | \********************************************************************/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | #include "ReadCard.h"
|
|---|
| 15 | #include "HVConfig.h"
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | HVConfig::HVConfig(FILE* fptr, char *configfile) {
|
|---|
| 19 |
|
|---|
| 20 | fLogPath = new char[FILENAME_MAX_SIZE];
|
|---|
| 21 | fUSBDevice = new char*[MAX_NUM_HVBOARDS];
|
|---|
| 22 | fCCClient = new char[FILENAME_MAX_SIZE];
|
|---|
| 23 |
|
|---|
| 24 | for (int i=0; i<MAX_NUM_HVBOARDS; i++) {
|
|---|
| 25 | fUSBDevice[i] = new char[FILENAME_MAX_SIZE];
|
|---|
| 26 | USBDeviceNumber[i] = 0;
|
|---|
| 27 |
|
|---|
| 28 | for (int j=0; j<MAX_NUM_CHAINS; j++)
|
|---|
| 29 | for (int k=0; k<2; k++)
|
|---|
| 30 | Coef[i][j][k] = 0.;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | NumHVBoards = 0;
|
|---|
| 34 | FileName = configfile;
|
|---|
| 35 | fStatusRefreshRate = 1.;
|
|---|
| 36 | fTimeOut = 1.;
|
|---|
| 37 | IsDAC = true;
|
|---|
| 38 | fHVCalibOffset = -.8;
|
|---|
| 39 | fHVCalibSlope = 0.0064;
|
|---|
| 40 | fHVMaxDiff = 1.0;
|
|---|
| 41 |
|
|---|
| 42 | if (configfile != NULL) {
|
|---|
| 43 | if (!ReadHVConfig(fptr, configfile)) {
|
|---|
| 44 | fprintf(fptr, "Error (HVConfig): could not configure HV control\n");
|
|---|
| 45 | exit(1);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | HVConfig::~HVConfig() {
|
|---|
| 52 |
|
|---|
| 53 | delete [] fLogPath;
|
|---|
| 54 |
|
|---|
| 55 | for (int i=0; i<MAX_NUM_HVBOARDS; i++)
|
|---|
| 56 | delete [] fUSBDevice[i];
|
|---|
| 57 | delete [] fUSBDevice;
|
|---|
| 58 |
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | int HVConfig::ReadHVConfig(FILE* fptr, char *configfile) {
|
|---|
| 63 |
|
|---|
| 64 | FILE *f;
|
|---|
| 65 | char str[MAX_COM_SIZE], dev[MAX_COM_SIZE];
|
|---|
| 66 | int j = 0;
|
|---|
| 67 |
|
|---|
| 68 | if ((f = fopen(configfile,"r")) == NULL) {
|
|---|
| 69 | fprintf(fptr,"Could not open configuration file: %s\n", configfile);
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 | else {
|
|---|
| 73 | fprintf(fptr,"Opening configuration file: %s\n", configfile);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | ReadCard("LogPath",fLogPath,'s',f);
|
|---|
| 77 |
|
|---|
| 78 | for (int i=0;i<MAX_NUM_HVBOARDS;i++) {
|
|---|
| 79 | sprintf(str,"Board%d",i);
|
|---|
| 80 |
|
|---|
| 81 | if (ReadCard(str, dev, 's', f)==0) {
|
|---|
| 82 | USBDeviceNumber[j] = i;
|
|---|
| 83 | sprintf(fUSBDevice[j++],"%s",dev);
|
|---|
| 84 | NumHVBoards++;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | ReadCard("TimeOut", &fTimeOut, 'f', f);
|
|---|
| 89 | ReadCard("StatusRefreshRate", &fStatusRefreshRate, 'f', f);
|
|---|
| 90 | ReadCard("CCPort", &fCCPort, 'I', f);
|
|---|
| 91 | ReadCard("CCClient", fCCClient, 's', f);
|
|---|
| 92 | ReadCard("IsDAC", &str, 's', f);
|
|---|
| 93 | ReadCard("HVCalibOffset", &fHVCalibOffset, 'f', f);
|
|---|
| 94 | ReadCard("HVCalibSlope", &fHVCalibSlope, 'f', f);
|
|---|
| 95 | ReadCard("HVMaxDiff", &fHVMaxDiff, 'f', f);
|
|---|
| 96 |
|
|---|
| 97 | if (strcmp(str,"TRUE"))
|
|---|
| 98 | IsDAC = false;
|
|---|
| 99 |
|
|---|
| 100 | fclose(f);
|
|---|
| 101 | return 1;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | int HVConfig::PrintHVConfig(FILE *fptr) {
|
|---|
| 106 |
|
|---|
| 107 | fprintf(fptr,"\n");
|
|---|
| 108 | fprintf(fptr,"********************************************* CONFIG ********************************************\n\n");
|
|---|
| 109 |
|
|---|
| 110 | fprintf(fptr," HV control configuration (%s):\n\n", FileName);
|
|---|
| 111 | fprintf(fptr," Log path: %s\n\n", fLogPath);
|
|---|
| 112 | fprintf(fptr," %.2d USB devices:\n\n", NumHVBoards);
|
|---|
| 113 |
|
|---|
| 114 | for (int i=0;i<NumHVBoards;i++)
|
|---|
| 115 | fprintf(fptr," Board%d: %s\n", USBDeviceNumber[i], fUSBDevice[i]);
|
|---|
| 116 |
|
|---|
| 117 | fprintf(fptr,"\n");
|
|---|
| 118 | fprintf(fptr," TimeOut: %.2f s\n", fTimeOut);
|
|---|
| 119 | fprintf(fptr," StatusRefreshRate: %.2f Hz\n\n",fStatusRefreshRate);
|
|---|
| 120 | fprintf(fptr," CCPort: %d\n", fCCPort);
|
|---|
| 121 | fprintf(fptr," CCClient: %s\n\n", fCCClient);
|
|---|
| 122 | fprintf(fptr," Set DAC values: %s\n\n", IsDAC ? "yes" : "no");
|
|---|
| 123 | fprintf(fptr," HVCalibOffset : %f\n\n", fHVCalibOffset);
|
|---|
| 124 | fprintf(fptr," HVCalibSlope : %f\n\n", fHVCalibSlope);
|
|---|
| 125 | fprintf(fptr," HVMaxDiff : %f\n\n", fHVMaxDiff);
|
|---|
| 126 |
|
|---|
| 127 | fprintf(fptr,"*************************************************************************************************\n\n");
|
|---|
| 128 |
|
|---|
| 129 | return 1;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | int HVConfig::WriteHVConfig(FILE* fptr, char *configfile) {
|
|---|
| 134 |
|
|---|
| 135 | FILE *f;
|
|---|
| 136 |
|
|---|
| 137 | time_t time_now_secs;
|
|---|
| 138 | struct tm *time_now;
|
|---|
| 139 |
|
|---|
| 140 | time(&time_now_secs);
|
|---|
| 141 | time_now = localtime(&time_now_secs);
|
|---|
| 142 |
|
|---|
| 143 | if ((f = fopen(configfile,"w")) == NULL) {
|
|---|
| 144 | fprintf(fptr,"Could not open file: %s\n", configfile);
|
|---|
| 145 | return 0;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | fprintf(f,"# Main configuration file for the HV control program %s, %04d %02d %02d, %02d:%02d:%02d\n",
|
|---|
| 149 | HV_CONTROL_VERSION,
|
|---|
| 150 | 1900 + time_now->tm_year,
|
|---|
| 151 | 1 + time_now->tm_mon,
|
|---|
| 152 | time_now->tm_mday,
|
|---|
| 153 | time_now->tm_hour,
|
|---|
| 154 | time_now->tm_min,
|
|---|
| 155 | time_now->tm_sec);
|
|---|
| 156 |
|
|---|
| 157 | fprintf(f,"# Note: this file will be updated at program exit\n\n");
|
|---|
| 158 |
|
|---|
| 159 | fprintf(f,"LogPath %s\n\n", fLogPath);
|
|---|
| 160 |
|
|---|
| 161 | fprintf(f,"TimeOut %.2f s # Timeout to return from read (%.2f,...%.2f) s\n\n",fTimeOut,MIN_TIMEOUT,MAX_TIMEOUT);
|
|---|
| 162 |
|
|---|
| 163 | fprintf(f,"StatusRefreshRate %.2f Hz # Status update rate (%.2f,...%.2f) Hz\n\n",fStatusRefreshRate,MIN_RATE,MAX_RATE);
|
|---|
| 164 |
|
|---|
| 165 | fprintf(f,"CCPort %d # Port used to look for commands from the Central Control\n",fCCPort);
|
|---|
| 166 | fprintf(f,"CCClient %s # Central Control client name\n\n",fCCClient);
|
|---|
| 167 | fprintf(f,"IsDAC %s # Define here if user input is interpreted as DAC value or voltage\n\n",((IsDAC) ? "TRUE" : "FALSE"));
|
|---|
| 168 | fprintf(f,"HVCalibOffset %f # Calibration of DAC to voltage values\n",fHVCalibOffset);
|
|---|
| 169 | fprintf(f,"HVCalibSlope %f # Calibration of DAC to voltage values\n\n",fHVCalibSlope);
|
|---|
| 170 | fprintf(f,"HVMaxDiff %f # Speed for HV changes limited to 1V/ms\n",fHVMaxDiff);
|
|---|
| 171 |
|
|---|
| 172 | fprintf(f,"# List of HV boards (at most %d HV boards will be recognized):\n",MAX_NUM_HVBOARDS);
|
|---|
| 173 |
|
|---|
| 174 | for (int i=0;i<NumHVBoards;i++)
|
|---|
| 175 | fprintf(f,"Board%d %s\n",USBDeviceNumber[i],fUSBDevice[i]);
|
|---|
| 176 |
|
|---|
| 177 | fprintf(fptr,"Configuration file successfully updated\n");
|
|---|
| 178 |
|
|---|
| 179 | fclose(f);
|
|---|
| 180 | return 1;
|
|---|
| 181 |
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|