Bladeren bron

implemented LaunchpadScreen class

Fabian Peter Hammerle 10 jaren geleden
bovenliggende
commit
e6634857b1
5 gewijzigde bestanden met toevoegingen van 139 en 5 verwijderingen
  1. 1 0
      .gitignore
  2. 104 2
      Launchpad.cpp
  3. 28 1
      Launchpad.h
  4. 4 1
      tests/Makefile.in
  5. 2 1
      tests/launchpad.cpp

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@
 /configure
 /m4
 /lib
+*.o

+ 104 - 2
Launchpad.cpp

@@ -18,6 +18,11 @@ bool LaunchpadColor::operator==(const LaunchpadColor& color) const
 	return red == color.red && green == color.green;
 }
 
+bool LaunchpadColor::operator!=(const LaunchpadColor& color) const
+{
+	return !operator==(color);
+}
+
 Launchpad::Launchpad()
 	: midiin(), midiout(), keyPressedCallback(0), keyReleasedCallback(0)
 {
@@ -165,7 +170,7 @@ void Launchpad::keyPressed(unsigned char x, unsigned char y)
 {
 	if(keyPressedCallback)
 	{
-		keyPressedCallback(x, y);
+		keyPressedCallback(x, y, keyEventCallbackData);
 	}
 }
 
@@ -173,6 +178,103 @@ void Launchpad::keyReleased(unsigned char x, unsigned char y)
 {
 	if(keyReleasedCallback)
 	{
-		keyReleasedCallback(x, y);
+		keyReleasedCallback(x, y, keyEventCallbackData);
+	}
+}
+
+
+void LaunchpadScreen::keyPressed(unsigned char x, unsigned char y)
+{
+}
+
+void LaunchpadScreen::keyReleased(unsigned char x, unsigned char y)
+{
+}
+
+void LaunchpadScreen::setColor(unsigned char x, unsigned char y, const LaunchpadColor& color)
+{
+    if(active && (!launchpad->issetColor(x, y) || launchpad->getColor(x, y) != color)) {
+        launchpad->setColor(x, y, color);
+    }
+
+    colors[x][y] = color;
+}
+
+void LaunchpadScreen::setColorAll(const LaunchpadColor& color)
+{
+	for(unsigned char x = 0; x < Launchpad::width; x++) {
+		for(unsigned char y = 0; y < Launchpad::height; y++) {
+			if(x != 8 || y != 8)
+			{
+				setColor(x, y, color);
+			}
+		}
+	}
+}
+
+void LaunchpadScreen::sync()
+{
+	for(unsigned char x = 0; x < Launchpad::width; x++) {
+		for(unsigned char y = 0; y < Launchpad::height; y++) {
+			if((x != 8 || y != 8)
+			    && (!launchpad->issetColor(x, y) || launchpad->getColor(x, y) != colors[x][y])) {
+                launchpad->setColor(x, y, colors[x][y]);
+			}
+		}
 	}
 }
+
+LaunchpadScreen::LaunchpadScreen()
+    : launchpad(0), active(false)
+{
+}
+
+void LaunchpadScreen::enable()
+{
+    if(!launchpad) {
+        throw RtMidiError(
+            "no launchpad set",
+            RtMidiError::INVALID_USE
+            );
+    }
+
+    launchpad->keyEventCallbackData = (void*)this;
+    launchpad->keyPressedCallback = keyPressedCallback;
+    launchpad->keyReleasedCallback = keyReleasedCallback;
+    sync();
+    active = true;
+}
+
+void LaunchpadScreen::disable()
+{
+    launchpad->keyPressedCallback = 0;
+    launchpad->keyReleasedCallback = 0;
+    launchpad->keyEventCallbackData = 0;
+    active = false;
+}
+
+bool LaunchpadScreen::enabled() const
+{
+    return active;
+}
+
+void LaunchpadScreen::setLaunchpad(Launchpad& l)
+{
+    if(enabled()) {
+        disable();
+        launchpad = &l;
+        enable();
+    } else {
+        launchpad = &l;
+    }
+}
+
+void LaunchpadScreen::keyPressedCallback(unsigned char x, unsigned char y, void* screen)
+{
+    ((LaunchpadScreen*)screen)->keyPressed(x, y);
+}
+
+void LaunchpadScreen::keyReleasedCallback(unsigned char x, unsigned char y, void* screen)
+{
+    ((LaunchpadScreen*)screen)->keyReleased(x, y);
+}

+ 28 - 1
Launchpad.h

@@ -11,12 +11,13 @@ public:
 	LaunchpadColor(unsigned char r, unsigned char g);
 
 	bool operator==(const LaunchpadColor& color) const;
+	bool operator!=(const LaunchpadColor& color) const;
 };
 
 class Launchpad
 {
 public:
-	typedef void (*KeyEventCallback)(unsigned char x, unsigned char y);
+	typedef void (*KeyEventCallback)(unsigned char x, unsigned char y, void* data);
 	
 	static const unsigned char width = 9;
 	static const unsigned char height = 9;
@@ -32,6 +33,7 @@ protected:
 public:
 	KeyEventCallback keyPressedCallback;
 	KeyEventCallback keyReleasedCallback;
+    void* keyEventCallbackData;
 
 	Launchpad();
 
@@ -47,3 +49,28 @@ protected:
 private:
 	static void midiMessageCallback(double timeStamp, MidiMessage &message, void *launchpad);
 };
+
+class LaunchpadScreen
+{
+    LaunchpadColor colors[Launchpad::width][Launchpad::height];
+    Launchpad* launchpad;
+    bool active;
+
+public:
+    LaunchpadScreen();
+    virtual void enable();
+    virtual void disable();
+    bool enabled() const;
+    void setLaunchpad(Launchpad& launchpad);
+
+protected:
+	virtual void keyPressed(unsigned char x, unsigned char y);
+	virtual void keyReleased(unsigned char x, unsigned char y);
+	void setColor(unsigned char x, unsigned char y, const LaunchpadColor& color);
+	void setColorAll(const LaunchpadColor& color);
+    void sync();
+
+private:
+    static void keyPressedCallback(unsigned char x, unsigned char y, void* screen);
+    static void keyReleasedCallback(unsigned char x, unsigned char y, void* screen);
+};

+ 4 - 1
tests/Makefile.in

@@ -1,7 +1,7 @@
 ### Do not edit -- Generated by 'configure --with-whatever' from Makefile.in
 ### RtMidi tests Makefile - for various flavors of unix
 
-PROGRAMS = midiprobe midiout qmidiin cmidiin sysextest launchpad
+PROGRAMS = midiprobe midiout qmidiin cmidiin sysextest launchpad launchpad-screen
 RM = /bin/rm
 SRC_PATH = ..
 INCLUDE = @top_srcdir@
@@ -39,6 +39,9 @@ sysextest : @srcdir@/sysextest.cpp $(OBJECTS)
 launchpad : @srcdir@/launchpad.cpp $(OBJECTS)
 	$(CC) $(CFLAGS) $(DEFS) -o launchpad $^ $(LIBRARY) ../MidiMessage.o ../Midi.o ../Launchpad.o
 
+launchpad-screen : @srcdir@/launchpad-screen.cpp $(OBJECTS)
+	$(CC) $(CFLAGS) $(DEFS) -o launchpad-screen $^ $(LIBRARY) ../MidiMessage.o ../Midi.o ../Launchpad.o
+
 clean : 
 	$(RM) -f $(OBJECT_PATH)/*.o
 	$(RM) -f $(PROGRAMS) *.exe

+ 2 - 1
tests/launchpad.cpp

@@ -15,7 +15,7 @@
 
 Launchpad l;
 
-void keyPressed(unsigned char x, unsigned char y)
+void keyPressed(unsigned char x, unsigned char y, void* data)
 {
     std::cout << "(" << (int)x << ", " << (int)y << ")" << std::endl;
 
@@ -30,6 +30,7 @@ int main()
 
     while(true) 
     {
+        SLEEP(1);
     }
 
     return 0;