Nucleo SPI Sequencer

Dependencies:   AverageAnalogIn N5110 Nucleo_rtos_UI_Test PinDetect RotaryEncoder Sequence mbed-rtos mbed FilterController

Fork of Nucleo_rtos_UI_Test by Ryo Od

Revision:
9:06ba77ce8b3c
Parent:
8:2c481dab525a
Child:
10:28082577ed5f
--- a/main.cpp	Sun Jun 12 08:49:34 2016 +0000
+++ b/main.cpp	Sun Jun 12 09:59:02 2016 +0000
@@ -3,7 +3,9 @@
  *
  * 2016.06.12
  *
- */
+ */ 
+ 
+#define UART_TRACE  (0)
  
 #include "mbed.h"
 #include "rtos.h"
@@ -13,11 +15,10 @@
 #include "AverageAnalogIn.h"
 #include "SpiSequenceSender.h"
 
+#define UART_TRACE  (0)
 #define SPI_RATE    (8000000)
 
 #define SEQUENCE_N  (16)
-#define OCTAVE_MIN  (-1)
-#define OCTAVE_MAX  (1)
 #define PITCH_MAX   (12)
 
 DigitalOut CheckPin(PC_8);
@@ -57,8 +58,7 @@
     PinDetect(PA_8,  PullUp),
 };
 
-// Grobal Variables
-
+// ToDo: Filterにモジュレーション信号を送る方法を考える。
 struct Filter {
     int cutOff;
     int resonance;
@@ -66,7 +66,6 @@
 } Filter;
 
 int currentStep = 0;
-int tempo = 120;
 bool isRunning = true;
 bool isDirty = true;
 
@@ -78,7 +77,7 @@
     char buff[20];
     
     //Lcd.clear();
-    sprintf(buff, "Note#: %d  ", currentStep);
+    sprintf(buff, "step#: %d  ", currentStep);
     Lcd.printString(buff, 0, 0);
     sprintf(buff, "pitch: %d  ", sequence[currentStep].getPitch());
     Lcd.printString(buff, 0, 1);
@@ -89,13 +88,23 @@
         isRunning, sequenceSender.getWaveShape());
     Lcd.printString(buff, 0, 3);
     sprintf(buff, "%3d %3d %3d", 
-        sequenceSender.getPulseWidth(), Filter.envMod, tempo);
+        sequenceSender.getPulseWidth(), Filter.envMod, sequenceSender.getBpm());
     Lcd.printString(buff, 0, 4);
     sprintf(buff, "%3d %3d", Filter.cutOff, Filter.resonance);
     Lcd.printString(buff, 0, 5);
     Lcd.refresh();
 }
 
+void initSequence()
+{
+    for (int i = 0; i < SEQUENCE_N; i++) {
+        Sequence& seq = sequenceSender.getSequences()[i];
+        seq.setPitch(i);
+        seq.setOctave(0);
+        seq.tie = true;
+    }
+}
+
 //------------------------------------------------------------------------
 // CallBack routines
 //------------------------------------------------------------------------
@@ -103,47 +112,63 @@
 {
     sequence[currentStep].setOctave(sequence[currentStep].getOctave() + 1);
     isDirty = true;
+    #if (UART_TRACE)
     printf("swOctaveUpPressed\r\n");
+    #endif
 }
 
 void swOctaveDownPressed()
 {
     sequence[currentStep].setOctave(sequence[currentStep].getOctave() - 1);
     isDirty = true;
+    #if (UART_TRACE)
     printf("swOctaveDownPressed\r\n");
+    #endif
 }
 
 void swNoteOnOffPressed()
 {
     sequence[currentStep].noteOn = !sequence[currentStep].noteOn;
     isDirty = true;
+    #if (UART_TRACE)
     printf("swNoteOnOffPressed\r\n");
+    #endif
 }
 
 void swTiePressed()
 {
     sequence[currentStep].tie = !sequence[currentStep].tie;
     isDirty = true;
+    #if (UART_TRACE)
     printf("swTiePressed\r\n");
+    #endif
 }
 
 void swAccentPressed()
 {
     sequence[currentStep].accent = !sequence[currentStep].accent;
     isDirty = true;
+    #if (UART_TRACE)
     printf("swAccentPressed\r\n");
+    #endif
 }
 
 void swRunStopPressed()
 {
     isRunning = !isRunning;
+    if (isRunning) {
+        sequenceSender.run(currentStep);
+    } else {
+        sequenceSender.stop();
+    }
     isDirty = true;
+    #if (UART_TRACE)
     printf("swRunStopPressed\r\n");
+    #endif
 }
 
 void swWaveShapePressed()
 {
-    //Oscillator.waveShape++;
     uint8_t shape = sequenceSender.getWaveShape();
     shape++;
     if (shape == sequenceSender.WAVESHAPE_N) {
@@ -151,7 +176,9 @@
     }
     sequenceSender.setWaveShape(shape);
     isDirty = true;
+    #if (UART_TRACE)
     printf("swWaveShapePressed\r\n");
+    #endif
 }
 
 //------------------------------------------------------------------------
@@ -193,10 +220,11 @@
             Filter.envMod = tmp;
             isDirty = true;
         }
-        // tempo
+        // bpm
         tmp = Pots[2].read_u16() >> 9;    // 7bit witdth
-        if (tmp != tempo) {
-            tempo = tmp;
+        tmp <<= 1;
+        if (tmp != sequenceSender.getBpm()) {
+            sequenceSender.setBpm(tmp);
             isDirty = true;
         }
         // cutoff
@@ -220,7 +248,9 @@
 //------------------------------------------------------------------------
 int main()
 {
+    #if (UART_TRACE)
     printf("\n\n\r*** RTOS UI Test ***\r\n");
+    #endif
     
     spiMaster.format(0, 8);
     spiMaster.frequency(SPI_RATE);
@@ -249,14 +279,7 @@
     Thread thRotEnc(pollingRotEncs, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
     Thread thPots(pollingPots, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
 
-    /*
-    for (int i = 0; i < SEQUENCE_N; i++) {
-        Sequence& seq = sequenceSender.getSequences()[i];
-        seq.setPitch(0);
-        seq.setOctave(0);
-        seq.tie = false;
-    }
-    */
+    initSequence();
     sequenceSender.run(0);
     
     // Main loop