![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Audio demo
main.cpp@0:ed8de6dbc531, 2015-07-11 (annotated)
- Committer:
- acracan
- Date:
- Sat Jul 11 11:20:10 2015 +0000
- Revision:
- 0:ed8de6dbc531
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acracan | 0:ed8de6dbc531 | 1 | #include "mbed.h" |
acracan | 0:ed8de6dbc531 | 2 | #include "tsi_sensor.h" |
acracan | 0:ed8de6dbc531 | 3 | |
acracan | 0:ed8de6dbc531 | 4 | /* This defines will be replaced by PinNames soon */ |
acracan | 0:ed8de6dbc531 | 5 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
acracan | 0:ed8de6dbc531 | 6 | #define ELEC0 9 |
acracan | 0:ed8de6dbc531 | 7 | #define ELEC1 10 |
acracan | 0:ed8de6dbc531 | 8 | #elif defined (TARGET_KL05Z) |
acracan | 0:ed8de6dbc531 | 9 | #define ELEC0 9 |
acracan | 0:ed8de6dbc531 | 10 | #define ELEC1 8 |
acracan | 0:ed8de6dbc531 | 11 | #else |
acracan | 0:ed8de6dbc531 | 12 | #error TARGET NOT DEFINED |
acracan | 0:ed8de6dbc531 | 13 | #endif |
acracan | 0:ed8de6dbc531 | 14 | Serial pc(USBTX, USBRX); |
acracan | 0:ed8de6dbc531 | 15 | AnalogOut DAC(PTE30); |
acracan | 0:ed8de6dbc531 | 16 | //global variables used by interrupt routine |
acracan | 0:ed8de6dbc531 | 17 | const int SAMPLE_COUNT = 128; |
acracan | 0:ed8de6dbc531 | 18 | float samples[SAMPLE_COUNT]; |
acracan | 0:ed8de6dbc531 | 19 | int sampleIdx = 0; |
acracan | 0:ed8de6dbc531 | 20 | float period = 1.0e-3f; |
acracan | 0:ed8de6dbc531 | 21 | Timer sensorTimer; |
acracan | 0:ed8de6dbc531 | 22 | PwmOut player(PTE31); |
acracan | 0:ed8de6dbc531 | 23 | TSIAnalogSlider tsi(ELEC0, ELEC1, 40); |
acracan | 0:ed8de6dbc531 | 24 | float sliderValue = 0.0; |
acracan | 0:ed8de6dbc531 | 25 | bool active = false; |
acracan | 0:ed8de6dbc531 | 26 | |
acracan | 0:ed8de6dbc531 | 27 | void computeSamples(); |
acracan | 0:ed8de6dbc531 | 28 | void writeSample(); |
acracan | 0:ed8de6dbc531 | 29 | |
acracan | 0:ed8de6dbc531 | 30 | int main(void) { |
acracan | 0:ed8de6dbc531 | 31 | DAC = 0.0f; |
acracan | 0:ed8de6dbc531 | 32 | computeSamples(); |
acracan | 0:ed8de6dbc531 | 33 | pc.printf("Hello World\n"); |
acracan | 0:ed8de6dbc531 | 34 | player.period(period); |
acracan | 0:ed8de6dbc531 | 35 | player = 0.5f; |
acracan | 0:ed8de6dbc531 | 36 | TPM0->SC |= TPM_SC_TOIE_MASK; |
acracan | 0:ed8de6dbc531 | 37 | NVIC_SetVector(TPM0_IRQn, (uint32_t)&writeSample); |
acracan | 0:ed8de6dbc531 | 38 | NVIC_EnableIRQ(TPM0_IRQn); |
acracan | 0:ed8de6dbc531 | 39 | |
acracan | 0:ed8de6dbc531 | 40 | sensorTimer.start(); |
acracan | 0:ed8de6dbc531 | 41 | writeSample(); |
acracan | 0:ed8de6dbc531 | 42 | pc.printf("Hello again\n"); |
acracan | 0:ed8de6dbc531 | 43 | while (true) { |
acracan | 0:ed8de6dbc531 | 44 | if (sensorTimer.read() > 100.0e-3f) { |
acracan | 0:ed8de6dbc531 | 45 | sliderValue = tsi.readPercentage(); |
acracan | 0:ed8de6dbc531 | 46 | active = sliderValue > 0.0f ? true : false; |
acracan | 0:ed8de6dbc531 | 47 | period = pow(10.0f, 0.5f * (1.0f - sliderValue)) * 7.0e-6f; |
acracan | 0:ed8de6dbc531 | 48 | player.period(period); |
acracan | 0:ed8de6dbc531 | 49 | sensorTimer.reset(); |
acracan | 0:ed8de6dbc531 | 50 | } |
acracan | 0:ed8de6dbc531 | 51 | } |
acracan | 0:ed8de6dbc531 | 52 | } |
acracan | 0:ed8de6dbc531 | 53 | |
acracan | 0:ed8de6dbc531 | 54 | void computeSamples() |
acracan | 0:ed8de6dbc531 | 55 | { |
acracan | 0:ed8de6dbc531 | 56 | for(int k = 0; k < SAMPLE_COUNT; k++) { |
acracan | 0:ed8de6dbc531 | 57 | samples[k]=(1.0f + sin(float(k) / float(SAMPLE_COUNT) * 6.283185f)) * 0.5f; |
acracan | 0:ed8de6dbc531 | 58 | } |
acracan | 0:ed8de6dbc531 | 59 | } |
acracan | 0:ed8de6dbc531 | 60 | |
acracan | 0:ed8de6dbc531 | 61 | void writeSample() |
acracan | 0:ed8de6dbc531 | 62 | { |
acracan | 0:ed8de6dbc531 | 63 | if (active) { |
acracan | 0:ed8de6dbc531 | 64 | sampleIdx = (sampleIdx + 1) % SAMPLE_COUNT; |
acracan | 0:ed8de6dbc531 | 65 | DAC = samples[sampleIdx]; |
acracan | 0:ed8de6dbc531 | 66 | } |
acracan | 0:ed8de6dbc531 | 67 | else { |
acracan | 0:ed8de6dbc531 | 68 | DAC = 0.5f; |
acracan | 0:ed8de6dbc531 | 69 | } |
acracan | 0:ed8de6dbc531 | 70 | TPM0->SC |= TPM_SC_TOF_MASK; |
acracan | 0:ed8de6dbc531 | 71 | } |