KIK 01 Prototype 05
Dependencies: AverageMCP3008 mbed-rtos mbed mcp3008
Fork of KIK01_Proto03 by
main.cpp@30:1291e20b1c53, 2017-10-19 (annotated)
- Committer:
- ryood
- Date:
- Thu Oct 19 08:01:49 2017 +0000
- Revision:
- 30:1291e20b1c53
- Parent:
- 29:947992b9904f
- Child:
- 31:3c7e1cd0d947
Output Noise Envelope from Dac2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 3:f89b400cfe57 | 1 | /* |
ryood | 3:f89b400cfe57 | 2 | * KIK01 |
ryood | 3:f89b400cfe57 | 3 | * Kick Machine |
ryood | 3:f89b400cfe57 | 4 | * |
ryood | 27:b07a55935230 | 5 | * 2017.10.19 Proto05: Add NOS01 Controller |
ryood | 17:a5d9908bd456 | 6 | * 2017.09.16 Proto04: SPI1 for AD8402 Wein Bridge DCO & Internal DAC for Dual-OTA-VCA |
ryood | 15:8f674acdac03 | 7 | * 2017.07.04 Proto03: MCP4922 DCA |
ryood | 13:43a43da257e3 | 8 | * 2017.06.19 Proto02 |
ryood | 3:f89b400cfe57 | 9 | * 2017.06.04 created. |
ryood | 3:f89b400cfe57 | 10 | * |
ryood | 3:f89b400cfe57 | 11 | */ |
ryood | 3:f89b400cfe57 | 12 | |
ryood | 0:2dcec10e9199 | 13 | #include "mbed.h" |
ryood | 0:2dcec10e9199 | 14 | #include "rtos.h" |
ryood | 13:43a43da257e3 | 15 | #include "mcp3008.h" |
ryood | 28:387c5a6d5206 | 16 | #include "AverageMCP3008.h" |
ryood | 22:a3bb7594f9bb | 17 | #include "EnvelopeAR.h" |
ryood | 0:2dcec10e9199 | 18 | |
ryood | 17:a5d9908bd456 | 19 | #define UART_TRACE (1) |
ryood | 12:5b498285d121 | 20 | #define PIN_CHECK (1) |
ryood | 0:2dcec10e9199 | 21 | #define TITLE_STR1 ("KIK01 Kick Machine") |
ryood | 26:866d672ba446 | 22 | #define TITLE_STR2 ("20171019") |
ryood | 0:2dcec10e9199 | 23 | |
ryood | 0:2dcec10e9199 | 24 | #define PI_F (3.1415926f) |
ryood | 15:8f674acdac03 | 25 | |
ryood | 29:947992b9904f | 26 | #define MCP3008_SPI_SPEED (1312500) |
ryood | 21:a527f51381d6 | 27 | #define AD8402_SPI_SPEED (10000000) |
ryood | 29:947992b9904f | 28 | #define ENVELOPE_UPDATE_RATE (8000) // Hz |
ryood | 17:a5d9908bd456 | 29 | |
ryood | 20:70a05941db8c | 30 | #define AD8402_RMAX (10000.0f) |
ryood | 20:70a05941db8c | 31 | #define AD8402_RMIN (50.0f) |
ryood | 20:70a05941db8c | 32 | |
ryood | 26:866d672ba446 | 33 | AnalogOut Dac1(PA_4); |
ryood | 27:b07a55935230 | 34 | AnalogOut Dac2(PA_5); |
ryood | 3:f89b400cfe57 | 35 | |
ryood | 26:866d672ba446 | 36 | // AD8402 SPI (SPI2) |
ryood | 26:866d672ba446 | 37 | // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 26:866d672ba446 | 38 | SPI SpiMAD8402(PB_15, PB_14, PB_13); |
ryood | 26:866d672ba446 | 39 | DigitalOut AD8402Cs(PC_4); |
ryood | 15:8f674acdac03 | 40 | |
ryood | 18:1bf4abf6895b | 41 | // MCP3008 SPI |
ryood | 26:866d672ba446 | 42 | // SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 26:866d672ba446 | 43 | SPI SpiMAdc(PB_5, PB_4, PB_3); |
ryood | 26:866d672ba446 | 44 | MCP3008 Adc1(&SpiMAdc, PB_10); |
ryood | 26:866d672ba446 | 45 | MCP3008 Adc2(&SpiMAdc, PA_8); |
ryood | 26:866d672ba446 | 46 | MCP3008 Adc3(&SpiMAdc, PA_9); |
ryood | 28:387c5a6d5206 | 47 | AverageMCP3008 AvgAdc1(&Adc1, 8); |
ryood | 28:387c5a6d5206 | 48 | AverageMCP3008 AvgAdc2(&Adc2, 8); |
ryood | 28:387c5a6d5206 | 49 | AverageMCP3008 AvgAdc3(&Adc3, 8); |
ryood | 8:bb34a4894337 | 50 | |
ryood | 18:1bf4abf6895b | 51 | // Sync |
ryood | 26:866d672ba446 | 52 | DigitalOut SyncPin(PA_10); |
ryood | 18:1bf4abf6895b | 53 | |
ryood | 14:8e96f97e261b | 54 | // Check pins |
ryood | 26:866d672ba446 | 55 | DigitalOut Dout1(PB_9); |
ryood | 26:866d672ba446 | 56 | DigitalOut Dout2(PB_8); |
ryood | 27:b07a55935230 | 57 | DigitalOut Dout3(PB_6); |
ryood | 14:8e96f97e261b | 58 | |
ryood | 9:d1e6eae9722b | 59 | EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f); |
ryood | 5:846772a77d33 | 60 | EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f); |
ryood | 29:947992b9904f | 61 | EnvelopeAR envelopeNoiseAmplitude(50, 200, 0.99f, 1.0f, 0.0f); |
ryood | 5:846772a77d33 | 62 | |
ryood | 25:b4977c7e0db7 | 63 | EnvelopeParam frequencyParam; |
ryood | 25:b4977c7e0db7 | 64 | EnvelopeParam amplitudeParam; |
ryood | 29:947992b9904f | 65 | EnvelopeParam noiseAmplitudeParam; |
ryood | 0:2dcec10e9199 | 66 | |
ryood | 3:f89b400cfe57 | 67 | volatile int ticks; |
ryood | 3:f89b400cfe57 | 68 | volatile float frequency; |
ryood | 3:f89b400cfe57 | 69 | volatile float amplitude; |
ryood | 29:947992b9904f | 70 | volatile float noiseAmplitude; |
ryood | 3:f89b400cfe57 | 71 | |
ryood | 5:846772a77d33 | 72 | volatile float bpm; |
ryood | 5:846772a77d33 | 73 | volatile int envelopeLength; |
ryood | 14:8e96f97e261b | 74 | volatile int stepLength; |
ryood | 0:2dcec10e9199 | 75 | |
ryood | 17:a5d9908bd456 | 76 | void AD8402Write(uint8_t address, uint8_t value) |
ryood | 17:a5d9908bd456 | 77 | { |
ryood | 21:a527f51381d6 | 78 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 79 | Dout2 = 1; |
ryood | 21:a527f51381d6 | 80 | #endif |
ryood | 21:a527f51381d6 | 81 | |
ryood | 18:1bf4abf6895b | 82 | AD8402Cs = 0; |
ryood | 21:a527f51381d6 | 83 | SpiMAD8402.write(address); |
ryood | 21:a527f51381d6 | 84 | SpiMAD8402.write(value); |
ryood | 18:1bf4abf6895b | 85 | AD8402Cs = 1; |
ryood | 17:a5d9908bd456 | 86 | wait_us(1); |
ryood | 21:a527f51381d6 | 87 | |
ryood | 21:a527f51381d6 | 88 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 89 | Dout2 = 0; |
ryood | 21:a527f51381d6 | 90 | #endif |
ryood | 17:a5d9908bd456 | 91 | } |
ryood | 17:a5d9908bd456 | 92 | |
ryood | 23:da7c5f7feff1 | 93 | void DcoSetFrequency(float freq) |
ryood | 17:a5d9908bd456 | 94 | { |
ryood | 17:a5d9908bd456 | 95 | const float c = 0.00000047; |
ryood | 17:a5d9908bd456 | 96 | float r = 1.0f / (2.0f * PI_F * frequency * c); |
ryood | 20:70a05941db8c | 97 | if (r < AD8402_RMIN) r = AD8402_RMIN; |
ryood | 20:70a05941db8c | 98 | if (r > AD8402_RMAX) r = AD8402_RMAX; |
ryood | 20:70a05941db8c | 99 | |
ryood | 20:70a05941db8c | 100 | uint8_t v = 256.0f * (r - AD8402_RMIN) / AD8402_RMAX; |
ryood | 21:a527f51381d6 | 101 | |
ryood | 17:a5d9908bd456 | 102 | AD8402Write(0, v); |
ryood | 17:a5d9908bd456 | 103 | AD8402Write(1, v); |
ryood | 23:da7c5f7feff1 | 104 | } |
ryood | 17:a5d9908bd456 | 105 | |
ryood | 27:b07a55935230 | 106 | void DcaSetAmplitude(int channel, float amp) |
ryood | 23:da7c5f7feff1 | 107 | { |
ryood | 27:b07a55935230 | 108 | switch (channel) { |
ryood | 27:b07a55935230 | 109 | case 1: |
ryood | 27:b07a55935230 | 110 | Dac1.write(amp); |
ryood | 27:b07a55935230 | 111 | break; |
ryood | 27:b07a55935230 | 112 | case 2: |
ryood | 27:b07a55935230 | 113 | Dac2.write(amp * 0.8f); // Avoid LED,s Non-Linearity |
ryood | 27:b07a55935230 | 114 | break; |
ryood | 27:b07a55935230 | 115 | default: |
ryood | 27:b07a55935230 | 116 | error("DcaSetAmplitude(): invalid channel"); |
ryood | 27:b07a55935230 | 117 | } |
ryood | 17:a5d9908bd456 | 118 | } |
ryood | 15:8f674acdac03 | 119 | |
ryood | 2:8dff77a1ee4d | 120 | void update() |
ryood | 2:8dff77a1ee4d | 121 | { |
ryood | 12:5b498285d121 | 122 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 123 | Dout1 = 1; |
ryood | 12:5b498285d121 | 124 | #endif |
ryood | 15:8f674acdac03 | 125 | |
ryood | 14:8e96f97e261b | 126 | // Output Sync Signal per steps |
ryood | 17:a5d9908bd456 | 127 | if (ticks % stepLength == 0) { |
ryood | 14:8e96f97e261b | 128 | SyncPin = 1; |
ryood | 14:8e96f97e261b | 129 | } |
ryood | 15:8f674acdac03 | 130 | |
ryood | 17:a5d9908bd456 | 131 | // set envelope parameters |
ryood | 25:b4977c7e0db7 | 132 | envelopeAmplitude.setParam(amplitudeParam); |
ryood | 25:b4977c7e0db7 | 133 | envelopeFrequency.setParam(frequencyParam); |
ryood | 30:1291e20b1c53 | 134 | envelopeNoiseAmplitude.setParam(noiseAmplitudeParam); |
ryood | 11:7e11404adca0 | 135 | |
ryood | 23:da7c5f7feff1 | 136 | frequency = envelopeFrequency.getAmplitude(ticks); |
ryood | 30:1291e20b1c53 | 137 | amplitude = envelopeAmplitude.getAmplitude(ticks); |
ryood | 30:1291e20b1c53 | 138 | noiseAmplitude = envelopeNoiseAmplitude.getAmplitude(ticks); |
ryood | 23:da7c5f7feff1 | 139 | |
ryood | 23:da7c5f7feff1 | 140 | DcoSetFrequency(frequency); |
ryood | 27:b07a55935230 | 141 | DcaSetAmplitude(1, amplitude); // DCO |
ryood | 30:1291e20b1c53 | 142 | DcaSetAmplitude(2, noiseAmplitude); // NOS01 |
ryood | 11:7e11404adca0 | 143 | |
ryood | 17:a5d9908bd456 | 144 | ticks++; |
ryood | 17:a5d9908bd456 | 145 | if (ticks >= envelopeLength) { |
ryood | 17:a5d9908bd456 | 146 | ticks = 0; |
ryood | 2:8dff77a1ee4d | 147 | } |
ryood | 15:8f674acdac03 | 148 | |
ryood | 14:8e96f97e261b | 149 | // Output SyncSignal |
ryood | 14:8e96f97e261b | 150 | SyncPin = 0; |
ryood | 15:8f674acdac03 | 151 | |
ryood | 12:5b498285d121 | 152 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 153 | Dout1 = 0; |
ryood | 12:5b498285d121 | 154 | #endif |
ryood | 2:8dff77a1ee4d | 155 | } |
ryood | 2:8dff77a1ee4d | 156 | |
ryood | 25:b4977c7e0db7 | 157 | void readParams() |
ryood | 5:846772a77d33 | 158 | { |
ryood | 28:387c5a6d5206 | 159 | bpm = AvgAdc1.read_input(7) * 180.0f + 60.0f; |
ryood | 12:5b498285d121 | 160 | envelopeLength = 60 * ENVELOPE_UPDATE_RATE / bpm; |
ryood | 14:8e96f97e261b | 161 | stepLength = envelopeLength / 4; |
ryood | 11:7e11404adca0 | 162 | |
ryood | 28:387c5a6d5206 | 163 | amplitudeParam.attack = AvgAdc1.read_input(0) * envelopeLength; |
ryood | 28:387c5a6d5206 | 164 | amplitudeParam.release = AvgAdc1.read_input(1) * envelopeLength; |
ryood | 28:387c5a6d5206 | 165 | amplitudeParam.v0 = AvgAdc1.read_input(4); |
ryood | 28:387c5a6d5206 | 166 | amplitudeParam.v1 = AvgAdc1.read_input(5); |
ryood | 28:387c5a6d5206 | 167 | amplitudeParam.v2 = AvgAdc1.read_input(6); |
ryood | 28:387c5a6d5206 | 168 | amplitudeParam.attackTauRatio = AvgAdc1.read_input(2) + 0.01f; |
ryood | 28:387c5a6d5206 | 169 | amplitudeParam.releaseTauRatio = AvgAdc1.read_input(3) + 0.01f; |
ryood | 11:7e11404adca0 | 170 | |
ryood | 28:387c5a6d5206 | 171 | frequencyParam.attack = AvgAdc2.read_input(0) * envelopeLength * 0.1f; |
ryood | 28:387c5a6d5206 | 172 | frequencyParam.release = AvgAdc2.read_input(1) * envelopeLength + 1; |
ryood | 28:387c5a6d5206 | 173 | frequencyParam.v0 = AvgAdc2.read_input(4) * 4000.0f; |
ryood | 28:387c5a6d5206 | 174 | frequencyParam.v1 = AvgAdc2.read_input(5) * 400.0f; |
ryood | 28:387c5a6d5206 | 175 | frequencyParam.v2 = AvgAdc2.read_input(6) * 400.0f; |
ryood | 28:387c5a6d5206 | 176 | frequencyParam.attackTauRatio = AvgAdc2.read_input(2) + 0.01f; |
ryood | 28:387c5a6d5206 | 177 | frequencyParam.releaseTauRatio = AvgAdc2.read_input(3) + 0.01f; |
ryood | 29:947992b9904f | 178 | |
ryood | 29:947992b9904f | 179 | noiseAmplitudeParam.attack = AvgAdc3.read_input(0) * envelopeLength; |
ryood | 29:947992b9904f | 180 | noiseAmplitudeParam.release = AvgAdc3.read_input(1) * envelopeLength; |
ryood | 29:947992b9904f | 181 | noiseAmplitudeParam.v0 = AvgAdc3.read_input(4); |
ryood | 29:947992b9904f | 182 | noiseAmplitudeParam.v1 = AvgAdc3.read_input(5); |
ryood | 29:947992b9904f | 183 | noiseAmplitudeParam.v2 = AvgAdc3.read_input(6); |
ryood | 29:947992b9904f | 184 | noiseAmplitudeParam.attackTauRatio = AvgAdc3.read_input(2) + 0.01f; |
ryood | 29:947992b9904f | 185 | noiseAmplitudeParam.releaseTauRatio = AvgAdc3.read_input(3) + 0.01f; |
ryood | 5:846772a77d33 | 186 | } |
ryood | 5:846772a77d33 | 187 | |
ryood | 0:2dcec10e9199 | 188 | int main() |
ryood | 0:2dcec10e9199 | 189 | { |
ryood | 27:b07a55935230 | 190 | #if UART_TRACE |
ryood | 27:b07a55935230 | 191 | printf("\r\n\n%s %s\r\n", TITLE_STR1, TITLE_STR2); |
ryood | 27:b07a55935230 | 192 | #endif |
ryood | 15:8f674acdac03 | 193 | |
ryood | 21:a527f51381d6 | 194 | SpiMAD8402.format(8, 0); |
ryood | 21:a527f51381d6 | 195 | SpiMAD8402.frequency(AD8402_SPI_SPEED); |
ryood | 17:a5d9908bd456 | 196 | |
ryood | 26:866d672ba446 | 197 | SpiMAdc.format(8, 0); |
ryood | 26:866d672ba446 | 198 | SpiMAdc.frequency(MCP3008_SPI_SPEED); |
ryood | 15:8f674acdac03 | 199 | |
ryood | 17:a5d9908bd456 | 200 | frequency = 100.0f; |
ryood | 0:2dcec10e9199 | 201 | amplitude = 1.0f; |
ryood | 29:947992b9904f | 202 | noiseAmplitude = 1.0f; |
ryood | 17:a5d9908bd456 | 203 | bpm = 120.0f; |
ryood | 17:a5d9908bd456 | 204 | |
ryood | 25:b4977c7e0db7 | 205 | readParams(); |
ryood | 11:7e11404adca0 | 206 | |
ryood | 3:f89b400cfe57 | 207 | ticks = 0; |
ryood | 2:8dff77a1ee4d | 208 | Ticker samplingTicker; |
ryood | 17:a5d9908bd456 | 209 | samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE)); |
ryood | 11:7e11404adca0 | 210 | |
ryood | 3:f89b400cfe57 | 211 | for (;;) { |
ryood | 13:43a43da257e3 | 212 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 213 | Dout3 = 1; |
ryood | 15:8f674acdac03 | 214 | #endif |
ryood | 25:b4977c7e0db7 | 215 | |
ryood | 25:b4977c7e0db7 | 216 | readParams(); |
ryood | 25:b4977c7e0db7 | 217 | |
ryood | 13:43a43da257e3 | 218 | #if (PIN_CHECK) |
ryood | 26:866d672ba446 | 219 | Dout3 = 0; |
ryood | 15:8f674acdac03 | 220 | #endif |
ryood | 11:7e11404adca0 | 221 | |
ryood | 11:7e11404adca0 | 222 | #if UART_TRACE |
ryood | 6:897d6392b408 | 223 | printf("%.1f\t%d\t", bpm, envelopeLength); |
ryood | 11:7e11404adca0 | 224 | |
ryood | 6:897d6392b408 | 225 | printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release); |
ryood | 6:897d6392b408 | 226 | printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2); |
ryood | 6:897d6392b408 | 227 | printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio); |
ryood | 6:897d6392b408 | 228 | |
ryood | 6:897d6392b408 | 229 | printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release); |
ryood | 6:897d6392b408 | 230 | printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2); |
ryood | 29:947992b9904f | 231 | printf("%.2f\t%.2f\t", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio); |
ryood | 29:947992b9904f | 232 | |
ryood | 29:947992b9904f | 233 | printf("%d\t%d\t", noiseAmplitudeParam.attack, noiseAmplitudeParam.release); |
ryood | 29:947992b9904f | 234 | printf("%.2f\t%.2f\t%.2f\t", noiseAmplitudeParam.v0, noiseAmplitudeParam.v1, noiseAmplitudeParam.v2); |
ryood | 29:947992b9904f | 235 | printf("%.2f\t%.2f\r\n", noiseAmplitudeParam.attackTauRatio, noiseAmplitudeParam.releaseTauRatio); |
ryood | 11:7e11404adca0 | 236 | #endif |
ryood | 11:7e11404adca0 | 237 | |
ryood | 13:43a43da257e3 | 238 | Thread::wait(1); |
ryood | 3:f89b400cfe57 | 239 | } |
ryood | 0:2dcec10e9199 | 240 | } |