forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob_file_writer.cpp
356 lines (291 loc) · 9.85 KB
/
ob_file_writer.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -----------------------------------------------------------------------------
BrainBay Version 1.7, GPL 2003-2010, contact: chris@shifz.org
MODULE: OB_FILE_WRITER.CPP
Author: Chris Veigl
Using this Object, a File containing raw or ASCII-integer values
of the connected signale can be written. Delimiters for the Columns can be selected
the Signals are connected to the input-ports.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
-----------------------------------------------------------------------------*/
#include "brainBay.h"
#include "ob_file_writer.h"
LRESULT CALLBACK FileWriterDlgHandler( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
FILE_WRITEROBJ * st;
static int actchn;
st = (FILE_WRITEROBJ *) actobject;
if ((st==NULL)||(st->type!=OB_FILE_WRITER)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
SetDlgItemText(hDlg, IDC_FILENAME, st->filename);
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "ASCII-Integer Values, TAB / CR+LF delimited");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "ASCII-Integer Values, Comma / CR+LF delimited");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "ASCII-Float Values, TAB / CR+LF delimited");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "ASCII-Float Values, Comma / CR+LF delimited");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "ASCII-BioExplorer with header");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "1-Channel raw integer 16bit signed");
SendDlgItemMessage(hDlg,IDC_FORMATCOMBO,CB_ADDSTRING,0,(LPARAM) (LPSTR) "1-Channel raw integer 16bit unsigned");
SendDlgItemMessage(hDlg, IDC_FORMATCOMBO, CB_SETCURSEL, st->format, 0L ) ;
if (st->state)
{
SetDlgItemText(hDlg,IDC_FILESTATUS,"writing File");
EnableWindow(GetDlgItem(hDlg, IDC_SELECT), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_START), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_STOP), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), FALSE);
}
else
{
if (st->file!=INVALID_HANDLE_VALUE)
{
SetDlgItemText(hDlg,IDC_FILESTATUS,"File opened");
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), TRUE);
}
else
{
SetDlgItemText(hDlg,IDC_FILESTATUS,"File not opened.");
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), FALSE);
}
EnableWindow(GetDlgItem(hDlg, IDC_SELECT), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_START), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_STOP), FALSE);
}
return TRUE;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
/*
case IDC_GENTESTFILE:
{
int i;
unsigned short chn1,chn2;
DWORD dwWritten;
// char tmp[1024];
struct {
unsigned char stream;
unsigned char nu1;
unsigned char chn1hi;
unsigned char chn1lo;
unsigned char chn2hi;
unsigned char chn2lo;
unsigned short nu2,nu3;
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char nu4;
} TX;
st->file=CreateFile(".\\archives\\sbg12bit.arc", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
for (i=0;i<2048;i++)
{
TX.nu1=0;
TX.nu2=0;
TX.nu3=0;
TX.nu4=0;
TX.stream= (unsigned char) ((i&3) << 6);
chn1 = (unsigned short) (2048.0+2000.0* sin ( ((float)i/256.0) * 3.1415 ));
chn2 = (unsigned short) (2048.0+1000.0* sin ( ((float)i/64.0) * 3.1415 ));;
TX.chn1hi= (unsigned char) ((chn1 & 0x0f00) >> 8);
TX.chn1lo= (unsigned char) (chn1 & 0xff);
TX.chn2hi= (unsigned char) ((chn2 & 0x0f00) >> 8);
TX.chn2lo= (unsigned char) (chn2 & 0xff);
TX.data1= (unsigned char) (i & 0x3f);
TX.data2= (unsigned char) (i & 0x80);
TX.data3= (unsigned char) ((i & 0x200)>>2);
WriteFile(st->file,& TX,sizeof(TX),&dwWritten,NULL);
}
CloseHandle(st->file);
}
break;
*/
case IDC_SELECT:
if (!strcmp(st->filename,"none"))
{
strcpy(st->filename,GLOBAL.resourcepath);
strcat(st->filename,"ARCHIVES\\*.*");
}
if (open_file_dlg(ghWndMain,st->filename, FT_TXT, OPEN_SAVE))
{
st->file=CreateFile(st->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (st->file==INVALID_HANDLE_VALUE)
{
SetDlgItemText(hDlg,IDC_FILESTATUS,"Could not create File");
strcpy(st->filename,"none");
st->state=0;
}
else
if (st->format==4)
{
char tmp[1024],tmp1[256];
int i,a;
DWORD dwWritten;
tmp[0]=0;
for(i=0;i<st->inports-1;i++)
{
sprintf(tmp1,"Sample Rate: %.4f",(float)PACKETSPERSECOND);
strcat(tmp,tmp1);
if (i!=st->inports-2) strcat (tmp,",");
else { a=strlen(tmp); tmp[a++]=13;tmp[a++]=10;tmp[a]=0;}
}
for(i=0;i<st->inports-1;i++)
{
sprintf(tmp1,"Source %d:",i+1);
strcat(tmp,tmp1);
strcat(tmp,st->in_ports[i].in_desc);
if (i!=st->inports-2) strcat (tmp,",");
else { a=strlen(tmp); tmp[a++]=13;tmp[a++]=10;tmp[a]=0;}
}
WriteFile(st->file,tmp,strlen(tmp),&dwWritten,NULL);
EnableWindow(GetDlgItem(hDlg, IDC_SELECT), FALSE); EnableWindow(GetDlgItem(hDlg, IDC_START), TRUE);
SetDlgItemText(hDlg,IDC_FILESTATUS,"File opened");
}
}
SetDlgItemText(hDlg,IDC_FILENAME,st->filename);
InvalidateRect(ghWndDesign,NULL,TRUE);
break;
case IDC_START:
if ((st->inports>0) &&(st->file!=INVALID_HANDLE_VALUE))
{
SetDlgItemText(hDlg,IDC_FILESTATUS, "writing File");
// START FILE WRITING
st->state=1;
EnableWindow(GetDlgItem(hDlg, IDC_START), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_STOP), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), FALSE);
} else SetDlgItemText(hDlg,IDC_FILESTATUS, "No Channels available.");
break;
case IDC_STOP:
SetDlgItemText(hDlg,IDC_FILESTATUS, "stopped");
st->state=0;
EnableWindow(GetDlgItem(hDlg, IDC_START), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_SELECT), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_STOP), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), TRUE);
break;
case IDC_CLOSE:
SetDlgItemText(hDlg,IDC_FILESTATUS, "file closed.");
st->state=0;
if (st->file!=INVALID_HANDLE_VALUE) CloseHandle(st->file);
st->file=INVALID_HANDLE_VALUE;
EnableWindow(GetDlgItem(hDlg, IDC_SELECT), TRUE);
EnableWindow(GetDlgItem(hDlg, IDC_START), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_CLOSE), FALSE);
InvalidateRect(ghWndDesign,NULL,TRUE);
break;
case IDC_FORMATCOMBO:
if (HIWORD(wParam)==CBN_SELCHANGE)
{
st->format=SendMessage(GetDlgItem(hDlg, IDC_FORMATCOMBO), CB_GETCURSEL , 0, 0);
}
break;
}
return TRUE;
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
return TRUE;
}
return FALSE;
}
//
// Object Implementation
//
FILE_WRITEROBJ::FILE_WRITEROBJ(int num) : BASE_CL()
{
outports = 0;
inports = 1;
width=70;
height=50;
state=0; format=0;
file=INVALID_HANDLE_VALUE;
strcpy(filename,"none");
}
void FILE_WRITEROBJ::update_inports(void)
{
if (state!=1)
{
inports=count_inports(this);
height=CON_START+inports*CON_HEIGHT+5;
InvalidateRect(ghWndMain,NULL,TRUE);
}
}
void FILE_WRITEROBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_FILE_WRITERBOX, ghWndStatusbox, (DLGPROC)FileWriterDlgHandler));
}
void FILE_WRITEROBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("filename",P_STRING,&filename);
load_property("format",P_INT,&format);
load_property("inports",P_INT,&inports);
height=CON_START+inports*CON_HEIGHT+5;
}
void FILE_WRITEROBJ::save(HANDLE hFile)
{
save_object_basics(hFile, this);
save_property(hFile,"filename",P_STRING,&filename);
save_property(hFile,"format",P_INT,&format);
save_property(hFile,"inports",P_INT,&inports);
}
void FILE_WRITEROBJ::incoming_data(int port, float value)
{
in_ports[port].value=value;
}
void FILE_WRITEROBJ::work(void)
{
int x;
char sztemp[100];
if ((inports==0)||(state==0)||(file==INVALID_HANDLE_VALUE)) return;
for (x=0;x<inports-1;x++)
{
if ((format==0) || (format==1))
wsprintf(sztemp,"%d",(int)in_ports[x].value);
else if ((format==2) || (format==3))
sprintf(sztemp,"%.2f",in_ports[x].value);
else if (format==4)
{
sprintf(sztemp,"%.11f",in_ports[x].value/1000000);
}
if (format<5)
{
if (x<inports-2)
{
if ((format==0) || (format==2)) strcat(sztemp,"\t");
else if ((format==1) || (format==3)) strcat(sztemp,", ");
else if (format==4) strcat(sztemp,",");
}
WriteFile(file,sztemp,strlen(sztemp),&dwWritten,NULL);
}
if ((format==5)&&(x==0))
{
sztemp[0]= (((int)in_ports[x].value) >> 8);
sztemp[1]= (((int)in_ports[x].value) & 0xff);
WriteFile(file,sztemp,2,&dwWritten,NULL);
}
if ((format==6)&&(x==0))
{
sztemp[0]= (((unsigned int)in_ports[x].value) >> 8);
sztemp[1]= (((unsigned int)in_ports[x].value) & 0xff);
WriteFile(file,sztemp,2,&dwWritten,NULL);
}
}
if (format<5)
{
wsprintf(sztemp,"\r\n");
WriteFile(file,sztemp,strlen(sztemp),&dwWritten,NULL);
}
}
FILE_WRITEROBJ::~FILE_WRITEROBJ()
{
if (file)
{
CloseHandle(file);
}
}