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