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