The VisAO Camera
libvisaoc.c
Go to the documentation of this file.
1 /************************************************************
2 * libvisaoc.c
3 *
4 * Author: Jared R. Males (jrmales@as.arizona.edu)
5 *
6 * VisAO software utilitites, definitions
7 *
8 ************************************************************/
9 
10 /** \file libvisaoc.c
11  * \author Jared R. Males
12  * \brief VisAO software utilitites, definitions in c
13  *
14 */
15 
16 #include "libvisao.h"
17 
18 void fifo_error_message(const char *msg, const char *file, int line)
19 {
20  (*global_error_report)(msg, file, line);
21 }
22 
23 int get_dio_fnames(char *fout, char *fin, char *fbase, int ch)
24 {
25  snprintf(fout, MAX_FNAME_SZ, "%s_out_%02i", fbase, ch);
26  snprintf(fin, MAX_FNAME_SZ, "%s_in_%02i", fbase, ch);
27  return 0;
28 }
29 
30 double ts_to_curr_time(struct timespec *tsp)
31 {
32  return ((double)tsp->tv_sec) + ((double)tsp->tv_nsec)/1e9;
33 }
34 
35 double tv_to_curr_time(struct timeval *tvp)
36 {
37  return ((double)tvp->tv_sec) + ((double)tvp->tv_usec)/1e6;
38 }
39 
40 double get_curr_time()
41 {
42  struct timespec tsp;
43  clock_gettime(CLOCK_REALTIME, &tsp);
44 
45  return ((double)tsp.tv_sec) + ((double)tsp.tv_nsec)/1e9;
46 }
47 
48 
49 int create_shmem(int * shmemid, key_t mkey, size_t sz)
50 {
51  char oss[256];
52  static int reported = 0;
53 
54  if((*shmemid = shmget(mkey, sz, IPC_CREAT | 0666))<0)
55  {
56  //If it failed, try to remove the shmem block and recreate it.
57  *shmemid = shmget(mkey, 1, 0666);
58  if(shmctl(*shmemid, IPC_RMID, 0) < 0)
59  {
60  if(!reported)
61  {
62  snprintf(oss, 256, "Could not remove shared memory with key %i.", mkey);
63  global_error_report(oss, __FILE__, __LINE__);
64  }
65  reported = 1;
66  return -1;
67  }
68  snprintf(oss, 256, "Removed shared memory with key %i.", mkey);
69  global_log_info(oss);
70 
71 
72  if((*shmemid = shmget(mkey, sz, IPC_CREAT | 0666))<0)
73  {
74  if(!reported)
75  {
76  snprintf(oss, 256, "Could not create shared memory with key %i.", mkey);
77  global_error_report(oss, __FILE__, __LINE__);
78  }
79  reported = 1;
80  return -1;
81  }
82  }
83 
84  snprintf(oss, 256, "Shared memory created with key %i.", mkey);
85  global_log_info(oss);
86 
87  return 0;
88 }
89 
90 void * attach_shm(size_t *sz, key_t mkey, int shmemid)
91 {
92  char oss[256];
93  void * shmemptr;
94  static int reported = 0;
95 
96  struct shmid_ds shmstats;
97 
98  if(shmemid == 0)
99  {
100  if((shmemid = shmget(mkey, 0, 0666))<0)
101  {
102  if(!reported)
103  {
104  snprintf(oss, 256, "Could not get shared memory with key %i.", mkey);
105  global_error_report(oss, __FILE__, __LINE__);
106  }
107  reported = 1;
108  return 0;
109  }
110  }
111 
112  if ((shmemptr = shmat(shmemid, 0, 0)) == (char *) -1)
113  {
114  if(!reported)
115  {
116  snprintf(oss, 256, "Could not attach shared memory with key %i.", mkey);
117  global_error_report(oss, __FILE__, __LINE__);
118  }
119  reported = 1;
120  return 0;
121  }
122 
123  if (shmctl(shmemid, IPC_STAT, &shmstats) < 0)
124  {
125  if(!reported)
126  {
127  snprintf(oss, 256, "Could not get shared memory stats with key %i.", mkey);
128  global_error_report(oss, __FILE__, __LINE__);
129  }
130  reported = 1;
131  return 0;
132  }
133 
134  *sz = shmstats.shm_segsz;
135 
136  snprintf(oss, 256, "Attached to shared memory with key %i of size %zu.", mkey , *sz);
137  global_log_info(oss);
138 
139  return shmemptr;
140 }
141 
142 double ComputeFramerate(double delay_base, double delay_inc, int rep)
143 {
144  return(1.0e6/ (delay_base + delay_inc * rep));
145 }
146 
147 int ComputeRepsFrameRate(double delay_base, double delay_inc, double fr)
148 {
149  int rep;
150 
151  rep = ((1.0e6/fr) - delay_base)/delay_inc;
152 
153  return rep;
154 }
155 
156 int ComputeRepsExpTime(double delay_base, double delay_inc, double et)
157 {
158  return ComputeRepsFrameRate(delay_base, delay_inc, 1./et);
159 }
160 
161 int sigproof_sleep(double secs, int *dienow)
162 {
163  double dt, t0, trem;
164  int dienow_tmp = 1;
165 
166  if(dienow == 0) dienow = &dienow_tmp;
167 
168  t0 = get_curr_time();
169 
170  while((dt = get_curr_time()-t0 < secs) && !(*dienow))
171  {
172  trem = 0.75*(secs-dt);
173  if(trem >= 1.) sleep((int)trem);
174  else usleep((int)(trem*1e6));
175  }
176 
177  return 0;
178 }
double get_curr_time()
Gets the current CLOCK_REALTIME time, returns it in seconds to double precision.
Definition: libvisaoc.c:40
void * attach_shm(size_t *sz, key_t mkey, int shmemid)
Attach to a shared memory buffer and get its size.
Definition: libvisaoc.c:90
double tv_to_curr_time(struct timeval *tvp)
Convert a timeval structure to double seconds.
Definition: libvisaoc.c:35
int get_dio_fnames(char *fout, char *fin, char *fbase, int ch)
Fills in the properly formatted dioserver channel fifo names.
Definition: libvisaoc.c:23
VisAO software utilitites, declarations.
int sigproof_sleep(double secs, int *dienow)
Signal proof sleep function.
Definition: libvisaoc.c:161
double ts_to_curr_time(struct timespec *tsp)
Convert a timespec structure to double seconds.
Definition: libvisaoc.c:30
void(* global_error_report)(const char *, const char *, int)
A global error reporting function, arguments are the report, file, and line.
int create_shmem(int *shmemid, key_t mkey, size_t sz)
Create a shared memory buffer.
Definition: libvisaoc.c:49
void(* global_log_info)(const char *)
A global info logging function.
#define MAX_FNAME_SZ
The maximum allowed filename length.
Definition: fifoutils.h:37
void fifo_error_message(const char *msg, const char *file, int line)
The fifoutils error reporting function.
Definition: libvisaoc.c:18
double ComputeFramerate(double delay_base, double delay_inc, int rep)
Calculate frame rate given Little Joe program parameters.
Definition: libvisaoc.c:142