forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ob_average.cpp
165 lines (140 loc) · 3.44 KB
/
ob_average.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
/* -----------------------------------------------------------------------------
BrainBay - Version 1.7, GPL 2003-2010
MODULE: OB_AVERAGE.CPP
Author: Chris Veigl
This Object outputs the Average of n Samples captured from it's input-port
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_average.h"
#include <iostream>
using namespace std;
AVERAGEOBJ::AVERAGEOBJ(int num) : BASE_CL()
{
outports = 1;
inports = 1;
strcpy(in_ports[0].in_name,"in");
strcpy(out_ports[0].out_name,"out");
accumulator = 0.0;
for (int i = 0; i < AVGSAMPLES; i++)
{
samples[i] = 0.0;
}
interval = 1;
writepos = 0;
added = 0;
}
void AVERAGEOBJ::session_start(void)
{
added = 0;
accumulator = 0;
}
void AVERAGEOBJ::session_reset(void)
{
added = 0;
accumulator = 0;
}
void AVERAGEOBJ::session_pos(long pos)
{
added = 0;
accumulator = 0;
}
void AVERAGEOBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_AVERAGEBOX, ghWndStatusbox, (DLGPROC)AverageDlgHandler));
}
void AVERAGEOBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("interval",P_INT,&interval);
}
void AVERAGEOBJ::save(HANDLE hFile)
{
save_object_basics(hFile,this);
save_property(hFile,"interval",P_INT,&interval);
}
void AVERAGEOBJ::incoming_data(int port, float value)
{
if (value!=INVALID_VALUE)
{
samples[writepos] = value;
accumulator += value;
added++;
if (added > interval)
{
int oldest = writepos - interval;
if (oldest < 0)
oldest += AVGSAMPLES;
accumulator -= samples[oldest];
added = interval;
}
writepos++;
if (writepos >= AVGSAMPLES)
writepos = 0;
}
}
void AVERAGEOBJ::work(void)
{
float average;
if (added)
{
average = accumulator / added;
pass_values(0, average);
}
}
void AVERAGEOBJ::change_interval(int newinterval)
{
interval = newinterval;
added = 0;
accumulator = 0;
}
AVERAGEOBJ::~AVERAGEOBJ() {}
LRESULT CALLBACK AverageDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool init;
AVERAGEOBJ * st;
st = (AVERAGEOBJ *) actobject;
if ((st==NULL)||(st->type!=OB_AVERAGE)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
SCROLLINFO lpsi;
lpsi.cbSize=sizeof(SCROLLINFO);
lpsi.fMask=SIF_RANGE|SIF_POS;
lpsi.nMin=1; lpsi.nMax=AVGSAMPLES - 1;
SetScrollInfo(GetDlgItem(hDlg,IDC_AVERAGEINTERVALBAR),SB_CTL,&lpsi, TRUE);
init = true;
SetScrollPos(GetDlgItem(hDlg,IDC_AVERAGEINTERVALBAR), SB_CTL,st->interval, TRUE);
SetDlgItemInt(hDlg, IDC_AVERAGEINTERVAL, st->interval, FALSE);
init = false;
break;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
return TRUE;
break;
case WM_HSCROLL:
{
int nNewPos;
if (!init && (nNewPos = get_scrollpos(wParam,lParam)) >= 0)
{
if (lParam == (long) GetDlgItem(hDlg,IDC_AVERAGEINTERVALBAR))
{
SetDlgItemInt(hDlg, IDC_AVERAGEINTERVAL, nNewPos, TRUE);
st->change_interval(nNewPos);
}
}
break;
}
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
return(TRUE);
}
return FALSE;
}