HOWTO: Get Phidgets working with FreeBSD

HOWTO: Get Phidgets working with FreeBSD
What are "Phidgets"?
Phidgets are a set of user friendly building blocks for low cost USB sensing and control from your PC.
1019_1_Home.jpg


Purpose
The purpose of this HOWTO is to demonstrate how to setup phidgets to work with FreeBSD. The target audience for this article is embedded developers who are looking to do / are currently doing hardware/software projects. (E.x. Drones, Robots, Single Application Computer, etc) In essence, if you are thinking of trying something with Rasberry Pi, Aurdino, ARM, small PC, etc then this article is for you.

Assumptions:
  • You possess a Phidget device.
  • You know how to use 'vi'.
  • You are a beginner.
  • You have a FreeBSD computer and a Mac (or UNIX-like) computer.
  • You are trying this out on a FreeBSD i386 or amd64 (I haven't tried with ARM so I cannot confirm!)

Steps:
On your FreeBSD Computer
(Note: Your Robot Computer)
Go to ports and install "libusb" driver
# cd /usr/ports/devel/libusb
# make install clean

On your Apple Macintosh Computer
i) Go to http://www.phidgets.com/docs/OS_-_Linux
ii) Download "Phidget Libraries" tar and unzip in your ~/Downloads folder.
iii) SCP the library to your machine.
# scp ~/Downloads/libphidget_2.1.8.20130320.tar [email=admin@192.168.0.14]admin@192.168.0.14[/email]:/tmp

Note:
admin = is the username I used to access the server
192.168.0.14 = is the server IP address of your FreeBSD computer

On your FreeBSD Computer
Make yourself root
sudo
<Note: Insert your password >

Then we'll traverse to the directory where we uploaded the tarball and install the contents.
# cd /tmp
# tar -zxvf libphidget_2.1.8.20130320.tar

Note:
z -- unzip
x -- extract the file
v -- verbose
f -- forcefully done

Alright! Let's go into the Phidget Library (which is written in C) and install it!
# cd /tmp/libphidget-2.1.8.20130320
# ./configure
# make
# make install

4) Confirm everything is working.
i) Create a file called "HelloWorld.c"
mkdir /tmp/test
cd /tmp/test
touch /tmp/HelloWorld.c

ii) Populate the "HelloWorld.c"
# vi /tmp/HelloWorld.c"

with the following contents:
Code:
/* 
 * Phidget Hello World Program for all devices
 * (c) Phidgets 2012
 */

#include <stdio.h>
#include <phidget21.h>

// -------------------- Event Functions ---------------------------------------

int CCONV AttachHandler (CPhidgetHandle device, void *userptr) {

    int serialNumber;
    const char *name;
    
    LocalErrorCatcher(
        CPhidget_getDeviceName(device, &name));
    LocalErrorCatcher(
        CPhidget_getSerialNumber(device, &serialNumber));
    
    printf("Hello Device %s, Serial Number: %d\n", name, serialNumber);
    
    return 0;
}

int CCONV DetachHandler (CPhidgetHandle device, void *userptr) {

    int serialNumber;
    const char *name;
    
    LocalErrorCatcher(
        CPhidget_getDeviceName(device, &name));
    LocalErrorCatcher(
        CPhidget_getSerialNumber(device, &serialNumber));
    
    printf("Goodbye Device %s, Serial Number: %d\n", name, serialNumber);
    
    return 0;
}

// When using an error handler with the manager, it takes a
// CPhidgetManagerHandle, when using an individual object, the
// object serves as its own handle.
int CCONV LibraryErrorHandler (CPhidgetManagerHandle device, void *usrptr, 
                    int errorCode, const char *errorDescription) {
    printf("Error Event: %d - %s\n", errorCode, errorDescription);
    return 0;
}

// This error handler can handle any CPhidget function that returns an int
int LocalErrorCatcher (int errorCode) {
	
	const char *errorDescription;
    
	// If the error code is 0, everything is okay
    if (errorCode != 0) {
    
        // Otherwise, you can print specific messages or perform actions by error value.
        switch (errorCode) {
           default:
               printf("Error: An error occurred with code %d.\n", errorCode);
               
               LocalErrorCatcher(
                   CPhidget_getErrorDescription (errorCode, &errorDescription));
               printf("The description for this error is: %s\n", errorDescription);
               break;
       }
    }
	return 0;
}

// -------------------- Main Code ---------------------------------------------

int main(int argc, char* argv[]) {

    int result; 
    const char *err;

    CPhidgetManagerHandle device = 0;
    LocalErrorCatcher(
        CPhidgetManager_create(&device));
    
    LocalErrorCatcher(
        CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle) device, 
                                         AttachHandler, NULL));
    
    LocalErrorCatcher(
        CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle ) device,
                                         DetachHandler, NULL));
    
    LocalErrorCatcher(
        CPhidgetManager_set_OnError_Handler((CPhidgetManagerHandle) device,
                                        LibraryErrorHandler, NULL));
    printf("Opening...\n");
    // Most opening and closing would be via a cast to
    // (CPhidgetHandle), however, this manager has its
    // own handle struct to cast to.
    LocalErrorCatcher(
        CPhidgetManager_open((CPhidgetManagerHandle) device));
    
    printf("Phidget Simple Playground (plug and unplug devices)\n");
    printf("Press Enter to end anytime...\n");
    getchar();
    
    printf("Closing...\n");
    LocalErrorCatcher(
        CPhidgetManager_close((CPhidgetManagerHandle) device));
    LocalErrorCatcher(
        CPhidgetManager_delete((CPhidgetManagerHandle) device));

    return 0;
}
Note: I did not write this file, it was written by Phidgets and not myself!

iii) Run:
# gcc HelloWorld.c -o HelloWorld -lphidget21
# ./HelloWorld

Note: The application will run and ask you to plug into the FreeBSD computer a single Phidget device. Do this, if everythign works, the application will print the device name that is connected and exit - if this happens then everything is working! Congratulations! :beergrin

5) Trouble?
http://www.phidgets.com/docs/OS_-_Linux

6) Next?
  • If you want to create an application in a different language (C++, Java, Objective-C, etc, etc) for any of the devices. Just look at this link where all examples & various languages exist.
  • If you're looking to build some-sort of hardware server, like for example a weather station with FreeBSD, now you can. The key idea is that whatever you can do on Linux, you can do on FreeBSD as well.
 
Back
Top