Calling API Functions

All LayGO API function names and argument sequences are identical to those documented in the C language API. For instance, the code to open a CID which might be written in C as:

#include <laygo.h>

LCid    cid;

cid = lgo_Open("PHYS0");

if (cid < 0)

translates directly into the following Tcl code:

package require Laygo

set cid [lgo_Open "PHYS0"]

if {$cid < 0} {
    PostOpenError $cid
} else {
    ConnectCid $cid

For functions which take C pointer types such as LDataBuffer or LCtlBuffer, substitute a buffer allocated with the LayGO memory allocation API. The C code to read data:

unsigned char buffer[1024];
LResult       result;

result = lgo_Read(cid, buffer, 1024);

if (result > 0)
    ProcessData(buffer, result);
else if (result == lgo_ERROR_EVENT_WAITING)

becomes the Tcl code:

set buffer [lgo_BufferNew 1024]
set result [lgo_Read $cid $buffer 1024]

if ($result > 0) {
    ProcessData $buffer $result
} elseif ($result == $Laygo::ERROR_EVENT_WAITING) {

Where a NULL pointer would be used in C, substitute the Tcl string NULL:

LResult    result;

result = lgo_ConnectRequest(cid, NULL, 0);


set result [lgo_ConnectRequest $cid NULL 0]

Finally, where a pointer to an integral type would be used in C to return a value, the Tcl function returns a list of values:

unsigned char buffer[128];
LBufferSize   size;
LResult       result;

size   = sizeof(buffer);
result = lgo_Event(cid, buffer, &size);

becomes in Tcl:

set size   128
set buffer [lgo_BufferNew $size]
set event  [lgo_Event $cid $buffer $size]
set result [lindex $event 0]
set size   [lindex $event 1]