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: #define NUM_PINS 12 robert@464: robert@464: // Breadboard wiring layout: robert@464: // 11 10 12 9 8 7 robert@464: // [ LED DISP ] robert@464: // 1 2 3 6 4 5 robert@464: robert@464: // Organised by display segments: robert@464: // e d . X c g b X X X f a robert@464: const int kPins[NUM_PINS] = {P8_07, P8_08, P8_09, P8_10, P8_11, P8_12, robert@464: P8_15, P8_16, P8_27, P8_28, P8_29, P8_30}; robert@464: robert@464: // Indices into the above array: pins 12, 9, 8, 6 robert@464: const int kDigits[4] = {9, 8, 7, 3}; robert@464: robert@464: int gCurrentlyDisplayingDigit = 0; robert@464: int gDigitDisplayTime = 0; robert@464: const int kDigitMaxDisplayTime = 44; robert@464: robert@464: int gState = 0; robert@464: int gStateCounter = 0; robert@464: const int kMaxState = 25; robert@464: robert@464: // . g f e d c b a robert@464: //const unsigned char kBELA[4] = {0x7C, 0x79, 0x38, 0x77}; robert@464: const unsigned char kBELA[4] = {0x7C, 0x7B, 0x38, 0x5F}; robert@464: const unsigned char kPerimeter[6] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20}; robert@464: robert@464: int gCharacterToDisplay[4] = {0, 0, 0, 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: for(int i = 0; i < NUM_PINS; i++) { robert@464: pinMode(context, 0, kPins[i], OUTPUT); robert@464: } 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: // Check for rotation between digits robert@464: if(--gDigitDisplayTime <= 0) { robert@464: gCurrentlyDisplayingDigit = (gCurrentlyDisplayingDigit + 1) % 4; robert@464: gDigitDisplayTime = kDigitMaxDisplayTime; robert@464: } robert@464: robert@464: // Write the currently displaying digit low and the rest high robert@464: for(int i = 0; i < 4; i++) robert@464: digitalWriteOnce(context, n, kPins[kDigits[i]], HIGH); robert@464: digitalWriteOnce(context, n, kPins[kDigits[gCurrentlyDisplayingDigit]], LOW); robert@464: robert@464: // Write the digit to the other outputs robert@464: digitalWriteOnce(context, n, kPins[11], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x01); // a robert@464: digitalWriteOnce(context, n, kPins[6], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x02); // b robert@464: digitalWriteOnce(context, n, kPins[4], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x04); // c robert@464: digitalWriteOnce(context, n, kPins[1], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x08); // d robert@464: digitalWriteOnce(context, n, kPins[0], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x10); // e robert@464: digitalWriteOnce(context, n, kPins[10], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x20); // f robert@464: digitalWriteOnce(context, n, kPins[5], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x40); // g robert@464: digitalWriteOnce(context, n, kPins[2], robert@464: gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x80); // . robert@464: robert@464: // Check for changing state robert@464: if(--gStateCounter <= 0) { robert@464: gState = (gState + 1) % kMaxState; robert@464: if(gState != (kMaxState - 1)) { robert@464: for(int i = 0; i < 4; i++) robert@464: gCharacterToDisplay[i] = 1 << (gState % 6); robert@464: gStateCounter = 2000; robert@464: } robert@464: else { robert@464: for(int i = 0; i < 4; i++) robert@464: gCharacterToDisplay[i] = kBELA[i]; robert@464: gStateCounter = 50000; robert@464: } 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: }