Mercurial > hg > beaglert
comparison examples/mpr121/render.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/mpr121/render.cpp@8d80eda512cd |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
1 #include <BeagleRT.h> | |
2 #include <Utilities.h> | |
3 #include <cmath> | |
4 #include <rtdk.h> | |
5 #include "I2C_MPR121.h" | |
6 | |
7 // How many pins there are | |
8 #define NUM_TOUCH_PINS 12 | |
9 | |
10 // Define this to print data to terminal | |
11 #undef DEBUG_MPR121 | |
12 | |
13 // Change this to change how often the MPR121 is read (in Hz) | |
14 int readInterval = 50; | |
15 | |
16 // Change this threshold to set the minimum amount of touch | |
17 int threshold = 40; | |
18 | |
19 // This array holds the continuous sensor values | |
20 int sensorValue[NUM_TOUCH_PINS]; | |
21 | |
22 // ---- test code stuff -- can be deleted for your example ---- | |
23 | |
24 // 12 notes of a C major scale... | |
25 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}; | |
26 | |
27 // This is internal stuff for the demo | |
28 float gNormFrequencies[NUM_TOUCH_PINS]; | |
29 float gPhases[NUM_TOUCH_PINS] = {0}; | |
30 | |
31 // ---- internal stuff -- do not change ----- | |
32 | |
33 I2C_MPR121 mpr121; // Object to handle MPR121 sensing | |
34 AuxiliaryTask i2cTask; // Auxiliary task to read I2C | |
35 | |
36 int readCount = 0; // How long until we read again... | |
37 int readIntervalSamples = 0; // How many samples between reads | |
38 | |
39 void readMPR121(); | |
40 | |
41 // setup() is called once before the audio rendering starts. | |
42 // Use it to perform any initialisation and allocation which is dependent | |
43 // on the period size or sample rate. | |
44 // | |
45 // userData holds an opaque pointer to a data structure that was passed | |
46 // in from the call to initAudio(). | |
47 // | |
48 // Return true on success; returning false halts the program. | |
49 | |
50 bool setup(BeagleRTContext *context, void *userData) | |
51 { | |
52 if(!mpr121.begin(1, 0x5A)) { | |
53 rt_printf("Error initialising MPR121\n"); | |
54 return false; | |
55 } | |
56 | |
57 i2cTask = BeagleRT_createAuxiliaryTask(readMPR121, 50, "beaglert-mpr121"); | |
58 readIntervalSamples = context->audioSampleRate / readInterval; | |
59 | |
60 for(int i = 0; i < NUM_TOUCH_PINS; i++) { | |
61 gNormFrequencies[i] = 2.0 * M_PI * gFrequencies[i] / context->audioSampleRate; | |
62 } | |
63 | |
64 return true; | |
65 } | |
66 | |
67 // render() is called regularly at the highest priority by the audio engine. | |
68 // Input and output are given from the audio hardware and the other | |
69 // ADCs and DACs (if available). If only audio is available, numAnalogFrames | |
70 // will be 0. | |
71 | |
72 void render(BeagleRTContext *context, void *userData) | |
73 { | |
74 for(int n = 0; n < context->audioFrames; n++) { | |
75 // Keep this code: it schedules the touch sensor readings | |
76 if(++readCount >= readIntervalSamples) { | |
77 readCount = 0; | |
78 BeagleRT_scheduleAuxiliaryTask(i2cTask); | |
79 } | |
80 | |
81 float sample = 0.0; | |
82 | |
83 // This code can be replaced with your favourite audio code | |
84 for(int i = 0; i < NUM_TOUCH_PINS; i++) { | |
85 float amplitude = sensorValue[i] / 400.0; | |
86 | |
87 // Prevent clipping | |
88 if(amplitude > 0.5) | |
89 amplitude = 0.5; | |
90 | |
91 sample += amplitude * sinf(gPhases[i]); | |
92 gPhases[i] += gNormFrequencies[i]; | |
93 if(gPhases[i] > 2.0 * M_PI) | |
94 gPhases[i] -= 2.0 * M_PI; | |
95 } | |
96 | |
97 for(int ch = 0; ch < context->audioChannels; ch++) | |
98 context->audioOut[context->audioChannels * n + ch] = sample; | |
99 } | |
100 } | |
101 | |
102 // cleanup() is called once at the end, after the audio has stopped. | |
103 // Release any resources that were allocated in setup(). | |
104 | |
105 void cleanup(BeagleRTContext *context, void *userData) | |
106 { | |
107 // Nothing to do here | |
108 } | |
109 | |
110 | |
111 // Auxiliary task to read the I2C board | |
112 void readMPR121() | |
113 { | |
114 for(int i = 0; i < NUM_TOUCH_PINS; i++) { | |
115 sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i)); | |
116 sensorValue[i] -= threshold; | |
117 if(sensorValue[i] < 0) | |
118 sensorValue[i] = 0; | |
119 #ifdef DEBUG_MPR121 | |
120 rt_printf("%d ", sensorValue[i]); | |
121 #endif | |
122 } | |
123 #ifdef DEBUG_MPR121 | |
124 rt_printf("\n"); | |
125 #endif | |
126 | |
127 // You can use this to read binary on/off touch state more easily | |
128 //rt_printf("Touched: %x\n", mpr121.touched()); | |
129 } |