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:
5:e4b68faa6801
Parent:
4:d9a72e07749f
Child:
6:4e089888b809
--- a/main.cpp	Fri May 27 00:23:46 2016 +0000
+++ b/main.cpp	Sun Jun 12 07:13:18 2016 +0000
@@ -1,7 +1,7 @@
 /*
- * Nucleo RTOS Sequencer User Interface Test.
+ * Nucleo SPI Sequencer
  *
- * 2016.05.27
+ * 2016.06.12
  *
  */
  
@@ -11,12 +11,27 @@
 #include "RotaryEncoder.h"
 #include "N5110.h"
 #include "AverageAnalogIn.h"
+#include "SpiSequenceSender.h"
 
-#define SEQUENCE_N  16
-#define OCTAVE_MIN  -1
-#define OCTAVE_MAX  1
-#define PITCH_MAX   12
+#define SPI_RATE    (8000000)
+
+#define SEQUENCE_N  (16)
+#define OCTAVE_MIN  (-1)
+#define OCTAVE_MAX  (1)
+#define PITCH_MAX   (12)
+
+DigitalOut CheckPin(PC_8);
 
+//------------------------------------------------------------------------
+// SPI Sequencer
+//------------------------------------------------------------------------
+SPI spiMaster(SPI_MOSI, SPI_MISO, SPI_SCK);
+Sequence sequence[SEQUENCE_N];
+SpiSequenceSender sequenceSender(&spiMaster, D10, sequence, SEQUENCE_N, 5);
+
+//------------------------------------------------------------------------
+// User Interface
+//------------------------------------------------------------------------
 // SPI2 Morpho
 //        VCC,   SCE,  RST,  D/C,   MOSI,  SCLK,  LED
 N5110 Lcd(PA_12, PB_1, PB_2, PB_12, PB_15, PB_13, PA_11);
@@ -42,17 +57,17 @@
     PinDetect(PA_8,  PullUp),
 };
 
-DigitalOut Led1(LED1);
-DigitalOut CheckPin(PC_8);
+//DigitalOut Led1(LED1);
+
 
 // Grobal Variables
-struct Sequence {
+struct sSequence {
     bool noteOn;
     int octave;
     int pitch;
     bool tie;
     bool accent;
-} Sequence[SEQUENCE_N];
+} sSequence[SEQUENCE_N];
 
 struct Oscillator {
     int waveForm;
@@ -70,6 +85,9 @@
 bool isRunning = true;
 bool isDirty = true;
 
+//------------------------------------------------------------------------
+// Fuctions
+//------------------------------------------------------------------------
 void updateLCD()
 {
     char buff[20];
@@ -77,12 +95,12 @@
     //Lcd.clear();
     sprintf(buff, "Note#: %d  ", currentNote);
     Lcd.printString(buff, 0, 0);
-    sprintf(buff, "pitch: %d  ", Sequence[currentNote].pitch);
+    sprintf(buff, "pitch: %d  ", sSequence[currentNote].pitch);
     Lcd.printString(buff, 0, 1);
-    sprintf(buff, "octave: %d  " ,Sequence[currentNote].octave);
+    sprintf(buff, "octave: %d  " ,sSequence[currentNote].octave);
     Lcd.printString(buff, 0, 2);
     sprintf(buff, "%1d %1d %1d %1d %3d", 
-        Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent,
+        sSequence[currentNote].noteOn, sSequence[currentNote].tie, sSequence[currentNote].accent,
         isRunning, Oscillator.waveForm);
     Lcd.printString(buff, 0, 3);
     sprintf(buff, "%3d %3d %3d", Oscillator.pulseWidth, Filter.envMod, tempo);
@@ -92,38 +110,40 @@
     Lcd.refresh();
 }
 
+//------------------------------------------------------------------------
 // CallBack routines
+//------------------------------------------------------------------------
 void swOctaveUpPressed()
 {
-    Sequence[currentNote].octave++;
+    sSequence[currentNote].octave++;
     isDirty = true;
     printf("swOctaveUpPressed\r\n");
 }
 
 void swOctaveDownPressed()
 {
-    Sequence[currentNote].octave--;
+    sSequence[currentNote].octave--;
     isDirty = true;
     printf("swOctaveDownPressed\r\n");
 }
 
 void swNoteOnOffPressed()
 {
-    Sequence[currentNote].noteOn = !Sequence[currentNote].noteOn;
+    sSequence[currentNote].noteOn = !sSequence[currentNote].noteOn;
     isDirty = true;
     printf("swNoteOnOffPressed\r\n");
 }
 
 void swTiePressed()
 {
-    Sequence[currentNote].tie = !Sequence[currentNote].tie;
+    sSequence[currentNote].tie = !sSequence[currentNote].tie;
     isDirty = true;
     printf("swTiePressed\r\n");
 }
 
 void swAccentPressed()
 {
-    Sequence[currentNote].accent = !Sequence[currentNote].accent;
+    sSequence[currentNote].accent = !sSequence[currentNote].accent;
     isDirty = true;
     printf("swAccentPressed\r\n");
 }
@@ -142,7 +162,10 @@
     printf("swWaveFormPressed\r\n");
 }
 
+//------------------------------------------------------------------------
 // Thread
+//------------------------------------------------------------------------
+/*
 void ledThread(void const *argument)
 {
     while (true) {
@@ -150,6 +173,7 @@
         Thread::wait(500);
     }
 }
+*/
 
 void pollingRotEncs(void const *argument)
 {
@@ -160,8 +184,8 @@
             isDirty = true;
         }
         int _pitch = RotEnc2.getVal();
-        if (_pitch != Sequence[currentNote].pitch) {
-            Sequence[currentNote].pitch = _pitch;
+        if (_pitch != sSequence[currentNote].pitch) {
+            sSequence[currentNote].pitch = _pitch;
             isDirty = true;
         }
         Thread::wait(10);
@@ -207,9 +231,15 @@
     }
 }
 
+//------------------------------------------------------------------------
+// Main routine
+//------------------------------------------------------------------------
 int main()
 {
     printf("\n\n\r*** RTOS UI Test ***\r\n");
+    
+    spiMaster.format(0, 8);
+    spiMaster.frequency(SPI_RATE);
 
     // Init devices
     RotEnc1.setInterval(500);
@@ -232,9 +262,17 @@
     Lcd.setBrightness(0.5); // put LED backlight on 50%
     
     // Thread start
-    Thread thLed(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
+    //Thread thLed(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
     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;
+    }
+    sequenceSender.run(0);
     
     // Main loop
     while (true) {