MCP3008 Version

Dependencies:   mbed-rtos mbed mcp3008

Fork of KIK01_Proto01 by Ryo Od

Revision:
10:79134dbb339d
Parent:
9:d1e6eae9722b
Child:
11:7e11404adca0
--- a/main.cpp	Sun Jun 04 11:39:50 2017 +0000
+++ b/main.cpp	Tue Jun 06 11:00:12 2017 +0000
@@ -9,12 +9,14 @@
 #include "mbed.h"
 #include "rtos.h"
 
+#define UART_TRACE      (1)
 #define TITLE_STR1      ("KIK01 Kick Machine")
-#define TITLE_STR2      ("20170604")
+#define TITLE_STR2      ("20170606")
 
 #define PI_F            (3.1415926f)
 #define SAMPLING_RATE   (96000)
 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
+#define UPDATE_RATE     (1000)
 
 AnalogOut Dac1(PA_5);
 
@@ -30,6 +32,7 @@
 AnalogIn Ain8(PC_4);
 AnalogIn Ain9(PB_1);
 AnalogIn Ain10(PC_5);
+AnalogIn Ain11(PA_6);
 
 class EnvelopeAR {
 public:
@@ -143,7 +146,7 @@
     if (phi >= 1.0f) {
         phi -= 2.0f;
     }
-    float level = sinf(PI_F * phi) * amplitude;
+    float level = cosf(PI_F * phi) * amplitude;
 
     Dac1.write((level * 0.7f + 1.0f) / 2.0f);
 }
@@ -166,11 +169,11 @@
 void update()
 {
     ticks++;
-    if (ticks >= SAMPLING_RATE / 1000) {
+    if (ticks >= SAMPLING_RATE / UPDATE_RATE) {
         ticks = 0;
         
         // set envelope parameters
-        envelopeLength = 60 * 1000 / bpm;
+        //envelopeLength = 60 * UPDATE_RATE / bpm;
         
         envelopeAmplitude.setAttack(amplitudeParam.attack);
         envelopeAmplitude.setRelease(amplitudeParam.release);
@@ -196,6 +199,7 @@
 void setParams()
 {
     bpm = Ain0.read() * 180.0f + 60.0f;
+    envelopeLength = 60 * UPDATE_RATE / bpm;
     
     amplitudeParam.attack = Ain1.read() * envelopeLength;
     amplitudeParam.release = Ain2.read() * envelopeLength;
@@ -206,7 +210,7 @@
     amplitudeParam.releaseTauRatio = Ain4.read() + 0.01f;
     
     frequencyParam.attack = Ain8.read() * envelopeLength * 0.1f;
-    frequencyParam.release = 300;
+    frequencyParam.release = Ain11.read() * envelopeLength + 1;
     frequencyParam.v0 = Ain5.read() * 4000.0f;
     frequencyParam.v1 = Ain6.read() * 400.0f;
     frequencyParam.v2 = Ain7.read() * 400.0f;
@@ -233,6 +237,8 @@
         
     for (;;) {
         setParams();
+        
+        #if UART_TRACE
         printf("%.1f\t%d\t", bpm, envelopeLength);
         
         printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
@@ -242,6 +248,7 @@
         printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
         printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
         printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
+        #endif
         
         Thread::wait(100);    
     }