comparison examples/06-Sensors/capacitive-touch/render.cpp @ 501:6962184f8567 prerelease

Additional name changes to doxygen example title.
author Robert Jack <robert.h.jack@gmail.com>
date Wed, 22 Jun 2016 00:34:07 +0100
parents
children ff6e9199c444
comparison
equal deleted inserted replaced
500:b935f890e512 501:6962184f8567
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 #include <cmath>
27 #include <rtdk.h>
28 #include "I2C_MPR121.h"
29
30 // How many pins there are
31 #define NUM_TOUCH_PINS 12
32
33 // Define this to print data to terminal
34 #undef DEBUG_MPR121
35
36 // Change this to change how often the MPR121 is read (in Hz)
37 int readInterval = 50;
38
39 // Change this threshold to set the minimum amount of touch
40 int threshold = 40;
41
42 // This array holds the continuous sensor values
43 int sensorValue[NUM_TOUCH_PINS];
44
45 // ---- test code stuff -- can be deleted for your example ----
46
47 // 12 notes of a C major scale...
48 float gFrequencies[NUM_TOUCH_PINS] = {261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25, 587.33, 659.25, 698.25, 783.99};
49
50 // This is internal stuff for the demo
51 float gNormFrequencies[NUM_TOUCH_PINS];
52 float gPhases[NUM_TOUCH_PINS] = {0};
53
54 // ---- internal stuff -- do not change -----
55
56 I2C_MPR121 mpr121; // Object to handle MPR121 sensing
57 AuxiliaryTask i2cTask; // Auxiliary task to read I2C
58
59 int readCount = 0; // How long until we read again...
60 int readIntervalSamples = 0; // How many samples between reads
61
62 void readMPR121();
63
64 // setup() is called once before the audio rendering starts.
65 // Use it to perform any initialisation and allocation which is dependent
66 // on the period size or sample rate.
67 //
68 // userData holds an opaque pointer to a data structure that was passed
69 // in from the call to initAudio().
70 //
71 // Return true on success; returning false halts the program.
72
73 bool setup(BelaContext *context, void *userData)
74 {
75 if(!mpr121.begin(1, 0x5A)) {
76 rt_printf("Error initialising MPR121\n");
77 return false;
78 }
79
80 i2cTask = Bela_createAuxiliaryTask(readMPR121, 50, "bela-mpr121");
81 readIntervalSamples = context->audioSampleRate / readInterval;
82
83 for(int i = 0; i < NUM_TOUCH_PINS; i++) {
84 gNormFrequencies[i] = 2.0 * M_PI * gFrequencies[i] / context->audioSampleRate;
85 }
86
87 return true;
88 }
89
90 // render() is called regularly at the highest priority by the audio engine.
91 // Input and output are given from the audio hardware and the other
92 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
93 // will be 0.
94
95 void render(BelaContext *context, void *userData)
96 {
97 for(int n = 0; n < context->audioFrames; n++) {
98 // Keep this code: it schedules the touch sensor readings
99 if(++readCount >= readIntervalSamples) {
100 readCount = 0;
101 Bela_scheduleAuxiliaryTask(i2cTask);
102 }
103
104 float sample = 0.0;
105
106 // This code can be replaced with your favourite audio code
107 for(int i = 0; i < NUM_TOUCH_PINS; i++) {
108 float amplitude = sensorValue[i] / 400.0;
109
110 // Prevent clipping
111 if(amplitude > 0.5)
112 amplitude = 0.5;
113
114 sample += amplitude * sinf(gPhases[i]);
115 gPhases[i] += gNormFrequencies[i];
116 if(gPhases[i] > 2.0 * M_PI)
117 gPhases[i] -= 2.0 * M_PI;
118 }
119
120 for(int ch = 0; ch < context->audioChannels; ch++)
121 context->audioOut[context->audioChannels * n + ch] = sample;
122 }
123 }
124
125 // cleanup() is called once at the end, after the audio has stopped.
126 // Release any resources that were allocated in setup().
127
128 void cleanup(BelaContext *context, void *userData)
129 {
130 // Nothing to do here
131 }
132
133
134 // Auxiliary task to read the I2C board
135 void readMPR121()
136 {
137 for(int i = 0; i < NUM_TOUCH_PINS; i++) {
138 sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
139 sensorValue[i] -= threshold;
140 if(sensorValue[i] < 0)
141 sensorValue[i] = 0;
142 #ifdef DEBUG_MPR121
143 rt_printf("%d ", sensorValue[i]);
144 #endif
145 }
146 #ifdef DEBUG_MPR121
147 rt_printf("\n");
148 #endif
149
150 // You can use this to read binary on/off touch state more easily
151 //rt_printf("Touched: %x\n", mpr121.touched());
152 }
153
154 /* ------------ Project Explantation ------------ */
155
156 /**
157 \example 06-capacitive-touch
158
159 Capacitive touch sensing with MPR121
160 ---------------------------
161
162 This sketch allows you to hook up an MPR121 capactive touch sensing device
163 to Bela, for example the SparkFun Capacitive Touch Sensor Breakout - MPR121.
164 The breakout board gives you 12 electrode connections.
165
166 To get this working with Bela you need to connect the breakout board to the I2C
167 terminal on the Bela board. See the Pin guide for details of which pin is which.
168
169 The sensor data will then be available for you to use in the array
170 `sensorValue[NUM_TOUCH_PINS]`.
171 */