aboutsummaryrefslogtreecommitdiff
path: root/src/k8055.h
blob: 3b42a1cc073f3bdea7b7f0650b3836ed8a7ac031 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* k8055 driver for libusb-1.0

 Copyright (c) 2012 by Jakob Odersky
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef K8055_H_
#define K8055_H_

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

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_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) */
	K8055_ERROR_OPEN = -7, /* error opening device handle (also applies for claiming and detaching kernel driver) */
	K8055_ERROR_CLOSED = -8, /* device is already closed */
	K8055_ERROR_WRITE = -9, /* write error */
	K8055_ERROR_READ = -10, /* read error */
	K8055_ERROR_INDEX = -11, /* invalid argument (i.e. trying to access analog channel >= 2) */
	K8055_ERROR_MEM = -12 /* memory allocation error */
};

/**Opens a K8055 device on the given port (i.e. address).
 * @return 0 on success
 * @return K8055_ERROR_INDEX if port is an invalid index
 * @return K8055_ERROR_INIT_LIBUSB on libusb initialization error
 * @return K8055_ERROR_NO_DEVICES if no usb devices are found on host system
 * @return K8055_ERROR_NO_K8055 if no K8055 board is found at the given port
 * @return K8055_ERROR_ACCESS if permission is denied to access a usb port
 * @return K8055_ERROR_OPEN if another error occured preventing the board to be opened */
int k8055_open_device(int port, k8055_device** device);

/** Closes the given device. */
void k8055_close_device(k8055_device* device);

/**Sets all digital ouputs according to the given bitmask.
 * @param device k8055 board
 * @param bitmask '1' for 'on', '0' for 'off'
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_all_digital(k8055_device* device, int bitmask);

/**Sets a digital output at given channel.
 * @param device k8055 board
 * @param channel channel of port
 * @param value output status: 'true' for on, 'false' for off
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_digital(k8055_device* device, int channel, bool value);

/**Sets the values of both analog outputs.
 * @param device k8055 board
 * @param analog0 value of first analog output
 * @param analog1 value of second analog output
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_all_analog(k8055_device* device, int analog0, int analog1);

/**Sets the value for an analog output at a given channel.
 * @param device k8055 board
 * @param channel channel of analog output (zero indexed)
 * @param value value of analog output [0-255]
 * @return K8055_ERROR_INDEX if channel is an invalid index
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_analog(k8055_device* device, int channel, int value);

/**Resets a hardware integrated counter of the Velleman K8055 board.
 * @param device k8055 board
 * @param counter index of counter (zero indexed)
 * @return K8055_ERROR_INDEX if counter is an invalid index
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_reset_counter(k8055_device* device, int counter);

/**Sets the debounce time of a hardware integrated counter of the Velleman K8055 board.
 * @param device k8055 board
 * @param counter index of counter (zero indexed)
 * @param debounce debounce value
 * @return K8055_ERROR_INDEX if counter is an invalid index
 * @return K8055_ERROR_CLOSED if the given device is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process*/
int k8055_set_debounce_time(k8055_device* device, int counter, int debounce);

/**Reads all current data of a given board into the passed parameters. NULL is a valid parameter.
 * Unless quick is set, data is read twice from the board to circumvent some kind of buffer and get current data.
 * @param device k8055 board
 * @param digitalBitmask bitmask value of digital inputs (there are 5 digital inputs)
 * @param analog0 value of first analog input
 * @param analog1 value of second analog input
 * @param counter0 value of first counter
 * @param counter1 value of second counter
 * @param quick if set, read data only once
 * @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* device, int *digitalBitmask, int *analog0,
		int *analog1, int *counter0, int *counter1, bool quick);
		
/**Gets a given board's current output status. NULL is a valid parameter.
 * Note: as the K8055's firmware does not provide any method for querying the board's output status,
 * this library only tracks the board's status by recording any successfull data writes.
 * Hence no guarantee can be given on the validity of the output status.
 * @param device k8055 board
 * @param digitalBitmask bitmask value of digital outputs (there are 8 digital outputs)
 * @param analog0 value of first analog output
 * @param analog1 value of second analog output
 * @param debounce0 value of first counter's debounce time [ms]
 * @param debounce1 value of second counter's debounce time [ms] */
void k8055_get_all_output(k8055_device* device, int* digitalBitmask, int *analog0,
		int *analog1, int *debounce0, int *debounce1);

#ifdef __cplusplus
}
#endif 

#endif /* K8055_H_ */