File: my_simulation.c

package info (click to toggle)
dxsamples 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 26,340 kB
  • sloc: ansic: 10,079; sh: 8,445; java: 1,772; makefile: 1,102
file content (70 lines) | stat: -rw-r--r-- 1,418 bytes parent folder | download | duplicates (5)
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
/*
 * sample "simulation" program which outputs results (x y values) on
 * a socket, which will be read by the program m_WatchSocket. See also
 * watchsocket.c
 */

#include <dx/dx.h> 
#include <stdio.h> 
#include <math.h>

extern Error CreateSocket(int *, short);


main(argc, argv)
   int argc;
   char *argv[];
{
  int     fd; 
  float   a, b, c, d, stepsize, i;
  int     numbytes_data;
  int     written;
  short   port = 2000;
  float   x, y, buffer[2], start, end;
  char    *message;

  start = 0;
  end = 100;

  /* 
   * grab the parameters off of the command line and convert to 
   * floating point numbers 
   */
  a = atof(argv[1]);
  b = atof(argv[2]);
  c = atof(argv[3]);
  d = atof(argv[4]);
  stepsize = atof(argv[5]);
  
  /* set up the socket */
  if (!CreateSocket(&fd, port)) {
     message = DXGetErrorMessage();     
     fprintf(stderr,message);
     exit(0);
  }

  /* 
   * number of bytes written each time for the data 
   * (two floating point numbers) 
   */ 
  numbytes_data = 2*(sizeof(float));


  /* run the simulation */
  for (i=start; i< end; i+=stepsize) {
    x = sin(a*i + c);
    y = sin(b*i + d);
    buffer[0] = x;
    buffer[1] = y;
    written = write(fd, buffer, numbytes_data);
    if (written < 0) {
      fprintf(stderr,"failure to write to socket\n");
      exit(0);
    }
  
    /* sleep for one second */ 
    sleep(1);
  }
  /* close the socket */
  close(fd);
}