Tutorials/k0SDR/Tutorial04a: CWriteOML_spectrum.h

File CWriteOML_spectrum.h, 5.2 KB (added by nilanjan, 11 years ago)
Line 
1#include <iostream>
2#include <cstring>
3#include <string>
4#include <boost/lexical_cast.hpp>
5#include "oml2/omlc.h"
6#include <stdio.h>
7
8#define FALSE (unsigned int) 0
9#define TRUE (unsigned int) 1
10
11#define RECORD_TO_SERVER 1
12#define RECORD_TO_FILE 2
13
14#define USE_BLOB // FRUITFULLY USED WITH RECORD_TO_SERVER
15
16
17// Should be called moving average
18class CWriteOML
19{
20
21 private:
22
23 unsigned int mMeasurementPoints;
24 unsigned int mHeaderPoints;
25 unsigned int mFFTLength;
26 unsigned int mIdxNull;
27 unsigned int mRecordMode;
28 unsigned int mUseBlob;
29
30 OmlMPDef* mp_def;
31 OmlMP* mp;
32
33 void createMeasurementPoint(OmlMPDef* pOmlMPDef, std::string str, OmlValueT type)
34 {
35 char* cptr;
36 if (str == "NULL")
37 {
38 pOmlMPDef->name = NULL;
39 pOmlMPDef->param_types = type;
40
41 }
42 else
43 {
44 cptr = new char[str.size()+1];
45 strcpy (cptr, str.c_str());
46 pOmlMPDef->name = cptr;
47 pOmlMPDef->param_types = type;
48 }
49 }
50
51 public:
52 CWriteOML()
53 {
54
55 }
56
57 void init(std::string db_filename, std::string server_name)
58 {
59 char hostname[16];
60 int argc;
61 const char** argv;
62 hostname[15] = '\0';
63 gethostname(hostname, 15);
64
65 std::string mode(server_name.c_str());
66
67 if (mode == "file")
68 {
69 mRecordMode = RECORD_TO_FILE;
70 mUseBlob = FALSE;
71 argc = 7;
72 const char* argv_file[] = {"./spectrum", "--oml-id",(const char*)hostname, "--oml-exp-id",db_filename.c_str(), "--oml-file",db_filename.c_str()};
73 argv = argv_file;
74 }
75 else
76 {
77 mRecordMode = RECORD_TO_SERVER;
78#ifdef USE_BLOB
79 mUseBlob = TRUE;
80#else
81 mUseBlob = FALSE;
82#endif
83 argc = 7;
84 const char* argv_server[] = {"./spectrum", "--oml-id",(const char*)hostname, "--oml-exp-id",db_filename.c_str(), "--oml-server", server_name.c_str()};
85 argv = argv_server;
86 }
87
88 int result = omlc_init ("spectrum", &argc, argv, NULL);
89 if (result == -1) {
90 std::cerr << "Could not initialize OML\n";
91 exit (1);
92 }
93 }
94
95 void start(unsigned int points)
96 {
97 int result;
98
99 mHeaderPoints = 5;
100 mFFTLength = points;
101
102 if (mUseBlob == TRUE)
103 mMeasurementPoints = mHeaderPoints + 1;
104 else
105 mMeasurementPoints = mHeaderPoints + (1*mFFTLength);
106
107 mIdxNull = mMeasurementPoints;
108
109 mp_def = new OmlMPDef [(sizeof(OmlMPDef) * (mMeasurementPoints+1) )];
110
111 // mHeaderPoints
112 createMeasurementPoint(&mp_def[0], "sampling", (OmlValueT)OML_INT32_VALUE);
113 createMeasurementPoint(&mp_def[1], "cfreq_MHz", (OmlValueT)OML_DOUBLE_VALUE);
114 createMeasurementPoint(&mp_def[2], "gain_dB", (OmlValueT)OML_INT32_VALUE);
115 createMeasurementPoint(&mp_def[3], "FFTLength", (OmlValueT)OML_INT32_VALUE);
116 createMeasurementPoint(&mp_def[4], "FFTNum", (OmlValueT)OML_STRING_VALUE);
117
118 // mFFTLength
119 if (mUseBlob == TRUE)
120 {
121 createMeasurementPoint(&mp_def[5], "FFTBins", (OmlValueT)OML_BLOB_VALUE);
122 }
123 else
124 {
125 for (unsigned int i = mHeaderPoints; i < mIdxNull; ++i)
126 {
127 std::string str = "bin_" + boost::lexical_cast<std::string>(i-mHeaderPoints);
128 createMeasurementPoint(&mp_def[i], (char*)str.c_str(), (OmlValueT)OML_DOUBLE_VALUE);
129 }
130 }
131
132 createMeasurementPoint(&mp_def[mIdxNull], "NULL", (OmlValueT)0);
133
134 mp = omlc_add_mp ("packet_info", mp_def);
135
136 if (mp == NULL) {
137 std::cerr << "Error: could not register Measurement Point \"packet_info\"";
138 exit (1);
139 }
140
141 result = omlc_start();
142 if (result == -1) {
143 std::cerr << "Error starting up OML measurement streams\n";
144 exit (1);
145 }
146 }
147
148 void insert(uint32_t samp, float cf, uint32_t gain, char* text, float* buff)
149 {
150 OmlValueU values[mMeasurementPoints];
151 float* fptr = buff;
152
153 omlc_set_long (values[0], samp);
154 omlc_set_double (values[1], cf);
155 omlc_set_long (values[2], gain);
156 omlc_set_long (values[3], mFFTLength);
157 omlc_set_string (values[4], text);
158
159 if (mUseBlob == TRUE)
160 {
161 values[5].blobValue.data = NULL;
162 values[5].blobValue.size = 0;
163 omlc_set_blob (values[5], (char *)fptr, 1*mFFTLength*sizeof(float));
164 //std::cerr << *fptr << std::endl;
165 }
166 else
167 {
168 for (unsigned int i = mHeaderPoints; i < mIdxNull; ++i)
169 {
170 omlc_set_double (values[i], *fptr++);
171 }
172 }
173
174 omlc_inject (mp, values);
175 }
176
177 void stop()
178 {
179 omlc_close();
180 }
181
182 void print()
183 {
184
185 for (unsigned int i = 0;i < mMeasurementPoints; ++i)
186 std::cout << (char*)((&mp_def[i])->name) << " = " << mp_def[i].param_types << std::endl;
187
188 }
189
190 void test()
191 {
192 unsigned numberOfBins = 8;
193
194 std::vector<std::complex<float> > vc;
195 vc.resize(numberOfBins);
196 vc.at(0) = std::complex<float>(1,2);
197 vc.at(1) = std::complex<float>(3,4);
198 vc.at(2) = std::complex<float>(5,6);
199 vc.at(3) = std::complex<float>(7,8);
200
201 init("spectrum_measurement","oml:3003");
202 start(numberOfBins);
203 print();
204 insert(8e6, 5000, 20, (char*)"fone", (float*)&vc.front());
205 insert(11, 22, 20, (char*)"fone!", (float*)&vc.front());
206 stop();
207 }
208
209
210};
211