aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2012-06-15 19:54:44 +0200
committerJakob Odersky <jodersky@gmail.com>2012-06-15 19:54:44 +0200
commitef10a022de5bf32e7a7fd8e4830ce43266d10cd6 (patch)
tree7e29ee64f3c231e1ffcdba5691d7c6889fb1b086
parent5c506a9f89cf8273b8b790fb77416fc68d96d20b (diff)
downloadk8055-ef10a022de5bf32e7a7fd8e4830ce43266d10cd6.tar.gz
k8055-ef10a022de5bf32e7a7fd8e4830ce43266d10cd6.tar.bz2
k8055-ef10a022de5bf32e7a7fd8e4830ce43266d10cd6.zip
format code
-rw-r--r--src/k8055.c152
-rw-r--r--src/k8055.h7
2 files changed, 80 insertions, 79 deletions
diff --git a/src/k8055.c b/src/k8055.c
index e383f3c..3a3daa8 100644
--- a/src/k8055.c
+++ b/src/k8055.c
@@ -86,8 +86,7 @@
| 5 |DIG|An1|An2| | | | |
+---+---+---+---+---+---+---+---+
-*/
-
+ */
#define PACKET_LENGTH 8
#define K8055_PRODUCT_ID 0x5500
@@ -132,15 +131,15 @@
/** Represents a Vellemean K8055 USB board. */
struct k8055_device {
-
- /** Data last read from device, used by read_data(). */
- unsigned char data_in[PACKET_LENGTH];
- /** Data to be sent to the device, used by k8055_write_data(). */
- unsigned char data_out[PACKET_LENGTH];
+ /** Data last read from device, used by read_data(). */
+ unsigned char data_in[PACKET_LENGTH];
+
+ /** Data to be sent to the device, used by k8055_write_data(). */
+ unsigned char data_out[PACKET_LENGTH];
- /** Underlying libusb handle to device. NULL if the device is not open. */
- libusb_device_handle *device_handle;
+ /** Underlying libusb handle to device. NULL if the device is not open. */
+ libusb_device_handle *device_handle;
};
/** Libusb context. */
@@ -157,11 +156,10 @@ static void print_error(const char * str) {
int k8055_open_device(int port, k8055_device** device) {
if (port < 0 || K8055_MAX_DEVICES <= port) {
- print_error("invalid port number, port p should be 0<=p<=3");
- return K8055_ERROR_INDEX;
+ print_error("invalid port number, port p should be 0<=p<=3");
+ return K8055_ERROR_INDEX;
}
-
if (k8055_open_devices == 0) { /* no devices are open */
int r = libusb_init(&context); /* initialize a new context */
if (r < 0) {
@@ -180,10 +178,11 @@ int k8055_open_device(int port, k8055_device** device) {
libusb_device *k8055 = NULL; /* device on port */
- for(size_t i = 0; i < size; ++i) { /* look for the device at given port */
+ for (size_t i = 0; i < size; ++i) { /* look for the device at given port */
struct libusb_device_descriptor descriptor;
libusb_get_device_descriptor(connected_devices[i], &descriptor);
- if (descriptor.idVendor == VELLEMAN_VENDOR_ID && descriptor.idProduct == (K8055_PRODUCT_ID + port))
+ if (descriptor.idVendor == VELLEMAN_VENDOR_ID
+ && descriptor.idProduct == (K8055_PRODUCT_ID + port))
k8055 = connected_devices[i];
}
if (k8055 == NULL) {
@@ -197,15 +196,16 @@ int k8055_open_device(int port, k8055_device** device) {
libusb_free_device_list(connected_devices, 1); /* we got the handle, free references to other devices */
if (r == LIBUSB_ERROR_ACCESS) {
- print_error("could not open device, you don't have the required permissions");
+ print_error(
+ "could not open device, you don't have the required permissions");
return K8055_ERROR_ACCESS;
} else if (r != 0) {
print_error("could not open device");
return K8055_ERROR_OPEN;
}
- if(libusb_kernel_driver_active(handle, 0) == 1) { /* find out if kernel driver is attached */
- if(libusb_detach_kernel_driver(handle, 0) != 0) { /* detach it */
+ if (libusb_kernel_driver_active(handle, 0) == 1) { /* find out if kernel driver is attached */
+ if (libusb_detach_kernel_driver(handle, 0) != 0) { /* detach it */
print_error("could not detach kernel driver");
return K8055_ERROR_OPEN;
}
@@ -216,14 +216,14 @@ int k8055_open_device(int port, k8055_device** device) {
print_error("could not claim interface");
return K8055_ERROR_OPEN;
}
-
+
k8055_device* _device = NULL;
_device = malloc(sizeof(k8055_device));
if (_device == NULL) {
print_error("could not allocate memory for device");
return K8055_ERROR_MEM;
}
-
+
(*_device).device_handle = handle;
*device = _device;
k8055_open_devices += 1;
@@ -237,65 +237,60 @@ void k8055_close_device(k8055_device* device) {
device->device_handle = NULL;
free(device);
device = NULL;
-
+
k8055_open_devices -= 1;
-
- if (k8055_open_devices <= 0) libusb_exit(context);
+
+ if (k8055_open_devices <= 0)
+ libusb_exit(context);
}
/** Writes the actual data contained in the device's data_out field to the usb endpoint.
* @return K8055_ERROR_CLOSED if the board is not open
* @return K8055_ERROR_WRITE if another error occurred during the write process */
static int k8055_write_data(k8055_device* device) {
- int write_status = 0;
-
- if (device->device_handle == NULL) {
- print_error("unable to write data, device not open");
- return K8055_ERROR_CLOSED;
- }
-
- int transferred = 0;
- for(int i=0; i < WRITE_TRIES; ++i) { /* number of tries on failure */
- write_status = libusb_interrupt_transfer(
- device->device_handle,
- USB_OUT_EP,
- (unsigned char *) device->data_out,
- PACKET_LENGTH,
- &transferred,
- USB_TIMEOUT);
- if (write_status == 0 && transferred == PACKET_LENGTH) break;
- }
- if (write_status != 0 || transferred != PACKET_LENGTH) {
- print_error("could not write packet");
- return K8055_ERROR_WRITE;
- }
- return 0;
+ int write_status = 0;
+
+ if (device->device_handle == NULL) {
+ print_error("unable to write data, device not open");
+ return K8055_ERROR_CLOSED;
+ }
+
+ int transferred = 0;
+ for (int i = 0; i < WRITE_TRIES; ++i) { /* number of tries on failure */
+ write_status = libusb_interrupt_transfer(device->device_handle,
+ USB_OUT_EP, (unsigned char *) device->data_out, PACKET_LENGTH,
+ &transferred, USB_TIMEOUT);
+ if (write_status == 0 && transferred == PACKET_LENGTH)
+ break;
+ }
+ if (write_status != 0 || transferred != PACKET_LENGTH) {
+ print_error("could not write packet");
+ return K8055_ERROR_WRITE;
+ }
+ return 0;
}
/** Reads data from the usb endpoint into the device's data_in field.
* @return K8055_ERROR_CLOSED if the board is not open
* @return K8055_ERROR_READ if another error occurred during the read process */
static int read_data(k8055_device* device, int cycles) {
- int read_status = 0;
-
- if (device->device_handle == NULL) {
- print_error("unable to read data, device not open");
- return K8055_ERROR_CLOSED;
- }
-
- int transferred = 0;
- for(int i = 0; i < READ_TRIES; ++i) { /* number of tries on failure */
- for(int j = 0; j < cycles; ++j) { /* read at least twice to get fresh data, (i.e. circumvent some kind of buffer) */
- read_status = libusb_interrupt_transfer(
- device->device_handle,
- USB_IN_EP,
- (unsigned char *) device->data_in,
- PACKET_LENGTH,
- &transferred,
- USB_TIMEOUT);
- }
- if (read_status == 0 && transferred == PACKET_LENGTH) break;
- }
+ int read_status = 0;
+
+ if (device->device_handle == NULL) {
+ print_error("unable to read data, device not open");
+ return K8055_ERROR_CLOSED;
+ }
+
+ int transferred = 0;
+ for (int i = 0; i < READ_TRIES; ++i) { /* number of tries on failure */
+ for (int j = 0; j < cycles; ++j) { /* read at least twice to get fresh data, (i.e. circumvent some kind of buffer) */
+ read_status = libusb_interrupt_transfer(device->device_handle,
+ USB_IN_EP, (unsigned char *) device->data_in, PACKET_LENGTH,
+ &transferred, USB_TIMEOUT);
+ }
+ if (read_status == 0 && transferred == PACKET_LENGTH)
+ break;
+ }
if (read_status != 0 || transferred != PACKET_LENGTH) {
print_error("could not read packet");
return K8055_ERROR_READ;
@@ -330,7 +325,8 @@ int k8055_set_digital(k8055_device* device, int channel, int value) {
unsigned char data = device->data_out[OUT_DIGITAL_OFFSET];
if (value == 0) /* off */
data = data & ~(1 << channel);
- else /* on */
+ else
+ /* on */
data = data | (1 << channel);
device->data_out[OUT_DIGITAL_OFFSET] = data;
@@ -379,10 +375,12 @@ int k8055_reset_counter(k8055_device* device, int counter) {
int k8055_set_debounce_time(k8055_device* device, int counter, int debounce) {
if (counter == 0) {
- device->data_out[OUT_COUNTER_0_DEBOUNCE_OFFSET] = k8055_int_to_debounce(debounce);
+ device->data_out[OUT_COUNTER_0_DEBOUNCE_OFFSET] = k8055_int_to_debounce(
+ debounce);
device->data_out[OUT_CMD_OFFEST] = CMD_SET_DEBOUNCE_1;
} else if (counter == 1) {
- device->data_out[OUT_COUNTER_1_DEBOUNCE_OFFSET] = k8055_int_to_debounce(debounce);
+ device->data_out[OUT_COUNTER_1_DEBOUNCE_OFFSET] = k8055_int_to_debounce(
+ debounce);
device->data_out[OUT_CMD_OFFEST] = CMD_SET_DEBOUNCE_2;
} else {
print_error("can't set debounce time for unknown counter");
@@ -392,15 +390,17 @@ int k8055_set_debounce_time(k8055_device* device, int counter, int debounce) {
return k8055_write_data(device);
}
-int k8055_get_all_input(k8055_device* device, int *bitmask, int *analog0, int *analog1, int *counter0, int *counter1, bool quick) {
+int k8055_get_all_input(k8055_device* device, int *bitmask, int *analog0,
+ int *analog1, int *counter0, int *counter1, bool quick) {
int cycles = 2;
- if (quick) cycles = 1;
+ if (quick)
+ cycles = 1;
int r = read_data(device, cycles);
- if (r != 0) return r;
+ if (r != 0)
+ return r;
if (bitmask != NULL)
- *bitmask = (
- ((device->data_in[IN_DIGITAL_OFFSET] >> 4) & 0x03) | /* Input 1 and 2 */
+ *bitmask = (((device->data_in[IN_DIGITAL_OFFSET] >> 4) & 0x03) | /* Input 1 and 2 */
((device->data_in[IN_DIGITAL_OFFSET] << 2) & 0x04) | /* Input 3 */
((device->data_in[IN_DIGITAL_OFFSET] >> 3) & 0x18)); /* Input 4 and 5 */
if (analog0 != NULL)
@@ -408,8 +408,10 @@ int k8055_get_all_input(k8055_device* device, int *bitmask, int *analog0, int *a
if (analog1 != NULL)
*analog1 = device->data_in[IN_ANALOG_1_OFFSET];
if (counter0 != NULL)
- *counter0 = (int) device->data_in[IN_COUNTER_0_OFFSET + 1] << 8 | device->data_in[IN_COUNTER_0_OFFSET];
+ *counter0 = (int) device->data_in[IN_COUNTER_0_OFFSET + 1] << 8
+ | device->data_in[IN_COUNTER_0_OFFSET];
if (counter1 != NULL)
- *counter1 = (int) device->data_in[IN_COUNTER_1_OFFSET + 1] << 8 | device->data_in[IN_COUNTER_1_OFFSET];
+ *counter1 = (int) device->data_in[IN_COUNTER_1_OFFSET + 1] << 8
+ | device->data_in[IN_COUNTER_1_OFFSET];
return 0;
}
diff --git a/src/k8055.h b/src/k8055.h
index d287098..75f379c 100644
--- a/src/k8055.h
+++ b/src/k8055.h
@@ -38,9 +38,7 @@ extern "C" {
typedef struct k8055_device k8055_device;
enum k8055_error_code {
- K8055_SUCCESS = 0,
- K8055_ERROR = -1,
- K8055_ERROR_INIT_LIBUSB = -2, /* error during libusb initialization */
+ K8055_SUCCESS = 0, K8055_ERROR = -1, K8055_ERROR_INIT_LIBUSB = -2, /* error during libusb initialization */
K8055_ERROR_NO_DEVICES = -3, /* no usb devices found on host machine */
K8055_ERROR_NO_K8055 = -4, /* Velleman k8055 cannot be found (on given port) */
K8055_ERROR_ACCESS = -6, /* access denied (insufficient permissions) */
@@ -125,7 +123,8 @@ int k8055_set_debounce_time(k8055_device*, int counter, int debounce);
* @return 0 on success
* @return K8055_ERROR_CLOSED if the given device is not open
* @return K8055_ERROR_READ if another error occurred during the read process */
-int k8055_get_all_input(k8055_device*, int *digitalBitmask, int *analog0, int *analog1, int *counter0, int *counter1, bool quick);
+int k8055_get_all_input(k8055_device*, int *digitalBitmask, int *analog0,
+ int *analog1, int *counter0, int *counter1, bool quick);
#ifdef __cplusplus
}