robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: robert@464: const int kStepLengthSlow = 1000; robert@464: const int kStepLengthFast = 500; robert@464: robert@464: int gStepLengthSamples = kStepLengthSlow; robert@464: robert@464: const int gPinA1 = P8_27; robert@464: const int gPinA2 = P8_28; robert@464: const int gPinB1 = P8_29; robert@464: const int gPinB2 = P8_30; robert@464: const int gPinServo = P9_16; robert@464: robert@464: int gStepCounter = 0; robert@464: int gPhase = 0; robert@464: robert@464: int gServoCounter = 0; robert@464: robert@464: robert@464: enum { robert@464: kStateMoveRight1 = 0, robert@464: kStateMoveLeft1, robert@464: kStateMoveRight2, robert@464: kStateMoveLeft2, robert@464: kStateMoveRight3, robert@464: kStateMoveLeft3, robert@464: kStateSpin, robert@464: kStateMax robert@464: }; robert@464: robert@464: int gState = 0; robert@464: int gStateCounter = 0; robert@464: robert@464: // setup() is called once before the audio rendering starts. robert@464: // Use it to perform any initialisation and allocation which is dependent robert@464: // on the period size or sample rate. robert@464: // robert@464: // userData holds an opaque pointer to a data structure that was passed robert@464: // in from the call to initAudio(). robert@464: // robert@464: // Return true on success; returning false halts the program. robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: // This project makes the assumption that the audio and digital robert@464: // sample rates are the same. But check it to be sure! robert@464: if(context->audioFrames != context->digitalFrames) { robert@464: rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n"); robert@464: return false; robert@464: } robert@464: robert@464: pinMode(context, 0, gPinA1, OUTPUT); robert@464: pinMode(context, 0, gPinA2, OUTPUT); robert@464: pinMode(context, 0, gPinB1, OUTPUT); robert@464: pinMode(context, 0, gPinB2, OUTPUT); robert@464: pinMode(context, 0, gPinServo, OUTPUT); robert@464: robert@464: return true; robert@464: } robert@464: robert@464: // render() is called regularly at the highest priority by the audio engine. robert@464: // Input and output are given from the audio hardware and the other robert@464: // ADCs and DACs (if available). If only audio is available, numMatrixFrames robert@464: // will be 0. robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: for(unsigned int n = 0; n < context->audioFrames; n++) { robert@464: if(gPhase == 0 || gPhase == 1) { robert@464: digitalWriteOnce(context, n, gPinB1, HIGH); robert@464: digitalWriteOnce(context, n, gPinB2, LOW); robert@464: } robert@464: else { robert@464: digitalWriteOnce(context, n, gPinB1, LOW); robert@464: digitalWriteOnce(context, n, gPinB2, HIGH); robert@464: } robert@464: robert@464: if(gPhase == 1 || gPhase == 2) { robert@464: digitalWriteOnce(context, n, gPinA1, HIGH); robert@464: digitalWriteOnce(context, n, gPinA2, LOW); robert@464: } robert@464: else { robert@464: digitalWriteOnce(context, n, gPinA1, LOW); robert@464: digitalWriteOnce(context, n, gPinA2, HIGH); robert@464: } robert@464: robert@464: if(--gServoCounter > 0) robert@464: digitalWriteOnce(context, n, gPinServo, HIGH); robert@464: else robert@464: digitalWriteOnce(context, n, gPinServo, LOW); robert@464: robert@464: if(++gStepCounter >= gStepLengthSamples) { robert@464: gStateCounter++; robert@464: robert@464: switch(gState) { robert@464: case kStateMoveRight1: robert@464: case kStateMoveRight2: robert@464: case kStateMoveRight3: robert@464: gPhase = (gPhase + 1) & 3; robert@464: break; robert@464: case kStateMoveLeft1: robert@464: case kStateMoveLeft2: robert@464: case kStateMoveLeft3: robert@464: gPhase = (gPhase + 3) & 3; robert@464: break; robert@464: case kStateSpin: robert@464: gPhase = (gPhase + 1) & 3; robert@464: break; robert@464: } robert@464: robert@464: if(gState == kStateSpin) { robert@464: if(gStateCounter >= 48) { robert@464: gStateCounter = 0; robert@464: gState = 0; robert@464: gStepLengthSamples = kStepLengthSlow; robert@464: } robert@464: } robert@464: else { robert@464: if(gStateCounter >= 16) { robert@464: gStateCounter = 0; robert@464: gState++; robert@464: if(gState & 1) robert@464: gServoCounter = 120; robert@464: else robert@464: gServoCounter = 80; robert@464: if(gState == kStateSpin) robert@464: gStepLengthSamples = kStepLengthFast; robert@464: } robert@464: } robert@464: robert@464: gStepCounter = 0; robert@464: } robert@464: } robert@464: } robert@464: robert@464: // cleanup() is called once at the end, after the audio has stopped. robert@464: // Release any resources that were allocated in setup(). robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: }