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 3_scope_analog
|
robert@285
|
12
|
robert@285
|
13 Connecting potentiometers
|
robert@285
|
14 -------------------------
|
robert@285
|
15
|
robert@285
|
16 This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and
|
robert@285
|
17 generates a sine wave with amplitude and frequency determined by their values.
|
robert@285
|
18 It's best to connect a 10K potentiometer to each of these analog inputs. Far
|
robert@285
|
19 left and far right pins of the pot go to 3.3V and GND, the middle should be
|
robert@285
|
20 connected to the analog in pins.
|
robert@285
|
21
|
robert@285
|
22 The sine wave is then plotted on the oscilloscope. Click the Open Scope button to
|
robert@285
|
23 view the results. As you turn the potentiometers you will see the amplitude and
|
robert@285
|
24 frequency of the sine wave change.
|
robert@285
|
25
|
robert@285
|
26 This project also shows as example of `map()` which allows you to re-scale a number
|
robert@285
|
27 from one range to another. Note that `map()` does not constrain your variable
|
robert@285
|
28 within the upper and lower limits. If you want to do this use the `constrain()`
|
robert@285
|
29 function.
|
robert@285
|
30
|
robert@285
|
31 */
|
robert@285
|
32
|
robert@285
|
33
|
robert@285
|
34
|
robert@285
|
35
|
l@273
|
36
|
l@273
|
37 #include <BeagleRT.h>
|
l@273
|
38 #include <cmath>
|
l@273
|
39 #include <Scope.h>
|
l@273
|
40
|
l@273
|
41 Scope scope;
|
l@273
|
42
|
l@273
|
43 float gInverseSampleRate;
|
l@273
|
44 float gPhase;
|
l@273
|
45
|
l@273
|
46 bool setup(BeagleRTContext *context, void *userData)
|
l@273
|
47 {
|
l@273
|
48
|
l@273
|
49 // setup the scope with 3 channels at the audio sample rate
|
l@273
|
50 scope.setup(3, context->audioSampleRate);
|
l@273
|
51
|
l@273
|
52 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
l@273
|
53 gPhase = 0.0;
|
l@273
|
54
|
l@273
|
55 return true;
|
l@273
|
56 }
|
l@273
|
57
|
l@273
|
58 void render(BeagleRTContext *context, void *userData)
|
l@273
|
59 {
|
l@273
|
60
|
l@273
|
61 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
l@273
|
62
|
l@273
|
63 // read analogIn channels 0 and 1
|
l@273
|
64 float in1 = analogReadFrame(context, n, 0);
|
l@273
|
65 float in2 = analogReadFrame(context, n, 1);
|
l@273
|
66
|
l@273
|
67 // map in1 to amplitude and in2 to frequency
|
l@273
|
68 float amplitude = in1 * 0.8f;
|
l@273
|
69 float frequency = map(in2, 0, 1, 100, 1000);
|
l@273
|
70
|
l@273
|
71 // generate a sine wave with the amplitude and frequency
|
l@273
|
72 float out = amplitude * sinf(gPhase);
|
l@273
|
73 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
|
l@273
|
74 if(gPhase > 2.0 * M_PI)
|
l@273
|
75 gPhase -= 2.0 * M_PI;
|
l@273
|
76
|
l@273
|
77 // log the sine wave and sensor values on the scope
|
l@273
|
78 scope.log(out, in1, in2);
|
l@273
|
79
|
l@273
|
80 // pass the sine wave to the audio outputs
|
l@273
|
81 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
|
l@273
|
82 context->audioOut[n * context->audioChannels + channel] = out;
|
l@273
|
83
|
l@273
|
84 }
|
l@273
|
85 }
|
l@273
|
86
|
l@273
|
87 void cleanup(BeagleRTContext *context, void *userData)
|
l@273
|
88 {
|
l@273
|
89
|
l@273
|
90 }
|