Building libfreenect with patched libusb under Mac OS X

You need:

  • macports
  • The following installed macports:
    • git-core
    • cmake
    • libtool
    • libusb-devel
1. Get sources:
  • libfreenect:
    • git clone https://github.com/OpenKinect/libfreenect.git
  • libusb:
    • git clone git://git.libusb.org/libusb.git
2. Apply patch and build libusb:
  1. cd path/to/libusb
  2. ./autogen.sh
  3. patch -p1 < path/to/libfreenect/platform/osx/libusb-osx-kinect.diff
  4. ./configure LDFLAGS=’-framework IOKit -framework CoreFoundation’
  5. make
  6. sudo make install
3. Start building libfreenect:
  1. cd path/to/libfreenect
  2. mkdir build
  3. cd build
  4. ccmake ..
      With the following changes:

    • LIBUSB_1_INCLUDE_DIR: /usr/local/include
    • LIBUSB_1_LIBRARY: /usr/local/lib/libusb-1.0.dylib
  5. press ‘c’ and afterwards ‘g’ to generate and exit
  6. cmake ..
  7. make
  8. sudo make install

Lisp: Emacs Autocomplete

December 29, 2010 4 comments

Today I hacked some lisp in emacs because I got two new books (Let over Lambda & Land of Lisp).
After a while when the function-definitions became more frequent I missed a language-specific autocomplete (and didn’t only want to use the built-in autocomplete that completes words you wrote earlier) and after some research I found the solution: Emacs Autocomplete. It integrates different language-dictionaries and inserts new words on the fly (with the right settings).

After installing the autocomplete into .emacs.d I inserted the following lines into my .emacs-file to enable it:

;; add .emacs.d to load path
(add-to-list 'load-path "~/.emacs.d")

;; enable autocomplete
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(global-auto-complete-mode t)
(auto-complete-mode t)

To get a lisp-specific autocomplete you’ve got to include ac-slime (found at github): put it into your .emacs.d-folder and include it in your .emacs-file via

;; add lisp autocomplete-support
(require 'ac-slime)
(add-hook 'slime-mode-hook 'set-up-slime-ac)

and to switch on autocomplete globally you have to apply a “dirty fix”:

;; dirty fix for having AC everywhere
(define-globalized-minor-mode real-global-auto-complete-mode
  auto-complete-mode (lambda ()
                       (if (not (minibufferp (current-buffer)))
                         (auto-complete-mode 1))))
(real-global-auto-complete-mode t)

Now only one thing is missing: the mentioned inserting of new words on the fly:

;; create and add new words to the dictionary on the fly
(when (require 'auto-complete-config nil 'noerror)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
  (setq ac-comphist-file  "~/.emacs.d/ac-comphist.dat")
  (ac-config-default))

Emacs for Mac OS X

October 30, 2010 Leave a comment

When switching from aquamacs to emacs you have to make some adjustments to use it properly under Mac OS X.
One of the adjustments is that you could not simply type in “alt-shift-7” to get the normal backslash “\”.
The solution:
Go to “Emacs” – “Preferences” – “Environment” – “Ns : GNUstep/Mac OS X specific features” and set

  • the “Ns Command Modifier” -> “meta” and
  • the “Ns Alternate Modifier” -> “No modifier“.

Now you can use your alt and meta-keys as usual.

Second I did not remember immediately how to set my previous colors and styles (e.g. the background color or the mouse color) so I will sum up my solution:

  • open your “~/.emacs
  • type in (and replace my colors with your own)
(setq default-frame-alist
    (append default-frame-alist
        '((foreground-color . "LawnGreen")
          (background-color . "Black")
          (cursor-color . "LawnGreen"))))

And of course Emacs is much better than vim!

C++: Qt and SQL

February 18, 2010 Leave a comment

Today I want to write something about Qt and its SQL-support. It is quite easy to use it, much more simple than working directly with ODBC and/or SQLite or other libraries because Qt has “build-in” support, you just have to add QT += sql to your .pro-file and specify which library you want to work with.

But let’s start now. I want to demonstrate how easy it is to create a simple timetable with SQLite that looks something like this if you wrap it into a GUI:

Here are the includes:

#include <QTableView>
#include <QSqlDriver>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
// #include <QSqlError>

At first, create the database using SQLite via

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("databaseName");

Then open it (and check it meanwhile) with

if(!db.open()) {
   exit(0); // or do something else
}

Now insert the table using a SQL query:

QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS tableName ("
           "monday varchar(200), "
           "tuesday varchar(200), "
           "wednesday varchar(200), "
           "thursday varchar(200), "
           "friday varchar(200), "
           "saturday varchar(200), "
           "sunday varchar(200))");

After that create a model:

QSqlTableModel model;
model.setTable("tableName");
model.setEditStrategy(QSqlTableModel::OnManualSubmit);

If you choose “OnManualSubmit” as edit strategy you have to save the changes you make inside the model by your own (explanation comes later).
Then set the model header data (Monday-Sunday in the picture):

model.setHeaderData(0, Qt::Horizontal, "Monday");
model.setHeaderData(1, Qt::Horizontal, "Tuesday");
model.setHeaderData(2, Qt::Horizontal, "Wednesday");
model.setHeaderData(3, Qt::Horizontal, "Thursday");
model.setHeaderData(4, Qt::Horizontal, "Friday");
model.setHeaderData(5, Qt::Horizontal, "Saturday");
model.setHeaderData(6, Qt::Horizontal, "Sunday");

and set the model to the view to display it:

model.select();
QTableView view;
view.setModel(model);

But what if you want to add an additional line with an entry in every row? The best news is that you don’t have to write any SQL for adding or deleting if you don’t want to:

int rowCnt = model.rowCount();
model.insertRow(rowCnt);
for(int i = 0; i < model.columnCount(); i++) {
   model.setData(model.index(rowCnt, i), "Put this Entry in every Row");
}
model.submitAll();
model.select();
view.setModel(model);

And if you want to delete the last row:

model.removeRow(model.rowCount() - 1);
model.submitAll();
model.select();
view.setModel(model);

You have to call “submitAll” because of the selected strategy ”OnManualSubmit” that doesn’t save by it’s own.
To clear the whole database, drop the table by querying

query.exec("DROP TABLE IF EXISTS tableName");

After setting up a model and a view you can change your table inside the view, press “save” and everything else is “done by Qt”!

You can find the whole Application at CallToPower – Software – qTimeTable, have fun experimenting with it!

Java, C++: ClassLoader and Qt Resource System

December 8, 2009 Leave a comment

Here a little comparison between the Java ClassLoader and the Qt Resource System:
If you want to load an icon and/or an image e.g. in a Java .jar-file you have to do it this way:

Icon icon;
Image image;
ClassLoader CLDR = this.getClass().getClassLoader();
String path = "src/to/images/name.png";
URL url = CLDR.getResource(path);
// An icon
if (!(url == null)) {
    icon = new ImageIcon(url);
}
// An image
try {
    image = ImageIO.read(CLDR.getResource(path));
} catch (IOException e) {
    // ExceptionHandling here
}

In Qt there is another approach:
The resources associated with an application are specified in a XML-based .qrc-file.
The specified paths are relative to the directory containing the .qrc-file (listed files must be located in the same directory or any sub-directory).
In the source-code the resources are accessible under the same name they have in the source tree, with a “:/” prefix.
Here an example:

<RCC>
    <qresource prefix="/dir/subDir" lang="en">
        <file alias="AliasName">subDir/FileName<file>
    <qresource>
</RCC>

In your source code you can access FileName (if FileName e.g. is a QFile) via

QFile file(":/dir/subdir/AliasName");

after adding the following line to your .pro-file:

RESOURCES = yourRsrcFileName.qrc

The option lang=”langCode” (here: “en”) is for the localization:
If you want to add another language to your application you have to have the file “AliasName_langCode” in your source tree.

And, of course, you don’t have to write the ressource file on your own if you use the QtCreator, there is a nice GUI included.

Mac OS X – “Automator”: Combine PDF files

October 10, 2009 4 comments

Today I had to find a program that combines/merges 2 or more PDF-files.
Because I didn’t want to use Adobe Acrobat (Pro?) I turned my internet search on and found an interesting program: The Mac OS X Automator.
Without reading any tutorial or something different (Automator is really self-explanatory, it’s like drag’n'drop everything!) I created a good-working “Automation”/Program that performs that task.
Here’s the workflow:

  1. Ask for finder-items
  2. Combine PDF pages
  3. Move Finder Items

Combine_PDF_files

- Update 11.10.2009 -
Created a sub-site named Automator-workflows at www.calltopower.de/automatorworkflows where you can see screenshots of the workflows and/or download the workflows and/or programs. I created among other things “Save Pictures from the current website”, “Take screenshot”, “Take image from webcam (iSight or other)”, “Pictures to PDF”, “Images to PDF”.

C: KaR – Exercise 1-14 – Horizontal histogram of char frequencies

October 4, 2009 2 comments

It’s time for another exercise from “Kernighan and Ritchie – The C Programming Language” – Exercise 1-14:

Write a program to print a histogram of the
frequencies of different characters in its input

The 2 main routines are:
1. Read in the characters and count up the position in an array

    /* Get a character */
    while((c = fgetc(text_file)) != EOF) {
        /* If c is a character */
        if(isalpha(c)) {
            /* Count up the array at chars position */
            char_counter[(int)(toascii(tolower(c))-toascii('a'))]++;
            /* Count up counter for total characters */
            chars_total++;
        }
    }

2. Print the horizontal histogram

    /* Print the horizontal histogram */
    printf("\n\tChar\tTimes\n");
    for(i = 0; i < ALPHABET_LENGTH; i++) {
        /* Print header */
        printf("\t%c\t%d\t", (i+'a'), char_counter[i]);
        for(j = 0; j < char_counter[i]; j++) {
            /* If diagram print limit not exceeded */
            if(j < diagramm_print_length) {
                printf("-");
            } else {
                /* Print a '+' and the number of chars >
                  diagram print limit and break the loop */
                printf(" + %d", (char_counter[i] - j));
                break;
            }
        }
        printf("\n");
    }
    printf("\t------------------------");
    printf("\n\tTotal chars: %d\n", chars_total);

Here the full program to download.

And if the program analyses itself you will get the following output:

SICK Robot Day 2009

October 3, 2009 3 comments

Today’s the SICK Robot Day 2009 (Link1 | Link2).
I worked on it for the University of Osnabrueck.
Our Robot named KURT avoids collisions, respects the rule “left yields right”, is able to detect numbers as well as markers and drives to the right numbers from 0-9 and/or vice versa.
So…good luck to everyone in Waldkirch.
Here some photos of Kurt2 at our self-made parcours:
kurt2_2 kurt2_1

And some Videos:

Java: Implementation of the Unix ls-Command (“List”)

September 28, 2009 1 comment

Here you can find my Interpretation of the List-Command ls (at Windows: dir) with the Options

  • -r (Recursion)
  • -s (Print Size)
  • -h (Print hidden Files)

or combined implemented in Java.
The main Routine who does the Listing-Work is

/**
 * Prints the Content of the Directory
 *
 * @param dir
 *            Directory
 * @param recur
 *            Recursion
 * @param size
 *            SizeRecursion
 * @param hidden
 *            Show hidden Files
 */
private static void list(File dir, boolean recur, boolean size,
        boolean hidden) {
    if ((dir != null) && dir.exists() && dir.isDirectory()) {
        File[] filelist = dir.listFiles();

        printSpaces(lvl - lvlStep);
        System.out.println(dir.getName() + ":");
        for (int index = 0; index < filelist.length; index++) {
            File file = filelist[index];
            // Print hidden Files
            if (hidden || !file.isHidden()) {
                // If Directory Recursion
                if (file.isDirectory() && recur) {
                    lvl += lvlStep;
                    list(file, recur, size, hidden);
                    lvl -= lvlStep;
                    // File Output
                } else {
                    String name = file.getName();
                    printSpaces(lvl);
                    // Print Size
                    if (size && !file.isDirectory()) {
                        long length = file.length();
                        System.out.printf("%-" + sizeLength + "dKB %-"
                                + nameLength + "s", length, name);
                    } else {
                        System.out.printf("%-" + nameLength + "s ", name);
                    }
                    System.out.println();
                }
            }
        }
    } else {
        System.err.println("Error: " + dir + " is not a Directory!");
    }
}

And some Screenshots of Directory-Listings:

Java: Text to Speech on Mac OS X

September 23, 2009 3 comments

Tonight I wanted to do something different: Text to Speech on my Mac – via Java.
So here the Main Routine to do that Task:

Runtime.getRuntime().exec(new String[] { "say", "-v", "YourVoice", "Text" });

(YourVoice is a Mac OS X-supported Voice, e.g. “Zarvox”)

You can see the full Class here.
For testing it without embedding the Class compile it

javac TextToSpeech.java

and run it with one of the Voices

java TextToSpeech Hi dude, whats up? Zarvox

or without (=random Voice)

java TextToSpeech Hi dude, whats up?

To see it live in Action have a Look at my jPortscanner.

Categories: Programming Tags: , , , , , , ,
Follow

Get every new post delivered to your Inbox.