File: CtrlOScope.cpp

package info (click to toggle)
audmes 0%2Bgit20200429-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 632 kB
  • sloc: cpp: 10,536; ansic: 275; makefile: 7
file content (436 lines) | stat: -rw-r--r-- 15,401 bytes parent folder | download
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* CtrlOScope.cpp
 * 
 *  Change Log:
 *  ===========
 *  20080822 - renamed from OScopeCtrl (because of mess in the project)
 *           - added hard-setting of vertical lines for linear plot
 *           - double buffering !!!
 *           - added event EVT_ERASE_BACKGROUND
 *           - some other small improvements
 *
 *  verze 0.0.1
 *   - X osa logaritmicka
 *   - Y osa linearni
 *   - zadna kontrola rozsahu !!!!
 */
/*
 * Copyright (C) 2008 Vaclav Peroutka <vaclavpe@seznam.cz>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "CtrlOScope.h"

#include <math.h>
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!

/////////////////////////////////////////////////////////////////////////////
// CtrlOScope
CtrlOScope::CtrlOScope( wxWindow* parent, wxString xname, wxString yname, int tracks)
  : wxControl(parent,-1,wxDefaultPosition,wxSize(300,200))
{
  //  int i_cnt;

  m_width = 350;
  m_height = 250;
  m_tracks = tracks;

  m_bgColor.Set(0,0,0);
  m_plColor.Set(128,128,128);
  m_trColor.Set(192,192,0);
  m_tr2Color.Set(0,192, 192);
  m_whColor.Set( 255, 255, 255);

  m_YUnit = yname;
  m_XUnit = xname;

  m_NumberOfVerticals = 0;

  m_UserText = wxT("");

  Connect( -1, wxEVT_SIZE,(wxObjectEventFunction)& CtrlOScope::OnSize);
  Connect( -1, wxEVT_PAINT,(wxObjectEventFunction)& CtrlOScope::OnPaint);
  Connect( -1, wxEVT_ERASE_BACKGROUND,(wxObjectEventFunction)& CtrlOScope::OnEraseBackground);

}  // CtrlOScope

/////////////////////////////////////////////////////////////////////////////
CtrlOScope::~CtrlOScope()
{
} // ~CtrlOScope

void CtrlOScope::SetTrack( wxArrayDouble ardbl)
{
  m_points = ardbl;
  wxClientDC dc( this);
  PaintAll( dc);
}

void CtrlOScope::SetTrack2( wxArrayDouble ardbl)
{
  m_points2 = ardbl;
  wxClientDC dc( this);
  PaintAll( dc);
}

/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::SetXRange(double dLower, double dUpper, int logrange)
{
  m_MinXValue = dLower;
  m_MaxXValue = dUpper;
  m_LogX = logrange;
}  // SetRange

void CtrlOScope::SetYRange(double dLower, double dUpper, int logrange, int WXUNUSED(itrack))
{
  m_MinYValue = dLower;
  m_MaxYValue = dUpper;
  m_LogY = logrange;
}  // SetRange

void CtrlOScope::SetXUnits(wxString WXUNUSED(string), wxString WXUNUSED(XMin), wxString WXUNUSED(XMax) )
{
}  // SetXUnits

void CtrlOScope::SetYUnits(wxString WXUNUSED(string), wxString WXUNUSED(YMin), wxString WXUNUSED(YMax) )
{
}  // SetYUnits

/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::SetGridColor(wxColour WXUNUSED(color))
{
}  // SetGridColor


/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::SetPlotColor(wxColour WXUNUSED(color), int WXUNUSED(iTrack))
{
}  // SetPlotColor

/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::SetBackgroundColor(wxColour WXUNUSED(color))
{
}  // SetBackgroundColor

/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::AppendPoints(double WXUNUSED(dNewPoint)[], int WXUNUSED(iTrack) )
{
} // AppendPoints
 
wxSize CtrlOScope::GetSize( void) const
{
  //      wxMessageBox("GetSize", "Application Error", wxOK | wxICON_ERROR, this);
  return wxSize( m_width, m_height);
}

wxSize CtrlOScope::DoGetBestSize( void) const
{
  //  wxMessageBox("GetBestSize", "Application Error", wxOK | wxICON_ERROR, this);
  return wxSize(  m_width, m_height);
}


////////////////////////////////////////////////////////////////////////////
void CtrlOScope::OnPaint(wxPaintEvent& WXUNUSED(event)) 
{
	wxPaintDC dc(this) ;  // device context for painting
	PaintAll( dc);
}

void CtrlOScope::PaintAll( wxDC & dc)
{
  // here will be the Double Buffer
  wxRect rec = GetClientRect();

            wxBitmap* bmpBlit = new wxBitmap(rec.width,rec.height, 32);
            wxMemoryDC* memDC= new wxMemoryDC();
            //clear the memdc with a certain background color
            memDC->SelectObject(*bmpBlit);
#if defined(__WXMSW__)
            memDC->BeginDrawing();
#endif
            memDC->Clear();

  PaintAllFunction( *memDC, 0, 0);

#if defined(__WXMSW__)
            memDC->EndDrawing();
#endif

            dc.Blit(rec.x,rec.y,rec.width,rec.height,memDC,0,0,wxCOPY);

            delete bmpBlit;
            delete memDC;

}

void CtrlOScope::PaintAllFunction( wxDC & dc, int rectX, int rectY)
{
  wxRect rec = GetClientRect();
  wxString bla;
  int tw, th;
  int twY, thY;
  int twX, thX;
  int bdist, ldist;

  //	bla.Printf ("Rect is %d, %d, %d, %d", rectX, rectY, rec.width, rec.height);
  //	wxMessageBox( bla, "Application Error", wxOK | wxICON_ERROR, this);

  // kresli pozadi
  dc.SetPen( wxPen( m_bgColor,1,1));
  dc.SetBrush( wxBrush(m_bgColor, wxSOLID));
  dc.DrawRectangle( rectX, rectY, rec.width, rec.height);

  dc.SetPen( wxPen( m_plColor,1,1));
  dc.SetBrush( wxBrush(m_plColor, wxSOLID));
  dc.SetTextForeground( m_whColor);
  dc.SetFont(wxFont(8, wxMODERN, wxNORMAL, wxNORMAL, 0, wxT("")));
  /* spocitat vysku pisma pro spodni odstup a sirky pto odstup zleva */
  dc.GetTextExtent( m_XUnit, &twX, &thX); bdist = thX+5;
  dc.GetTextExtent( m_YUnit, &twY, &thY); ldist = twY + 5;
  bla.Printf(wxT("%.1f"), m_MinYValue); dc.GetTextExtent(bla, &tw, &th);
  if (ldist < (tw+5)) { ldist = tw+5;}
  bla.Printf(wxT("%.1f"), m_MaxYValue); dc.GetTextExtent(bla, &tw, &th);
  if (ldist < (tw+5)) { ldist = tw+5;}

  /* tisk legendy */
  if (m_YUnit != wxT("")) {
    dc.SetBrush( wxBrush(m_bgColor, wxSOLID));
    dc.DrawRectangle( 3, rectY+rec.height/2-2,twY+4, thY+4);
    dc.SetBrush( wxBrush(m_plColor, wxSOLID));
    dc.DrawText(m_YUnit, 5, rectY+rec.height/2);
  } else {
    ldist = tw;
  }
  if (m_XUnit == wxT("")) {
    dc.GetTextExtent( _T("T"), &twX, &thX); bdist = thX+5;
  } else {
    dc.GetTextExtent( m_XUnit, &twX, &thX); bdist = thX+5;
    dc.SetBrush( wxBrush(m_bgColor, wxSOLID));
    dc.DrawRectangle(rectX+rec.width/2-2, rectY+rec.height-bdist-2,twX+4, thX+4);
    dc.SetBrush( wxBrush(m_plColor, wxSOLID));
    dc.DrawText(m_XUnit, rectX+rec.width/2, rectY+rec.height-bdist);
  }

  /* spocitat jak casto se budou kreslit horizontalni cary */
  /* cara bude minimalne kazdych 30 pixelu; pocet 2,5,10,20,50 atd. */
  int nline = 50; int ydiv = 0; int nstrt = nline;
  while (ydiv == 0) {
    if ( rec.height < nline) {
      ydiv = 1*nline/nstrt;
    } else if ( rec.height < 2*nline) {
      ydiv = 2*nline/nstrt;
    } else if ( rec.height < 5*nline) {
      ydiv = 5*nline/nstrt;
    }
    nline *= 10;
  }
  //bla.Printf("ydiv = %d", ydiv); dc.DrawText(bla, rectX+40,rectY+20);
  for (int i = 0; i <= ydiv; i++) {
    /* kresli vsechny cary */
    float ystep = 1.0*(rec.height-bdist-5)/ydiv;
    dc.DrawLine(rectX+ldist, (int)(rec.height+rectY-ystep*i-bdist),rectX+rec.width-5, (int)(rec.height+rectY-ystep*i-bdist));
    bla.Printf(wxT("%.1f"), m_MinYValue+(m_MaxYValue-m_MinYValue)*i/ydiv); dc.GetTextExtent(bla, &tw, &th);
    if (ydiv == i) {
      dc.DrawText(bla, (int)(rectX+ldist-tw),(int)(rec.height+rectY-ystep*i-bdist) );
    } else {
      dc.DrawText(bla, (int)(rectX+ldist-tw),(int)(rec.height+rectY-ystep*i-bdist-th) );
    }
  }
  if ( ((m_MinYValue+m_MaxYValue) < 0.01)&&(1 == ydiv%2)) {
    /* kresli nulovou caru */
    dc.DrawLine(rectX+ldist, rectY+5+(rec.height-bdist-5)/2,rectX+rec.width-5, rectY+5+(rec.height-bdist-5)/2);
  }

  /* spocitat jak casto se budou kreslit vertikalni cary */
  /* ******************************************************************* */
  /* ******************************************************************* */
  /* ******************************************************************* */
  if( 1 == m_LogX) {
    /* logaritmicke meritko - spocitat pocet dekad a potom neco dale */
    /* cara bude minimalne kazdych 100 pixelu na dekadu; pak bude 1, 3, 10 */
    int ndecs = (int)( log10( m_MaxXValue/m_MinXValue)+.9);
    float npix = rec.width/ndecs;
    int xdiv;

    if ( npix < 70) {
      xdiv = 1;
    } else if ( npix < 200) {
      xdiv = 3;
    } else {
      xdiv = 10;
    }

    float xstep = 1.0*(rec.width-ldist-5)/ndecs; // pocet bodu na dekadu
    for (int i = 0; i <= ndecs; i++) {
      /* kresli vsechny cary */
      dc.DrawLine((int)(rectX+ldist+xstep*i), rectY+5, (int)(rectX+ldist+xstep*i), rectY+rec.height-bdist);
      /* nakresli vnitrni cary */
      if (3 == xdiv) {
	dc.DrawLine((int)(rectX+ldist+xstep*(i+log10(2))), rectY+5, (int)(rectX+ldist+xstep*(i+log10(2))), rectY+rec.height-bdist);
	dc.DrawLine((int)(rectX+ldist+xstep*(i+log10(5))), rectY+5, (int)(rectX+ldist+xstep*(i+log10(5))), rectY+rec.height-bdist);
      }
      if (10 == xdiv) {
	for( int j = 2; j <10; j++) {
	  dc.DrawLine((int)(rectX+ldist+xstep*(i+log10(j))), rectY+5, (int)(rectX+ldist+xstep*(i+log10(j))), rectY+rec.height-bdist);
	}
      }
      bla.Printf(wxT("%.1f"), m_MinXValue*pow(10,i)); dc.GetTextExtent(bla, &tw, &th);
      if ((xstep*i+ldist)<(tw/2)) {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i), rectY+rec.height-bdist);
      } else if ((ldist+xstep*i+tw/2) < rec.width) {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i-tw/2), rectY+rec.height-bdist);
      } else {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i-tw), rectY+rec.height-bdist);
      }
    }

    /* zobrazit body */
    //bla.Printf("Pocet bodu: %d", m_points.GetCount()); wxMessageBox(bla,"D",wxOK);
    dc.SetPen( wxPen( m_trColor,1,1));
    int lastx = 0, lasty = 0;
    // new plot function - walking through X points and select values from the table
    if ( m_points.GetCount() > 0) {
      for (long int i = 0; i < rec.width-ldist-5; i++) {
	// find the frequency
	float xfreq = m_MinXValue*pow(10,i/xstep);
	// find the position in the m_points[] array
	unsigned long int xposit = (unsigned long int)((xfreq/m_MaxXValue)*m_points.GetCount());

	if ( xposit < m_points.GetCount() ) { 

	  float ydatapoint = m_points[xposit];

	  if (ydatapoint > m_MaxYValue) {ydatapoint = m_MaxYValue; }
	  if (ydatapoint < m_MinYValue) { ydatapoint = m_MinYValue; }

	  float ypoint = rec.height-bdist - (rec.height-bdist-5)*( ydatapoint-m_MinYValue)/(m_MaxYValue-m_MinYValue);
	  if ( 0 == i ) {
	    dc.DrawPoint( (int) (i+rectX+ldist),  (int)(ypoint));
	  } else {
	    dc.DrawLine( lastx, lasty,  (int) (i+rectX+ldist),  (int)(ypoint));
	  }
	  lastx = (int)(i+rectX+ldist); lasty = (int)(ypoint);
	} // else skip the frequencies out ot FFT
      }
    }
    // right channel
    dc.SetPen( wxPen( m_tr2Color,1,1));
    if ( m_points2.GetCount() > 0) {
      for (long int i = 0; i < rec.width-ldist-5; i++) {
	// find the frequency
	float xfreq = m_MinXValue*pow(10,i/xstep);
	// find the position in the m_points[] array
	unsigned long int xposit = (unsigned long int)((xfreq/m_MaxXValue)*m_points2.GetCount());

	if ( xposit < m_points2.GetCount() ) { 

	  float ydatapoint = m_points2[xposit];

	  if (ydatapoint > m_MaxYValue) {ydatapoint = m_MaxYValue; }
	  if (ydatapoint < m_MinYValue) { ydatapoint = m_MinYValue; }

	  float ypoint = rec.height-bdist - (rec.height-bdist-5)*( ydatapoint-m_MinYValue)/(m_MaxYValue-m_MinYValue);
	  if ( 0 == i ) {
	    dc.DrawPoint( (int) (i+rectX+ldist),  (int)(ypoint));
	  } else {
	    dc.DrawLine( lastx, lasty,  (int) (i+rectX+ldist),  (int)(ypoint));
	  }
	  lastx = (int)(i+rectX+ldist); lasty = (int)(ypoint);
	} // else skip the frequencies out ot FFT
      }
    }

    /*  **************** linearni meritko *********************** */
  } else {
    /* cara bude minimalne kazdych 30 pixelu; pocet 2,5,10,20,50 atd. */
    // if we have m_NumberOfVerticals == 0, let's compute steps. But otherwise not
    int nline = 100; int xdiv = m_NumberOfVerticals; int nstrt = nline;
    while (xdiv == 0) {
      if ( rec.width < nline) {
	xdiv = 1*nline/nstrt;
      } else if ( rec.width < 2*nline) {
	xdiv = 2*nline/nstrt;
      } else if ( rec.width < 5*nline) {
	xdiv = 5*nline/nstrt;
      }
      nline *= 10;
    }

    for (int i = 0; i <= xdiv; i++) {
      /* kresli vsechny cary */
      float xstep = 1.0*(rec.width-ldist-5)/xdiv;

      dc.DrawLine((int)(rectX+xstep*i+ldist),rec.height+rectY-bdist, (int)(rectX+xstep*i+ldist),rectY+5);
      bla.Printf(wxT("%.1f"), m_MinXValue+(m_MaxXValue-m_MinXValue)*i/xdiv); dc.GetTextExtent(bla, &tw, &th);
      if ((xstep*i+ldist)<(tw/2)) {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i), rectY+rec.height-bdist);
      } else if ((ldist+xstep*i+tw/2) < rec.width) {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i-tw/2), rectY+rec.height-bdist);
      } else {
	dc.DrawText(bla, (int)(rectX+ldist+xstep*i-tw), rectY+rec.height-bdist);
      }
    }
    /* kresli body v SetTracks */
    dc.SetPen( wxPen( m_trColor,1,1));
    int lastx = 0, lasty = 0;
    float xpoint;
    for (unsigned int i = 0; i < m_points.GetCount(); i++) {
      /* prepocitat vstupni bod na X osu */
      //float xpoint = i+ldist; // linearni zavislost
      xpoint = ldist+i*(rec.width-ldist-5)/(m_MaxXValue-m_MinXValue);
      /* prepocitat Y hodnotu na Y osu - orezat hodnoty */
      if (m_points[i] > m_MaxYValue) { m_points[i] = m_MaxYValue; }
      if (m_points[i] < m_MinYValue) { m_points[i] = m_MinYValue; }
      float ypoint = rec.height-bdist - (rec.height-bdist-5)*(m_points[i]-m_MinYValue)/(m_MaxYValue-m_MinYValue);
      if ( 0 == i) {
	dc.DrawLine( (int)(xpoint),(int)(ypoint),(int)(xpoint),(int)(ypoint));
      } else {
	dc.DrawLine(lastx, lasty,(int)(xpoint),(int)(ypoint));
      }
      lastx = (int)(xpoint); lasty = (int)(ypoint);
    }
    dc.SetPen( wxPen( m_tr2Color,1,1));
    for (unsigned int i = 0; i < m_points2.GetCount(); i++) {
      /* prepocitat vstupni bod na X osu */
      //float xpoint = i+ldist; // linearni zavislost
      xpoint = ldist+i*(rec.width-ldist-5)/(m_MaxXValue-m_MinXValue);
      /* prepocitat Y hodnotu na Y osu - orezat hodnoty */
      if (m_points2[i] > m_MaxYValue) { m_points2[i] = m_MaxYValue; }
      if (m_points2[i] < m_MinYValue) { m_points2[i] = m_MinYValue; }
      float ypoint = rec.height-bdist - (rec.height-bdist-5)*(m_points2[i]-m_MinYValue)/(m_MaxYValue-m_MinYValue);
      if ( 0 == i) {
	dc.DrawLine( (int)(xpoint),(int)(ypoint),(int)(xpoint),(int)(ypoint));
      } else {
	dc.DrawLine(lastx, lasty,(int)(xpoint),(int)(ypoint));
      }
      lastx = (int)(xpoint); lasty = (int)(ypoint);
    }

    if (wxT("") != m_UserText) {
      dc.SetTextForeground( m_whColor);
      dc.DrawText( m_UserText, rectX+m_UserTextPosX, rectY+m_UserTextPosY);
    }
  }

} // OnPaint

/////////////////////////////////////////////////////////////////////////////
void CtrlOScope::OnSize(wxSizeEvent& event) //UINT nType, int cx, int cy)
{
  event.Skip();
} // OnSize