Browse Source

adding 'joystick->osc' soft

tth 1 month ago
parent
commit
03bdd5ab88
4 changed files with 159 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 3
    0
      Makefile
  3. 12
    0
      doc/gadgets-osc.tex
  4. 143
    0
      osc-joy.c

+ 1
- 0
.gitignore View File

@@ -3,6 +3,7 @@ MANIFEST
3 3
 tarball
4 4
 
5 5
 osc2cursor
6
+osc-joy
6 7
 
7 8
 doc/*.toc
8 9
 doc/*.log

+ 3
- 0
Makefile View File

@@ -9,4 +9,7 @@ OPTS = -Wall -g -DDEBUG_LEVEL=1
9 9
 osc2cursor:        osc2cursor.c ${DEPS} functions/libpocosc.a 
10 10
 	gcc ${OPTS} $< functions/libpocosc.a -llo -lcurses -o $@
11 11
 
12
+osc-joy:        osc-joy.c ${DEPS} functions/libpocosc.a 
13
+	gcc ${OPTS} $< functions/libpocosc.a -llo -o $@
14
+
12 15
 # ----------------------------------------------------

+ 12
- 0
doc/gadgets-osc.tex View File

@@ -31,6 +31,18 @@ De quoi parle-t-on exactement ?
31 31
 
32 32
 % -------------------------------------------------------------------
33 33
 
34
+\subsection{osc-joy}
35
+
36
+\begin{verbatim}
37
+         * joystick -> osc       Aug  2 2019 *
38
+        -r      remote host (localhost)
39
+        -p      remote UDP port (9000)
40
+        -j      joystick device (/dev/input/js0)
41
+        -v      increase verbosity
42
+        -o      offset added to button number
43
+        -I      change text id ("suck my stick")
44
+\end{verbatim}
45
+
34 46
 \pagebreak
35 47
 \tableofcontents
36 48
 \printindex

+ 143
- 0
osc-joy.c View File

@@ -0,0 +1,143 @@
1
+/*
2
+ *   Pour compiler :
3
+ *     gcc -Wall osc-joy.c -llo -o osc-joy
4
+ *
5
+ *   Options :
6
+ *        -j joydevice			default /dev/input/js0
7
+ *        -h destination (or IP addr)	default localhost 
8
+ *        -p destport			default is 9000
9
+ *        -o nnn			offset for button number
10
+ */  
11
+
12
+
13
+#include  <fcntl.h>
14
+#include  <stdio.h>
15
+#include  <stdlib.h>
16
+#include  <unistd.h>
17
+#include  <string.h>
18
+#include  <sys/ioctl.h>
19
+
20
+#include <linux/joystick.h>
21
+#include <lo/lo.h>
22
+
23
+#include  "functions/senders.h"
24
+
25
+/*   default values, can be changed on command line */
26
+
27
+#define REMOTE_HOST	"localhost"		/* just loling */
28
+#define REMOTE_PORT	"9000"
29
+#define	JOY_DEVICE	"/dev/input/js0"
30
+#define MY_TEXT_ID	"suck my stick"
31
+
32
+/* ----------------------------------------------------------------- */
33
+int		verbosity = 0;
34
+int		button_offset = 0;
35
+
36
+char		*my_id = MY_TEXT_ID;
37
+
38
+/* ----------------------------------------------------------------- */
39
+static void help(int k)
40
+{
41
+puts("\t * joystick -> osc       "__DATE__" *");
42
+puts("\t-r\tremote host ("REMOTE_HOST")");
43
+puts("\t-p\tremote UDP port ("REMOTE_PORT")");
44
+puts("\t-j\tjoystick device ("JOY_DEVICE")");
45
+puts("\t-v\tincrease verbosity");
46
+puts("\t-o\toffset added to button number");
47
+puts("\t-I\tchange text id (\""MY_TEXT_ID"\")");
48
+exit(0);
49
+}
50
+/* ----------------------------------------------------------------- */
51
+int main(int argc, char *argv[])
52
+{
53
+int		foo, joy_fd;
54
+struct js_event js;
55
+int		x_pos, y_pos, flag;
56
+char		joy_name[128];
57
+int		opt;
58
+char		*remote_host = REMOTE_HOST;
59
+char		*remote_port = REMOTE_PORT;
60
+char		*joy_device = JOY_DEVICE;
61
+
62
+/* parsing command line options */
63
+while ((opt = getopt(argc, argv, "hp:r:vj:o:I:")) != -1) {
64
+	switch (opt) {
65
+		case 'h':	help(0);		break;
66
+		case 'r':	remote_host = optarg;	break;
67
+		case 'p':	remote_port = optarg;	break;
68
+		case 'j':	joy_device = optarg;	break;
69
+		case 'v':	verbosity++;		break;
70
+		case 'o':	button_offset = atoi(optarg);
71
+				break;
72
+		case 'I':	my_id = optarg;		break;
73
+		default:	exit(1);		break;
74
+		}
75
+	}
76
+
77
+if (verbosity) {
78
+	fprintf(stderr, "%s is sending to %s:%s\n", argv[0],
79
+			remote_host, remote_port);
80
+	fprintf(stderr, "  the stick '%s'is on %s\n", my_id, joy_device);
81
+	} 
82
+
83
+lo_address t =  lo_address_new(remote_host, remote_port);
84
+
85
+if( ( joy_fd = open(joy_device , O_RDONLY)) == -1 ) {
86
+	fprintf(stderr, "Couldn't open %s\n", JOY_DEVICE);
87
+	exit(1);
88
+	}
89
+
90
+if (verbosity) {
91
+	if (ioctl(joy_fd, JSIOCGNAME(sizeof(joy_name)), joy_name) < 0)
92
+		strncpy(joy_name, "Unknown", sizeof(joy_name));
93
+	fprintf(stderr, "Name: %s\n", joy_name);
94
+	}
95
+
96
+send_data_id(t, my_id);
97
+
98
+x_pos = y_pos = 0;
99
+for (;;) {
100
+	foo = read(joy_fd, &js, sizeof(struct js_event));
101
+	if (8 != foo) {
102
+		fprintf(stderr, "err reading joy\n");
103
+		exit(1);
104
+		}
105
+
106
+	/* calibration datas ignored */
107
+	if (js.type > 128)	continue;
108
+
109
+	if (verbosity>1) {	
110
+		fprintf(stderr, "%10u  %2d  %2d %7d\n",
111
+			js.time, js.type, js.number, js.value);
112
+		}
113
+			
114
+	if (2==js.type)	{		/* oscillating stick */
115
+		flag = 0;
116
+		switch (js.number)
117
+			{
118
+			case 0:
119
+				x_pos = js.value;
120
+				flag = 1;
121
+				break;
122
+			case 1:
123
+				y_pos = js.value;
124
+				flag = 1;
125
+				break;
126
+			}
127
+				
128
+		if (flag) {
129
+			foo = send_data_xy(t, x_pos, y_pos);
130
+			flag = 0;
131
+			}			
132
+		}
133
+
134
+	if (1==js.type)	{		/* it's a button */
135
+		foo = send_data_button(t, js.number+button_offset,
136
+						 js.value);
137
+		}
138
+		
139
+	}
140
+	
141
+return 0;
142
+}
143
+/* ----------------------------------------------------------------- */

Loading…
Cancel
Save