-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
71 lines (42 loc) · 1.41 KB
/
io.c
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
// Wrapper for libComedi I/O.
// These functions provide and interface to libComedi limited to use in
// the real time lab.
//
// 2006, Martin Korsgaard
#include "io.h"
#include "channels.h"
#include <comedilib.h>
static comedi_t *it_g = NULL;
int io_init() {
int i = 0;
int status = 0;
it_g = comedi_open("/dev/comedi0");
if (it_g == NULL)
return 0;
for (i = 0; i < 8; i++) {
status |= comedi_dio_config(it_g, PORT1, i, COMEDI_INPUT);
status |= comedi_dio_config(it_g, PORT2, i, COMEDI_OUTPUT);
status |= comedi_dio_config(it_g, PORT3, i + 8, COMEDI_OUTPUT);
status |= comedi_dio_config(it_g, PORT4, i + 16, COMEDI_INPUT);
}
return (status == 0);
}
void io_set_bit(int channel) {
comedi_dio_write(it_g, channel >> 8, channel & 0xff, 1);
}
void io_clear_bit(int channel) {
comedi_dio_write(it_g, channel >> 8, channel & 0xff, 0);
}
void io_write_analog(int channel, int value) {
comedi_data_write(it_g, channel >> 8, channel & 0xff, 0, AREF_GROUND, value);
}
int io_read_bit(int channel) {
unsigned int data = 0;
comedi_dio_read(it_g, channel >> 8, channel & 0xff, &data);
return (int)data;
}
int io_read_analog(int channel) {
lsampl_t data = 0;
comedi_data_read(it_g, channel >> 8, channel & 0xff, 0, AREF_GROUND, &data);
return (int)data;
}