BaseMachine UI Controllerに分離

Dependencies:   AverageAnalogIn PinDetect RotaryEncoder Sequence SequencerDisplay mbed-rtos mbed st7567LCD BaseMachineComon

Fork of BaseMachine_Sequencer by Ryo Od

Revision:
38:cafa2fff5565
Parent:
37:d4e50159baee
Child:
40:2f9616bdcf65
--- a/main.cpp	Fri Sep 09 04:19:19 2016 +0000
+++ b/main.cpp	Sat Oct 01 22:15:20 2016 +0000
@@ -2,6 +2,7 @@
  * main.cpp
  * SpiSequencerSender_test
  *
+ * 2016.10.02 UI Controllerに分割
  * 2016.08.20 mbed Rev 121 / mbed-rtos Rev 117で動作確認
  *
  */
@@ -13,26 +14,25 @@
 #include "RotaryEncoder.h"
 #include "AverageAnalogIn.h"
 
-#define UART_TRACE  (0)
-#include "SpiSequenceSender.h"
-#include "EnvelopeGenerator.h"
-#include "SpiAmpController.h"
-#include "SpiFilterController.h"
+#define UART_TRACE  (1)
+#include "Sequence.h"
 #include "ST7565_SequencerDisplay.h"
 
-#define TITLE_STR1  ("BaseMachine Sequencer")
-#define TITLE_STR2  ("20160909")
+#define TITLE_STR1  ("BaseMachine UI Controller")
+#define TITLE_STR2  ("20161002")
 
 #define SEQUENCE_N  (16)
-#define SPI_RATE    (16000000)  // Actual frequency about 5MHz
+#define SPI1_RATE   (10000000)
+#define SPI2_RATE   (1000000)
 
-const int samplingPeriod = 1;   // ms
-const int bpm = 120;
-const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod;
-const int waveShape = SpiSequenceSender::WAVESHAPE_SQUARE;
+//const int samplingPeriod = 1;   // ms
+//const int bpm = 120;
+//const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod;
+//const int waveShape = SpiSequenceSender::WAVESHAPE_SQUARE;
 const int baseNoteNumber = 36;
 const int bpmMax = 240;
 const int bpmMin = 60;
+const int waveShapeMax = 1;
 const int UImodeMax = 2;
 
 // Initial Sequence
@@ -45,6 +45,8 @@
 //
 //SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
 SPI spiMaster(PA_7, PA_6, PA_5);
+DigitalOut SpiMCs(PB_6);
+InterruptIn stepChangeInterrupt(PC_7);
 
 //ST7565(PinName mosi, PinName sclk, PinName cs, PinName rst, PinName a0);
 ST7565 gLCD(PB_15, PB_13, PB_12, PB_2, PB_1);
@@ -52,7 +54,7 @@
 AverageAnalogIn AinPulseWidth(PC_2);
 AverageAnalogIn AinCutOff(PB_0);
 AverageAnalogIn AinResonance(PC_1);
-AverageAnalogIn AinEnvMod(PC_3);
+AverageAnalogIn AinAccentLevel(PC_3);
 AverageAnalogIn AinLevel(PC_0);
 AverageAnalogIn AinDuration(PA_4);
 AverageAnalogIn AinDecay(PA_1);
@@ -71,31 +73,44 @@
 PinDetect PinAccent(PC_8, PullUp);
 PinDetect PinRunStop(PC_9, PullUp);
 
-PwmOut LCDBackLight(PA_11);
-
 // Grobal Variables
 //
 Sequence sequences[SEQUENCE_N];
-SpiSequenceSender sequenceSender(&spiMaster, D9, sequences, SEQUENCE_N, samplingPeriod, bpm);
-
-Envelope envelope(4095, envelopeLength, envelopeLength*3/4, envelopeLength/2, 2047);
-EnvelopeGenerator envelopeGenerator;
-SpiAmpController ampController(&spiMaster, D8, D7);
-
-SpiFilterController filterController(&spiMaster, D10);
-
 ST7565_SequencerDisplay sequencerDisplay(&gLCD, sequences, SEQUENCE_N);
 
-volatile int currentStep = 0;
-volatile int playingStep = 0;
-volatile bool isRunning = false;
-volatile bool isDirty = true;
+struct OscillatorParam {
+    uint8_t waveShape;
+    uint8_t pulseWidth;
+} oscillatorParam;
+
+struct FilterParam {
+    uint8_t cutoff;
+    uint8_t resonance;
+} filterParam;
+
+struct EnvelopeParam {
+    int8_t level;
+    int8_t length;
+    int8_t duration;
+    int8_t decay;
+    int8_t sustain;
+} envelopeParam;
+
+uint8_t bpm = 120;
+uint8_t accentLevel = 127;
+   
+int currentStep = 0;
+int playingStep = 0;
+bool isRunning = false;
+bool isDirty = true;
+int UImode = 0;
+
+volatile bool isStepChanged = false;
 volatile uint8_t pinFlag = 0x00;
-volatile int UImode = 0;
 
 enum PinBit {
     bWaveShape  = 0x01,
-    bUIMode       = 0x02,
+    bUIMode     = 0x02,
     bOctaveUp   = 0x04,
     bOctaveDown = 0x08,
     bNoteOnOff  = 0x10,
@@ -104,34 +119,12 @@
     bRunStop    = 0x80
 };
 
-// とりあえずの変数(後でClassのメンバ変数に格納)
-volatile uint8_t envMod = 127;
-
 //------------------------------------------------------------------------
-// Callback functions
+// InterruptIn ISR
 //------------------------------------------------------------------------
-void updateTicks(int ticks)
+void updateStep()
 {
-    if (ticks == 0) {
-        envelopeGenerator.init(envelope);
-        // LCDにStepを表示するフラグを立てる
-        //sequencerDisplay.update(SequencerDisplay::run, sequenceSender.getStep());
-        playingStep = sequenceSender.getStep();
-        isDirty = true;
-    }
-
-    if (sequences[sequenceSender.getStep()].isNoteOn()) {
-        uint16_t level = envelopeGenerator.getModLevel();
-        if (!sequences[sequenceSender.getStep()].isAccent()) {
-            level = level * 1 / 2;
-        }
-        ampController.outDca(level);
-    } else {
-        ampController.outDca(0);
-    }
-    envelopeGenerator.update();
-    
-    filterController.outDcf();
+    isStepChanged = true;
 }
 
 //------------------------------------------------------------------------
@@ -182,25 +175,22 @@
 //------------------------------------------------------------------------
 void pollingPots()
 {
-    sequenceSender.setPulseWidth(AinPulseWidth.read_u16() >> 8);
+    oscillatorParam.pulseWidth = AinPulseWidth.read_u16() >> 8;
 
-    filterController.setCutoff(AinCutOff.read_u16() >> 8);
-    filterController.setResonance(AinResonance.read_u16() >> 8);
+    filterParam.cutoff = AinCutOff.read_u16() >> 8;
+    filterParam.resonance = AinResonance.read_u16() >> 8;
     
-    // テスト用にバックライトの調整に使用
-    LCDBackLight = AinEnvMod.read();
-    
-    envelope.setLevel(AinLevel.read_u16() >> 4);
-    envelope.setDuration(AinDuration.read() * envelopeLength);
-    envelope.setDecay(AinDecay.read() * envelopeLength);
-    envelope.setSustain(AinSustain.read() * 4095);
+    envelopeParam.level = AinLevel.read_u16() >> 8;
+    envelopeParam.duration = AinDuration.read_u16() >> 8;
+    envelopeParam.decay = AinDecay.read_u16() >> 8;
+    envelopeParam.sustain = AinSustain.read_u16() >> 8;
 }
 
 void pollingRotEncs()
 {
     int _bpm = RotEncBpm.getVal();
-    if (_bpm != sequenceSender.getBpm()) {
-        sequenceSender.setBpm(_bpm);
+    if (_bpm != bpm) {
+        bpm = _bpm;
         sequencerDisplay.setBpm(_bpm);
         isDirty = true;
     }
@@ -226,13 +216,13 @@
         #if (UART_TRACE)
         printf("PinWaveShape Pushed\r\n");
         #endif
-        uint8_t waveShape = sequenceSender.getWaveShape();
-        waveShape++;
-        if (waveShape == SpiSequenceSender::WAVESHAPE_N) {
-            waveShape = 0;
+        uint8_t _waveShape = oscillatorParam.waveShape;
+        _waveShape++;
+        if (_waveShape == waveShapeMax) {
+            _waveShape = 0;
         }
-        sequenceSender.setWaveShape(waveShape);
-        sequencerDisplay.setWaveShape(waveShape);
+        oscillatorParam.waveShape = _waveShape;
+        sequencerDisplay.setWaveShape(_waveShape);
         pinFlag &= ~bWaveShape;
         isDirty = true;
     }
@@ -253,7 +243,6 @@
         #if (UART_TRACE)
         printf("PinOctaveUp Pushed\r\n");
         #endif
-        //sequences[currentStep].setOctave(sequences[currentStep].getOctave() + 1);
         sequencerDisplay.setOctave(sequencerDisplay.getOctave() + 1);
         pinFlag &= ~bOctaveUp;
         isDirty = true;
@@ -263,7 +252,6 @@
         #if (UART_TRACE)
         printf("PinOctaveDown Pushed\r\n");
         #endif
-        //sequences[currentStep].setOctave(sequences[currentStep].getOctave() - 1);
         sequencerDisplay.setOctave(sequencerDisplay.getOctave() - 1);
         pinFlag &= ~bOctaveDown;
         isDirty = true;
@@ -301,11 +289,8 @@
         printf("PinRunStop Pushed\r\n");
         #endif
         if (isRunning) {
-            ampController.outDca(0);
-            sequenceSender.stop();
             isRunning = false;
         } else {
-            sequenceSender.run(currentStep);
             isRunning = true;
         }
         pinFlag &= ~bRunStop;
@@ -320,7 +305,7 @@
     
     gLCD.clear();
 
-    sprintf(buff, "Run:%d BPM:%03d", isRunning, sequenceSender.getBpm());
+    sprintf(buff, "Run:%d BPM:%03d", isRunning, bpm);
     gLCD.drawstring(0, col++, buff);
 
     sprintf(buff, "Stp:%02d Nto:%d Pch:%02d",
@@ -331,16 +316,16 @@
         sequencerDisplay.getOctave(), sequences[currentStep].isTie(),sequences[currentStep].isAccent());
     gLCD.drawstring(0, col++, buff);
 
-    sprintf(buff, "Wsp:%d Mod:%d", sequenceSender.getWaveShape(), UImode);
+    sprintf(buff, "Wsp:%d Mod:%d", oscillatorParam.waveShape, UImode);
     gLCD.drawstring(0, col++, buff);
     
-    sprintf(buff, "PW :%4d   CO :%4d", sequenceSender.getPulseWidth(), filterController.getCutoff());
+    sprintf(buff, "PW :%4d   CO :%4d", oscillatorParam.pulseWidth, filterParam.cutoff);
     gLCD.drawstring(0, col++, buff);
-    sprintf(buff, "RSO:%4d   ENV:%4d", filterController.getResonance(), envMod);
+    sprintf(buff, "RSO:%4d   ACL:%4d", filterParam.resonance, accentLevel);
     gLCD.drawstring(0, col++, buff);
-    sprintf(buff, "LVL:%4d   DUR:%4d", envelope.getLevel(), envelope.getDuration());
+    sprintf(buff, "LVL:%4d   DUR:%4d", envelopeParam.level, envelopeParam.duration);
     gLCD.drawstring(0, col++, buff);
-    sprintf(buff, "DCY:%4d   SUS:%4d", envelope.getDecay(), envelope.getSustain());
+    sprintf(buff, "DCY:%4d   SUS:%4d", envelopeParam.decay, envelopeParam.sustain);
     gLCD.drawstring(0, col++, buff);
     
     gLCD.display();
@@ -369,7 +354,7 @@
     gLCD.drawstring(0, col++, buff);
     sprintf(buff, "PulseWidth %5d", AinPulseWidth.read_u16() >> 8);
     gLCD.drawstring(0, col++, buff);
-    sprintf(buff, "EnvMod     %5d", AinEnvMod.read_u16() >> 8);
+    sprintf(buff, "AccentLvl  %5d", AinAccentLevel.read_u16() >> 8);
     gLCD.drawstring(0, col++, buff);
     
     gLCD.display();
@@ -381,21 +366,16 @@
 int main()
 {
     #if (UART_TRACE)
-    printf("*** BaseMachine Sequencer ***\r\n");
+    printf("*** BaseMachine UI Controller ***\r\n");
     #endif
     
     //--------------------------------------------------------------------
     // Setup Devices
     //
     spiMaster.format(8, 0);
-    spiMaster.frequency(SPI_RATE);
-    
-    // Mute output
-    ampController.outDca(0);
-    
-    LCDBackLight.period_ms(10); 
-    LCDBackLight = 0.6f;
-    gLCD.set_spi_frequency(SPI_RATE);
+    spiMaster.frequency(SPI1_RATE);
+
+    gLCD.set_spi_frequency(SPI2_RATE);
     gLCD.begin(0x12);
     gLCD.clear();
     gLCD.drawstring(0, 0, TITLE_STR1);
@@ -445,7 +425,7 @@
     Sequence::setBaseNoteNumber(baseNoteNumber);
     
     for (int i = 0; i < SEQUENCE_N; i++) {
-        Sequence& seq = sequenceSender.getSequences()[i];
+        Sequence& seq = sequences[i];
         seq.setPitch(pitch[i]);
         seq.setNoteOn(noteOn[i]);
         seq.setTie(tie[i]);
@@ -453,11 +433,6 @@
     }
     RotEncPitch.setVal(sequences[0].getPitch());
     
-    envelopeGenerator.init(envelope);
-
-    sequenceSender.attachUpdate(&updateTicks);
-    sequenceSender.setWaveShape(waveShape);
-    
     //--------------------------------------------------------------------
     // Main loop
     //