KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Revision:
15:8f674acdac03
Parent:
14:8e96f97e261b
Child:
16:30cb822e072f
--- a/main.cpp	Sun Jun 25 15:38:44 2017 +0000
+++ b/main.cpp	Thu Jul 06 05:15:17 2017 +0000
@@ -2,6 +2,7 @@
  * KIK01
  * Kick Machine
  *
+ * 2017.07.04 Proto03: MCP4922 DCA
  * 2017.06.19 Proto02
  * 2017.06.04 created.
  *
@@ -11,21 +12,27 @@
 #include "rtos.h"
 #include "mcp3008.h"
 
-#define UART_TRACE      (1)
+#include "SpiAmpController.h"
+
+#define UART_TRACE      (0)
 #define PIN_CHECK       (1)
 #define TITLE_STR1      ("KIK01 Kick Machine")
-#define TITLE_STR2      ("20170625")
+#define TITLE_STR2      ("20170704")
 
 #define PI_F            (3.1415926f)
-#define SAMPLING_RATE   (48000)
+#define SAMPLING_RATE   (16000)
 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
-#define ENVELOPE_UPDATE_RATE    (48000)
+#define ENVELOPE_UPDATE_RATE    SAMPLING_RATE
+
+#define AMP_VREF    0x0fff
 
 AnalogOut Dac1(PA_4);
 
-SPI spiM(SPI_MOSI, SPI_MISO, SPI_SCK);
-MCP3008 Adc0(spiM, D9);
-MCP3008 Adc1(spiM, D10);
+SPI SpiM(SPI_MOSI, SPI_MISO, SPI_SCK);
+MCP3008 Adc0(&SpiM, D10);
+MCP3008 Adc1(&SpiM, D9);
+
+SpiAmpController ampController(&SpiM, D8, D7);
 
 // Check pins
 DigitalOut Dout0(D2);
@@ -168,7 +175,10 @@
 volatile int envelopeLength;
 volatile int stepLength;
 
-void generateWave()
+//-----------------------------------------------------------------------------//
+// Internal DCA
+//
+void generateWave_DcaInternal()
 {
     phi += phiDelta;
     if (phi >= 1.0f) {
@@ -179,7 +189,7 @@
     Dac1.write((level * 0.7f + 1.0f) / 2.0f);
 }
 
-void generateEnvelope()
+void generateEnvelope_DcaInternal()
 {
     // Frequency Envelope
     frequency = envelopeFrequency.getAmplitude(envelopeTicks);
@@ -195,22 +205,59 @@
     }
 }
 
+//-----------------------------------------------------------------------------//
+// External DCA
+//
+void generateWave_DcaExternal()
+{
+    phi += phiDelta;
+    if (phi >= 1.0f) {
+        phi -= 2.0f;
+    }
+    //float level = cosf(PI_F * phi) * amplitude;
+    float level = cosf(PI_F * phi);
+
+    Dac1.write((level * 0.7f + 1.0f) / 2.0f);
+}
+
+void generateEnvelope_DcaExternal()
+{
+    // Frequency Envelope
+    frequency = envelopeFrequency.getAmplitude(envelopeTicks);
+    phiDelta = 2.0f * frequency / SAMPLING_RATE;
+
+    // Amplitude Envelope
+    amplitude = envelopeAmplitude.getAmplitude(envelopeTicks);
+
+    SpiM.lock();
+    SpiM.frequency(16000000);
+    ampController.outDca(amplitude * 4096);
+    SpiM.frequency(2000000);
+    SpiM.unlock();
+
+    envelopeTicks++;
+    if (envelopeTicks >= envelopeLength) {
+        envelopeTicks = 0;
+        phi = PI_F / 2.0f;
+    }
+}
+
 void update()
 {
 #if (PIN_CHECK)
-        Dout0 = 1;
+    Dout0 = 1;
 #endif
-    
+
     // Output Sync Signal per steps
     if (envelopeTicks % stepLength == 0) {
         SyncPin = 1;
     }
-    
+
     ticks++;
     if (ticks >= SAMPLING_RATE / ENVELOPE_UPDATE_RATE) {
 #if (PIN_CHECK)
         Dout1 = 1;
-#endif        
+#endif
         ticks = 0;
 
         // set envelope parameters
@@ -230,18 +277,18 @@
         envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
         envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
 
-        generateEnvelope();
+        generateEnvelope_DcaExternal();
 #if (PIN_CHECK)
         Dout1 = 0;
 #endif
     }
-    generateWave();
-    
+    generateWave_DcaExternal();
+
     // Output SyncSignal
     SyncPin = 0;
-    
+
 #if (PIN_CHECK)
-        Dout0 = 0;
+    Dout0 = 0;
 #endif
 }
 
@@ -271,7 +318,10 @@
 int main()
 {
     printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
-    
+
+    SpiM.format(0, 0);
+    SpiM.frequency(2000000);
+
     frequency = 1000.0f;
     phiDelta = 2.0f * frequency / SAMPLING_RATE;
     amplitude = 1.0f;
@@ -288,11 +338,11 @@
     for (;;) {
 #if (PIN_CHECK)
         Dout2 = 1;
-#endif        
+#endif
         setParams();
 #if (PIN_CHECK)
         Dout2 = 0;
-#endif        
+#endif
 
 #if UART_TRACE
         printf("%.1f\t%d\t", bpm, envelopeLength);