robert@372: /* robert@372: ____ _____ _ _ robert@372: | __ )| ____| | / \ robert@372: | _ \| _| | | / _ \ robert@372: | |_) | |___| |___ / ___ \ robert@372: |____/|_____|_____/_/ \_\.io robert@372: robert@372: */ robert@372: robert@372: /* robert@372: * robert@372: * Andrew McPherson and Victor Zappi robert@372: * Queen Mary, University of London robert@372: */ robert@372: robert@372: /** robert@372: \example 2_digital_blink robert@372: robert@372: Blinking an LED robert@372: --------------- robert@372: robert@372: This sketch shows the simplest case of digital out. robert@372: robert@372: - Connect an LED in series with a 470ohm resistor between P8_07 and ground. robert@372: robert@372: The led is blinked on and off by setting the digital pin `HIGH` and `LOW` every interval seconds which is set in robert@372: `render()`. robert@372: robert@372: In `setup()` the pin mode must be set to output mode via `pinMode()`. For example: robert@372: `pinMode(context, 0, P8_07, OUTPUT)`. robert@372: In `render()` the output of the digital pins is set by `digitalWrite()`. For example: robert@372: `digitalWrite(context, n, P8_07, status)` where `status` can be equal to robert@372: either `HIGH` or `LOW`. When set `HIGH` the pin will give 3.3V, when set to robert@372: `LOW` 0V. robert@372: robert@372: To keep track of elapsed time we have a sample counter count. When the count reaches robert@372: a certain limit it switches state to either `HIGH` or `LOW` depending on its current robert@372: value. In this case the limit is `context->digitalSampleRate*interval` which robert@372: allows us to write the desired interval in seconds, stored in `interval`. robert@372: */ robert@372: giuliomoro@301: #include giuliomoro@74: #include giuliomoro@74: #include giuliomoro@74: giuliomoro@74: // setup() is called once before the audio rendering starts. giuliomoro@74: // Use it to perform any initialisation and allocation which is dependent giuliomoro@74: // on the period size or sample rate. giuliomoro@74: // giuliomoro@74: // userData holds an opaque pointer to a data structure that was passed giuliomoro@74: // in from the call to initAudio(). giuliomoro@74: // giuliomoro@74: // Return true on success; returning false halts the program. giuliomoro@74: giuliomoro@301: bool setup(BelaContext *context, void *userData) giuliomoro@74: { andrewm@310: pinMode(context, 0, P8_07, OUTPUT); giuliomoro@74: return true; giuliomoro@74: } giuliomoro@74: giuliomoro@74: // render() is called regularly at the highest priority by the audio engine. giuliomoro@74: // Input and output are given from the audio hardware and the other giuliomoro@74: // ADCs and DACs (if available). If only audio is available, numAnalogFrames giuliomoro@74: // will be 0. giuliomoro@74: giuliomoro@75: /* basic_blink giuliomoro@75: * Connect an LED in series with a 470ohm resistor between P8_07 and ground. giuliomoro@75: * The LED will blink every @interval seconds. giuliomoro@75: */ giuliomoro@75: giuliomoro@301: void render(BelaContext *context, void *userData) giuliomoro@74: { giuliomoro@74: static int count=0; //counts elapsed samples giuliomoro@74: float interval=0.5; //how often to toggle the LED (in seconds) giuliomoro@74: static int status=GPIO_LOW; giuliomoro@74: for(unsigned int n=0; ndigitalFrames; n++){ giuliomoro@74: if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed giuliomoro@74: count=0; //reset the counter andrewm@308: // status=digitalRead(context, 0, P8_07); andrewm@81: if(status==GPIO_LOW) { //toggle the status andrewm@308: digitalWrite(context, n, P8_07, status); //write the status to the LED andrewm@81: status=GPIO_HIGH; andrewm@81: } andrewm@81: else { andrewm@308: digitalWrite(context, n, P8_07, status); //write the status to the LED andrewm@81: status=GPIO_LOW; andrewm@81: } giuliomoro@74: } giuliomoro@74: count++; giuliomoro@74: } giuliomoro@74: } giuliomoro@74: giuliomoro@74: // cleanup() is called once at the end, after the audio has stopped. giuliomoro@74: // Release any resources that were allocated in setup(). giuliomoro@74: giuliomoro@301: void cleanup(BelaContext *context, void *userData) giuliomoro@74: { giuliomoro@74: // Nothing to do here giuliomoro@74: }