Mercurial > hg > beaglert
comparison examples/03-Analog/scope-analog/render.cpp @ 542:3016638b4da2 prerelease
Analog examples updated
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Fri, 24 Jun 2016 13:00:31 +0100 |
parents | 1cec96845a23 |
children |
comparison
equal
deleted
inserted
replaced
541:c301cc07ae11 | 542:3016638b4da2 |
---|---|
35 { | 35 { |
36 | 36 |
37 // setup the scope with 3 channels at the audio sample rate | 37 // setup the scope with 3 channels at the audio sample rate |
38 scope.setup(3, context->audioSampleRate); | 38 scope.setup(3, context->audioSampleRate); |
39 | 39 |
40 // Check if analog channels are enabled | |
41 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) { | |
42 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n"); | |
43 return false; | |
44 } | |
45 | |
46 // Check that we have the same number of inputs and outputs. | |
47 if(context->audioInChannels != context->audioOutChannels || | |
48 context->analogInChannels != context-> analogOutChannels){ | |
49 printf("Error: for this project, you need the same number of input and output channels.\n"); | |
50 return false; | |
51 } | |
52 | |
40 gInverseSampleRate = 1.0 / context->audioSampleRate; | 53 gInverseSampleRate = 1.0 / context->audioSampleRate; |
41 gPhase = 0.0; | 54 gPhase = 0.0; |
42 | 55 |
43 return true; | 56 return true; |
44 } | 57 } |
64 | 77 |
65 // log the sine wave and sensor values on the scope | 78 // log the sine wave and sensor values on the scope |
66 scope.log(out, in1, in2); | 79 scope.log(out, in1, in2); |
67 | 80 |
68 // pass the sine wave to the audio outputs | 81 // pass the sine wave to the audio outputs |
69 for(unsigned int channel = 0; channel < context->audioChannels; channel++) | 82 for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { |
70 context->audioOut[n * context->audioChannels + channel] = out; | 83 audioWrite(context, n, channel, out); |
84 } | |
85 | |
71 | 86 |
72 } | 87 } |
73 } | 88 } |
74 | 89 |
75 void cleanup(BelaContext *context, void *userData) | 90 void cleanup(BelaContext *context, void *userData) |
79 | 94 |
80 | 95 |
81 /** | 96 /** |
82 \example scope-analog/render.cpp | 97 \example scope-analog/render.cpp |
83 | 98 |
84 Connecting potentiometers | 99 Scoping sensor input |
85 ------------------------- | 100 ------------------------- |
86 | 101 |
87 This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and | 102 This example reads from analogue inputs 0 and 1 via `analogRead()` and |
88 generates a sine wave with amplitude and frequency determined by their values. | 103 generates a sine wave with amplitude and frequency determined by their values. |
89 It's best to connect a 10K potentiometer to each of these analog inputs. Far | 104 It's best to connect a 10K potentiometer to each of these analog inputs. Far |
90 left and far right pins of the pot go to 3.3V and GND, the middle should be | 105 left and far right pins of the pot go to 3.3V and GND, the middle should be |
91 connected to the analog in pins. | 106 connected to the analog in pins. |
92 | 107 |
93 The sine wave is then plotted on the oscilloscope. Click the Open Scope button to | 108 The sine wave is then plotted on the oscilloscope. Click the Open Scope button to |
94 view the results. As you turn the potentiometers you will see the amplitude and | 109 view the results. As you turn the potentiometers you will see the amplitude and |
95 frequency of the sine wave change. | 110 frequency of the sine wave change. You can also see the two sensor readings plotted |
111 on the oscilloscope. | |
112 | |
113 The scope is initialised in `setup()` where the number of channels and sampling rate | |
114 are set. | |
115 | |
116 ````` | |
117 scope.setup(3, context->audioSampleRate); | |
118 ````` | |
119 | |
120 We can then pass signals to the scope in `render()` using: | |
121 | |
122 `````` | |
123 scope.log(out, in1, in2); | |
124 `````` | |
96 | 125 |
97 This project also shows as example of `map()` which allows you to re-scale a number | 126 This project also shows as example of `map()` which allows you to re-scale a number |
98 from one range to another. Note that `map()` does not constrain your variable | 127 from one range to another. Note that `map()` does not constrain your variable |
99 within the upper and lower limits. If you want to do this use the `constrain()` | 128 within the upper and lower limits. If you want to do this use the `constrain()` |
100 function. | 129 function. |