EE445M RTOS
Taken at the University of Texas Spring 2015
graphlib.c
Go to the documentation of this file.
1 #include <limits.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include "graphlib.h"
7 
8 /* Features to add if we ever get around to it
9 
10  - [ ] draw graph title
11  - [ ] draw axis titles
12  - [ ] draw x = 0 when visible
13  */
14 
15 char* strdup(const char *str) {
16  if (str != null) {
17  char *copy = malloc(strlen(str) + 1);
18  if (copy != null)
19  return strcpy(copy, str);
20  }
21  return null;
22 }
23 
24 /* Method to create an instance of a graph. */
25 graph* GLCreateGraph(long x_min, long x_max, long y_min, long y_max, long x_steps) {
26  graph* g = (graph*) calloc(1, sizeof(graph));
27  g->x_min = x_min;
28  g->x_max = x_max;
29  g->y_min = y_min;
30  g->y_max = y_max;
31 
32  g->data = (long*) calloc(x_steps, sizeof(long));
33  g->x_index = 0;
34  g->x_index_max = x_steps;
35 
36  g->all_data_points_valid = false;
37 
38  return (graph*) g;
39 }
40 
41 /* Set the title of graph and return the head of its new char*. */
42 char* GLSetTitle(graph* g, char* title) {
43  g->title = strdup(title);
44  return g->title;
45 }
46 
47 /* Set the titles of the x and y axes. */
48 void GLLabelAxes(graph* g, char* x_axis, char* y_axis) {
49  g->x_title = strdup(x_axis);
50  g->y_title = strdup(y_axis);
51 }
52 
53 /* Push a data point onto the end of the graph. The graph will
54  * automatically wrap around when there is no more room in the
55  * internal buffer. */
56 void GLPushDataPoint(graph* g, long y_val) {
57  /* Procedure: push the y data point at the current x_position and
58  * then increment the current x_position. */
59  g->data[g->x_index] = y_val;
61 
62  if (g->x_index >= g->x_index_max) {
63  g->x_index = 0;
64  g->all_data_points_valid = true; // one-way toggle to true
65  }
66 }
67 
68 /* Find a value that, when graphed, will not be visible on the graph
69  * due to the current range of the graph's window. Note that resizing
70  * the window may result in 'visible' OffScreenValues. */
72  long off_screen_value = g->y_max + 1;
73  if (off_screen_value < g->y_max) {
74  off_screen_value = g->y_min - 1;
75  } if (off_screen_value > g->y_min) {
76  off_screen_value = LONG_MIN;
77  }
78  return off_screen_value;
79 }
80 
81 /* Method to destroy a graph instance. Corresponds to
82  * GLCreateGraph() */
84  free(g->data);
85  free(g->title);
86  free(g->x_title);
87  free(g->y_title);
88  free(g);
89 }
long GLOffScreenValue(graph *g)
Definition: graphlib.c:71
char * y_title
Definition: graphlib.h:32
long most_recent_data_point
Definition: graphlib.h:66
char * GLSetTitle(graph *g, char *title)
Definition: graphlib.c:42
char * strdup(const char *str)
Definition: graphlib.c:15
void GLDestroyGraph(graph *g)
Definition: graphlib.c:83
#define null
Definition: defines.h:29
void GLLabelAxes(graph *g, char *x_axis, char *y_axis)
Definition: graphlib.c:48
graph * GLCreateGraph(long x_min, long x_max, long y_min, long y_max, long x_steps)
Definition: graphlib.c:25
long * data
Definition: graphlib.h:41
long x_max
Definition: graphlib.h:37
unsigned long x_index_max
Definition: graphlib.h:45
void GLPushDataPoint(graph *g, long y_val)
Definition: graphlib.c:56
char * x_title
Definition: graphlib.h:31
Definition: graphlib.h:29
unsigned long x_index
Definition: graphlib.h:44
long x_min
Definition: graphlib.h:37
char * title
Definition: graphlib.h:33
long y_min
Definition: graphlib.h:38
long y_max
Definition: graphlib.h:38
bool all_data_points_valid
Definition: graphlib.h:55