robert@285: /* robert@285: ____ _____ _ _ robert@285: | __ )| ____| | / \ robert@285: | _ \| _| | | / _ \ robert@285: | |_) | |___| |___ / ___ \ robert@285: |____/|_____|_____/_/ \_\.io robert@285: robert@285: */ robert@285: robert@285: /** robert@285: \example 3_scope_analog robert@285: robert@285: Connecting potentiometers robert@285: ------------------------- robert@285: robert@285: This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and robert@285: generates a sine wave with amplitude and frequency determined by their values. robert@285: It's best to connect a 10K potentiometer to each of these analog inputs. Far robert@285: left and far right pins of the pot go to 3.3V and GND, the middle should be robert@285: connected to the analog in pins. robert@285: robert@285: The sine wave is then plotted on the oscilloscope. Click the Open Scope button to robert@285: view the results. As you turn the potentiometers you will see the amplitude and robert@285: frequency of the sine wave change. robert@285: robert@285: This project also shows as example of `map()` which allows you to re-scale a number robert@285: from one range to another. Note that `map()` does not constrain your variable robert@285: within the upper and lower limits. If you want to do this use the `constrain()` robert@285: function. robert@285: robert@285: */ robert@285: robert@285: robert@285: robert@285: l@273: l@273: #include l@273: #include l@273: #include l@273: l@273: Scope scope; l@273: l@273: float gInverseSampleRate; l@273: float gPhase; l@273: l@273: bool setup(BeagleRTContext *context, void *userData) l@273: { l@273: l@273: // setup the scope with 3 channels at the audio sample rate l@273: scope.setup(3, context->audioSampleRate); l@273: l@273: gInverseSampleRate = 1.0 / context->audioSampleRate; l@273: gPhase = 0.0; l@273: l@273: return true; l@273: } l@273: l@273: void render(BeagleRTContext *context, void *userData) l@273: { l@273: l@273: for(unsigned int n = 0; n < context->audioFrames; n++) { l@273: l@273: // read analogIn channels 0 and 1 l@273: float in1 = analogReadFrame(context, n, 0); l@273: float in2 = analogReadFrame(context, n, 1); l@273: l@273: // map in1 to amplitude and in2 to frequency l@273: float amplitude = in1 * 0.8f; l@273: float frequency = map(in2, 0, 1, 100, 1000); l@273: l@273: // generate a sine wave with the amplitude and frequency l@273: float out = amplitude * sinf(gPhase); l@273: gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; l@273: if(gPhase > 2.0 * M_PI) l@273: gPhase -= 2.0 * M_PI; l@273: l@273: // log the sine wave and sensor values on the scope l@273: scope.log(out, in1, in2); l@273: l@273: // pass the sine wave to the audio outputs l@273: for(unsigned int channel = 0; channel < context->audioChannels; channel++) l@273: context->audioOut[n * context->audioChannels + channel] = out; l@273: l@273: } l@273: } l@273: l@273: void cleanup(BeagleRTContext *context, void *userData) l@273: { l@273: l@273: }