User Interface Test on mbed RTOS

Dependencies:   N5110 RotaryEncoder mbed-rtos mbed PinDetect

Files at this revision

API Documentation at this revision

Comitter:
ryood
Date:
Fri May 27 00:23:46 2016 +0000
Parent:
3:8c8020dfd82f
Commit message:
???????????????

Changed in this revision

AverageAnalogIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8c8020dfd82f -r d9a72e07749f AverageAnalogIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AverageAnalogIn.h	Fri May 27 00:23:46 2016 +0000
@@ -0,0 +1,36 @@
+#ifndef _AVERAGE_ANALOG_H_
+#define _AVERAGE_ANALOG_H_
+
+#include "mbed.h"
+
+class AverageAnalogIn {
+public:
+    AverageAnalogIn(PinName _pin, int _bufferSize=8) : m_AnalogIn(_pin), bufferSize(_bufferSize), index(0) {
+        buffer = new unsigned short[bufferSize];
+    }
+    
+    ~AverageAnalogIn() {
+        delete buffer;
+    }
+    
+    unsigned short read_u16() {
+        buffer[index] = m_AnalogIn.read_u16();
+        index++;
+        if (index == bufferSize) {
+            index = 0;
+        }
+        unsigned int sum = 0;
+        for (int i = 0; i < bufferSize; i++) {
+            sum += buffer[i];
+        }
+        return sum / bufferSize;
+    }
+
+private:
+    AnalogIn m_AnalogIn;
+    int bufferSize;
+    int index;
+    unsigned short *buffer;
+};
+
+#endif //_AVERAGE_ANALOG_H_
\ No newline at end of file
diff -r 8c8020dfd82f -r d9a72e07749f main.cpp
--- a/main.cpp	Wed May 25 08:14:42 2016 +0000
+++ b/main.cpp	Fri May 27 00:23:46 2016 +0000
@@ -1,8 +1,16 @@
+/*
+ * Nucleo RTOS Sequencer User Interface Test.
+ *
+ * 2016.05.27
+ *
+ */
+ 
 #include "mbed.h"
 #include "rtos.h"
 #include "PinDetect.h"
 #include "RotaryEncoder.h"
 #include "N5110.h"
+#include "AverageAnalogIn.h"
 
 #define SEQUENCE_N  16
 #define OCTAVE_MIN  -1
@@ -16,12 +24,12 @@
 RotaryEncoder RotEnc1(D2, D3, 0, SEQUENCE_N - 1, 0);
 RotaryEncoder RotEnc2(D4, D5, 0, PITCH_MAX, 0);
 
-AnalogIn Pots[] = {
-    AnalogIn(A0),
-    AnalogIn(A1),
-    AnalogIn(A2),
-    AnalogIn(A3),
-    AnalogIn(A4),
+AverageAnalogIn Pots[] = {
+    AverageAnalogIn(A0),
+    AverageAnalogIn(A1),
+    AverageAnalogIn(A2),
+    AverageAnalogIn(A3),
+    AverageAnalogIn(A4),
 };
 
 PinDetect Pins[] = {
@@ -35,6 +43,7 @@
 };
 
 DigitalOut Led1(LED1);
+DigitalOut CheckPin(PC_8);
 
 // Grobal Variables
 struct Sequence {
@@ -45,35 +54,41 @@
     bool accent;
 } Sequence[SEQUENCE_N];
 
+struct Oscillator {
+    int waveForm;
+    int pulseWidth;    
+} Oscillator;
+
+struct Filter {
+    int cutOff;
+    int resonance;
+    int envMod;
+} Filter;
+
 int currentNote = 0;
-int waveForm = 0;
+int tempo = 120;
 bool isRunning = true;
 bool isDirty = true;
 
-void ledThread(void const *argument)
-{
-    while (true) {
-        Led1 = !Led1;
-        Thread::wait(500);
-    }
-}
-
 void updateLCD()
 {
     char buff[20];
     
-    Lcd.clear();
-    Lcd.printString("RTOS UI Test.", 0, 0);
-    sprintf(buff, "Note#: %d", currentNote);
+    //Lcd.clear();
+    sprintf(buff, "Note#: %d  ", currentNote);
+    Lcd.printString(buff, 0, 0);
+    sprintf(buff, "pitch: %d  ", Sequence[currentNote].pitch);
     Lcd.printString(buff, 0, 1);
-    sprintf(buff, "pitch: %d", Sequence[currentNote].pitch);
+    sprintf(buff, "octave: %d  " ,Sequence[currentNote].octave);
     Lcd.printString(buff, 0, 2);
-    sprintf(buff, "octave: %d" ,Sequence[currentNote].octave);
+    sprintf(buff, "%1d %1d %1d %1d %3d", 
+        Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent,
+        isRunning, Oscillator.waveForm);
     Lcd.printString(buff, 0, 3);
-    sprintf(buff, "%d %d %d %d %d", 
-        Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent,
-        isRunning, waveForm);
+    sprintf(buff, "%3d %3d %3d", Oscillator.pulseWidth, Filter.envMod, tempo);
     Lcd.printString(buff, 0, 4);
+    sprintf(buff, "%3d %3d", Filter.cutOff, Filter.resonance);
+    Lcd.printString(buff, 0, 5);
     Lcd.refresh();
 }
 
@@ -122,27 +137,74 @@
 
 void swWaveFormPressed()
 {
-    waveForm++;
+    Oscillator.waveForm++;
     isDirty = true;
     printf("swWaveFormPressed\r\n");
 }
 
-void pollingRotEncs()
+// Thread
+void ledThread(void const *argument)
+{
+    while (true) {
+        Led1 = !Led1;
+        Thread::wait(500);
+    }
+}
+
+void pollingRotEncs(void const *argument)
 {
-    int _note = RotEnc1.getVal();
-    if (_note != currentNote) {
-        currentNote = _note;
-        isDirty = true;
-    }
-    int _pitch = RotEnc2.getVal();
-    if (_pitch != Sequence[currentNote].pitch) {
-        Sequence[currentNote].pitch = _pitch;
-        isDirty = true;
+    while (true) {
+        int _note = RotEnc1.getVal();
+        if (_note != currentNote) {
+            currentNote = _note;
+            isDirty = true;
+        }
+        int _pitch = RotEnc2.getVal();
+        if (_pitch != Sequence[currentNote].pitch) {
+            Sequence[currentNote].pitch = _pitch;
+            isDirty = true;
+        }
+        Thread::wait(10);
     }
 }
 
-void pollingPots()
+void pollingPots(void const *argument)
 {
+    unsigned short tmp;
+    
+    while (true) {
+        // pulse width
+        tmp = Pots[0].read_u16() >> 9;    // 7bit witdth
+        if (tmp != Oscillator.pulseWidth) {
+            Oscillator.pulseWidth = tmp;
+            isDirty = true;
+        }
+        // filter envelope moduration 
+        tmp = Pots[1].read_u16() >> 9;    // 7bit witdth
+        if (tmp != Filter.envMod) {
+            Filter.envMod = tmp;
+            isDirty = true;
+        }
+        // tempo
+        tmp = Pots[2].read_u16() >> 9;    // 7bit witdth
+        if (tmp != tempo) {
+            tempo = tmp;
+            isDirty = true;
+        }
+        // cutoff
+        tmp = Pots[3].read_u16() >> 10;    // 6bit witdth
+        if (tmp != Filter.cutOff) {
+            Filter.cutOff = tmp;
+            isDirty = true;
+        }
+        // resonance
+        tmp = Pots[4].read_u16() >> 10;    // 6bit witdth
+        if (tmp != Filter.resonance) {
+            Filter.resonance = tmp;
+            isDirty = true;
+        }
+        Thread::wait(20);
+    }
 }
 
 int main()
@@ -170,12 +232,13 @@
     Lcd.setBrightness(0.5); // put LED backlight on 50%
     
     // Thread start
-    Thread thread1(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);
     
     // Main loop
     while (true) {
-        pollingRotEncs();
-        pollingPots();
+        CheckPin = !CheckPin;
         if (isDirty) {
             updateLCD();
             isDirty = false;