BaseMachine UI Controllerに分離

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

Fork of BaseMachine_Sequencer by Ryo Od

Revision:
44:b1bdccdbf67e
Parent:
43:12d11507c077
Child:
45:3c7143da38d1
--- a/main.cpp	Fri Oct 07 07:48:22 2016 +0000
+++ b/main.cpp	Fri Oct 07 09:02:29 2016 +0000
@@ -22,11 +22,28 @@
 #define TITLE_STR2  ("20161007")
 
 #define SEQUENCE_N  (16)
-#define SPI1_RATE   (10000000)
+#define SPI1_RATE   (3000000)
 #define SPI2_RATE   (1000000)
 #define POT_RESOLUTION  (7)     // bit
 #define AIN_AVERAGE     (32)    // AnalogInを移動平均する要素数
 
+enum SequencerCmd {
+    CMD_RCV_PLAYING_STEP = 0x01,
+    CMD_RUN              = 0x11,
+    CMD_BPM              = 0x12,
+    CMD_ACCENT_LEVEL     = 0x13,
+    CMD_WAVE_SHAPE       = 0x21,
+    CMD_PULSE_WIDTH      = 0x22,
+    CMD_CUTOFF           = 0x31,
+    CMD_RESONANCE        = 0x32,
+    CMD_LEVEL            = 0x41,
+    CMD_DURATION         = 0x42,
+    CMD_DECAY            = 0x43,
+    CMD_SUSTAIN          = 0x44,
+    CMD_NOTE             = 0x51,
+    CMD_PITCH            = 0x52, 
+};
+
 const int baseNoteNumber = 36;
 const int bpmMax = 240;
 const int bpmMin = 60;
@@ -173,6 +190,18 @@
 //------------------------------------------------------------------------
 // Functions
 //------------------------------------------------------------------------
+uint16_t SpiSendParams(uint8_t cmd, uint8_t data)
+{
+    uint16_t sendVal = ((uint16_t)cmd << 8) | data;
+    
+    SpiMasterCs = 0;
+    uint16_t recievedVal = SpiMaster.write(sendVal);
+    SpiMasterCs = 1;
+    //Thread::wait(1);
+    
+    return recievedVal;
+}
+
 void pollingPots()
 {
     uint8_t pulseWidth = AinPulseWidth.read_u16() >> (16 - POT_RESOLUTION);
@@ -512,7 +541,7 @@
     // Initialize SPI Master
     // 
     SpiMasterCs = 1;
-    SpiMaster.format(8, 0);
+    SpiMaster.format(16, 0);
     SpiMaster.frequency(SPI1_RATE);
     
     stepChangeInterrupt.fall(&updateStep);
@@ -542,19 +571,14 @@
         pollingRotEncs();
         pollingPins();
         
-        // Send Params to SPI Slave
-        if (isDirty || isStepChanged) {
-            SpiMasterCs = 0;
-            uint8_t receivedVal = SpiMaster.write(currentStep);
-            SpiMasterCs = 1;
+        // Recieve playing step from SPI slave
+        if (isStepChanged) {
+            uint16_t recievedVal = SpiSendParams(CMD_RCV_PLAYING_STEP, 0x55);   // Send dummy data
+            playingStep = (uint8_t)(recievedVal & 0xff);
+            isStepChanged = false;
+            isDirty = true;
+        }
 
-            if (isStepChanged) {
-                playingStep = receivedVal;
-                isStepChanged = false;
-                isDirty = true;
-            }
-        }
-        
         // Display
         if (!isRunning) {
             if (isDirty) {