Mercurial > hg > beaglert
comparison examples/11-Extras/7segment/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 #define NUM_PINS 12 | |
28 | |
29 // Breadboard wiring layout: | |
30 // 11 10 12 9 8 7 | |
31 // [ LED DISP ] | |
32 // 1 2 3 6 4 5 | |
33 | |
34 // Organised by display segments: | |
35 // e d . X c g b X X X f a | |
36 const int kPins[NUM_PINS] = {P8_07, P8_08, P8_09, P8_10, P8_11, P8_12, | |
37 P8_15, P8_16, P8_27, P8_28, P8_29, P8_30}; | |
38 | |
39 // Indices into the above array: pins 12, 9, 8, 6 | |
40 const int kDigits[4] = {9, 8, 7, 3}; | |
41 | |
42 int gCurrentlyDisplayingDigit = 0; | |
43 int gDigitDisplayTime = 0; | |
44 const int kDigitMaxDisplayTime = 44; | |
45 | |
46 int gState = 0; | |
47 int gStateCounter = 0; | |
48 const int kMaxState = 25; | |
49 | |
50 // . g f e d c b a | |
51 //const unsigned char kBELA[4] = {0x7C, 0x79, 0x38, 0x77}; | |
52 const unsigned char kBELA[4] = {0x7C, 0x7B, 0x38, 0x5F}; | |
53 const unsigned char kPerimeter[6] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20}; | |
54 | |
55 int gCharacterToDisplay[4] = {0, 0, 0, 0}; | |
56 | |
57 // setup() is called once before the audio rendering starts. | |
58 // Use it to perform any initialisation and allocation which is dependent | |
59 // on the period size or sample rate. | |
60 // | |
61 // userData holds an opaque pointer to a data structure that was passed | |
62 // in from the call to initAudio(). | |
63 // | |
64 // Return true on success; returning false halts the program. | |
65 | |
66 bool setup(BelaContext *context, void *userData) | |
67 { | |
68 // This project makes the assumption that the audio and digital | |
69 // sample rates are the same. But check it to be sure! | |
70 if(context->audioFrames != context->digitalFrames) { | |
71 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n"); | |
72 return false; | |
73 } | |
74 | |
75 for(int i = 0; i < NUM_PINS; i++) { | |
76 pinMode(context, 0, kPins[i], OUTPUT); | |
77 } | |
78 | |
79 return true; | |
80 } | |
81 | |
82 // render() is called regularly at the highest priority by the audio engine. | |
83 // Input and output are given from the audio hardware and the other | |
84 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
85 // will be 0. | |
86 | |
87 void render(BelaContext *context, void *userData) | |
88 { | |
89 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
90 // Check for rotation between digits | |
91 if(--gDigitDisplayTime <= 0) { | |
92 gCurrentlyDisplayingDigit = (gCurrentlyDisplayingDigit + 1) % 4; | |
93 gDigitDisplayTime = kDigitMaxDisplayTime; | |
94 } | |
95 | |
96 // Write the currently displaying digit low and the rest high | |
97 for(int i = 0; i < 4; i++) | |
98 digitalWriteOnce(context, n, kPins[kDigits[i]], HIGH); | |
99 digitalWriteOnce(context, n, kPins[kDigits[gCurrentlyDisplayingDigit]], LOW); | |
100 | |
101 // Write the digit to the other outputs | |
102 digitalWriteOnce(context, n, kPins[11], | |
103 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x01); // a | |
104 digitalWriteOnce(context, n, kPins[6], | |
105 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x02); // b | |
106 digitalWriteOnce(context, n, kPins[4], | |
107 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x04); // c | |
108 digitalWriteOnce(context, n, kPins[1], | |
109 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x08); // d | |
110 digitalWriteOnce(context, n, kPins[0], | |
111 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x10); // e | |
112 digitalWriteOnce(context, n, kPins[10], | |
113 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x20); // f | |
114 digitalWriteOnce(context, n, kPins[5], | |
115 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x40); // g | |
116 digitalWriteOnce(context, n, kPins[2], | |
117 gCharacterToDisplay[gCurrentlyDisplayingDigit] & 0x80); // . | |
118 | |
119 // Check for changing state | |
120 if(--gStateCounter <= 0) { | |
121 gState = (gState + 1) % kMaxState; | |
122 if(gState != (kMaxState - 1)) { | |
123 for(int i = 0; i < 4; i++) | |
124 gCharacterToDisplay[i] = 1 << (gState % 6); | |
125 gStateCounter = 2000; | |
126 } | |
127 else { | |
128 for(int i = 0; i < 4; i++) | |
129 gCharacterToDisplay[i] = kBELA[i]; | |
130 gStateCounter = 50000; | |
131 } | |
132 } | |
133 } | |
134 } | |
135 | |
136 // cleanup() is called once at the end, after the audio has stopped. | |
137 // Release any resources that were allocated in setup(). | |
138 | |
139 void cleanup(BelaContext *context, void *userData) | |
140 { | |
141 | |
142 } |