This is a simple implementation of Oscilloscope control. This control shows last N points of user data. It has up to 8 channels (number of channels are defined by
OSC_MAX_CHANNELS parameter in header file). Each channel can show only one curve (in this version). Oscilloscope includes zoom in implementation by X (Horizontal), by Y (Vertical), and by both axes simultaneously (Rectangle). Zoom out is also available by pressing right mouse button in channel area (hold CTRL key to full zoom out).
Using the code
To use this control in your application:
- Design the dialog and add the Static control.
- Add the Oscilloscope.h header file to your project.
- Assign a
OscilloscopeCtrl to your static control.
OscilloscopeCtrl control to ID using the
Don't forget to turn on any channels you need.
#include <span class="code-string">"Oscilloscope.h"
class CMainDlg : public CDialogImpl<CMainDlg>
LRESULT OnInitDialog(UINT , WPARAM ,
LPARAM , BOOL& );
LRESULT CMainDlg::OnInitDialog(UINT , WPARAM ,
LPARAM , BOOL& )
m_oscill.SubclassWindow( ::GetDlgItem( IDC_AREA ) );
After creating Oscilloscope control, you can set special colors for curves and background (for each channel), point styles (how to draw the data points of curves), turn on/off grid, padding, and set padding coefficient. Padding is auto calculating value and used to offset curve by Y axis. By increasing padding coefficient (k>1), it is possible to decrease the gap between curve and channel top boundary box. If k<1, the gap will be increased (then k != 0). You can change oscilloscope layout by turning on/off X and Y axes and changing their positions (up or bottom for X axis; left or right for Y axis). Important: X axis is only one for all channels and Y axis is unique for every channel.
m_oscil.UseGrid(true); m_oscil.UsePadding(true); m_oscil.PaddingCoef(2.0); m_oscil.PointStyle(UTriangle); m_oscil.GetChannel(0)->SetLnColor( RGB(0,255,0) ); m_oscil.GetChannel(0)->SetBgColor( RGB(0,0,0) ); m_oscil.BottomXAxis(true); m_oscil.LeftYAxis(true); ...
Then you need to supply data for oscilloscope (pointers to double arrays). It's important that double arrays you pass to oscilloscope have to be global. To increase drawing speed, oscilloscope doesn't copy data to its buffers (!!!). The double arrays contains only Y values, oscilloscope calculates X values automatically by using user time.
m_oscil.SetPoints(m_time, m_values, m_cnt);
Removed using namespace
std from header file
Added double buffering to avoid flickering on draw (thanks to Michal Mecinski)
The small demo program shows how to use the oscilloscope control. Add buttons to control its layout, to change zoom mode, points styles, last points, and etc. You can change source code for any future usage.