소스 검색

launchpad screen working again

Fabian Peter Hammerle 10 년 전
부모
커밋
e3a4d1e902
5개의 변경된 파일149개의 추가작업 그리고 22개의 파일을 삭제
  1. 3 1
      CMakeLists.txt
  2. 104 0
      LaunchpadScreen.cpp
  3. 33 0
      LaunchpadScreen.h
  4. 3 0
      tests/CMakeLists.txt
  5. 6 21
      tests/launchpad-screen.cpp

+ 3 - 1
CMakeLists.txt

@@ -4,8 +4,10 @@ project(midi)
 include_directories("${CMAKE_SOURCE_DIR}")
 include_directories("${CMAKE_SOURCE_DIR}/include")
 
+file(GLOB srcfiles *.cpp)
+
 add_definitions(-pthread)
 add_definitions(-std=c++11)
 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
-add_library(midi Input.cpp Output.cpp Message.cpp Launchpad.cpp LaunchpadColor.cpp)
+add_library(midi ${srcfiles})
 # target_link_libraries(midi /usr/local/lib/librtmidi.so.2.1.0) 

+ 104 - 0
LaunchpadScreen.cpp

@@ -0,0 +1,104 @@
+#include "LaunchpadScreen.h"
+#include <string>
+
+namespace midi {
+
+void LaunchpadScreen::keyPressed(unsigned char x, unsigned char y)
+{
+}
+
+void LaunchpadScreen::keyReleased(unsigned char x, unsigned char y)
+{
+}
+
+const LaunchpadColor& LaunchpadScreen::getColor(unsigned char x, unsigned char y) const
+{
+    return colors[x][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 "no launchpad set";
+    }
+
+    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);
+}
+
+} // namespace

+ 33 - 0
LaunchpadScreen.h

@@ -0,0 +1,33 @@
+#pragma once
+#include "LaunchpadColor.h"
+#include "Launchpad.h"
+
+namespace midi {
+
+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);
+	const LaunchpadColor& getColor(unsigned char x, unsigned char y) const;
+	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);
+};
+
+} // namespace

+ 3 - 0
tests/CMakeLists.txt

@@ -10,3 +10,6 @@ target_link_libraries(midiout midi /usr/local/lib/librtmidi.so.2)
 
 add_executable(launchpad launchpad.cpp)
 target_link_libraries(launchpad midi /usr/local/lib/librtmidi.so.2) 
+
+add_executable(launchpad-screen launchpad-screen.cpp)
+target_link_libraries(launchpad-screen midi /usr/local/lib/librtmidi.so.2) 

+ 6 - 21
tests/launchpad-screen.cpp

@@ -1,29 +1,17 @@
 #include <iostream>
-#include <cstdlib>
-#include "RtMidi.h"
-#include "MidiMessage.h"
-#include "Launchpad.h"
+#include "LaunchpadScreen.h"
 
-// Platform-dependent sleep routines.
-#if defined(__WINDOWS_MM__)
-  #include <windows.h>
-  #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
-#else // Unix variants
-  #include <unistd.h>
-  #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
-#endif
-
-struct DrawScreen : public LaunchpadScreen
+struct DrawScreen : public midi::LaunchpadScreen
 {
     virtual void keyPressed(unsigned char x, unsigned char y);
 
     void all(unsigned short a, unsigned short b)
     {
-        setColorAll(LaunchpadColor(a, b));
+        setColorAll(midi::LaunchpadColor(a, b));
     }
 };
 
-Launchpad l;
+midi::Launchpad l;
 DrawScreen s[8];
     
 void DrawScreen::keyPressed(unsigned char x, unsigned char y)
@@ -35,7 +23,7 @@ void DrawScreen::keyPressed(unsigned char x, unsigned char y)
         disable();
         s[x].enable();
     } else {
-        setColor(x, y, LaunchpadColor(3 - getColor(x, y).red, 3 - getColor(x, y).green));
+        setColor(x, y, midi::LaunchpadColor(3 - getColor(x, y).red, 3 - getColor(x, y).green));
     }
 }
 
@@ -47,10 +35,7 @@ int main()
     }
     s[4].enable();
 
-    while(true) 
-    {
-        SLEEP(1);
-    }
+    std::cin.ignore();
 
     return 0;
 }