robert@285
|
1 /*
|
robert@285
|
2 ____ _____ _ _
|
robert@285
|
3 | __ )| ____| | / \
|
robert@285
|
4 | _ \| _| | | / _ \
|
robert@285
|
5 | |_) | |___| |___ / ___ \
|
robert@285
|
6 |____/|_____|_____/_/ \_\.io
|
robert@285
|
7
|
robert@285
|
8 */
|
robert@285
|
9
|
robert@285
|
10 /**
|
robert@285
|
11 \example 1_scope_basic
|
robert@285
|
12
|
robert@285
|
13 Oscilloscope in-browser
|
robert@285
|
14 -----------------------
|
robert@285
|
15
|
robert@285
|
16 This example demonstrates the scope feature of the IDE.
|
robert@285
|
17
|
robert@285
|
18 The scope is instantiated at the top of the file via `Scope scope;`
|
robert@285
|
19
|
robert@285
|
20 In `setup()` we define how many channels the scope should have and the sample
|
robert@285
|
21 rate that it should run at via `scope.setup(3, context->audioSampleRate)`.
|
robert@285
|
22
|
robert@285
|
23 In `render()` we choose what the scope log via `scope.log(out, out2, out3)`.
|
robert@285
|
24 In this example the scope is logging three sine waves with different phases. To see
|
robert@285
|
25 the output click on the <b>Open Scope</b> button.
|
robert@285
|
26
|
robert@285
|
27 An additional option is to set the trigger of the oscilloscope from within `render()`.
|
robert@285
|
28 In this example we are triggering the scope when oscillator 1 becomes less than
|
robert@285
|
29 oscillator 2 via `scope.trigger()`. Note that this functionality only takes effect
|
robert@285
|
30 when the triggering mode is set to custom in the scope UI.
|
robert@285
|
31
|
robert@285
|
32 */
|
robert@285
|
33
|
robert@285
|
34
|
l@273
|
35 #include <BeagleRT.h>
|
l@273
|
36 #include <Scope.h>
|
l@273
|
37 #include <cmath>
|
l@273
|
38
|
l@273
|
39 // set the frequency of the oscillators
|
l@273
|
40 float gFrequency = 110.0;
|
l@273
|
41 float gPhase;
|
l@273
|
42 float gInverseSampleRate;
|
l@273
|
43
|
l@273
|
44 // instantiate the scope
|
l@273
|
45 Scope scope;
|
l@273
|
46
|
l@273
|
47 bool setup(BeagleRTContext *context, void *userData)
|
l@273
|
48 {
|
l@273
|
49 // tell the scope how many channels and the sample rate
|
l@273
|
50 scope.setup(3, context->audioSampleRate);
|
l@273
|
51
|
l@273
|
52 gPhase = 0;
|
l@273
|
53 gInverseSampleRate = 1.0f/context->audioSampleRate;
|
l@273
|
54
|
l@273
|
55 return true;
|
l@273
|
56 }
|
l@273
|
57
|
l@273
|
58 float lastOut = 0.0;
|
l@273
|
59 float lastOut2 = 0.0;
|
l@273
|
60 void render(BeagleRTContext *context, void *userData)
|
l@273
|
61 {
|
l@273
|
62 // iterate over the audio frames and create three oscillators, seperated in phase by PI/2
|
l@273
|
63 for (unsigned int n=0; n<context->audioFrames; n++){
|
l@273
|
64 float out = 0.8f * sinf(gPhase);
|
l@273
|
65 float out2 = 0.8f * sinf(gPhase - M_PI/2);
|
l@273
|
66 float out3 = 0.8f * sinf(gPhase + M_PI/2);
|
l@273
|
67 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
|
l@273
|
68 if(gPhase > 2.0 * M_PI)
|
l@273
|
69 gPhase -= 2.0 * M_PI;
|
l@273
|
70
|
l@273
|
71 // log the three oscillators to the scope
|
l@273
|
72 scope.log(out, out2, out3);
|
l@273
|
73
|
l@273
|
74 // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2
|
l@273
|
75 // note this has no effect unless trigger mode is set to custom in the scope UI
|
l@273
|
76 if (lastOut >= lastOut2 && out < out2){
|
l@273
|
77 scope.trigger();
|
l@273
|
78 }
|
l@273
|
79
|
l@273
|
80 lastOut = out;
|
l@273
|
81 lastOut2 = out2;
|
l@273
|
82 }
|
l@273
|
83 }
|
l@273
|
84
|
l@273
|
85 void cleanup(BeagleRTContext *context, void *userData)
|
l@273
|
86 {
|
l@273
|
87
|
l@273
|
88 }
|