[vc_row][vc_column width=”1/1″][vc_column_text]A lot of people use Arduino, AVR to build an oscilloscope, and feel a lot of fun, so someone may ask if pcDuino can do it? Now we use pcDuino to build an oscilloscope.
[/vc_column_text][vc_tour][vc_tab title=”Parts List” tab_id=”1389578711-1-49″][vc_column_text]1.pcDuino
2 Several DuPont line.[/vc_column_text][/vc_tab][vc_tab title=”Software Environment” tab_id=”1389578711-2-10″][vc_column_text]
- pcDuino onboard ubuntu
- GCC 4.6
- QT 4.8.5: http://qt-project.org/downloads
- OpenGL: qt library comes
- Arduino SDK (c_enviroment): https://github.com/pcduino/c_enviroment
As usual, first we stroke some ideas , because I do not have LCD module ,so we can only use a monitor to act as a screen of the oscilloscope (27 -inch oscilloscope ) . Because it involves the drawing , and here I use OpenGL as drawing tools.
The principle is very simple, read the voltage change signal source via analogRead function pcDuino , and then build an OpenGL coordinate system , X -axis represents time , Y axis represents voltage , continuous voltage curves plotted over time , we get what we need waveform images.
QT and OpenGL environment to build , refer to this post , not repeat them here : http://www.pcduino.org/forum.php?mod=viewthread&tid=21&highlight =% E5% 9C% A8pcduino% E5% AE% 89% E8 % A3% 85Qt
Let’s look at the key GLWidget class code :
# include “OpenGLwidget.h”
# include <QTimer>
# include <QKeyEvent>
# include <math.h>
# include <iostream>
# include <GL/glu.h>
# include <Arduino.h>
# include <Serial.h>
# include <wiring_private.h>
/ / For performance reasons macro definitions to calculate the necessary constants
/ / step defines the x -axis opengl step , i.e. the x -axis are divided into a -1 to a window width (in pixels )
# define STEP 2/800.0
/ / units defines the window 800 of the x-axis points
# define UNITS 800
/ / 12bits ADC pcduino range from 0 to 4095 , plus or minus for each 4095
# define ANALOGVALUE 4095 * 2
/ / Vertex two-dimensional array definition opengl , and 800 points, each point consisting of two float
GLfloat ver [UNITS] [2];
/ / Define the initial value of the x-axis , ie, the leftmost screen -1.0
GLfloat xstep = -1.0;
/ / openglwidget constructor , set the window size, window title , and update opengl canvas signal
OpenGLWidget :: OpenGLWidget (QWidget * parent)
: QGLWidget (parent) {
setGeometry (0, 30, 800, 600);
setWindowTitle (“Joker’s OpenGL Framework”);
t = new QTimer (this);
connect (t, SIGNAL (timeout ()), this, SLOT (updateGL ()));
t-> start (0);
}
OpenGLWidget :: ~ OpenGLWidget () {
}
/ / initialization function of opengl
void OpenGLWidget :: initializeGL () {
glShadeModel (GL_SMOOTH);
/ / Set the color empty color parameters were RGBA, here is one I feel more handsome gray 🙂
glClearColor (0.3, 0.3, 0.3, 1.0);
glClearDepth (1.0);
/ / Open the depth test
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/ / Open opengl antialiasing
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_BLEND);
glEnable (GL_POINT_SMOOTH);
glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable (GL_POLYGON_SMOOTH);
glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
}
/ / opengl rendering function is called once to draw a picture
void OpenGLWidget :: paintGL () {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
/ / Clear the color buffer
glClear (GL_COLOR_BUFFER_BIT);
/ / Define viewpoints and perspectives pitch origin , as this example is only 2d graphics, can be ignored
gluLookAt (0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/ / Draw two line represents the x and y axes
glBegin (GL_LINES);
glColor3f (0.5,0.5,0.5);
/ / These two points can draw the x-axis
glVertex2f (-1.0f, 0.0f);
glVertex2f (1.0f, 0.0f);
/ / These two points can draw the y-axis
glVertex2f (0.0f,-1.0f);
glVertex2f (0.0f, 1.0f);
glEnd ();
/ / Draw the waveform , with a line strip opengl definition , line strip is actually a set of points connected by a straight line
glBegin (GL_LINE_STRIP);
/ / Define line strip of color , here is my opinion, more handsome green , haha
glColor3f (0.0,1.0,0.0);
/ / This will be achieved by circulating analogRead voltage values and converts the voltage value of the waveform in a y write vertex array
for (int i = 0; i <UNITS; i + +) {
* (ver [i]) = xstep + = STEP;
* (ver [i] +1) = analogRead (5) / ANALOGVALUE;
glVertex2fv (ver [i]);
}
/ / std :: cout << analogRead (5) / 4095.0 * 2 << std :: endl;
glEnd ();
/ / The vertex data pushed opengl rendering pipeline , start drawing
glFlush ();
}
/ / Callback function to change the window size
void OpenGLWidget :: resizeGL (int width, int height) {
/ / std :: cout << width << “*” << height << std :: endl;
if (height == 0)
{
height = 1;
}
glViewport (0,0, (GLint) width, (GLint) height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
/ / gluPerspective (45.0, (GLfloat) width / (GLfloat) height, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
The callback function / / keyboard keys, press esc to exit
void OpenGLWidget :: keyPressEvent (QKeyEvent * e) {
switch (e-> key ()) {
case Qt :: Key_Escape:
close ();
}
}
Compile and run , because I do not have a signal generator , I took a PWM signal pcduino simple test:
200Hz square wave PWM output , it seems clear.
We speed up the frequency to 800, shorten the duty cycle
Measured bandwidth of 2kHz can only support so,
Tired of watching neat square wave signal, I simply input an audio signal:
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.