Binary core API

From ADVACAM Wiki
Jump to navigation Jump to search

Core/basic binary (C) API introduction

The PIXet is a multi-platform software developed in ADVACAM company. It is a basic software that allows measurement control and saving of measured data with Medipix detectors. It supports Medipix2, Medipix3, Timepix and Timepix3 detectors and all the readout-devices sold by ADVACAM company such as FitPIX, AdvaPIX, WidePIX, etc. It is written in C++ language and uses multi-platform Qt libraries.

This document describes a developer interface of the PIXet software. This developer interface consists of dynamic linked library pxcore.dll (Windows) or libpxcore.so (Mac or Linux), the corresponding header file for the library pxcapi.h and few other supporting libraries (fitpix.dll, Visual Studio runtime libraries, etc.)

The core API with the pxcore library, allowing basic measurements and device settings.
Files:

  • pxcapi.h API header file
  • pxcore.dll or pxcore.so binary libraries for Windows or Linux
  • pxcore.lib static linging file for easier using on Windows (compile time only)
  • common.h common file defining basic types, constatns and usefull macros. It's not necessary, but it can be useful.

And need some auxiliary files and directories:

Requirements

Hardware

This API requires computer with x86 compatible architecture (no ARM), 64bit Windows or Linux and connected some Advacam hardware with imaging chip. Medipix3, Timepix, Timepix2, Timepix3, etc. Some functions are universal for all hardwares (pxcInitialize, pxcGetDeviceName, etc), some is specialized for only one chip type (pxcMeasureSingleFrameTpx3 is Timepix3 only).

Specialized functions have names with chip type included:

  • pxcSetTimepixCalibrationEnabled – Timepix only (no Timepix3)
  • pxcMeasureTpx3DataDrivenMode – Timepix3 only
  • pxcMeasureSingleFrameMpx3 – Medipix3 only

The attempt to use the function if compatible hardware (in initialized state) not present, end with error.
Return code is PXCERR_UNEXPECTED_ERROR.

Software

All the API functions have heads in pxcapi.h, implemented for Windows in the pxcore.dll and for linking must use the pxcore.lib in the linker settings. Implementation for Linux is in the libcore.so.
Compiled program need the pixet.ini file with proper hwlibs list inside, necessary hardware dll files (eq minipix.dll for Minipixes), optional special files (eq zestwpx.bit for Widepixes), subdirectory “factory” with default config files for all present imaging devices (eq MiniPIX-I08-W0060.xml) and the Pixet core will create subdirectory “configs” to save changed configs on exit.
See The Pixet core, additional libraries and other files
Usually, for build, just set the compiler to use 64bit and the linker to use the pxcore.lib file.

In Microsoft visual studio, it is also necessary to insert the use of WIN32 definition into the project settings (C/C++ / Preprocessor / Preprocessor definitions):

Visual Studio Project Settings
Visual Studio Project Settings

The Pixet core, additional libraries and other files

Main files:

  • pxcapi.h API header file
  • pxcore.dll or pxcore.so binary libraries for Windows or Linux
  • pxcore.lib static linging file for easier using on Windows (compile time only)

And need some auxiliary files and directories:

Where to get these files?

All need files except hwlibs and configs are located in the zip file with name like us:
PIXet SDK 1.6.4 (Build 765) Win.zip

And all files except LIB files for Windows compilation are located in the Pixet directory:

All files except LIB files for Windows compilation are located in the Pixet directory
All files except LIB files for Windows compilation are located in the Pixet directory

Example of the project directory

Example of the project dirrectory
Example of the project dirrectory

On the right is a screenshot of the Windows CLR APP project directory in Visual Studio that uses Minipix Tpx3. The marked files were copied from the Advacam SDK and the "factory" directory contains the configuration XML file for the device. It is important that the name is complete, eg MiniPIX-I08-W0060.xml. This file will be used on first launch.

The directory "configs" is created when Pixet core is terminated and contains a configuration XML file with saved current settings. This file will be used on each subsequent startup and updated on each subsequent exit.

The "logs" directory is created when Pixet core is started for the first time and contains LOG files from device activity and backups of these files for the last 10 starts.

Contents of the pixet.ini file:

[hwlibs]
minipix.dll

(x64, Myform... and TPx3-1... are from the MS Visual studio project)

Don't forget to set up WIN32 and pxcore.lib in the project settings as described in the parent chapter.

Tip: How to create the Windows CLR APP:
MSDN:create-c-windows-forms-application-in-visual-studio-2017

Example of the minimalistic program directory

Example of the minimalistic project dirrectory
Example of the minimalistic project dirrectory


API Functions

Simple commandline example

Example code

This is simple example of commandline C program, whitch initializes the Pixet core and device, sets it's operation mode, measures single frame, saves the frame to some files and deinitializes the Pixet core with all the connected devices.
(Timepix3 only)

#include "pxcapi.h"

int main() {
    int rc; // return code
    
    printf("Initializing...\n");
    rc = pxcInitialize();
    printf("pxcInitialize: %d (0 is OK)\n", rc);
    
    rc = pxcSetTimepix3Mode(0, PXC_TPX3_OPM_TOATOT); // sets OPM of device with index 0
    printf("pxcSetTimepix3Mode: %d (0 is OK)\n", rc);
    
    // pxcMeasureMultipleFrames(deviceIndex, frameCount, acqTime, triggerSettings);
    rc = pxcMeasureMultipleFrames(0, 3, 1, PXC_TRG_NO);
    printf("pxcMeasureMultipleFrames: %d (0 is OK)\n", rc);
    
    // pxcSaveMeasuredFrame(deviceIndex, frameLastIndex, filename);
    rc = pxcSaveMeasuredFrame(0, 0, "testImg0.png");
    printf("pxcSaveMeasuredFrame 0: %d (0 is OK)\n", rc);
    rc = pxcSaveMeasuredFrame(0, 1, "testImg1.txt");
    printf("pxcSaveMeasuredFrame 1: %d (0 is OK)\n", rc);
    rc = pxcSaveMeasuredFrame(0, 2, "testImg2.pbf");
    printf("pxcSaveMeasuredFrame 2: %d (0 is OK)\n", rc);
    
    rc = pxcExit();
    printf("pxcExit: %d (0 is OK)\n", rc);
}

Note: If You want test it in device other than Timepix3, You can comment lines with pxcSetTimepix3Mode. But then it is not clear what will be measured.

Building using cmake on Windows with Visual Studio installed

Example of CMakeLists.txt file for compiling this using cmake (C++ file is named "minipix1.cpp"):

cmake_minimum_required(VERSION 3.10)
project(minipix1)

# include_directories(${CMAKE_SOURCE_DIR})
# link_directories(${CMAKE_SOURCE_DIR})
add_library(pxcore SHARED IMPORTED)
set_property(TARGET pxcore PROPERTY IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/pxcore.dll")
set_property(TARGET pxcore PROPERTY IMPORTED_IMPLIB  "${CMAKE_SOURCE_DIR}/pxcore.lib")
add_executable(minipix1 minipix1.cpp)
target_link_libraries(minipix1 pxcore)

Example of the Cmake building script:

rmdir /s /q build
mkdir build
cd build
cmake -DCMAKE_GENERATOR_PLATFORM=x64 .. 

msbuild /P:Configuration=Release ALL_BUILD.vcxproj 

cd .. 
copy pxcore.dll build\Release\pxcore.dll
copy minipix.dll build\Release\minipix.dll
copy pixet.ini build\Release\pixet.ini
echo build\Release\minipix1.exe > run.cmd

User can finally run the run.cmd to run the program.

Building on Linux using GCC

Example build.sh:

#!/bin/bash
gcc -o build-out minipix1.cpp -Wno-write-strings -L. -lpxcore -lminipix  -ldl -lm -lc -g

Example run.sh to run the output executable:

#!/bin/bash
LD_LIBRARY_PATH=. ./build-out
# run last compiled example

Auxiliary functions

This chapter describes auxiliary functions. There are need for normal using of the device, but it not measuring.

Start-up and end


pxcInitialize


This function initializes the Pixet software and all connected devices. This function has to be called first before any other function except pxcGetLastError.
Definition
PXCAPI int pxcInitialize(int argc = 0, char const* argv[] = NULL);

Parameters

  • argc – number of program command line arguments (optional parameter)
  • argv – command line program arguments (optional parameter)
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

int rc = pxcInitialize();


pxcExit


This function deinitializes Pixet software and all the connected devices. This function has to be called as last function before unloading the pxcore library.
Definition
PXCAPI int pxcExit();

Parameters

  • {{{1}}}
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

int rc = pxcExit();


pxcRefreshDevices


This function looks for newly connected devices and removed disconnected devices from the device list.
Definition
PXCAPI int pxcRefreshDevices();

Parameters

  • {{{1}}}
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

int rc = pxcRefreshDevices();


pxcReconnectDevice


If the device was disconnected or experienced communication problems, this function will try to reconnect the device and reinitialize it. Like as do the pxcExit and pxcInitialize, but only for one device index. The process is:
  1. Saves the device config to the “configs” directory
  2. Disconnects the device
  3. Connects the device
  4. Loads the device config
Definition
PXCAPI int pxcReconnectDevice(unsigned deviceIndex);

Parameters

  • deviceIndex - index of the device, starting from zero
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

int rc = pxcReconnectDevice(0); // reconnect device with index 0


Parameter Get/Set functions (direct)


Functions described in this chapter working directly, function name defines parameter name and type.

Example setting operation mode


// Set the operating mode
rc = pxcSetTimepix3Mode(deviceIndex, PXC_TPX3_OPM_TOATOT);
printErrors("pxcSetTimepix3Mode", rc);

Example: List of devices with parameters


#include <stdio.h>
#include "pxcapi.h"
#define CHT_Si 0
#define CHT_CdTe 1
char chipType = CHT_Unknown;
int main(int argc, char const* argv[]) { // #######################################
    int rc = pxcInitialize();
    if (rc) {
        printf("Could not initialize Pixet:\n");
        printErrors("pxcInitialize", rc, ENTER_ON);
        return -1;
    }
    int connectedDevicesCount = pxcGetDevicesCount();
    printf("Connected devices: %d\n", connectedDevicesCount);
    if (connectedDevicesCount == 0) return pxcExit();
    for (unsigned devIdx = 0; (signed)devIdx < connectedDevicesCount; devIdx++) {
        char deviceName[256];
        memset(deviceName, 0, 256);
        pxcGetDeviceName(devIdx, deviceName, 256);
        char chipID[256];
        memset(chipID, 0, 256);
        pxcGetDeviceChipID(devIdx, 0, chipID, 256);
        printf("Device %d: Name %s, (first ChipID: %s)\n", devIdx, deviceName, chipID);
    }
    double bias;
    rc = pxcGetBias(devIdx, &bias);
    if (bias < 0.0) {
        if (devIdx == 0) chipType = CHT_CdTe;
        printf("Chip material detected: CdTe\n");
    } else if (bias == 0.0) {
        printf("Chip material not detected!\n");
    } else {
        if (devIdx == 0) chipType = CHT_Si;
        printf("Chip material detected: Si\n");
    }
    printf("=================================================================\n");
    
    // here can be working code (calling some example function from this manual)
    
    return pxcExit();
}

pxcGetLastError


Returns text of last error. This function can be called even before pxcInitialize()
Definition
PXCAPI int pxcGetLastError(char* errorMsgBuffer, unsigned size);

Parameters

  • errorMsgBuffer - buffer where text will be saved
  • size - size of supplied buffer
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

char msg[200];<br> pxcGetLastError(msg, 200);<br> printf("Error msg: %s\n", msg);


Example1 (console)

#define ERRMSG_BUFF_SIZE 512
#define ENTER_ON true
#define ENTER_OFF false
// (the function used in most examples in this manual)
void printErrors(const char* fName, int rc, bool enter) {
   char errorMsg[ERRMSG_BUFF_SIZE];
   pxcGetLastError(errorMsg, ERRMSG_BUFF_SIZE);
   if (errorMsg[0]>0) {
       printf("%s %d err: %s", fName, rc, errorMsg);
   } else {
       printf("%s %d err: ---", fName, rc);
   }
   if (enter) printf("\n");
}

Now you can use it:

rc = pxcSetTimepix3Mode(deviceIndex, PXC_TPX3_OPM_EVENT_ITOT);
printErrors("pxcSetTimepix3Mode", rc, ENTER_ON);

If mode set was suscessfull, result is:

pxcSetTimepix3Mode 0 err: ---

If not, you can see some as:

pxcSetTimepix3Mode -2 err: Invalid device index


Example2 (Windows CLR)

const unsigned cErrBufSize = 512;
// primary use to show function name, return code, last error message
bool errorToList(const char* fName, int rc) {
    char errorMsg[cErrBufSize];
    char cMsg[cErrBufSize];
    String^ eMsg;
    pxcGetLastError(errorMsg, cErrBufSize);
    if (rc!=0) {
        sprintf(cMsg, "%s %d err: %s", fName, rc, errorMsg);
        eMsg = gcnew String(errorMsg);
    } else {
        sprintf(cMsg, "%s %d err: ---", fName, rc);
    };
    String^ sMsg = gcnew String(cMsg);
    listMessages->Items->Add(sMsg);
    return (rc!=0);
}

Now you can use it:

rc = pxcSetTimepix3Mode(deviceIndex, PXC_TPX3_OPM_EVENT_ITOT);
if (printErrorToList("pxcSetTimepix3Mode", rc)) return rc;

pxcGetDevicesCount


This function returns number of connected and initialized devices.
Definition
PXCAPI int pxcGetDevicesCount();

Parameters

  • {{{1}}}
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
Number of devices, otherwise the return value is a PXCERR_XXX code

Note

{{{Not}}}

Warning

{{{War}}}

Example

printf("Connected devices count: %d\n", pxcGetDevicesCount());


pxcGetDeviceName


This function returns the full name of the selected device.
Definition
PXCAPI int pxcGetDeviceName((unsigned deviceIndex, char* nameBuffer, unsigned size);

Parameters

  • deviceIndex - index of the device, starting from zero
  • nameBuffer - buffer where the name of the device will be saved. Cannot be NULL
  • size - size of the supplied name buffer
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

char name[100];<br> pxcGetDeviceName(deviceIndex, name, 100);<br> printf("Dev %d name: %s\n", deviceIndex, msg);


pxcGetDeviceChipCount


This function returns number of chips in the device.
Definition
PXCAPI int pxcGetDeviceChipCount(unsigned deviceIndex);

Parameters

  • deviceIndex - index of the device, starting from zero
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
Number of chips if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

printf("Dev %d has chip count: %d\n", deviceIndex, pxcGetDeviceChipCount(deviceIndex));


pxcGetDeviceChipID


This function returns the ID of chip of the detector connected to the readout device.
Definition
PXCAPI int pxcGetDeviceChipID((unsigned deviceIndex, unsigned chipIndex, char* chipIDBuffer, unsigned size);

Parameters

  • deviceIndex - index of the device, starting from zero
  • chipIndex – index of the chip in the device, starting from zero
  • chipIDBuffer - buffer where the chipID of the detector will be saved. Cannot be NULL
  • size - size of the supplied chipID buffer
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

char id[50];<br> pxcGetDeviceChipID(deviceIndex, 0, id, 50);<br> printf("Chip 0 of dev %d have ID: %s\n", deviceIndex, msg);


pxcGetDeviceSerial


This function returns the devise serial number.
Definition
PXCAPI int pxcGetDeviceSerial(unsigned deviceIndex);

Parameters

  • deviceIndex - index of the device, starting from zero
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
Serial number (>0) if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

printf("Dev %d has serial number: %d\n", deviceIndex, pxcGetDeviceSerial(deviceIndex));


pxcGetDeviceDimmensions


This function gets pixel width and height of the device.
Definition
PXCAPI int pxcGetDeviceDimmensions(unsigned deviceIndex, unsigned *w, unsigned *h);

Parameters

  • deviceIndex - index of the device, starting from zero
  • w – pointer to unsigned variable where the width of the device will be returned
  • h – pointer to unsigned variable where the height of the device will be returned
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}

Example

unsigned w, h;<br>
int rc = pxcGetDeviceDimmensions(deviceIndex, &w, &h);<br>
if (rc==0) printf("Width: %d, Height %d [px]\n", w, h);<br>
else printf("pxcGetDeviceDimmensions failed, code %d\n", rc);



Definition
PXCAPI int ();

Parameters

(no pars)
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}



Definition
PXCAPI int ();

Parameters

(no pars)
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}



Definition
PXCAPI int ();

Parameters

(no pars)
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}



Definition
PXCAPI int ();

Parameters

(no pars)
  • {{{2}}}
  • {{{3}}}
  • {{{4}}}
  • {{{5}}}
  • {{{6}}}
  • {{{7}}}
  • {{{8}}}
Return value
0 if successful, otherwise the return value is a PXCERR_XXX code.

Note

{{{Not}}}

Warning

{{{War}}}