Mercurial > hg > beaglert
comparison examples/scope_analogue/render.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/scope_analogue/render.cpp@0ee6eebb567a |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
1 // this example reads the analogue inputs 0 and 1 | |
2 // and generates a sine wave with an amplitude and | |
3 // frequency determined by their values | |
4 // it then plots these on the oscilloscope | |
5 | |
6 #include <BeagleRT.h> | |
7 #include <cmath> | |
8 #include <Scope.h> | |
9 | |
10 Scope scope; | |
11 | |
12 float gInverseSampleRate; | |
13 float gPhase; | |
14 | |
15 bool setup(BeagleRTContext *context, void *userData) | |
16 { | |
17 | |
18 // setup the scope with 3 channels at the audio sample rate | |
19 scope.setup(3, context->audioSampleRate); | |
20 | |
21 gInverseSampleRate = 1.0 / context->audioSampleRate; | |
22 gPhase = 0.0; | |
23 | |
24 return true; | |
25 } | |
26 | |
27 void render(BeagleRTContext *context, void *userData) | |
28 { | |
29 | |
30 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
31 | |
32 // read analogIn channels 0 and 1 | |
33 float in1 = analogReadFrame(context, n, 0); | |
34 float in2 = analogReadFrame(context, n, 1); | |
35 | |
36 // map in1 to amplitude and in2 to frequency | |
37 float amplitude = in1 * 0.8f; | |
38 float frequency = map(in2, 0, 1, 100, 1000); | |
39 | |
40 // generate a sine wave with the amplitude and frequency | |
41 float out = amplitude * sinf(gPhase); | |
42 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; | |
43 if(gPhase > 2.0 * M_PI) | |
44 gPhase -= 2.0 * M_PI; | |
45 | |
46 // log the sine wave and sensor values on the scope | |
47 scope.log(out, in1, in2); | |
48 | |
49 // pass the sine wave to the audio outputs | |
50 for(unsigned int channel = 0; channel < context->audioChannels; channel++) | |
51 context->audioOut[n * context->audioChannels + channel] = out; | |
52 | |
53 } | |
54 } | |
55 | |
56 void cleanup(BeagleRTContext *context, void *userData) | |
57 { | |
58 | |
59 } |