aboutsummaryrefslogtreecommitdiff
path: root/src/k8055.h
blob: 21f6ae91dfc5d9f8bc82f13c25d9bf6dcba11b87 (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
/* 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_

#ifdef __cplusplus
extern "C" {
#endif

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) */
};

/**Opens a K8055 device on the given port (i.e. address).
 * @return 0 on success
 * @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);

/** Closes a board at the given port. */
void k8055_close_device(int port);

/**Sets all digital ouputs according to the given bitmask.
 * @param port address of board
 * @param bitmask '1' for 'on', '0' for 'off'
 * @return K8055_ERROR_INDEX if port is an invalid index
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_all_digital(int port, int bitmask);

/**Sets a digital output at given channel.
 * @param port address of board
 * @param channel channel of port
 * @param value output status: '1' for 'on', '0' for 'off'
 * @return K8055_ERROR_INDEX if port is an invalid index
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_digital(int port, int channel, int value);

/**Sets the values of both analog outputs.
 * @param port address of board
 * @param analog1 value of first analog output
 * @param analog2 value of second analog output
 * @return K8055_ERROR_INDEX if port is an invalid index
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_set_all_analog(int port, int analog1, int analog2);

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

/**Resets a hardware integrated counter of the Velleman K8055 board.
 * @param port address of board
 * @param counter index of counter (zero indexed)
 * @return K8055_ERROR_INDEX if port or counter are invalid indices
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process */
int k8055_reset_counter(int port, int counter);

/**Sets the debounce time of a hardware integrated counter of the Velleman K8055 board.
 * @param port address of board
 * @param counter index of counter (zero indexed)
 * @param debounce debounce value
 * @return K8055_ERROR_INDEX if port or counter are invalid indices
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_WRITE if another error occurred during the write process*/
int k8055_set_debounce_time(int port, int counter, int debounce);

/**Reads all current data of a given board into the passed parameters. NULL is a valid parameter.
 * Data is read twice from the borad to circumvent some kind of buffer and get actual data.
 * @param port address of board
 * @param digitalBitmask bitmask value of digital inputs
 * @param analog1 value of first analog input
 * @param analog2 value of second analog input
 * @param counter1 value of first counter
 * @param counter2 value of second counter
 * @return 0 on success
 * @return K8055_ERROR_INDEX if port or counter are invalid indices
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_READ if another error occurred during the read process */
int k8055_get_all_input(int port, int *digitalBitmask, int *analog1, int *analog2, int *counter1, int *counter2);

/**Reads all current data of a given board into the passed parameters. NULL is a valid parameter.
 * This function reads data once from the board and no guarantee of actuality is given.
 * @param port address of board
 * @param digitalBitmask bitmask value of digital inputs
 * @param analog1 value of first analog input
 * @param analog2 value of second analog input
 * @param counter1 value of first counter
 * @param counter2 value of second counter
 * @return 0 on success
 * @return K8055_ERROR_INDEX if port or counter are invalid indices
 * @return K8055_ERROR_CLOSED if the board associated to the given port is not open
 * @return K8055_ERROR_READ if another error occurred during the read process */
int k8055_quick_get_all_input(int port, int *digitalBitmask, int *analog1, int *analog2, int *counter1, int *counter2);

#ifdef __cplusplus
}
#endif 

#endif /* K8055_H_ */