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