program USBcmdTest2;
{$mode objfpc}{$H+}
uses crt;
// -------------------------------------------
// C++ source code, example progs and .DLL for USB_IO_HID-stick downloaded from
// http://pan.baidu.com/s/1sjI4r5F?qq-pf-to=pcqq.c2c
//
// More info:
// - C++ <--> Pascal comparison: http://www.pp4s.co.uk/main/tu-c-table.html
// - C++ tutorial: http://www.cplusplus.com/doc/tutorial/pointers/
// - How to use usb_io_interface library:
// - https://github.com/pavel-a/usb-digital-io16-hid/blob/master/doc/Readme_usb_io_interface_lib.md
//
// -------------------------------------------
// usb_io_interface.dll contains:
//
// usb_io_init()
// Call this before calling other library functions
//
// usb_io_get_device_list()
// Enumerates all digital i/o devices plugged into the PC. Returns pointer to
// linked list of usb_io_device_info structures.
// Caller should free this list after finishing using the devices, by passing it
// to usb_io_free_device_list.
//
// usb_io_free_device_list(usb_io_list);
// - frees the list returned by usb_io_get_device_list
//
// usb_io_open_device(usb_io_list)
// - opens a digital i/o device
//
// usb_io_get_all_pin_info(dev, &pi[0])
// - returns array of pin_info structures for each pin
//
// usb_io_read_input_pin_value(dev, pin_IN, &inp)
// - read state of an input pin
//
// usb_io_set_pin_mode(dev, pin_IN, INPUT_MODE, ipm);
// - configure one I/O pin as input or output
//
// usb_io_write_output_pin_value(dev, pin_OUT, LOW_LVL)
// set state of an output pin
//
// usb_io_close_device(hnd)
// - closes the device handle opened by usb_io_open_device
//
// usb_io_set_work_led_mode(dev, WORK_LED_BLINK)
// turns the green "work" LED on or off
//
// usb_io_uninit()
// Finalizes the library
// -----------------------------------------------------------------------------
const INPUT_MODE = 0; OUTPUT_MODE = 1;
LOW_LEVEL = 0; HIGHT_LEVEL = 1; // HIGHT [sic] !
NO_INNNER_PULL_UP = 0; INNER_PULL_UP = 1;
CLOSE_WORK_LED = 0; OPEN_WORK_LED = 1;
USBIODLL = 'usb_io_interface.dll'; // .DLL supplied by device seller/manuf.
type pin_infoT = record // struct pin_info
pinIndex : word; // { unsigned pinIndex;
pinmode : word; // unsigned pinMode;
pinvalue : word; // unsigned pinValue; };
end;
pin_info_arrayT = array[0..16] of pin_infoT;
ppin_arrayT = ^pin_info_arrayT;
// /*usb io board infomation structure */
pUSB_io_device_infoT = ^USB_io_device_info;
USB_io_device_info = record // struct usb_io_device_info
serial_number : pChar; // { unsigned char *serial_number;
device_path : pChar; // char *device_path;
next : pUSB_io_device_infoT; // usb_io_device_info* next; };
end;
pin_modeT = INPUT_MODE..OUTPUT_MODE;
pin_levelT = LOW_LEVEL..HIGHT_LEVEL;
input_pin_modeT = NO_INNNER_PULL_UP..INNER_PULL_UP;
work_led_modeT = CLOSE_WORK_LED..OPEN_WORK_LED;
var pin_mode : pin_modeT; // enum pin_mode {INPUT_MODE = 0, OUTPUT_MODE = 1};
pin_level : pin_levelT; // enum pin_level {LOW_LEVEL = 0, HIGHT_LEVEL = 1};
input_pin_mode: input_pin_modeT; // enum input_pin_mode {NO_INNNER_PULL_UP = 0,INNER_PULL_UP = 1};
work_led_mode : work_led_modeT; // enum work_led_mode {CLOSE_WORK_LED = 0,OPEN_WORK_LED = 1};
errcode, devs, hand1, hand2, pinnr : integer;
pDev, pDev1, pDev2 : pUSB_io_device_infoT;
level : word;
//
// ============================.DLL functions ==================================
//
{/*init the USB IO Libary
@returns: This function returns 0 on success and -1 on error. */
int EXPORT_API usb_io_init(void); }
function usb_io_init():integer;
stdcall; external USBIODLL name 'usb_io_init';
{/*Finalize the USB IO Libary.
This function frees all of the static data associated with USB IO Libary.
It should be called at the end of execution to avoid memory leaks.
@returns:This function returns 0 on success and -1 on error. */}
//int EXPORT_API usb_io_uninit(void);
function usb_io_uninit():integer;
stdcall; external USBIODLL name 'usb_io_uninit';
{/*Enumerate the USB IO Devices.
@returns: This function returns a pointer to a linked list of type
struct #usb_io_device_info, containing information about the usb io devices
attached to the system, or NULL in the case of failure.
Free this linked list by calling free_usb_io_device_list(). */}
//struct usb_io_device_info EXPORT_API * usb_io_get_device_list(void); }
function usb_io_get_device_list():pusb_io_device_infoT;
stdcall; external USBIODLL name 'usb_io_get_device_list';
{/*This function frees a linked list created by get_usb_io_device_list().*/ }
//void EXPORT_API usb_io_free_device_list(struct usb_io_device_info*);
procedure usb_io_free_device_list(pDev:pusb_io_device_infoT);
stdcall; external USBIODLL name 'usb_io_free_device_list';
{/*open a usb io device
@return: This funcation returns a valid handle to the device on success or 0 on failure. */}
//int EXPORT_API usb_io_open_device(struct usb_io_device_info *device_info);
function usb_io_open_device(pDev:pusb_io_device_infoT):integer;
stdcall; external USBIODLL name 'usb_io_open_device';
{/*close a usb io device opend by open_usb_io_device() */ }
//void EXPORT_API usb_io_close_device(int hHandle);
procedure usb_io_close_device(hhand:integer);
stdcall; external USBIODLL name 'usb_io_close_device';
{/*open or close the green indicator light
@param:
hHandle: the return value of open_usb_io_device();
led_mode: open or close
@returns: returns 0 on success and others on error.
@description: when opened the work led, the green led will flashing when works. */}
//int EXPORT_API usb_io_set_work_led_mode(int hHandle, work_led_mode led_mode);
function usb_io_set_work_led_mode(hHandle:integer; led_mode:work_led_modeT):integer;
stdcall; external USBIODLL name 'usb_io_set_work_led_mode';
{/* set the work mode of io pin
@param:
hHandle: the return value of open_usb_io_device();
pinIndex: pin number of usb io. valid number is 0 to 15
mode: input mode or output mode
innerPullUp: this param will be used when the pinIndex was set INPUT_MODE.
When set param mode to INPUT_MODE and set innerPullUp to INNER_PULL_UP,
the value (read out use read_usb_io_input_pin_value() ) of pinIndex pin equal 1
if no input on pinIndex pin.
We advice you to set the param innerPullUp to INNER_PULL_UP when param mode is INPUT_MODE.
@returns: return 0 on success, others on failure. */ }
//int EXPORT_API usb_io_set_pin_mode(int hHandle, unsigned pinIndex, pin_mode mode, input_pin_mode innerPullUp);
function usb_io_set_pin_mode(hHandle:integer; pinIndex:word; mode:pin_modeT;innerPullUp:input_pin_modeT):integer;
stdcall; external USBIODLL name 'usb_io_set_pin_mode';
{/*set the value of output pin
@param:
hHandle: the return value of open_usb_io_device();
pinIndex: pin number of usb io. valid number is 0 to 15
level: low or high level
Attention: ouputPinIndex must be a output pin that was set by set_usb_io_pin_mode() funcation.
@returns: return 0 on success, other on failure */ }
//int EXPORT_API usb_io_write_output_pin_value(int hHandle, unsigned ouputPinIndex, pin_level level);
function usb_io_write_output_pin_value(hHandle:integer; outputPinIndex:word; level:pin_levelT):integer;
stdcall; external USBIODLL name 'usb_io_write_output_pin_value';
{/*get the value of input pin of pinIndex
@params:
hHandle: the retrun value of open_usb_io_device();
pinIndex: pin number of usb io. valid number is 0 to 15
level: a non-null pointer. the value of input pin stored in this param. 0 -- low level, 1 -- high level
@returns: return 0 on success, other on failure */ }
//int EXPORT_API usb_io_read_input_pin_value(int hHandle, unsigned pinIndex, unsigned *level);
function usb_io_read_input_pin_value(hHandle:integer; outputPinIndex:word; VAR level:word):integer;
stdcall; external USBIODLL name 'usb_io_read_input_pin_value';
{/*get the value of all pin
@param:
hHandle: the retrun value of open_usb_io_device();
@returns: return 0 on success, other on failure */ }
//int EXPORT_API usb_io_get_all_pin_info(int hHandle, pin_info info[16]);
function usb_io_get_all_pin_info(hHandle:integer; info:pin_info_arrayT):integer;
stdcall; external USBIODLL name 'usb_io_get_all_pin_info';
//
// ============================ end .DLL functions =============================
//
// more procs & funcs go here
//
procedure errmess(ec:integer);
begin
write ('error code: ', ec, ' ');
if ec = 0 then writeln ('OK.')
else writeln ('error.');
end; // errmess
// -------------------
procedure polldevice(devnr,hand:integer);
var pin_nr :integer;
kar :char;
begin
clrscr;
writeln('Poll all 16 input pins of dev ',devnr);
writeln('Hit any key to stop');
repeat
gotoxy(1,3);
writeln ('pinnr level ');
for pin_nr := 0 to 15 do begin
write (pin_nr:3,' = ');
errcode := usb_io_read_input_pin_value(hand,pin_nr,level);
if errcode = 0 then writeln (level)
else writeln ('?');
end;
until keypressed;
kar := readkey;
end; // polldevice
// ----------------------------- main prog -------------------------------------
begin // main program
writeln ('Prog. USB IO cmd test 2.');
writeln ('------------------------');
writeln ('init IO devices:');
errcode := usb_io_init();
errmess (errcode);
writeln ('------------------------');
//
// -------------------------------------------
//
writeln ('Get device list:');
devs := 0; // assume number of USB devices = 0
pDev1 := usb_io_get_device_list(); // pointer to USB dev #1 in list
if pDev1 = nil then begin
writeln ('No USB IO devices found.');
end
else begin; // at least one device found
pDev := pDev1; // save pointer to dev #1
pdev2 := pdev^.next; // save pointer to dev #2
while pDev <> nil do begin
devs := devs+1;
writeln(' Dev ',devs);
writeln(' - serial number = ');
writeln (pDev1^.serial_number);
writeln(' - device path = ');
writeln (pDev1^.device_path );
pDev := pdev^.next;
end;
writeln(devs, ' USB IO devices found.');
writeln ('Hit <enter> to continue...');
readln;
//
// -------------------------------------------
//
clrscr;
writeln;
writeln ('Open dev 1');
hand1 := usb_io_open_device(pDev1);
if hand1 > 0 then begin // device opened OK, turn LED on
writeln ('LED on... ');
errcode := usb_io_set_work_led_mode(hand1,1);
errmess(errcode);
if errcode = 0 then begin // LED turned on ok, turn it off
writeln ('Hit <enter> to turn LED off');
readln;
writeln ('LED off.');
errcode := usb_io_set_work_led_mode(hand1,0);
errmess(errcode);
end;
writeln ('Hit <enter> to set pins to output...');
readln;
clrscr;
writeln ('Set all 16 pins of dev 1 to output:');
for pinnr := 0 to 15 do begin
writeln ('pinnr ',pinnr);
errcode := usb_io_set_pin_mode(hand1,pinnr,OUTPUT_MODE,INNER_PULL_UP);
errmess(errcode);
end;
writeln ('Hit <enter> to make pins high...');
readln;
clrscr;
writeln('Make all 16 pins of dev 1 high:');
for pinnr := 0 to 15 do begin
writeln ('pinnr ',pinnr);
errcode := usb_io_write_output_pin_value(hand1,pinnr,1);
errmess(errcode);
end;
writeln;
usb_io_close_device(hand1);
writeln ('dev 1 closed');
end
else begin // handle = 0, error opening device
errmess (hand1);
END;
//
// -------------------------------------------
//
writeln ('Hit <enter> to turn dev 2 LED on...');
readln;
clrscr;
writeln;
writeln ('Open dev 2');
hand2 := usb_io_open_device(pDev2);
if hand2 > 0 then begin // device opened OK, turn LED on
writeln ('LED on... ');
errcode := usb_io_set_work_led_mode(hand2,1);
errmess(errcode);
if errcode = 0 then begin // LED turned on ok, turn it off
writeln ('Hit <enter> to turn dev2 LED off');
readln;
writeln ('LED off.');
errcode := usb_io_set_work_led_mode(hand2,0);
errmess(errcode);
end;
writeln ('Hit <enter> to set all 16 dev 2 pins to input...');
readln;
clrscr;
writeln('Set pins of dev 2 to input:');
for pinnr := 0 to 15 do begin
writeln ('pinnr ',pinnr);
errcode := usb_io_set_pin_mode(hand2,pinnr,INPUT_MODE,INNER_PULL_UP);
if errcode = 0 then writeln ('ok')
else errmess(errcode);
end;
writeln ('Hit <enter> to read pins...');
readln;
polldevice(2,hand2);
writeln;
usb_io_close_device(hand2);
writeln ('dev 2 closed');
end
else begin // error opening device
errmess (hand2);
END;
end;
writeln ('------------------------');
//
// -------------------------------------------
//
writeln ('Hit <enter> to finish...');
readln;
clrscr;
writeln ('Free device list:');
usb_io_free_device_list(pDev1); // even if NIL
writeln ('UN-init IO devices:');
errcode := usb_io_uninit();
errmess (errcode);
writeln ('------------------------');
writeln;
writeln('Hit any key to exit...');
readln;
end. // main program