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 <Utilities.h>
|
giuliomoro@74
|
44 #include <cmath>
|
giuliomoro@74
|
45 #include <rtdk.h>
|
giuliomoro@74
|
46
|
giuliomoro@74
|
47 // setup() is called once before the audio rendering starts.
|
giuliomoro@74
|
48 // Use it to perform any initialisation and allocation which is dependent
|
giuliomoro@74
|
49 // on the period size or sample rate.
|
giuliomoro@74
|
50 //
|
giuliomoro@74
|
51 // userData holds an opaque pointer to a data structure that was passed
|
giuliomoro@74
|
52 // in from the call to initAudio().
|
giuliomoro@74
|
53 //
|
giuliomoro@74
|
54 // Return true on success; returning false halts the program.
|
giuliomoro@74
|
55
|
giuliomoro@301
|
56 bool setup(BelaContext *context, void *userData)
|
giuliomoro@74
|
57 {
|
andrewm@310
|
58 pinMode(context, 0, P8_07, OUTPUT);
|
giuliomoro@74
|
59 return true;
|
giuliomoro@74
|
60 }
|
giuliomoro@74
|
61
|
giuliomoro@74
|
62 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@74
|
63 // Input and output are given from the audio hardware and the other
|
giuliomoro@74
|
64 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
|
giuliomoro@74
|
65 // will be 0.
|
giuliomoro@74
|
66
|
giuliomoro@75
|
67 /* basic_blink
|
giuliomoro@75
|
68 * Connect an LED in series with a 470ohm resistor between P8_07 and ground.
|
giuliomoro@75
|
69 * The LED will blink every @interval seconds.
|
giuliomoro@75
|
70 */
|
giuliomoro@75
|
71
|
giuliomoro@301
|
72 void render(BelaContext *context, void *userData)
|
giuliomoro@74
|
73 {
|
giuliomoro@74
|
74 static int count=0; //counts elapsed samples
|
giuliomoro@74
|
75 float interval=0.5; //how often to toggle the LED (in seconds)
|
giuliomoro@74
|
76 static int status=GPIO_LOW;
|
giuliomoro@74
|
77 for(unsigned int n=0; n<context->digitalFrames; n++){
|
giuliomoro@74
|
78 if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed
|
giuliomoro@74
|
79 count=0; //reset the counter
|
andrewm@308
|
80 // status=digitalRead(context, 0, P8_07);
|
andrewm@81
|
81 if(status==GPIO_LOW) { //toggle the status
|
andrewm@308
|
82 digitalWrite(context, n, P8_07, status); //write the status to the LED
|
andrewm@81
|
83 status=GPIO_HIGH;
|
andrewm@81
|
84 }
|
andrewm@81
|
85 else {
|
andrewm@308
|
86 digitalWrite(context, n, P8_07, status); //write the status to the LED
|
andrewm@81
|
87 status=GPIO_LOW;
|
andrewm@81
|
88 }
|
giuliomoro@74
|
89 }
|
giuliomoro@74
|
90 count++;
|
giuliomoro@74
|
91 }
|
giuliomoro@74
|
92 }
|
giuliomoro@74
|
93
|
giuliomoro@74
|
94 // cleanup() is called once at the end, after the audio has stopped.
|
giuliomoro@74
|
95 // Release any resources that were allocated in setup().
|
giuliomoro@74
|
96
|
giuliomoro@301
|
97 void cleanup(BelaContext *context, void *userData)
|
giuliomoro@74
|
98 {
|
giuliomoro@74
|
99 // Nothing to do here
|
giuliomoro@74
|
100 }
|