Skip to content

Commit

Permalink
some fixes to initialisation and allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Marije Baalman committed Sep 12, 2013
1 parent 6188ee8 commit ab9125e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
17 changes: 2 additions & 15 deletions hidapi2osc/hidapi2osc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

//TODO: add copyright notice

#include <assert.h>
// #include <curses.h>
Expand All @@ -12,8 +12,6 @@

#include <lo/lo.h>

// #include "../hidapi/hidapi.h"
// #include "../hidapi_parser/hidapi_parser.h"

#include <hidapi.h>
#include <hidapi_parser.h>
Expand Down Expand Up @@ -50,7 +48,7 @@ static void osc_element_cb( struct hid_device_element *el, void *data)
lo_message_add_int32( m1, el->usage_page );
lo_message_add_int32( m1, el->usage );
lo_message_add_int32( m1, el->value );
// lo_message_add_float( m1, hid_element_map_logical( el ) ); // TODO: this one is not found???
lo_message_add_float( m1, hid_element_map_logical( el ) ); // TODO: this one is not found???
lo_send_message_from( t, s, "/hid/element/data", m1 );
lo_message_free(m1);
}
Expand Down Expand Up @@ -481,17 +479,6 @@ int main(int argc, char** argv)
exit(1);
}

// FIXME: We don't need video, but without it SDL will fail to work in SDL_WaitEvent()
// if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
// if(SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENT | SDL_INIT_JOYSTICK) < 0)
// {
// fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
// exit(1);
// }
// else
// {
// atexit(SDL_Quit);

if (argc == 2 && (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0))
{
Expand Down
54 changes: 45 additions & 9 deletions hidapi_parser/hidapi_parser.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//TODO: add copyright notice

#include <stdio.h>
#include <stdlib.h>
// #include <math.h>

#include "hidapi_parser.h"

// SET IN CMAKE
// #define DEBUG_PARSER

//// ---------- HID descriptor parser
Expand Down Expand Up @@ -105,12 +108,41 @@
#define BITMASK1(n) ((1ULL << (n)) - 1ULL)


void hid_descriptor_init( struct hid_device_descriptor * devd){
hid_set_descriptor_callback(devd, NULL, NULL);
hid_set_element_callback(devd, NULL, NULL);
struct hid_device_descriptor * hid_new_descriptor(){
struct hid_device_descriptor * descriptor;
descriptor = (struct hid_device_descriptor *) malloc( sizeof( struct hid_device_descriptor) );
// hid_descriptor_init( descriptor );

descriptor->first = NULL;
hid_set_descriptor_callback(descriptor, NULL, NULL);
hid_set_element_callback(descriptor, NULL, NULL);
return descriptor;
}

struct hid_device_element * hid_new_element(){
struct hid_device_element * element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
element->next = NULL;
return element;
}

void hid_free_element( struct hid_device_element * ele ){
free( ele );
}

// void hid_descriptor_init( struct hid_device_descriptor * devd){
// devd->first = NULL;
// hid_set_descriptor_callback(devd, NULL, NULL);
// hid_set_element_callback(devd, NULL, NULL);
// }

void hid_free_descriptor( struct hid_device_descriptor * devd){
struct hid_device_element * cur_element = devd->first;
struct hid_device_element * next_element;
while (cur_element != NULL ) {
next_element = cur_element->next;
free( cur_element );
cur_element = next_element;
}
free( devd );
// hid_set_descriptor_callback(devd, NULL, NULL);
// hid_set_element_callback(devd, NULL, NULL);
Expand Down Expand Up @@ -278,7 +310,8 @@ int hid_parse_report_descriptor( char* descr_buf, int size, struct hid_device_de
#endif
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
struct hid_device_element * new_element = hid_new_element();
// = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
new_element->index = descriptor->num_elements;
new_element->io_type = 1;
new_element->type = next_val; //TODO: parse this for more detailed info
Expand Down Expand Up @@ -328,7 +361,8 @@ int hid_parse_report_descriptor( char* descr_buf, int size, struct hid_device_de
#endif
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
struct hid_device_element * new_element = hid_new_element();
// struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
new_element->index = descriptor->num_elements;
new_element->io_type = 2;
new_element->type = next_val; //TODO: parse this for more detailed info
Expand Down Expand Up @@ -378,7 +412,8 @@ int hid_parse_report_descriptor( char* descr_buf, int size, struct hid_device_de
#endif
// add the elements for this report
for ( j=0; j<current_report_count; j++ ){
struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
struct hid_device_element * new_element = hid_new_element();
// struct hid_device_element * new_element = (struct hid_device_element *) malloc( sizeof( struct hid_device_element ) );
new_element->index = descriptor->num_elements;
new_element->io_type = 3;
new_element->type = next_val; //TODO: parse this for more detailed info
Expand Down Expand Up @@ -461,7 +496,7 @@ int hid_parse_report_descriptor( char* descr_buf, int size, struct hid_device_de
}

float hid_element_map_logical( struct hid_device_element * element ){
float result = element->logical_min + ( element->value/( element->logical_max - element->logical_min ) );
float result = (float) element->logical_min + ( (float) element->value/( (float) element->logical_max - (float) element->logical_min ) );
return result;
}

Expand Down Expand Up @@ -609,8 +644,9 @@ struct hid_device_descriptor * hid_read_descriptor( hid_device * devd ){
printf("Unable to read report descriptor\n");
return NULL;
} else {
descriptor = (struct hid_device_descriptor *) malloc( sizeof( struct hid_device_descriptor) );
hid_descriptor_init( descriptor );
descriptor = hid_new_descriptor();
// descriptor = (struct hid_device_descriptor *) malloc( sizeof( struct hid_device_descriptor) );
// hid_descriptor_init( descriptor );
hid_parse_report_descriptor( descr_buf, res, descriptor );
return descriptor;
}
Expand Down
11 changes: 7 additions & 4 deletions hidapi_parser/hidapi_parser.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//TODO: add copyright notice

#ifndef HIDAPI_PARSER_H__
#define HIDAPI_PARSER_H__

Expand Down Expand Up @@ -87,11 +89,12 @@ struct hid_device_descriptor * hid_read_descriptor( hid_device *devd );
struct hid_dev_desc * hid_open_device( unsigned short vendor, unsigned short product, const wchar_t *serial_number );
extern void hid_close_device( struct hid_dev_desc * devdesc );


// typedef void (*event_cb_t)(const struct hid_device_element *element, void *user_data);

void hid_descriptor_init( struct hid_device_descriptor * devd);
struct hid_device_descriptor * hid_new_descriptor();
void hid_free_descriptor( struct hid_device_descriptor * devd);
struct hid_device_element * hid_new_element();
void hid_free_element( struct hid_device_element * ele);

// void hid_descriptor_init( struct hid_device_descriptor * devd);

void hid_set_descriptor_callback( struct hid_device_descriptor * devd, hid_descriptor_callback cb, void *user_data );
void hid_set_element_callback( struct hid_device_descriptor * devd, hid_element_callback cb, void *user_data );
Expand Down
39 changes: 29 additions & 10 deletions hidparsertest/hidparsertest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
// #include <conio.h>

#include <hidapi.h>
#include "hidapi_parser.h"
Expand All @@ -19,7 +20,7 @@ void list_devices( void ){
devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("Device Found\n type: 0x%04hx 0x%04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
Expand Down Expand Up @@ -49,7 +50,6 @@ void print_element_info( struct hid_device_element *element ){
element->phys_min, element->phys_max,
element->unit_exponent, element->unit,
element->report_size, element->report_id, element->report_index );

}

void print_device_info( hid_device *handle ){
Expand Down Expand Up @@ -104,7 +104,7 @@ int main(int argc, char* argv[]){
int res;
unsigned char buf[256];
unsigned char descr_buf[HIDAPI_MAX_DESCRIPTOR_SIZE];

struct hid_dev_desc *devdesc;
// struct hid_device_descriptor *descriptor;
// hid_device *handle;
Expand All @@ -117,6 +117,19 @@ int main(int argc, char* argv[]){
if (hid_init())
return -1;
list_devices();

int vendor_id;
int product_id;

if (argc == 3 ){
vendor_id = atoi( argv[1] );
product_id = atoi( argv[1] );
} else {
printf( "please run again with vendor and product id to open specified device, e.g. hidparsertest 0x044f 0xd003\n" );
return 0;
}
printf( "vendor %i, product %i", vendor_id, product_id );


devdesc = hid_open_device( 0x044f, 0xd003, NULL );
if (!devdesc){
Expand All @@ -131,9 +144,7 @@ int main(int argc, char* argv[]){
// print_device_info( handle );

print_device_info( devdesc->device );

char my_custom_data[40] = "Hello!";


// descriptor = hid_read_descriptor( handle );
// if ( descriptor == NULL ){
// printf("unable to read descriptor\n");
Expand All @@ -151,19 +162,27 @@ int main(int argc, char* argv[]){
//
// }

hid_set_descriptor_callback( devdesc->descriptor, (hid_descriptor_callback) my_descriptor_cb, my_custom_data );
hid_set_element_callback( devdesc->descriptor, (hid_element_callback) my_element_cb, my_custom_data );

// Set the hid_read() function to be non-blocking.
// hid_set_nonblocking(handle, 1);

struct hid_device_element * cur_element = devdesc->descriptor->first;

while (cur_element) {
printf( "number of elements in device: %i\n", devdesc->descriptor->num_elements );
while (cur_element != NULL ) {
printf("cur_element %i\n", cur_element );
print_element_info( cur_element );
// printf("press key to continue\n" );
// getchar();
cur_element = cur_element->next;
}

printf("press key to continue\n" );
getchar();

char my_custom_data[40] = "Hello!";
hid_set_descriptor_callback( devdesc->descriptor, (hid_descriptor_callback) my_descriptor_cb, my_custom_data );
hid_set_element_callback( devdesc->descriptor, (hid_element_callback) my_element_cb, my_custom_data );

// Request state (cmd 0x81). The first byte is the report number (0x1).
// buf[0] = 0x1;
// buf[1] = 0x81;
Expand Down
2 changes: 2 additions & 0 deletions hidtest/hidtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
which use HIDAPI.
********************************************************/

//TODO: add copyright notice

#include <stdio.h>
#include <wchar.h>
#include <string.h>
Expand Down

0 comments on commit ab9125e

Please sign in to comment.