BaseMachine UI Controllerに分離

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

Fork of BaseMachine_Sequencer by Ryo Od

Revision:
41:48864f5f5424
Parent:
40:2f9616bdcf65
Child:
42:3c71f925a47b
--- a/main.cpp	Sat Oct 01 22:37:05 2016 +0000
+++ b/main.cpp	Sat Oct 01 23:12:21 2016 +0000
@@ -25,10 +25,6 @@
 #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 baseNoteNumber = 36;
 const int bpmMax = 240;
 const int bpmMin = 60;
@@ -46,8 +42,8 @@
 // Devices
 //
 //SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
-SPI spiMaster(PA_7, PA_6, PA_5);
-DigitalOut SpiMCs(PB_6);
+SPI SpiMaster(PA_7, PA_6, PA_5);
+DigitalOut SpiMasterCs(PB_6);
 InterruptIn stepChangeInterrupt(PC_7);
 
 //ST7565(PinName mosi, PinName sclk, PinName cs, PinName rst, PinName a0);
@@ -56,11 +52,11 @@
 AverageAnalogIn AinPulseWidth(PC_2);
 AverageAnalogIn AinCutOff(PB_0);
 AverageAnalogIn AinResonance(PC_1);
-AverageAnalogIn AinAccentLevel(PC_3);
 AverageAnalogIn AinLevel(PC_0);
 AverageAnalogIn AinDuration(PA_4);
 AverageAnalogIn AinDecay(PA_1);
 AverageAnalogIn AinSustain(PA_0);
+AverageAnalogIn AinAccentLevel(PC_3);
 
 RotaryEncoder RotEncStep(PB_9, PB_8, 0, SEQUENCE_N - 1, 0);
 RotaryEncoder RotEncPitch(PB_5, PB_4, 0, Sequence::getMaxPitch() - 1, 0);
@@ -322,7 +318,7 @@
         sequencerDisplay.getOctave(), sequences[currentStep].isTie(),sequences[currentStep].isAccent());
     gLCD.drawstring(0, col++, buff);
 
-    sprintf(buff, "Wsp:%d Mod:%d", oscillatorParam.waveShape, UImode);
+    sprintf(buff, "Wsp:%d Mod:%d pStp:%d", oscillatorParam.waveShape, UImode, playingStep);
     gLCD.drawstring(0, col++, buff);
     
     sprintf(buff, "PW :%4d   CO :%4d", oscillatorParam.pulseWidth, filterParam.cutoff);
@@ -378,9 +374,6 @@
     //--------------------------------------------------------------------
     // Setup Devices
     //
-    spiMaster.format(8, 0);
-    spiMaster.frequency(SPI1_RATE);
-
     gLCD.set_spi_frequency(SPI2_RATE);
     gLCD.begin(0x12);
     gLCD.clear();
@@ -423,7 +416,16 @@
     
     PinRunStop.attach_asserted(&swRunStopPressed);
     PinRunStop.setAssertValue(0);
-    PinRunStop.setSampleFrequency(); 
+    PinRunStop.setSampleFrequency();
+    
+    //--------------------------------------------------------------------
+    // Initialize SPI Master
+    // 
+    SpiMasterCs = 1;
+    SpiMaster.format(8, 0);
+    SpiMaster.frequency(SPI1_RATE);
+    
+    stepChangeInterrupt.fall(&updateStep);
     
     //--------------------------------------------------------------------
     // Initialize objects
@@ -443,9 +445,25 @@
     // Main loop
     //
     for (;;) {
+        // Polling Devices
         pollingPots();
         pollingRotEncs();
         pollingPins();
+        
+        // Send Params to SPI Slave
+        if (isDirty || isStepChanged) {
+            SpiMasterCs = 0;
+            uint8_t receivedVal = SpiMaster.write(currentStep);
+            SpiMasterCs = 1;
+
+            if (isStepChanged) {
+                playingStep = receivedVal;
+                isStepChanged = false;
+                isDirty = true;
+            }
+        }
+        
+        // Display
         if (!isRunning) {
             if (isDirty) {
                 switch (UImode) {
@@ -464,20 +482,6 @@
         } else {
             if (isDirty) {
                 sequencerDisplay.update(SequencerDisplay::run, currentStep, playingStep);
-                /*
-                static int cnt = 0;
-                char buffer[32];
-                sprintf(buffer, "%d", cnt);
-                cnt++;
-                gLCD.clear();
-                gLCD.drawstring(0, 0, buffer);
-                gLCD.display();
-                */
-                //gLCD.fillrect((currentStep-1) * 7, 1, 7, 1, 0);
-                //gLCD.clear();
-                //gLCD.fillrect(currentStep * 7, 1, 7, 3, 1);
-                //gLCD.display();
-                
                 isDirty = false;
             }
         }