File: channel.h

package info (click to toggle)
quickplot 0.10.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 16,640; sh: 11,163; makefile: 395
file content (220 lines) | stat: -rw-r--r-- 5,444 bytes parent folder | download | duplicates (4)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
  Quickplot - an interactive 2D plotter

  Copyright (C) 1998-2011  Lance Arsenault


  This file is part of Quickplot.

  Quickplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  Quickplot is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Quickplot.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <math.h>


#ifndef _QP_DEBUG_H_
#error "You must include qp_debug.h before this file."
#endif



/* QP_CHANNEL_FORM_SERIES 0
   QP_CHANNEL_FORM_FUNC   1

   QP_TYPE_UNKNOWN            0
   QP_TYPE_SHORT              1
   QP_TYPE_INT                2
   .
   .
   etc
   .
   .
   .
   QP_TYPE_MAX
 */


#define ARRAY_LENGTH  (4*1024)

/* a small positive double used to avoid dividing
 * by zero when scaling plot values. */
#define SMALL_DOUBLE  (100.0*DBL_EPSILON)
#define LARGE_DOUBLE  (DBL_MAX/10.0)


struct qp_channel_series
{
  /* TYPE_SHORT or TYPE_INT ... etc type in the arrays */

  /* each element in the qp_dllist is an array of values */
  size_t array_current_index; /* index of the current value
                                 read in the current array */
  size_t array_last_index; /* the index of the last value
                              in the last array */
  void *last_array; /* points to last array */

  /* The copies of this channel will share these arrays.
   * So the larger memory usage is not replicated. */
  struct qp_dllist *arrays; /* list of arrays.  The value
                             * in each list element is just
                             * the array of value_types that
                             * was made with malloc(). */

  int is_increasing;
  int is_decreasing;
  int has_nan; /* has values like +/-NAN or +/-INF */
   
  double min, max; /* for zoom/scale calculations */

  /* We can make copies of qp_channel_series.
   * The qp_channel_series is just a writer/reader of the
   * arrays.  Once you start making copies you can no
   * longer write. */
  int *ref_count;
};



struct qp_channel_func_double
{
  double (*func)(double x);
  double xmax, xmin;
  double ymax, ymin;
};


struct qp_channel
{
  int form;
  int value_type;
  /* A pointer to put extra stuff at */
  void *data;
  uint64_t id;  /* unique id for this channel and all copies */
  union
  {
    struct qp_channel_series series;
    struct qp_channel_func_double func_double;
  };
};

static
inline
int is_good_double(double x)
{
  if(x > -LARGE_DOUBLE && x < LARGE_DOUBLE &&
      x != NAN && x != -NAN)
    return 1;
  return 0;
}


/* return 1 if the value last read is valid and not past
 * a leading or trailing edge. */
static inline
int qp_channel_series_is_reading(qp_channel_t channel)
{
  ASSERT(channel->form == QP_CHANNEL_FORM_SERIES);
  return (qp_dllist_val(channel->series.arrays))?1:0;
}

extern
qp_channel_t qp_channel_create(int channel_form, int value_type);

extern
qp_channel_t qp_channel_linear_create(double start, double step);

/* if orig != NULL this returns a channel that points to the
 * same data as orig.  After a copy is made you may not write
 * to the channel any more. */

extern
qp_channel_t qp_channel_func_double_create(double (*func)(double),
    double xmin, double xmax, double ymin, double ymax);


static inline
qp_channel_t qp_channel_series_create(qp_channel_t orig, int value_type)
{
  if(orig)
  {
    /* make a copy that is a channel reader */
    struct qp_channel *c;

    ASSERT(orig->value_type > 0 && orig->value_type <= QP_TYPE_MAX);
    ASSERT(orig->form == QP_CHANNEL_FORM_SERIES);
    ASSERT(orig->series.arrays);

    c = (struct qp_channel *) qp_malloc(sizeof(*c));
    c->form = QP_CHANNEL_FORM_SERIES;
    c->id = orig->id;
    c->value_type = orig->value_type;
    c->series.array_current_index = (size_t) -1;
    c->series.array_last_index = orig->series.array_last_index;
    c->series.last_array = orig->series.last_array;
    c->series.arrays = qp_dllist_create(orig->series.arrays);
    c->series.ref_count = orig->series.ref_count;
    c->series.is_increasing = orig->series.is_increasing;
    c->series.is_decreasing = orig->series.is_decreasing;
    c->series.min = orig->series.min;
    c->series.max = orig->series.max;
    c->series.has_nan = orig->series.has_nan;

    ++(*(c->series.ref_count));
    return c;
  }
  else
    /* make a new one */
    return qp_channel_create(QP_CHANNEL_FORM_SERIES, value_type);
}


extern
void qp_channel_destroy(qp_channel_t channel);


static inline
size_t qp_channel_series_length(struct qp_channel *c)
{
  ASSERT(c);
  ASSERT(c->form == QP_CHANNEL_FORM_SERIES);
  ASSERT(c->series.arrays);

  return (qp_dllist_length(c->series.arrays)-1)*ARRAY_LENGTH +
    c->series.array_last_index + 1;
}


static inline
int qp_channel_equals(struct qp_channel *c1, struct qp_channel *c2)
{
  ASSERT(c1);
  ASSERT(c2);
  if(!c1 || !c2)
    return 0;
  if(c1->id == c2->id)
    return 1;
  return 0;
}



#ifdef QP_DEBUG
extern
void qp_channel_debug_append(struct qp_channel *c);
#endif