Nucleo Sequencer of my Base Machine

Dependencies:   Amp AverageAnalogIn Envelope FilterController Sequence BaseMachineComon mbed-rtos mbed

Fork of SpiSequenceSender_Test by Ryo Od

Revision:
20:00d3b6ace8c7
Parent:
19:241a4b355413
Child:
21:c4d09aaa52b9
--- a/main.cpp	Tue Aug 23 11:20:39 2016 +0000
+++ b/main.cpp	Tue Aug 23 11:47:44 2016 +0000
@@ -10,6 +10,7 @@
 #include "rtos.h"
 #include "st7565LCD.h"
 #include "PinDetect.h"
+#include "RotaryEncoder.h"
 #include "AverageAnalogIn.h"
 
 #define UART_TRACE  (0)
@@ -28,8 +29,11 @@
 const int samplingPeriod = 1;   // ms
 const int bpm = 120;
 const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod;
-const int waveShape = SpiSequenceSender::WAVESHAPE_SAW;
-const int baseNoteNumber = 48;
+const int waveShape = SpiSequenceSender::WAVESHAPE_SQUARE;
+const int baseNoteNumber = 36;
+const int pitchMax = 12;
+const int bpmMax = 240;
+const int bpmMin = 60;
 
 // Initial Sequence
 const int noteOn[SEQUENCE_N] = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0 };
@@ -54,6 +58,10 @@
 AverageAnalogIn AinDecay(PA_1);
 AverageAnalogIn AinSustain(PA_0);
 
+RotaryEncoder RotEncStep(D2, D3, 0, SEQUENCE_N - 1, 0);
+RotaryEncoder RotEncPitch(D4, D5, 0, pitchMax, 0);
+RotaryEncoder RotEncBpm(D14, D15, bpmMin, bpmMax, 120);
+
 PinDetect PinWaveShape(PD_2, PullUp);
 PinDetect PinModNumber(PC_11, PullUp);
 PinDetect PinOctaveUp(PC_10, PullUp);
@@ -178,24 +186,40 @@
 //------------------------------------------------------------------------
 void pollingPots()
 {
+    //pulseWidth = AinPulseWidth.read_u16() >> 8;
+
     filterController.setCutoff(AinCutOff.read_u16() >> 8);
     filterController.setResonance(AinResonance.read_u16() >> 8);
+    //envMod     = AinEnvMod.read_u16()     >> 8;
     
     //envelope.setLevel(AinLevel.read_u16() >> 4);
     envelope.setLevel(4095);
     envelope.setDuration(AinDuration.read() * envelopeLength);
     envelope.setDecay(AinDecay.read() * envelopeLength);
     envelope.setSustain(AinSustain.read() * 4095);
-/*    
-    //pulseWidth = AinPulseWidth.read_u16() >> 8;
-    cutOff     = AinCutOff.read_u16()     >> 8;
-    resonance  = AinResonance.read_u16()  >> 8;
-    //envMod     = AinEnvMod.read_u16()     >> 8;
-    //level      = AinLevel.read_u16()      >> 8;
-    duration   = AinDuration.read_u16()   >> 8;
-    decay      = AinDecay.read_u16()      >> 8;
-    sustain    = AinSustain.read_u16()    >> 8;
-*/
+}
+
+void pollingRotEncs()
+{
+    int _bpm = RotEncBpm.getVal();
+    if (_bpm != sequenceSender.getBpm()) {
+        sequenceSender.setBpm(_bpm);
+        isDirty = true;
+    }
+    
+    int _step = RotEncStep.getVal();
+    if (_step != currentStep) {
+        currentStep = _step;
+        // syncronize sequence value & Rotary Encoder's value
+        RotEncPitch.setVal(sequences[currentStep].getPitch());
+        isDirty = true;
+    }
+    
+    int _pitch = RotEncPitch.getVal();
+    if (_pitch != sequences[currentStep].getPitch()) {
+        sequences[currentStep].setPitch(_pitch);
+        isDirty = true;
+    }
 }
 
 void pollingPins()
@@ -348,7 +372,9 @@
     dumpToLCD();
     Thread::wait(1000);
     
-    sequencerDisplay.update(SequencerDisplay::stop, 0);
+    RotEncStep.setInterval(100);
+    RotEncPitch.setInterval(100);
+    RotEncBpm.setInterval(100);
     
     PinWaveShape.attach_asserted(&swWaveShapePressed);
     PinWaveShape.setAssertValue(0);
@@ -403,8 +429,9 @@
     //sequenceSender.run(0);
     
     for (;;) {
+        pollingPots();
+        pollingRotEncs();
         pollingPins();
-        pollingPots();
 #if 0        
         /*
         sequenceSender.setPulseWidth(sequenceSender.getPulseWidth() + 4);