summaryrefslogtreecommitdiffstats
path: root/libk8055/libk8055.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2011-02-28 23:44:19 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2011-02-28 23:44:19 +0100
commitb83640023007b1cb56b70e955d0a3dc18e6b1d0a (patch)
tree1f20372eac9f88e1e1d6ea33a67611a0499367a2 /libk8055/libk8055.c
parent851ba3b9f302449c1b5def87574d523d873e4fe2 (diff)
downloadk8055-b83640023007b1cb56b70e955d0a3dc18e6b1d0a.zip
k8055-b83640023007b1cb56b70e955d0a3dc18e6b1d0a.tar.gz
open up API in a more usual way, keeping Velleman API alive
Diffstat (limited to 'libk8055/libk8055.c')
-rw-r--r--libk8055/libk8055.c333
1 files changed, 198 insertions, 135 deletions
diff --git a/libk8055/libk8055.c b/libk8055/libk8055.c
index ff288aa..2df72bd 100644
--- a/libk8055/libk8055.c
+++ b/libk8055/libk8055.c
@@ -87,7 +87,6 @@
#include "k8055.h"
#define STR_BUFF 256
-#define PACKET_LEN 8
#define READ_RETRY 3
#define WRITE_RETRY 3
@@ -118,14 +117,6 @@
/* set debug to 0 to not print excess info */
int debug = 0;
-/* globals for datatransfer */
-struct k8055_dev {
- unsigned char data_in[PACKET_LEN+1];
- unsigned char data_out[PACKET_LEN+1];
- struct usb_dev_handle* device_handle;
- int dev_no;
-};
-
static struct k8055_dev k8055d[K8055_MAX_DEV];
static struct k8055_dev* curr_dev;
@@ -181,45 +172,40 @@ static int takeover_device( usb_dev_handle* udev, int interface ) {
return 0;
}
-/* Open device - scan through usb busses looking for the right device, claim it and then open the device */
-int OpenDevice( long board_address ) {
- if( board_address<0 || board_address>=K8055_MAX_DEV ) return K8055_ERROR;
- if( k8055d[board_address].dev_no!=0 ) return board_address;
+int open_device( struct k8055_dev* dev, long board_address ) {
usb_init();
usb_find_busses();
usb_find_devices();
int ipid = K8055_IPID + ( int )board_address;
struct usb_bus* busses = usb_get_busses();
for( struct usb_bus* bus=busses; bus; bus=bus->next ) {
- for( struct usb_device* dev=bus->devices; dev; dev=dev->next ) {
- if( ( dev->descriptor.idVendor==VELLEMAN_VENDOR_ID ) && ( dev->descriptor.idProduct==ipid ) ) {
- struct k8055_dev* kdev = &k8055d[board_address];
- kdev->dev_no = 0;
- kdev->device_handle = usb_open( dev );
- if( kdev->device_handle==0 ) {
+ for( struct usb_device* usb_dev=bus->devices; usb_dev; usb_dev=usb_dev->next ) {
+ if( ( usb_dev->descriptor.idVendor==VELLEMAN_VENDOR_ID ) && ( usb_dev->descriptor.idProduct==ipid ) ) {
+ dev->dev_no = 0;
+ dev->device_handle = usb_open( usb_dev );
+ if( dev->device_handle==NULL ) {
if( debug ) fprintf( stderr,"usb_open failure : %s\n", usb_strerror() );
return K8055_ERROR;
}
- if( debug ) fprintf( stderr, "Velleman Device Found @ Address %s Vendor 0x0%x Product ID 0x0%x\n", dev->filename, dev->descriptor.idVendor, dev->descriptor.idProduct );
- if( takeover_device( kdev->device_handle, 0 )<0 ) {
+ if( debug ) fprintf( stderr, "Velleman Device Found @ Address %s Vendor 0x0%x Product ID 0x0%x\n", usb_dev->filename, usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct );
+ if( takeover_device( dev->device_handle, 0 )<0 ) {
if( debug ) fprintf( stderr, "Can not take over the device from the OS driver\n" );
- usb_close( kdev->device_handle );
- kdev->device_handle = NULL;
+ usb_close( dev->device_handle );
+ dev->device_handle = NULL;
return K8055_ERROR;
} else {
- memset( kdev->data_out,0,PACKET_LEN );
- kdev->dev_no = board_address + 1;
- kdev->data_out[0] = CMD_RESET;
- k8055_write( kdev );
- if ( k8055_read( kdev )==0 ) {
+ memset( dev->data_out,0,PACKET_LEN );
+ dev->dev_no = board_address + 1;
+ dev->data_out[0] = CMD_RESET;
+ k8055_write( dev );
+ if ( k8055_read( dev )==0 ) {
if( debug ) fprintf( stderr, "Device %d ready\n",board_address );
- curr_dev = kdev;
return board_address;
} else {
if( debug ) fprintf( stderr, "Device %d not ready\n",board_address );
- kdev->dev_no = 0;
- usb_close( kdev->device_handle );
- kdev->device_handle = NULL;
+ dev->dev_no = 0;
+ usb_close( dev->device_handle );
+ dev->device_handle = NULL;
return K8055_ERROR;
}
}
@@ -230,205 +216,195 @@ int OpenDevice( long board_address ) {
return K8055_ERROR;
}
-/* Close the Current device */
-int CloseDevice() {
- if ( curr_dev->dev_no == 0 ) {
+int close_device( struct k8055_dev* dev ) {
+ if ( dev->dev_no == 0 ) {
if ( debug ) fprintf( stderr, "Current device is not open\n" );
return 0;
}
- if( curr_dev->device_handle==NULL ) {
+ if( dev->device_handle==NULL ) {
if ( debug ) fprintf( stderr, "Current device is marked as open, but device hanlde is NULL\n" );
- curr_dev->dev_no = 0;
+ dev->dev_no = 0;
return 0;
}
- int rc = usb_close( curr_dev->device_handle );
+ int rc = usb_close( dev->device_handle );
if ( rc >= 0 ) {
- curr_dev->dev_no = 0;
- curr_dev->device_handle = NULL;
+ dev->dev_no = 0;
+ dev->device_handle = NULL;
}
return rc;
}
-/* New function in version 2 of Velleman DLL, should return deviceno if OK */
-long SetCurrentDevice( long deviceno ) {
- if ( deviceno < 0 || deviceno >= K8055_MAX_DEV ) return K8055_ERROR;
- if ( k8055d[deviceno].dev_no == 0 ) return K8055_ERROR;
- curr_dev = &k8055d[deviceno];
- return deviceno;
-}
-
-/* New function in version 2 of Velleman DLL, should return devices-found bitmask or 0*/
-long SearchDevices( void ) {
- int retval = 0;
+long search_devices( void ) {
+ int ret = 0;
usb_init();
usb_find_busses();
usb_find_devices();
struct usb_bus* busses = usb_get_busses();
for ( struct usb_bus* bus = busses; bus; bus = bus->next ) {
- for( struct usb_device* dev=bus->devices; dev; dev=dev->next ) {
- if ( dev->descriptor.idVendor == VELLEMAN_VENDOR_ID ) {
- if( dev->descriptor.idProduct == K8055_IPID + 0 ) retval |= 0x01;
- if( dev->descriptor.idProduct == K8055_IPID + 1 ) retval |= 0x02;
- if( dev->descriptor.idProduct == K8055_IPID + 2 ) retval |= 0x04;
- if( dev->descriptor.idProduct == K8055_IPID + 3 ) retval |= 0x08;
+ for( struct usb_device* usb_dev=bus->devices; usb_dev; usb_dev=usb_dev->next ) {
+ if ( usb_dev->descriptor.idVendor == VELLEMAN_VENDOR_ID ) {
+ if( usb_dev->descriptor.idProduct == K8055_IPID + 0 ) ret |= 0x01;
+ if( usb_dev->descriptor.idProduct == K8055_IPID + 1 ) ret |= 0x02;
+ if( usb_dev->descriptor.idProduct == K8055_IPID + 2 ) ret |= 0x04;
+ if( usb_dev->descriptor.idProduct == K8055_IPID + 3 ) ret |= 0x08;
/* else some other kind of Velleman board */
}
}
}
- if( debug ) fprintf( stderr,"found devices : %X\n",retval );
- return retval;
+ if( debug ) fprintf( stderr,"found devices : %X\n",ret );
+ return ret;
}
-long ReadAnalogChannel( long channel ) {
+long read_analog_channel( struct k8055_dev* dev, long channel ) {
if ( !( channel==1 || channel==2 ) ) return K8055_ERROR;
- if ( k8055_read( curr_dev )==0 ) {
+ if ( k8055_read( dev )==0 ) {
if ( channel==1 ) {
- return curr_dev->data_in[ANALOG_1_OFFSET];
+ return dev->data_in[ANALOG_1_OFFSET];
} else {
- return curr_dev->data_in[ANALOG_2_OFFSET];
+ return dev->data_in[ANALOG_2_OFFSET];
}
}
return K8055_ERROR;
}
-int ReadAllAnalog( long* data1, long* data2 ) {
- if ( k8055_read( curr_dev )!=0 ) return K8055_ERROR;
- *data1 = curr_dev->data_in[ANALOG_1_OFFSET];
- *data2 = curr_dev->data_in[ANALOG_2_OFFSET];
+int read_all_analog( struct k8055_dev* dev, long* data1, long* data2 ) {
+ if ( k8055_read( dev )!=0 ) return K8055_ERROR;
+ *data1 = dev->data_in[ANALOG_1_OFFSET];
+ *data2 = dev->data_in[ANALOG_2_OFFSET];
return 0;
}
-int OutputAnalogChannel( long channel, long data ) {
+int output_analog_channel( struct k8055_dev* dev ,long channel, long data ) {
if ( !( channel==1 || channel==2 ) ) return K8055_ERROR;
- curr_dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
+ dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
if ( channel==1 ) {
- curr_dev->data_out[ANALOG_1_OFFSET] = ( unsigned char )data;
+ dev->data_out[ANALOG_1_OFFSET] = ( unsigned char )data;
} else {
- curr_dev->data_out[ANALOG_2_OFFSET] = ( unsigned char )data;
+ dev->data_out[ANALOG_2_OFFSET] = ( unsigned char )data;
}
- return k8055_write( curr_dev );
+ return k8055_write( dev );
}
-int OutputAllAnalog( long data1, long data2 ) {
- curr_dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
- curr_dev->data_out[2] = ( unsigned char )data1;
- curr_dev->data_out[3] = ( unsigned char )data2;
- return k8055_write( curr_dev );
+int output_all_analog( struct k8055_dev* dev, long data1, long data2 ) {
+ dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
+ dev->data_out[2] = ( unsigned char )data1;
+ dev->data_out[3] = ( unsigned char )data2;
+ return k8055_write( dev );
}
-int ClearAllAnalog() {
- return OutputAllAnalog( 0, 0 );
+int clear_all_analog( struct k8055_dev* dev ) {
+ return output_all_analog( dev, 0, 0 );
}
-int ClearAnalogChannel( long channel ) {
+int clear_analog_channel( struct k8055_dev* dev, long channel ) {
if ( !( channel==1 || channel==2 ) ) return K8055_ERROR;
if ( channel==1 ) {
- return OutputAnalogChannel( 1, 0 );
+ return output_analog_channel( dev, 1, 0 );
} else {
- return OutputAnalogChannel( 2, 0 );
+ return output_analog_channel( dev, 2, 0 );
}
}
-int SetAnalogChannel( long channel ) {
+int set_analog_channel( struct k8055_dev* dev, long channel ) {
if ( !( channel==1 || channel==2 ) ) return K8055_ERROR;
if ( channel == 2 ) {
- return OutputAnalogChannel( 2, 0xff );
+ return output_analog_channel( dev, 2, 0xff );
} else {
- return OutputAnalogChannel( 1, 0xff );
+ return output_analog_channel( dev, 1, 0xff );
}
}
-int SetAllAnalog() {
- return OutputAllAnalog( 0xff, 0xff );
+int set_all_analog( struct k8055_dev* dev ) {
+ return output_all_analog( dev, 0xff, 0xff );
}
-int WriteAllDigital( long data ) {
- curr_dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
- curr_dev->data_out[1] = ( unsigned char )data;
- return k8055_write( curr_dev );
+int write_all_digital( struct k8055_dev* dev, long data ) {
+ dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
+ dev->data_out[1] = ( unsigned char )data;
+ return k8055_write( dev );
}
-int ClearDigitalChannel( long channel ) {
+int clear_digital_channel( struct k8055_dev* dev, long channel ) {
unsigned char data;
if ( channel<1 || channel>8 ) return K8055_ERROR;
- data = curr_dev->data_out[1] & ~( 1 << ( channel-1 ) );
+ data = dev->data_out[1] & ~( 1 << ( channel-1 ) );
return WriteAllDigital( data );
}
-int ClearAllDigital() {
- return WriteAllDigital( 0x00 );
+int clear_all_digital( struct k8055_dev* dev ) {
+ return write_all_digital( dev, 0x00 );
}
-int SetDigitalChannel( long channel ) {
+int set_digital_channel( struct k8055_dev* dev, long channel ) {
unsigned char data;
if ( channel<1 || channel>8 ) return K8055_ERROR;
- data = curr_dev->data_out[1] | ( 1 << ( channel-1 ) );
- return WriteAllDigital( data );
+ data = dev->data_out[1] | ( 1 << ( channel-1 ) );
+ return write_all_digital( dev, data );
}
-int SetAllDigital() {
- return WriteAllDigital( 0xff );
+int set_all_digital( struct k8055_dev* dev ) {
+ return write_all_digital( dev, 0xff );
}
-int ReadDigitalChannel( long channel ) {
+int read_digital_channel( struct k8055_dev* dev, long channel ) {
int rval;
if ( channel<1 || channel>8 ) return K8055_ERROR;
- if ( ( rval = ReadAllDigital() ) == K8055_ERROR ) return K8055_ERROR;
+ if ( ( rval = read_all_digital( dev ) ) == K8055_ERROR ) return K8055_ERROR;
return ( ( rval & ( 1 << ( channel-1 ) ) ) > 0 );
}
-long ReadAllDigital() {
+long read_all_digital( struct k8055_dev* dev ) {
int return_data = 0;
- if ( k8055_read( curr_dev )!=0 ) return K8055_ERROR;
+ if ( k8055_read( dev )!=0 ) return K8055_ERROR;
return_data = (
- ( ( curr_dev->data_in[0] >> 4 ) & 0x03 ) | /* Input 1 and 2 */
- ( ( curr_dev->data_in[0] << 2 ) & 0x04 ) | /* Input 3 */
- ( ( curr_dev->data_in[0] >> 3 ) & 0x18 ) ); /* Input 4 and 5 */
+ ( ( dev->data_in[0] >> 4 ) & 0x03 ) | /* Input 1 and 2 */
+ ( ( dev->data_in[0] << 2 ) & 0x04 ) | /* Input 3 */
+ ( ( dev->data_in[0] >> 3 ) & 0x18 ) ); /* Input 4 and 5 */
return return_data;
}
-int ReadAllValues( long int* data1, long int* data2, long int* data3, long int* data4, long int* data5 ) {
- if ( k8055_read( curr_dev )!=0 ) return K8055_ERROR;
+int read_all_values( struct k8055_dev* dev, long int* data1, long int* data2, long int* data3, long int* data4, long int* data5 ) {
+ if ( k8055_read( dev )!=0 ) return K8055_ERROR;
*data1 = (
- ( ( curr_dev->data_in[0] >> 4 ) & 0x03 ) | /* Input 1 and 2 */
- ( ( curr_dev->data_in[0] << 2 ) & 0x04 ) | /* Input 3 */
- ( ( curr_dev->data_in[0] >> 3 ) & 0x18 ) ); /* Input 4 and 5 */
- *data2 = curr_dev->data_in[ANALOG_1_OFFSET];
- *data3 = curr_dev->data_in[ANALOG_2_OFFSET];
- *data4 = *( ( short int* )( &curr_dev->data_in[COUNTER_1_OFFSET] ) );
- *data5 = *( ( short int* )( &curr_dev->data_in[COUNTER_2_OFFSET] ) );
+ ( ( dev->data_in[0] >> 4 ) & 0x03 ) | /* Input 1 and 2 */
+ ( ( dev->data_in[0] << 2 ) & 0x04 ) | /* Input 3 */
+ ( ( dev->data_in[0] >> 3 ) & 0x18 ) ); /* Input 4 and 5 */
+ *data2 = dev->data_in[ANALOG_1_OFFSET];
+ *data3 = dev->data_in[ANALOG_2_OFFSET];
+ *data4 = *( ( short int* )( &dev->data_in[COUNTER_1_OFFSET] ) );
+ *data5 = *( ( short int* )( &dev->data_in[COUNTER_2_OFFSET] ) );
return 0;
}
-int SetAllValues( int DigitalData, int AdData1, int AdData2 ) {
- curr_dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
- curr_dev->data_out[1] = ( unsigned char )DigitalData;
- curr_dev->data_out[2] = ( unsigned char )AdData1;
- curr_dev->data_out[3] = ( unsigned char )AdData2;
- return k8055_write( curr_dev );
+int set_all_values( struct k8055_dev* dev, int digital_data, int ad_data1, int ad_data2 ) {
+ dev->data_out[0] = CMD_SET_ANALOG_DIGITAL;
+ dev->data_out[1] = ( unsigned char )digital_data;
+ dev->data_out[2] = ( unsigned char )ad_data1;
+ dev->data_out[3] = ( unsigned char )ad_data2;
+ return k8055_write( dev );
}
-int ResetCounter( long counter ) {
+int reset_counter( struct k8055_dev* dev, long counter ) {
if ( !( counter==1 || counter==2 ) ) return K8055_ERROR;
- curr_dev->data_out[0] = 0x02 + ( unsigned char )counter;
- curr_dev->data_out[3+counter] = 0x00;
- return k8055_write( curr_dev );
+ dev->data_out[0] = 0x02 + ( unsigned char )counter;
+ dev->data_out[3+counter] = 0x00;
+ return k8055_write( dev );
}
-long ReadCounter( long counter ) {
+long read_counter( struct k8055_dev* dev, long counter ) {
if ( !( counter==1 || counter==2 ) ) return K8055_ERROR;
- if ( k8055_read( curr_dev )!=0 ) return K8055_ERROR;
+ if ( k8055_read( dev )!=0 ) return K8055_ERROR;
if ( counter==1 ) {
- return *( ( short int* )( &curr_dev->data_in[COUNTER_1_OFFSET] ) );
+ return *( ( short int* )( &dev->data_in[COUNTER_1_OFFSET] ) );
} else {
- return *( ( short int* )( &curr_dev->data_in[COUNTER_2_OFFSET] ) );
+ return *( ( short int* )( &dev->data_in[COUNTER_2_OFFSET] ) );
}
}
-int SetCounterDebounceTime( long counter, long debounce_time ) {
+int set_counter_debounce_time( struct k8055_dev* dev, long counter, long debounce_time ) {
float value;
if ( !( counter==1 || counter==2 ) ) return K8055_ERROR;
- curr_dev->data_out[0] = ( unsigned char )counter;
+ dev->data_out[0] = ( unsigned char )counter;
/*
* the velleman k8055 use a exponetial formula to split up the
* debounce_time 0-7450 over value 1-255. I've tested every value and
@@ -443,11 +419,98 @@ int SetCounterDebounceTime( long counter, long debounce_time ) {
value = sqrtf( debounce_time / 0.115 );
/* simple round() function) */
if ( value > ( ( int )value + 0.49999999 ) ) value+=1;
- curr_dev->data_out[5+counter] = ( unsigned char )value;
- if ( debug ) fprintf( stderr, "Debouncetime%d value for k8055:%d\n",( int )counter, curr_dev->data_out[5+counter] );
- return k8055_write( curr_dev );
+ dev->data_out[5+counter] = ( unsigned char )value;
+ if ( debug ) fprintf( stderr, "Debouncetime%d value for k8055:%d\n",( int )counter, dev->data_out[5+counter] );
+ return k8055_write( dev );
}
+/** Velleman API ***************************************************************************************************************************/
char* Version( void ) {
return( VERSION );
}
+/* New function in version 2 of Velleman DLL, should return devices-found bitmask or 0*/
+long SearchDevices( void ) {
+ return search_devices();
+}
+/* Open device - scan through usb busses looking for the right device, claim it and then open the device */
+int OpenDevice( long board_address ) {
+ if( board_address<0 || board_address>=K8055_MAX_DEV ) return K8055_ERROR;
+ if( k8055d[board_address].dev_no!=0 ) return board_address;
+ int ret = open_device( &k8055d[board_address], board_address );
+ if ( ret != K8055_ERROR ) {
+ curr_dev = &k8055d[board_address];
+ return ret;
+ }
+ return K8055_ERROR;
+}
+/* Close the Current device */
+int CloseDevice() {
+ return close_device( curr_dev );
+}
+/* New function in version 2 of Velleman DLL, should return deviceno if OK */
+long SetCurrentDevice( long deviceno ) {
+ if ( deviceno < 0 || deviceno >= K8055_MAX_DEV ) return K8055_ERROR;
+ if ( k8055d[deviceno].dev_no == 0 ) return K8055_ERROR;
+ curr_dev = &k8055d[deviceno];
+ return deviceno;
+}
+long ReadAnalogChannel( long channel ) {
+ return read_analog_channel( curr_dev, channel );
+}
+int ReadAllAnalog( long* data1, long* data2 ) {
+ return read_all_analog( curr_dev, data1, data2 );
+}
+int OutputAnalogChannel( long channel, long data ) {
+ return output_analog_channel( curr_dev, channel, data );
+}
+int OutputAllAnalog( long data1, long data2 ) {
+ return output_all_analog( curr_dev, data1, data2 );
+}
+int ClearAllAnalog() {
+ return clear_all_analog( curr_dev );
+}
+int ClearAnalogChannel( long channel ) {
+ return clear_analog_channel( curr_dev, channel );
+}
+int SetAnalogChannel( long channel ) {
+ return set_analog_channel( curr_dev, channel );
+}
+int SetAllAnalog() {
+ return set_all_analog( curr_dev );
+}
+int WriteAllDigital( long data ) {
+ return write_all_digital( curr_dev, data );
+}
+int ClearDigitalChannel( long channel ) {
+ return clear_digital_channel( curr_dev, channel );
+}
+int ClearAllDigital() {
+ return clear_all_digital( curr_dev );
+}
+int SetDigitalChannel( long channel ) {
+ return set_digital_channel( curr_dev, channel );
+}
+int SetAllDigital() {
+ return set_all_digital( curr_dev );
+}
+int ReadDigitalChannel( long channel ) {
+ return read_digital_channel( curr_dev, channel );
+}
+long ReadAllDigital() {
+ return read_all_digital( curr_dev );
+}
+int ReadAllValues( long int* data1, long int* data2, long int* data3, long int* data4, long int* data5 ) {
+ return read_all_values( curr_dev, data1, data2, data3, data4, data5 );
+}
+int SetAllValues( int DigitalData, int AdData1, int AdData2 ) {
+ return set_all_values( curr_dev, DigitalData, AdData1, AdData2 );
+}
+int ResetCounter( long counter ) {
+ return reset_counter( curr_dev, counter );
+}
+long ReadCounter( long counter ) {
+ return read_counter( curr_dev, counter );
+}
+int SetCounterDebounceTime( long counter, long debounce_time ) {
+ return set_counter_debounce_time( curr_dev, counter, debounce_time );
+}