thinkgear_api_macosx_example.1404245563.txt.gz · Last modified: 2014/07/01 13:12 by neurosky

This is an old revision of the document!

ThinkGear API Mac OS X example

NOTE: There has been an update to thinkgear_testapp.c under the \ThinkGear Communications Driver\win32 directory. Included is the alternate file: thinkgear_testraw.c

ThinkGear Communications Driver is deprecated as of Developer Tools 2.5.

 * This program serves as a simple example of how one can use ThinkGear.bundle inside their Core Foundation
 * (e.g. Cocoa and Carbon-based) apps. For more details on OS X bundles, read:
 * Or check the "How to use the ThinkGear API in Xcode (Mac OS X)" document in the ThinkGear documentation.
 * Note: When executing the program, make sure ThinkGear.bundle is in the same current directory.
#include <CoreFoundation/CoreFoundation.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
 * Baud rate for use with TG_Connect() and TG_SetBaudrate().
#define TG_BAUD_1200         1200
#define TG_BAUD_2400         2400
#define TG_BAUD_4800         4800
#define TG_BAUD_9600         9600
#define TG_BAUD_57600       57600
#define TG_BAUD_115200     115200
 * Data format for use with TG_Connect() and TG_SetDataFormat().
#define TG_STREAM_PACKETS      0
#define TG_STREAM_5VRAW        1
 * Data type that can be requested from TG_GetValue().
#define TG_DATA_BATTERY      0
#define TG_DATA_ATTENTION    2
#define TG_DATA_RAW          4
#define TG_DATA_DELTA        5
#define TG_DATA_THETA        6
#define TG_DATA_ALPHA1       7
#define TG_DATA_ALPHA2       8
#define TG_DATA_BETA1        9
#define TG_DATA_BETA2       10
#define TG_DATA_GAMMA1      11
#define TG_DATA_GAMMA2      12
CFURLRef bundleURL;           // path reference to bundle
CFBundleRef thinkGearBundle;  // bundle reference
int connectionID = -1;        // ThinkGear connection handle
 * ThinkGear function pointers
int (*TG_GetDriverVersion)() = NULL;
int (*TG_GetNewConnectionId)() = NULL;
int (*TG_Connect)(int, const char *, int, int) = NULL;
int (*TG_ReadPackets)(int, int) = NULL;
float (*TG_GetValue)(int, int) = NULL;
int (*TG_Disconnect)(int) = NULL;
void (*TG_FreeConnection)(int) = NULL;
 * This function handles signal interrupts. 
 * Basically perform cleanup on the objects and then exit the program.
void siginthandler(int sig){
   fprintf(stderr, "\nDisconnecting...\n");
   // close the connection
   if(connectionID != -1){
   // release the bundle references
 * The main driver for this program.
 * Handle command-line arguments, initialize the ThinkGear connection,
 * and handle output.
int main (int argc, const char * argv[]) {
   // register the signal interrupt handler
   signal(SIGINT, siginthandler);
   // cmd line argument checking
   if(argc < 2){
      fprintf(stderr, "Usage: %s portname\n", argv[0]);
   const char * portname = argv[1];       // port name
   int retVal = -1;                       // return values from TG functions
   int numPackets = 0;                    // number of packets returned from ReadPackets
   float signalQuality = 0.0;             // poor signal status
   float attention = 0.0;                 // eSense attention
   float meditation = 0.0;                // eSense meditation
   // create the path reference to the bundle
   bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
   // create the bundle reference
   thinkGearBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
   // make sure the bundle actually exists
      fprintf(stderr, "Error: Could not find ThinkGear.bundle. Does it exist in the current directory?\n");
   // now start setting the function pointers
   TG_GetDriverVersion =   (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_GetDriverVersion"));
   TG_GetNewConnectionId = (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_GetNewConnectionId"));
   TG_Connect =            (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_Connect"));
   TG_ReadPackets =        (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_ReadPackets"));
   TG_GetValue =           (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_GetValue"));
   TG_Disconnect =         (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_Disconnect"));
   TG_FreeConnection =     (void *)CFBundleGetFunctionPointerForName(thinkGearBundle, CFSTR("TG_FreeConnection"));
   // check for any invalid function pointers
   if(!TG_GetDriverVersion || !TG_GetNewConnectionId || !TG_Connect || !TG_ReadPackets ||
      !TG_GetValue || !TG_Disconnect || !TG_FreeConnection){
      fprintf(stderr, "Error: Expected functions in ThinkGear.bundle were not found. Are you using the right version?\n");
   // get the connection ID
   connectionID = TG_GetNewConnectionId();
   fprintf(stderr, "Connecting to %s ... ", portname);
   // attempt to connect
   retVal = TG_Connect(connectionID, portname, TG_BAUD_9600, TG_STREAM_PACKETS);
   // check whether the connection attempt was successful
      fprintf(stderr, "connected.\n"); 
      // loop until we get the interrupt signal from the console. control
      // then gets passed onto the signal handler function
         // sleep for half a second
         // read the packets from the output stream
         numPackets = TG_ReadPackets(connectionID, -1);
         // check whether we've received any new packets
         if(numPackets > 0){
            // if so, parse them
            signalQuality = TG_GetValue(connectionID, TG_DATA_POOR_SIGNAL);
            attention = TG_GetValue(connectionID, TG_DATA_ATTENTION);
            meditation = TG_GetValue(connectionID, TG_DATA_MEDITATION);
            // then output everything
            fprintf(stdout, "\rPoorSig: %3.0f, Att: %3.0f, Med: %3.0f", signalQuality, attention, meditation);
   else {
      fprintf(stderr, "unable to connect. (%d)\n", retVal);
   return 0;

About NeuroSky

NeuroSky technologies enable the most insightful and easy-to-understand health and wellness biometrics for mobile solutions, wearable devices, and service providers. The company’s proprietary, market-leading biosensor technologies provide a foundation for analyzing biometric data in a way that’s never before been practical. NeuroSky-enabled solutions deliver unique insights into body and mind health and wellness that can motivate people to make better lifestyle choices.