Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

319 lines
8.3 KiB

  1. /* Copyright (C) 2011-2013 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* dev.c */
  14. #ifdef HAVE_CONFIG_H
  15. #include "../config.h"
  16. #endif
  17. #ifdef HAVE_COMEDILIB_H
  18. #define _WITH_COMEDILIB
  19. #endif
  20. #include <stdio.h>
  21. #include <string.h>
  22. static int DFL_did=0, DFL_sid=0, DFL_cid=0;
  23. #ifdef _WITH_COMEDILIB
  24. #include <comedilib.h>
  25. static char *subdev_types[]={
  26. "unused",
  27. "analog input",
  28. "analog output",
  29. "digital input",
  30. "digital output",
  31. "digital I/O",
  32. "counter",
  33. "timer",
  34. "memory",
  35. "calibration",
  36. "processor",
  37. "serial digital I/O"
  38. };
  39. static comedi_t *it;
  40. static long n_it = -1;
  41. static char *cmd_src(int src,char *buf)
  42. {
  43. buf[0]=0;
  44. if(src&TRIG_NONE)strcat(buf,"none|");
  45. if(src&TRIG_NOW)strcat(buf,"now|");
  46. if(src&TRIG_FOLLOW)strcat(buf, "follow|");
  47. if(src&TRIG_TIME)strcat(buf, "time|");
  48. if(src&TRIG_TIMER)strcat(buf, "timer|");
  49. if(src&TRIG_COUNT)strcat(buf, "count|");
  50. if(src&TRIG_EXT)strcat(buf, "ext|");
  51. if(src&TRIG_INT)strcat(buf, "int|");
  52. #ifdef TRIG_OTHER
  53. if(src&TRIG_OTHER)strcat(buf, "other|");
  54. #endif
  55. if(strlen(buf)==0){
  56. sprintf(buf,"unknown(0x%08x)",src);
  57. }else{
  58. buf[strlen(buf)-1]=0;
  59. }
  60. return buf;
  61. }
  62. static void probe_max_1chan(comedi_t *it,int s)
  63. {
  64. comedi_cmd cmd;
  65. char buf[100];
  66. printf(" command fast 1chan:\n");
  67. if(comedi_get_cmd_generic_timed(it, s, &cmd, 1, 1)<0){
  68. printf(" not supported\n");
  69. }else{
  70. printf(" start: %s %d\n",
  71. cmd_src(cmd.start_src,buf),cmd.start_arg);
  72. printf(" scan_begin: %s %d\n",
  73. cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
  74. printf(" convert: %s %d\n",
  75. cmd_src(cmd.convert_src,buf),cmd.convert_arg);
  76. printf(" scan_end: %s %d\n",
  77. cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
  78. printf(" stop: %s %d\n",
  79. cmd_src(cmd.stop_src,buf),cmd.stop_arg);
  80. }
  81. }
  82. static void get_command_stuff(comedi_t *it,int s)
  83. {
  84. comedi_cmd cmd;
  85. char buf[100];
  86. if(comedi_get_cmd_src_mask(it,s,&cmd)<0){
  87. printf("not supported\n");
  88. }else{
  89. printf("\n");
  90. printf(" start: %s\n",cmd_src(cmd.start_src,buf));
  91. printf(" scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
  92. printf(" convert: %s\n",cmd_src(cmd.convert_src,buf));
  93. printf(" scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
  94. printf(" stop: %s\n",cmd_src(cmd.stop_src,buf));
  95. probe_max_1chan(it,s);
  96. }
  97. }
  98. static int devOpen(long n)
  99. {
  100. char filename[24];
  101. if (n == n_it) return 1; /* deja fait */
  102. n_it=-1;
  103. sprintf(filename,"/dev/comedi%ld",n);
  104. if (! (it = comedi_open(filename))) {
  105. printf("%s : Error access !\n",filename);
  106. return 0;
  107. }
  108. n_it = n;
  109. return 1;
  110. }
  111. #endif /* for _WITH_COMEDILIB */
  112. /* *********************************************************************/
  113. #include "stackN.h"
  114. #include "dev.h"
  115. #include "err.h"
  116. #define LBUFD 200
  117. static char buf[LBUFD];
  118. void IF_listDev (void)
  119. {
  120. FILE * fd;
  121. int id,ns;
  122. char driver[24], nomdev[24];
  123. if ((fd = fopen("/proc/comedi","r")) == NULL)
  124. printf("No devices !\n");
  125. else {
  126. while (fgets(buf,LBUFD,fd) != NULL) {
  127. ns = -1;
  128. sscanf(buf,"%d: %s %s %d\n",&id,driver,nomdev,&ns);
  129. if (ns != -1)
  130. printf(" %2d : %-20s driver %-20s %4d subdevices\n",id,nomdev,driver,ns);
  131. }
  132. }
  133. }
  134. #ifdef _WITH_COMEDILIB
  135. static void show_subdev(int i, int V)
  136. {
  137. int j, type, chan,n_chans, n_ranges, subdev_flags;
  138. comedi_range *rng;
  139. printf("subdevice %d :",i);
  140. type = comedi_get_subdevice_type(it, i);
  141. printf(" type %d (%s) ",type,subdev_types[type]);
  142. if (type==COMEDI_SUBD_UNUSED) return;
  143. subdev_flags = comedi_get_subdevice_flags(it, i);
  144. printf("- flags 0x%08x\n",subdev_flags);
  145. n_chans=comedi_get_n_channels(it,i);
  146. printf(" nb of channels : %d",n_chans);
  147. if(!comedi_maxdata_is_chan_specific(it,i)){
  148. printf(" - max data value = %lu\n",(unsigned long)comedi_get_maxdata(it,i,0));
  149. } else {
  150. printf("\n max data value: (channel specific)\n");
  151. for(chan=0;chan<n_chans;chan++){
  152. printf(" chan%d: %lu\n",chan,
  153. (unsigned long)comedi_get_maxdata(it,i,chan));
  154. }
  155. }
  156. if (V >= n_chans) {
  157. printf("Error : CId must be < %d !!\n", n_chans);
  158. return;
  159. }
  160. printf(" ranges (Volts) :");
  161. if(!comedi_range_is_chan_specific(it,i)){
  162. n_ranges=comedi_get_n_ranges(it,i,0);
  163. printf(" all chans:");
  164. for(j=0;j<n_ranges;j++){
  165. rng=comedi_get_range(it,i,0,j);
  166. printf(" [%g,%g]",rng->min,rng->max);
  167. }
  168. printf("\n");
  169. } else {
  170. printf("\n");
  171. for (chan=0;chan<n_chans;chan++){
  172. n_ranges=comedi_get_n_ranges(it,i,chan);
  173. printf(" chan%d:",chan);
  174. for (j=0;j<n_ranges;j++){
  175. rng=comedi_get_range(it,i,chan,j);
  176. printf(" [%g,%g]",rng->min,rng->max);
  177. }
  178. printf("\n");
  179. }
  180. }
  181. printf(" command : ");
  182. get_command_stuff(it,i);
  183. }
  184. #endif
  185. void IF_showDev (void)
  186. {
  187. long n;
  188. if (!getParLong(&n)) return;
  189. #ifdef _WITH_COMEDILIB
  190. int i, n_subdev;
  191. if (! devOpen(n)) return;
  192. printf("overall info:\n");
  193. printf(" version code : 0x%06x\n", comedi_get_version_code(it));
  194. printf(" driver name : %s\n", comedi_get_driver_name(it));
  195. printf(" board name : %s\n", comedi_get_board_name(it));
  196. printf(" nb subdevices : %d\n", n_subdev = comedi_get_n_subdevices(it));
  197. for (i = 0; i < n_subdev; i++) {
  198. show_subdev(i,0);
  199. }
  200. #else
  201. messErr(17);
  202. #endif
  203. }
  204. void IF_devShowDflt (void)
  205. {
  206. #ifdef _WITH_COMEDILIB
  207. int n_subdev;
  208. printf("Default : DId=%d, SId=%d CId=%d\n", DFL_did, DFL_sid, DFL_cid);
  209. if (! devOpen(DFL_did)) return;
  210. printf("overall info:\n");
  211. printf(" version code : 0x%06x\n", comedi_get_version_code(it));
  212. printf(" driver name : %s\n", comedi_get_driver_name(it));
  213. printf(" board name : %s\n", comedi_get_board_name(it));
  214. printf(" nb subdevices : %d\n", n_subdev = comedi_get_n_subdevices(it));
  215. if (DFL_sid < n_subdev)
  216. show_subdev(DFL_sid,DFL_cid);
  217. else printf("Error : SId must be < %d !!\n", n_subdev);
  218. #else
  219. messErr(17);
  220. #endif
  221. }
  222. static void dev_read(int did, int sid, int cid)
  223. {
  224. #ifdef _WITH_COMEDILIB
  225. lsampl_t data;
  226. if (! devOpen(did)) return;
  227. if (comedi_data_read(it,sid,cid,0,0,&data) < 0)
  228. messErr(18);
  229. else putLong((long)data);
  230. #else
  231. messErr(17);
  232. #endif
  233. }
  234. void IF_devDflR (void)
  235. {
  236. dev_read(DFL_did, DFL_sid, DFL_cid);
  237. }
  238. void IF_devRead (void)
  239. {
  240. long n, sd, ch;
  241. if (!getParLong(&ch)) return;
  242. if (!getParLong(&sd)) return;
  243. if (!getParLong(&n)) return;
  244. dev_read(n, sd, ch);
  245. }
  246. void IF_devDflt (void)
  247. {
  248. long n, sd, ch;
  249. if (!getParLong(&ch)) return;
  250. if (!getParLong(&sd)) return;
  251. if (!getParLong(&n)) return;
  252. DFL_did=n;
  253. DFL_sid=sd;
  254. DFL_cid=ch;
  255. }
  256. static void dev_write(int did, int sid, int cid, long d)
  257. {
  258. #ifdef _WITH_COMEDILIB
  259. lsampl_t data;
  260. if (! devOpen(did)) return;
  261. data = (lsampl_t)d;
  262. if (comedi_data_write(it,sid,cid,0,0,data) < 0)
  263. messErr(18);
  264. #else
  265. messErr(17);
  266. #endif
  267. }
  268. void IF_devWrite (void)
  269. {
  270. long d, n, sd, ch;
  271. if (!getParLong(&ch)) return;
  272. if (!getParLong(&sd)) return;
  273. if (!getParLong(&n)) return;
  274. if (!getParLong(&d)) return;
  275. dev_write(n, sd, ch, d);
  276. return;
  277. }
  278. void IF_devDflW (void)
  279. {
  280. long d;
  281. if (!getParLong(&d)) return;
  282. dev_write(DFL_did, DFL_sid, DFL_cid, d);
  283. return;
  284. }