KDjKernel is a base class for "low level" realtime input/output communication with KDj devices. KDjExtended is a base class for "high level" realtime input/output communication with KDj devices. Each class instance supports only a single KDj controller connection. KDj controller data is passed to the user as raw bytes using an std::vector<unsigned char> (messages in advance). The length of these messages is always 8 bytes.
#include "KontrolDj.h" class myDerivedKDjClass : public KDjExtended{}; int main() { myDerivedKDjClass *myKDj = 0; // myDerivedKDjClass constructor. try { myKDj = new myDerivedKDjClass(); } catch (KDjError &error) { // Handle the exception here error.printMessage(); } // Clean up. delete myKDj; }
Obviously, this example doesn't demonstrate any of the real functionality of the KDjExtended class. However, all uses of the derived classes must begin with construction and must end with class destruction. Further, it is necessary that all class methods which can throw a C++ exception be called within a try/catch block.
// Probing Devices // kdjinfo.cpp #include <iostream> #include "KontrolDj.h" class myDerivedKDjClass : public KDjExtended{}; int main() { myDerivedKDjClass *myKDj = 0; // myDerivedKDjClass constructor. try { myKDj = new myDerivedKDjClass(); } catch (KDjError &error) { error.printMessage(); exit(EXIT_FAILURE); } // Check devices. unsigned int nDevices = myKDj->getDeviceCount(); std::cout << "\nThere are " << nDevices << " KDj devices available.\n"; std::string deviceName; for ( unsigned int i=0; i<nDevices; i++ ) { try { deviceName = myKDj->getDeviceName(i); } catch (KDjError &error) { error.printMessage(); goto cleanup; } std::cout << " KDj device #" << i+1 << ": " << deviceName << '\n'; } // Clean up. cleanup: delete myKDj; return 0; }
// Sending messages to KDj controller // kdjout.cpp #include <iostream> #include "KontrolDj.h" class myDerivedKDjClass : public KDjExtended{}; int main() { myDerivedKDjClass *myKDj = 0; myKDj = new myDerivedKDjClass(); std::vector<unsigned char> message(8); // Check available devices. unsigned int nDevices = myKDj->getDeviceCount(); if ( nDevices == 0 ) { std::cout << "No KDj devices available!\n"; goto cleanup; } // Open first available device. myKDj->openDevice( 0 ); // Send out a series of command messages. // Set the controller in slave mode (using high level function). myKDj->setControllerMode( 1 ); SLEEP( 2000 ); // Platform-dependent. // Turn on all the leds. myKDj->setLedsStates( 0xFFFF ); SLEEP( 2000 ); // Platform-dependent. // Turn off all the leds. myKDj->setLedsStates( 0x0000 ); SLEEP( 2000 ); // Platform-dependent. // Turn on led_play_A (number 10). myKDj->setLedState( 10, 1 ); SLEEP( 2000 ); // Platform-dependent. // Turn on led_cue_A (number 13). myKDj->setLedState( 13, 1 ); SLEEP( 2000 ); // Platform-dependent. // Set the controller in master mode (using low level function). message.at(0) = KDJ_SET_CONTROLLER_MODE; message.at(1) = 0; myKDj->sendMessage( &message ); // Clean up cleanup: delete myKDj; return 0; }
std::vector<unsigned char> container. When no message is available, the function returns an empty container. The default read timeout is 0.5 seconds. This value may be modified in the KontrolDj.cpp source file. The maximum FIFO receive buffer size is 384 bytes (48 messages). If the maximum FIFO receive buffer size limit is reached, subsequent incoming messages are discarded until the FIFO buffer size is reduced.In the following example, we omit some necessary error checking and details regarding OS-dependent functions. For a more complete example, see the lkdjin.cpp program in the tests directory.
// Receiving messages ( low level ) // lkdjin.cpp #include <iostream> #include <signal.h> #include "KontrolDj.h" class myDerivedKDjClass : public KDjKernel{}; bool done; static void finish(int ignore){ done = true; } int main() { myDerivedKDjClass *myKDj = new myDerivedKDjClass(); std::vector<unsigned char> message; int i; // Check available devices. unsigned int nDevices = myKDj->getDeviceCount(); if ( nDevices == 0 ) { std::cout << "No KDj devices available!\n"; goto cleanup; } myKDj->openDevice( 0 ); // Install an interrupt handler function. done = false; (void) signal(SIGINT, finish); // Periodically check KDj FIFO buffer. std::cout << "Reading messages from buffer ... quit with Ctrl-C.\n"; while ( !done ) { if( message.size() == 0 ) message.assign( 8, 0 ); myKDj->getMessage( &message ); if( !message.empty() ) { std::cout << "Received command message #: " << (int) message.at(0) << '\n'; } } // Clean up cleanup: delete myKDj; return 0; }
By default, some messages are ignored in the KDjExtended::doBufferProcessing() process, but this function can be modified to control how these messages are handled.
In the following example, we omit some necessary error checking. For a more complete example, see the hkdjin.cpp program in the tests directory.
// Receiving messages ( high level ) // hkdjin.cpp #include <iostream> #include "KontrolDj.h" class myDerivedKDjClass : public KDjExtended { protected: void onButtonPress( unsigned char button, unsigned char flags, std::vector<unsigned char> *midiMessage ){ std::cout << "Button pressed number #: " << (int)button << '\n'; }; }; bool done; static void finish(int ignore){ done = true; } int main() { myDerivedKDjClass *myKDj = new myDerivedKDjClass(); // Check available devices. unsigned int nDevices = myKDj->getDeviceCount(); if ( nDevices == 0 ) { std::cout << "No KDj devices available!\n"; goto cleanup; } myKDj->openDevice( 0 ); // Install an interrupt handler function. done = false; (void) signal(SIGINT, finish); // Periodically check KDj FIFO buffer. std::cout << "Reading and processing messages from buffer ... quit with Ctrl-C.\n"; while ( !done ) { myKDj->doBufferProcessing(); } // Clean up cleanup: delete myKDj; return 0; }
| OS: | Driver API: | Preprocessor Definition: | Library or Framework: | Example Compiler Statement: |
| Linux | __KDJ_LINUX_API__ | | | |
| Macintosh OS X | IOKit | __KDJ_MACOSX_API__ | IOKit | compiler specific |
| Windows | FTD2XX | __KDJ_WINDOWS_API__ | FTD2XX.lib, FTD2XX.dll | compiler specific |
__KDJ_DEBUG__ to the compiler ( or uncomment the definition at the top of the file KontrolDj.cpp ). A variety of warning messages will be displayed which may help in determining the problem. Also try using the programs included in the test directory. The program kdjinfo displays all KDj devices found.Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
![]() |
Copyright © 2005 by LSD Software. All Rights Reserved. Maintained by LSD, info at kontrol-dj.com |