forked from therandomfactory/speckle-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoriel_tcl.cpp
206 lines (163 loc) · 5.22 KB
/
oriel_tcl.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* \file oriel_tcl.cpp
* \brief Tcl wrapper for Oriel filter wheel control
*
*/
/*
This file contains the tcl interface routine to high level API for Nessi Oriel filter wheel
Author : Dave Mills (The Random Factory)
Date : July 7th 2017
This Source Code Form is subject to the terms of the GNU Public
License, v. 2.1. If a copy of the GPL was not distributed with this file,
You can obtain one at https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
Copyright(c) 2017 The Random Factory (www.randomfactory.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tcl.h"
#include "tk.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdint.h>
#include "GenOneLinuxUSB.h"
extern "C" {
/**
* \brief ClientData Tcl handle
* \param Tcl_Interp interpreter pointer
* \param argc Argument count
* \param arcv Arguments
*
*/
int tcl_oriel_connect(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
/**
* \brief ClientData Tcl handle
* \param Tcl_Interp interpreter pointer
* \param argc Argument count
* \param arcv Arguments
*
*/
int tcl_oriel_write_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
/**
* \brief ClientData Tcl handle
* \param Tcl_Interp interpreter pointer
* \param argc Argument count
* \param arcv Arguments
*
*/
int tcl_oriel_read_result(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
/**
* \brief ClientData Tcl handle
* \param Tcl_Interp interpreter pointer
* \param argc Argument count
* \param arcv Arguments
*
*/
int tcl_oriel_disconnect(ClientData clientData, Tcl_Interp *interp, int argc, char **argv);
/**
* \brief Initialize the wrapper commands
* \param Tcl_Interp interpreter pointer
*
*/
int orielAppInit(Tcl_Interp *interp);
char *result;
GenOneLinuxUSB *filterWheel[3];
//GenOneLinuxUSB filterWheelA = GenOneLinuxUSB(1);
//GenOneLinuxUSB filterWheelB = GenOneLinuxUSB(2);
int tcl_oriel_connect(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
int status=0;
int id=0;
int ubus,uaddr;
result = malloc(256);
/* Check number of arguments provided and return an error if necessary */
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",argv[0]," id\"", (char *)NULL);
return TCL_ERROR;
}
sscanf(argv[1],"%d",&id);
filterWheel[id] = (GenOneLinuxUSB *) new GenOneLinuxUSB(id);
if (filterWheel[id]->m_Device == 0) {
sprintf(result,"USB Error: Failed to open wheel - %d",id);
Tcl_SetResult(interp,result,TCL_STATIC);
return TCL_ERROR;
}
ubus = filterWheel[id]->GetBus();
uaddr = filterWheel[id]->GetAddress();
sprintf(result,"%d %3.3d:%3.3d",filterWheel[id]->m_Device,ubus,uaddr);
Tcl_SetResult(interp,result,TCL_STATIC);
return TCL_OK;
}
int tcl_oriel_write_cmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
int status=0;
int handle;
char command[128];
/* Check number of arguments provided and return an error if necessary */
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",argv[0]," id command\"", (char *)NULL);
return TCL_ERROR;
}
sscanf(argv[1], "%d", &handle);
sscanf(argv[2], "%s", &command);
filterWheel[handle]->write_cmd(command);
if (status < 0) {
sprintf(result,"Failed to send command - %d",status);
Tcl_SetResult(interp,result,TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
}
int tcl_oriel_read_result(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
int status=0;
int handle;
char reply[128];
/* Check number of arguments provided and return an error if necessary */
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",argv[0]," id\"", (char *)NULL);
return TCL_ERROR;
}
sscanf(argv[1], "%d", &handle);
status = filterWheel[handle]->read_result(reply,128);
if (status <= 0) {
sprintf(result,"Failed to get reply - %d",status);
Tcl_SetResult(interp,result,TCL_STATIC);
return TCL_ERROR;
}
Tcl_AppendResult(interp, reply, (char *)NULL);
return TCL_OK;
}
int tcl_oriel_disconnect(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
int status=0;
int handle;
/* Check number of arguments provided and return an error if necessary */
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",argv[0]," id\"", (char *)NULL);
return TCL_ERROR;
}
sscanf(argv[1], "%d", &handle);
delete &filterWheel[handle];
return TCL_OK;
}
/* Routine : orielAppInit
Purpose : This routine is called from tclAppInit and initializes all the oriel
tcl interface routines
*/
int orielAppInit(Tcl_Interp *interp)
{
/* Initialize the new commands */
Tcl_CreateCommand(interp, "oriel_connect", (Tcl_CmdProc *) tcl_oriel_connect, NULL, NULL);
Tcl_CreateCommand(interp, "oriel_disconnect", (Tcl_CmdProc *) tcl_oriel_disconnect, NULL, NULL);
Tcl_CreateCommand(interp, "oriel_write_cmd", (Tcl_CmdProc *) tcl_oriel_write_cmd, NULL, NULL);
Tcl_CreateCommand(interp, "oriel_read_result", (Tcl_CmdProc *) tcl_oriel_read_result, NULL, NULL);
return TCL_OK;
}
}