annotate examples/basic_blink/render.cpp @ 430:2e01a9d6cb58 prerelease

Removed all the function()
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 16 Jun 2016 19:25:01 +0100
parents 9dc5a0ccad25
children
rev   line source
robert@372 1 /*
robert@372 2 ____ _____ _ _
robert@372 3 | __ )| ____| | / \
robert@372 4 | _ \| _| | | / _ \
robert@372 5 | |_) | |___| |___ / ___ \
robert@372 6 |____/|_____|_____/_/ \_\.io
robert@372 7
robert@372 8 */
robert@372 9
robert@372 10 /*
robert@372 11 *
robert@372 12 * Andrew McPherson and Victor Zappi
robert@372 13 * Queen Mary, University of London
robert@372 14 */
robert@372 15
robert@372 16 /**
robert@372 17 \example 2_digital_blink
robert@372 18
robert@372 19 Blinking an LED
robert@372 20 ---------------
robert@372 21
robert@372 22 This sketch shows the simplest case of digital out.
robert@372 23
robert@372 24 - Connect an LED in series with a 470ohm resistor between P8_07 and ground.
robert@372 25
robert@372 26 The led is blinked on and off by setting the digital pin `HIGH` and `LOW` every interval seconds which is set in
robert@372 27 `render()`.
robert@372 28
robert@372 29 In `setup()` the pin mode must be set to output mode via `pinMode()`. For example:
robert@372 30 `pinMode(context, 0, P8_07, OUTPUT)`.
robert@372 31 In `render()` the output of the digital pins is set by `digitalWrite()`. For example:
robert@372 32 `digitalWrite(context, n, P8_07, status)` where `status` can be equal to
robert@372 33 either `HIGH` or `LOW`. When set `HIGH` the pin will give 3.3V, when set to
robert@372 34 `LOW` 0V.
robert@372 35
robert@372 36 To keep track of elapsed time we have a sample counter count. When the count reaches
robert@372 37 a certain limit it switches state to either `HIGH` or `LOW` depending on its current
robert@372 38 value. In this case the limit is `context->digitalSampleRate*interval` which
robert@372 39 allows us to write the desired interval in seconds, stored in `interval`.
robert@372 40 */
robert@372 41
giuliomoro@301 42 #include <Bela.h>
giuliomoro@74 43 #include <cmath>
giuliomoro@74 44 #include <rtdk.h>
giuliomoro@74 45
giuliomoro@74 46 // setup() is called once before the audio rendering starts.
giuliomoro@74 47 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@74 48 // on the period size or sample rate.
giuliomoro@74 49 //
giuliomoro@74 50 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@74 51 // in from the call to initAudio().
giuliomoro@74 52 //
giuliomoro@74 53 // Return true on success; returning false halts the program.
giuliomoro@74 54
giuliomoro@301 55 bool setup(BelaContext *context, void *userData)
giuliomoro@74 56 {
andrewm@310 57 pinMode(context, 0, P8_07, OUTPUT);
giuliomoro@74 58 return true;
giuliomoro@74 59 }
giuliomoro@74 60
giuliomoro@74 61 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@74 62 // Input and output are given from the audio hardware and the other
giuliomoro@74 63 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
giuliomoro@74 64 // will be 0.
giuliomoro@74 65
giuliomoro@75 66 /* basic_blink
giuliomoro@75 67 * Connect an LED in series with a 470ohm resistor between P8_07 and ground.
giuliomoro@75 68 * The LED will blink every @interval seconds.
giuliomoro@75 69 */
giuliomoro@75 70
giuliomoro@301 71 void render(BelaContext *context, void *userData)
giuliomoro@74 72 {
giuliomoro@74 73 static int count=0; //counts elapsed samples
giuliomoro@74 74 float interval=0.5; //how often to toggle the LED (in seconds)
giuliomoro@74 75 static int status=GPIO_LOW;
giuliomoro@74 76 for(unsigned int n=0; n<context->digitalFrames; n++){
giuliomoro@74 77 if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed
giuliomoro@74 78 count=0; //reset the counter
andrewm@308 79 // status=digitalRead(context, 0, P8_07);
andrewm@81 80 if(status==GPIO_LOW) { //toggle the status
andrewm@308 81 digitalWrite(context, n, P8_07, status); //write the status to the LED
andrewm@81 82 status=GPIO_HIGH;
andrewm@81 83 }
andrewm@81 84 else {
andrewm@308 85 digitalWrite(context, n, P8_07, status); //write the status to the LED
andrewm@81 86 status=GPIO_LOW;
andrewm@81 87 }
giuliomoro@74 88 }
giuliomoro@74 89 count++;
giuliomoro@74 90 }
giuliomoro@74 91 }
giuliomoro@74 92
giuliomoro@74 93 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@74 94 // Release any resources that were allocated in setup().
giuliomoro@74 95
giuliomoro@301 96 void cleanup(BelaContext *context, void *userData)
giuliomoro@74 97 {
giuliomoro@74 98 // Nothing to do here
giuliomoro@74 99 }