PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Modules/FSGui/FSData/main.cpp

https://bitbucket.org/AIBMI/trodes
C++ | 228 lines | 153 code | 32 blank | 43 comment | 22 complexity | 9afa6864a65479ac203e993f4e9abf5a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * fsdata.cpp: Program for getting data from trodes for
  3. * realtime processing
  4. *
  5. *
  6. *
  7. * Copyright 2014 Loren M. Frank
  8. *
  9. * This program is part of the nspike data acquisition package.
  10. * nspike is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * nspike is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with nspike; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <spikecommon.h>
  25. #include <spike_fs_defines.h>
  26. /* function definitions that allow us to use spike_dsp_shared.h */
  27. void DisplayErrorMessage(char *);
  28. void DisplayStatusMessage(char *);
  29. char tmpstring[200];
  30. SocketInfo server_message[MAX_CONNECTIONS]; // the structure for the server messaging
  31. SocketInfo client_message[MAX_CONNECTIONS]; // the structure for the client messaging
  32. SocketInfo server_data[MAX_CONNECTIONS]; // the structure for receiving data
  33. SocketInfo client_data[MAX_CONNECTIONS]; // the structure for sendin data (currently unused)
  34. #include "spike_dsp_shared.h"
  35. #include "../src-main/spike_dsp_shared.cpp"
  36. void fsdataexit(int status);
  37. void Usage(void);
  38. int main(int argc, char **argv)
  39. {
  40. char savebuf[SAVE_BUF_SIZE];
  41. int savebufsize;
  42. short datatype;
  43. int nspikes;
  44. int maxfds;
  45. fd_set readfds;
  46. char tmpstring[100]; // a temporary buffer
  47. char filename[100];
  48. int message; // a temporary message variable
  49. int messagedata[MAX_BUFFER_SIZE]; // message data can contain a sysinfo or channelinfo structure
  50. int messagedatalen; // the length of the data in the message
  51. int i, id, j, nxtarg;
  52. struct timeval tval, lasttval;
  53. struct timezone tzone;
  54. int fd;
  55. int datalen;
  56. int nwritten;
  57. int totalwritten;
  58. int writesize;
  59. SysInfo *systmp;
  60. u32 *u32ptr;
  61. sysinfo.program_type = SPIKE_FS_DATA;
  62. fprintf(STATUSFILE, "spike_fsdata: starting\n");
  63. sysinfo.statusfile == NULL;
  64. if (STATUSFILE == NULL) {
  65. /* open up the status file if it is not stderr*/
  66. gethostname(tmpstring,80);
  67. sprintf(filename, "spike_fsdata_status_%s", tmpstring);
  68. if ((STATUSFILE = fopen(filename, "w")) == NULL) {
  69. fprintf(stderr, "spike_fsdata: error opening status file\n");
  70. exit(-1);
  71. }
  72. }
  73. /* process the arguments */
  74. sysinfo.acq = 0;
  75. sysinfo.fsdataon = 0;
  76. if (StartNetworkMessaging(server_message, client_message, server_data,
  77. client_data) < 0) {
  78. fprintf(STATUSFILE, "spike_fsdata: Error starting network data messaging\n");
  79. fsdataexit(1);
  80. }
  81. /* get the current time */
  82. gettimeofday(&lasttval, &tzone);
  83. while (1) {
  84. /* set up the initial list of file descriptors to watch */
  85. SetupFDList(&readfds, &maxfds, server_message, server_data);
  86. select(maxfds, &readfds, NULL, NULL, NULL);
  87. id = 0;
  88. /* check for incoming data. Note that we save all of the incoming data
  89. * to the data buffer, as the individual programs that send us data
  90. * have already filtered it */
  91. while ((i = netinfo.datainfd[id]) != -1) {
  92. if (FD_ISSET(server_data[i].fd, &readfds)) {
  93. message = GetMessage(server_data[i].fd, savebuf, &savebufsize,
  94. 1);
  95. if (sysinfo.fsdataon) {
  96. switch(i) {
  97. case SPIKE_DAQ:
  98. if (message == SPIKE_DATA) {
  99. datatype = SPIKE_DATA_TYPE;
  100. /* get the number of each electrode and increment
  101. * the number of spikes on them
  102. nspikes = savebufsize / sizeof(SpikeBuffer);
  103. sptr = (FSDataSpikeBuffer *) savebuf; */
  104. }
  105. else if (message == CONTINUOUS_DATA) {
  106. datatype = CONTINUOUS_DATA_TYPE;
  107. }
  108. else if (message == DIGITALIO_EVENT) {
  109. datatype = DIGITALIO_DATA_TYPE;
  110. }
  111. break;
  112. case SPIKE_POSDAQ:
  113. datatype = POSITION_DATA_TYPE;
  114. break;
  115. }
  116. ProcessData(datatype, savebuf, savebufsize);
  117. }
  118. }
  119. id++;
  120. }
  121. id = 0;
  122. /* check for messages */
  123. while ((i = netinfo.messageinfd[id]) != -1) {
  124. if (FD_ISSET(server_message[i].fd, &readfds)) {
  125. message = GetMessage(server_message[i].fd,
  126. (char *) messagedata, &messagedatalen, 0);
  127. switch(message) {
  128. case STOP_ACQUISITION:
  129. sysinfo.acq = 0;
  130. SendMessage(client_message[SPIKE_MAIN].fd, ACQUISITION_STOPPED,
  131. NULL, 0);
  132. break;
  133. case START_ACQUISITION:
  134. sysinfo.acq = 1;
  135. SendMessage(client_message[SPIKE_MAIN].fd, ACQUISITION_STARTED, NULL, 0);
  136. break;
  137. case FS_DATA_INFO:
  138. /* get the fsdatainfo structure */
  139. memcpy((char *) &fsdatainfo, messagedata,
  140. sizeof(FSDataInfo));
  141. /* we also need to reset any relevant variables */
  142. ResetFSStatus();
  143. break;
  144. case FS_DATA_START:
  145. sysinfo.fsdataon = 1;
  146. break;
  147. case FS_DATA_STOP:
  148. sysinfo.fsdataon = 0;
  149. /* move on to the next buffer so that we can start
  150. * fresh */
  151. break;
  152. case SYSTEM_INFO:
  153. systmp = (SysInfo *) messagedata;
  154. sysinfo.maxelectnum = systmp->maxelectnum;
  155. break;
  156. case DIGIO_INFO:
  157. /* copy the DigIOInfo structure */
  158. memcpy(&digioinfo, messagedata, sizeof(DigIOInfo));
  159. break;
  160. case EXIT:
  161. fsdataexit(0);
  162. break;
  163. default:
  164. /* Send the message on to the ProcessMessage function */
  165. ProcessMessage(message, (char *) messagedata, messagedatalen);
  166. break;
  167. }
  168. }
  169. id++;
  170. }
  171. }
  172. return 0;
  173. }
  174. void fsdataexit(int status)
  175. {
  176. SendMessage(client_message[SPIKE_MAIN].fd, EXITING, NULL, 0);
  177. /* sleep so that all of the other programs have a chance to get the message*/
  178. sleep(1);
  179. CloseSockets(server_message);
  180. CloseSockets(client_message);
  181. CloseSockets(server_data);
  182. fclose(STATUSFILE);
  183. exit(status);
  184. }
  185. void DisplayErrorMessage(char *message)
  186. {
  187. fprintf(stderr, "spike_fsdata error: %s\n", message);
  188. }
  189. void DisplayStatusMessage(char *message)
  190. {
  191. fprintf(stderr, "spike_fsdata status: %s\n", message);
  192. }
  193. #include <stdio.h>
  194. int main(int argc, char *argv[])
  195. {
  196. }