comparison examples/11-Extras/stepper/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
1 /*
2 ____ _____ _ _
3 | __ )| ____| | / \
4 | _ \| _| | | / _ \
5 | |_) | |___| |___ / ___ \
6 |____/|_____|_____/_/ \_\
7
8 The platform for ultra-low latency audio and sensor processing
9
10 http://bela.io
11
12 A project of the Augmented Instruments Laboratory within the
13 Centre for Digital Music at Queen Mary University of London.
14 http://www.eecs.qmul.ac.uk/~andrewm
15
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
19
20 The Bela software is distributed under the GNU Lesser General Public License
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */
23
24
25 #include <Bela.h>
26
27 const int kStepLengthSlow = 1000;
28 const int kStepLengthFast = 500;
29
30 int gStepLengthSamples = kStepLengthSlow;
31
32 const int gPinA1 = P8_27;
33 const int gPinA2 = P8_28;
34 const int gPinB1 = P8_29;
35 const int gPinB2 = P8_30;
36 const int gPinServo = P9_16;
37
38 int gStepCounter = 0;
39 int gPhase = 0;
40
41 int gServoCounter = 0;
42
43
44 enum {
45 kStateMoveRight1 = 0,
46 kStateMoveLeft1,
47 kStateMoveRight2,
48 kStateMoveLeft2,
49 kStateMoveRight3,
50 kStateMoveLeft3,
51 kStateSpin,
52 kStateMax
53 };
54
55 int gState = 0;
56 int gStateCounter = 0;
57
58 // setup() is called once before the audio rendering starts.
59 // Use it to perform any initialisation and allocation which is dependent
60 // on the period size or sample rate.
61 //
62 // userData holds an opaque pointer to a data structure that was passed
63 // in from the call to initAudio().
64 //
65 // Return true on success; returning false halts the program.
66
67 bool setup(BelaContext *context, void *userData)
68 {
69 // This project makes the assumption that the audio and digital
70 // sample rates are the same. But check it to be sure!
71 if(context->audioFrames != context->digitalFrames) {
72 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n");
73 return false;
74 }
75
76 pinMode(context, 0, gPinA1, OUTPUT);
77 pinMode(context, 0, gPinA2, OUTPUT);
78 pinMode(context, 0, gPinB1, OUTPUT);
79 pinMode(context, 0, gPinB2, OUTPUT);
80 pinMode(context, 0, gPinServo, OUTPUT);
81
82 return true;
83 }
84
85 // render() is called regularly at the highest priority by the audio engine.
86 // Input and output are given from the audio hardware and the other
87 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
88 // will be 0.
89
90 void render(BelaContext *context, void *userData)
91 {
92 for(unsigned int n = 0; n < context->audioFrames; n++) {
93 if(gPhase == 0 || gPhase == 1) {
94 digitalWriteOnce(context, n, gPinB1, HIGH);
95 digitalWriteOnce(context, n, gPinB2, LOW);
96 }
97 else {
98 digitalWriteOnce(context, n, gPinB1, LOW);
99 digitalWriteOnce(context, n, gPinB2, HIGH);
100 }
101
102 if(gPhase == 1 || gPhase == 2) {
103 digitalWriteOnce(context, n, gPinA1, HIGH);
104 digitalWriteOnce(context, n, gPinA2, LOW);
105 }
106 else {
107 digitalWriteOnce(context, n, gPinA1, LOW);
108 digitalWriteOnce(context, n, gPinA2, HIGH);
109 }
110
111 if(--gServoCounter > 0)
112 digitalWriteOnce(context, n, gPinServo, HIGH);
113 else
114 digitalWriteOnce(context, n, gPinServo, LOW);
115
116 if(++gStepCounter >= gStepLengthSamples) {
117 gStateCounter++;
118
119 switch(gState) {
120 case kStateMoveRight1:
121 case kStateMoveRight2:
122 case kStateMoveRight3:
123 gPhase = (gPhase + 1) & 3;
124 break;
125 case kStateMoveLeft1:
126 case kStateMoveLeft2:
127 case kStateMoveLeft3:
128 gPhase = (gPhase + 3) & 3;
129 break;
130 case kStateSpin:
131 gPhase = (gPhase + 1) & 3;
132 break;
133 }
134
135 if(gState == kStateSpin) {
136 if(gStateCounter >= 48) {
137 gStateCounter = 0;
138 gState = 0;
139 gStepLengthSamples = kStepLengthSlow;
140 }
141 }
142 else {
143 if(gStateCounter >= 16) {
144 gStateCounter = 0;
145 gState++;
146 if(gState & 1)
147 gServoCounter = 120;
148 else
149 gServoCounter = 80;
150 if(gState == kStateSpin)
151 gStepLengthSamples = kStepLengthFast;
152 }
153 }
154
155 gStepCounter = 0;
156 }
157 }
158 }
159
160 // cleanup() is called once at the end, after the audio has stopped.
161 // Release any resources that were allocated in setup().
162
163 void cleanup(BelaContext *context, void *userData)
164 {
165
166 }