various fixes for push2 support. Now setups video display when enabled
authorPaul Davis <paul@linuxaudiosystems.com>
Thu, 16 Jun 2016 04:07:43 +0000 (00:07 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Tue, 27 Sep 2016 19:59:29 +0000 (14:59 -0500)
libs/ardour/ardour/debug.h
libs/ardour/debug.cc
libs/surfaces/push2/interface.cc
libs/surfaces/push2/push2.cc
libs/surfaces/push2/push2.h
libs/surfaces/push2/render.cc
libs/surfaces/push2/wscript
libs/surfaces/wscript

index d2a258fb5614183ea7fd7a79034e461364968627..0c259b480f25c1d3cdb30f9d7226edf1f3768fed 100644 (file)
@@ -81,6 +81,7 @@ namespace PBD {
                LIBARDOUR_API extern DebugBits VSTCallbacks;
                LIBARDOUR_API extern DebugBits FaderPort;
                LIBARDOUR_API extern DebugBits VCA;
+               LIBARDOUR_API extern DebugBits Push2;
 
        }
 }
index 0aa9216c53556ca6bf27a6a91d28f3f666c67fa0..f52181611dd047cadaa08d2a8defa6933e1a5643 100644 (file)
@@ -78,3 +78,4 @@ PBD::DebugBits PBD::DEBUG::BackendPorts = PBD::new_debug_bit ("backendports");
 PBD::DebugBits PBD::DEBUG::VSTCallbacks = PBD::new_debug_bit ("vstcallbacks");
 PBD::DebugBits PBD::DEBUG::FaderPort = PBD::new_debug_bit ("faderport");
 PBD::DebugBits PBD::DEBUG::VCA = PBD::new_debug_bit ("vca");
+PBD::DebugBits PBD::DEBUG::Push2 = PBD::new_debug_bit ("push2");
index 9c89099433363c64701741cc45f9cf2ae439f16b..898a7b01920d35081bec7e36bb12671257bdf484 100644 (file)
@@ -1,5 +1,5 @@
 /*
-       Copyright (C) 2006,2007 Paul Davis
+       Copyright (C) 2017 Paul Davis
 
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
@@ -79,9 +79,8 @@ push2_request_buffer_factory (uint32_t num_requests)
        return Push2::request_factory (num_requests);
 }
 
-// Field names commented out by JE - 06-01-2010
-static ControlProtocolDescriptor mackie_descriptor = {
-       /*name :              */   "Ableton Push2",
+static ControlProtocolDescriptor push2_descriptor = {
+       /*name :              */   "Ableton Push 2",
        /*id :                */   "uri://ardour.org/surfaces/push2:0",
        /*ptr :               */   0,
        /*module :            */   0,
@@ -97,4 +96,4 @@ static ControlProtocolDescriptor mackie_descriptor = {
        /*request_buffer_factory */ push2_request_buffer_factory
 };
 
-extern "C" ARDOURSURFACE_API ControlProtocolDescriptor* protocol_descriptor () { return &mackie_descriptor; }
+extern "C" ARDOURSURFACE_API ControlProtocolDescriptor* protocol_descriptor () { return &push2_descriptor; }
index 355fc6205070475cb91fcb4cb2186fd1212e114b..be25357e267cca82d8f79b2ed110462836e894b4 100644 (file)
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include <cairomm/context.h>
+#include <cairomm/surface.h>
+#include <pangomm/layout.h>
+
+#include "pbd/compose.h"
+#include "pbd/debug.h"
 #include "pbd/failed_constructor.h"
 
+#include "ardour/debug.h"
+
 #include "push2.h"
 
 using namespace ARDOUR;
@@ -30,24 +38,68 @@ using namespace ArdourSurface;
 
 #include "pbd/abstract_ui.cc" // instantiate template
 
+const int Push2::cols = 960;
+const int Push2::rows = 160;
+const int Push2::pixels_per_row = 1024;
+
+#define ABLETON 0x2982
+#define PUSH2   0x1967
+
 Push2::Push2 (Session& s)
        : ControlProtocol (s, string (X_("Ableton Push2")))
        , AbstractUI<Push2Request> (name())
        , handle (0)
+       , device_buffer (0)
+       , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
+{
+}
+
+Push2::~Push2 ()
+{
+       close ();
+}
+
+int
+Push2::open ()
 {
        if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
-               throw failed_constructor ();
+               return -1;
        }
 
        libusb_claim_interface (handle, 0x00);
+
+       device_frame_buffer[0] = new uint16_t[rows*pixels_per_row];
+       device_frame_buffer[1] = new uint16_t[rows*pixels_per_row];
+
+       memset (device_frame_buffer[0], 0, sizeof (uint16_t) * rows * pixels_per_row);
+       memset (device_frame_buffer[1], 0, sizeof (uint16_t) * rows * pixels_per_row);
+
+       frame_header[0] = 0xef;
+       frame_header[1] = 0xcd;
+       frame_header[2] = 0xab;
+       frame_header[3] = 0x89;
+       memset (&frame_header[4], 0, 12);
+
+       return 0;
 }
 
-Push2::~Push2 ()
+int
+Push2::close ()
 {
+       vblank_connection.disconnect ();
+
        if (handle) {
                libusb_release_interface (handle, 0x00);
                libusb_close (handle);
        }
+
+       delete [] device_frame_buffer[0];
+       device_frame_buffer[0] = 0;
+
+       delete [] device_frame_buffer[1];
+       device_frame_buffer[1] = 0;
+
+       return 0;
 }
 
 bool
@@ -57,10 +109,12 @@ Push2::probe ()
        libusb_init (NULL);
 
        if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
+               DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
                return false;
        }
 
        libusb_close (h);
+       DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
        return true;
 }
 
@@ -78,7 +132,7 @@ Push2::request_factory (uint32_t num_requests)
 void
 Push2::do_request (Push2Request * req)
 {
-       // DEBUG_TRACE (DEBUG::MackieControl, string_compose ("doing request type %1\n", req->type));
+       DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
        if (req->type == CallSlot) {
 
                call_slot (MISSING_INVALIDATOR, req->the_slot);
@@ -92,7 +146,160 @@ Push2::do_request (Push2Request * req)
 int
 Push2::stop ()
 {
+       close ();
        BaseUI::quit ();
 
        return 0;
 }
+
+/** render host-side frame buffer (a Cairo ImageSurface) to the current
+ * device-side frame buffer. The device frame buffer will be pushed to the
+ * device on the next call to vblank()
+ */
+
+int
+Push2::render ()
+{
+       /* ensure that all drawing has been done before we fetch pixel data */
+
+       frame_buffer->flush ();
+
+       const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
+       const uint8_t* data = frame_buffer->get_data ();
+
+       /* fill frame buffer (320kB) */
+
+       Glib::Threads::Mutex::Lock lm (fb_lock);
+
+       uint16_t* fb = (uint16_t*) device_frame_buffer[device_buffer];
+
+       for (int row = 0; row < rows; ++row) {
+
+               const uint8_t* dp = data + row * stride;
+
+               for (int col = 0; col < cols; ++col) {
+
+                       /* fetch r, g, b (range 0..255). Ignore alpha */
+
+                       const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
+                       const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
+                       const int b = *((const uint32_t*)dp) & 0xff;
+
+                       /* convert to 5 bits, 6 bits, 5 bits, respectively */
+                       /* generate 16 bit BGB565 value */
+
+                       *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
+
+                       dp += 4;
+               }
+
+               /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
+                  byte USB buffers
+               */
+
+               fb += 64; /* 128 bytes = 64 int16_t */
+       }
+
+       /* swap buffers (under lock protection) */
+       // device_buffer = (device_buffer ? 0 : 1);
+
+       return 0;
+}
+
+bool
+Push2::vblank ()
+{
+       int transferred = 0;
+       const int timeout_msecs = 1000;
+       int err;
+
+       if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
+               return false;
+       }
+
+       {
+               Glib::Threads::Mutex::Lock lm (fb_lock);
+
+               if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer[device_buffer] , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+int
+Push2::set_active (bool yn)
+{
+       DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
+
+       if (yn == active()) {
+               return 0;
+       }
+
+       if (yn) {
+
+               if (open ()) {
+                       DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
+                       close ();
+                       return -1;
+               }
+
+               /* start event loop */
+
+               BaseUI::run ();
+
+               // connect_session_signals ();
+
+               /* say hello */
+
+               Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (frame_buffer);
+               if (!context) {
+                       cerr << "Cannot create context\n";
+                       return -1;
+               }
+               Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
+               if (!layout) {
+                       cerr << "Cannot create layout\n";
+                       return -1;
+               }
+
+               layout->set_text ("hello, Ardour");
+               Pango::FontDescription fd ("Sans Bold 12");
+               layout->set_font_description (fd);
+
+               context->set_source_rgb (0.0, 1.0, 1.0);
+               context->rectangle (0, 0, 960, 160);
+               context->fill ();
+               context->set_source_rgb (0.0, 0.0, 0.0);
+               context->rectangle (50, 50, 860, 60);
+               context->fill ();
+               context->move_to (60, 60);
+               context->set_source_rgb ((random()%255) / 255.0, (random()%255) / 255.0, (random()%255) / 255.0);
+               layout->update_from_cairo_context (context);
+               layout->show_in_cairo_context (context);
+
+               render ();
+
+               /* set up periodic task used to push a frame buffer to the
+                * device (25fps). The device can handle 60fps, but we don't
+                * need that frame rate.
+                */
+
+               Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
+               vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
+               vblank_timeout->attach (main_loop()->get_context());
+
+       } else {
+
+               BaseUI::quit ();
+               close ();
+
+       }
+
+       ControlProtocol::set_active (yn);
+
+       DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
+
+       return 0;
+}
index e5397e6ca8b6e7efb161dfe76343828b4f46c7ec..fb301a62a7c9e1e3f0d0e0ff8f60844ad5332fe7 100644 (file)
 
 #include <libusb.h>
 
+#include <cairomm/refptr.h>
+#include <glibmm/threads.h>
+
 #define ABSTRACT_UI_EXPORTS
 #include "pbd/abstract_ui.h"
 #include "midi++/types.h"
 #include "ardour/types.h"
 #include "control_protocol/control_protocol.h"
 
-#define ABLETON 0x2982
-#define PUSH2   0x1967
+namespace Cairo {
+       class ImageSurface;
+}
 
 namespace ArdourSurface {
 
@@ -53,10 +57,27 @@ class Push2 : public ARDOUR::ControlProtocol
        static bool probe ();
        static void* request_factory (uint32_t);
 
+       int set_active (bool yn);
+
    private:
        libusb_device_handle *handle;
+       Glib::Threads::Mutex fb_lock;
+       uint8_t   frame_header[16];
+       uint16_t* device_frame_buffer[2];
+       int  device_buffer;
+       Cairo::RefPtr<Cairo::ImageSurface> frame_buffer;
+       sigc::connection vblank_connection;
+
+       static const int cols;
+       static const int rows;
+       static const int pixels_per_row;
+
        void do_request (Push2Request*);
        int stop ();
+       int open ();
+       int close ();
+       int render ();
+       bool vblank ();
 };
 
 
index 590c8e51d38c102ab1846e4e64eb2cea1e35ae92..27a145df1d175979479a0d54cc4d24d684396cbb 100644 (file)
@@ -2,63 +2,3 @@
 #include <unistd.h>
 #include <assert.h>
 
-#include <pangomm/layout.h>
-#include <cairomm/context.h>
-#include <cairomm/surface.h>
-
-int
-deliver_image_surface (libusb_device_handle* handle, Cairo::RefPtr<Cairo::ImageSurface> surface)
-{
-       static uint8_t headerPkt[] = { 0xef, 0xcd, 0xab, 0x89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-       static uint16_t dataPkt[160*1024];
-
-       const Cairo::Format format = surface->get_format();
-
-       if (format != Cairo::FORMAT_ARGB32) {
-               return -1;
-       }
-
-       unsigned char *data = surface->get_data ();
-       const int width = surface->get_width();
-       const int height = surface->get_height();
-       const int stride = surface->get_stride();
-
-       assert (width == 960);
-       assert (height == 160);
-
-       /* fill a data packet (320kB) */
-
-       uint16_t* pkt_ptr = (uint16_t*) dataPkt;
-
-       for (int row = 0; row < height; ++row) {
-
-               uint8_t* dp = data + row * stride;
-
-               for (int col = 0; col < width; ++col) {
-
-                       /* fetch r, g, b (range 0..255). Ignore alpha */
-                       const int r = (*((uint32_t*)dp) >> 16) & 0xff;
-                       const int g = (*((uint32_t*)dp) >> 8) & 0xff;
-                       const int b = *((uint32_t*)dp) & 0xff;
-
-                       /* convert to 5 bits, 6 bits, 5 bits, respectively */
-                       /* generate 16 bit BGB565 value */
-
-                       *pkt_ptr++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
-
-                       dp += 4;
-               }
-
-               /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
-                  byte USB buffers
-               */
-
-               pkt_ptr += 64; /* 128 bytes = 64 int16_t */
-       }
-
-       int transferred = 0;
-
-       libusb_bulk_transfer (handle, 0x01, headerPkt, sizeof(headerPkt), &transferred, 1000);
-       libusb_bulk_transfer (handle, 0x01, (uint8_t*) dataPkt, sizeof(dataPkt), &transferred, 1000);
-}
-
index e572ffedd1fd2b04ca3ad741f74873b7fb8abb86..379f1199d749d2e7f629160e366b41d39f09ab89 100644 (file)
@@ -6,17 +6,16 @@ import os
 top = '.'
 out = 'build'
 
+print "this is push2"
+
 def options(opt):
     autowaf.set_options(opt)
     
 def configure(conf):
     conf.load ('compiler_cxx')
     autowaf.configure(conf)
-    autowaf.check_pkg(conf, 'cairomm-1.0', uselib_store='CAIROMM', atleast_version='1.8.4')
-    autowaf.check_pkg(conf, 'pangomm-1.5', uselib_store='CAIROMM', atleast_version='1.4')
-
-def configure(conf):
-    autowaf.configure(conf)
+    autowaf.check_pkg(conf, 'pangomm-1.4', uselib_store='PANGOMM', atleast_version='1.4', mandatory=True)
+    autowaf.check_pkg(conf, 'cairomm-1.0', uselib_store='CAIROMM', atleast_version='1.8.4', mandatory=True)
 
 def build(bld):
     obj = bld(features = 'cxx cxxshlib')
index 006c907db2d418feb10ff2c321896f4b0b4f63e7..0c736733a12da7c8602752215b349128af6a6eb2 100644 (file)
@@ -45,6 +45,8 @@ def configure(conf):
 
     if conf.is_defined('HAVE_USB'):
         children += [ 'push2' ]
+    else:
+        print ('You are missing the libusb-1.0 development package needed to compile Push2 support')
     
     if autowaf.check_pkg (conf, 'liblo', mandatory=False, uselib_store="LO", atleast_version="0.24"):
         children += [ 'osc' ]