-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
132 lines (79 loc) · 2.34 KB
/
func.cpp
File metadata and controls
132 lines (79 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <bitset>
#include <cstdlib>
#include <stdio.h>
#include <callfunc.h>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <cstdlib>
#include <bitset>
using namespace std;
//Setting control register
unsigned short *SetControlReg (int control)
{
const int nwords = 4; //total number of words including header
unsigned short *ret = new unsigned short[nwords]; //number of words including header
std::string headerstr = readheader ("SetControlReg"); //getting header from .dat file
cout << headerstr.c_str() << endl;
unsigned short header = bintohex (headerstr.c_str ());
cout << "header is " << hex << (int) header << endl;
ret = GetSixteenBits (control, nwords);
ret[0] = header;
return ret;
}
//Getting HPTDC control register
unsigned short *
GetControlReg (void)
{
const int nbytes = 1; //total number of bytes including header
unsigned short *ret = new unsigned short[nbytes]; //number of bytes including header
std::string headerstr = readheader ("GetControlReg"); //getting header from .dat file
unsigned short header = bintohex (headerstr.c_str ());
ret[0] = header;
ret[1]=0;
ret[2]=0;
ret[3]=0;
return ret;
}
// AUXILIAR FUNCTIONS
std::string readheader (string header = "")
{
std::ifstream inputFile ("data.dat");
std::string line;
std::string result = "0";
while (std::getline (inputFile, line))
{
std::string col1, col2;
std::istringstream ss (line);
ss >> col1 >> col2;
if (col1 == header)
result = col2;
}
return result;
}
unsigned short
bintohex (const char *digits)
{
unsigned short res = 0;
while (*digits)
res = (res << 1) | (*digits++ - '0');
return res;
}
unsigned short *
GetSixteenBits (int parameter, const int nwords)
{
unsigned short *ret = new unsigned short[nwords]; //number of words including header
const int wordsafterheader = nwords - 1;
int factor = 65535; // mask lowest 16 bits in decimal
for (int i = 0; i < wordsafterheader; i++)
{
int nEventspart = parameter & factor; // mask next 16 bits
unsigned short nEventspartfinal = nEventspart >> 16 * i; // rightshift by 14 bits
ret[wordsafterheader - i] = nEventspartfinal;
factor *= 65536;
}
ret[0] = 0; //filling the header with 0 for now
return ret;
}