Distance Sensor Embedded Systems Project SID: 200864479 James Erringham-Bruce

Dependencies:   N5110 SRF02-JEB mbed

Committer:
ll13j7b
Date:
Thu May 05 14:22:27 2016 +0000
Revision:
3:ab75e6a12701
Parent:
2:01f697b856de
Final Code Submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ll13j7b 2:01f697b856de 1 /**
ll13j7b 1:f82359c58eda 2 @file Graph.h
ll13j7b 1:f82359c58eda 3 @brief Header file containing member functions and variables
ll13j7b 1:f82359c58eda 4 @author James Erringham-Bruce
ll13j7b 1:f82359c58eda 5 */
ll13j7b 1:f82359c58eda 6
ll13j7b 1:f82359c58eda 7 #ifndef GRAPH_H
ll13j7b 1:f82359c58eda 8 #define GRAPH_H
ll13j7b 1:f82359c58eda 9 #include "mbed.h" // mbed library
ll13j7b 1:f82359c58eda 10 #include "DataController.h"
ll13j7b 1:f82359c58eda 11 #include <deque> // std::deque
ll13j7b 1:f82359c58eda 12
ll13j7b 1:f82359c58eda 13
ll13j7b 1:f82359c58eda 14 // creating the class used in plotting the graph
ll13j7b 1:f82359c58eda 15 class Graph
ll13j7b 1:f82359c58eda 16 {
ll13j7b 1:f82359c58eda 17 // defining the public functions and variables
ll13j7b 1:f82359c58eda 18 public:
ll13j7b 1:f82359c58eda 19 /**
ll13j7b 2:01f697b856de 20 function is used to plot a graph on the LCD screen as a visual display to the user so the user can compare previous data
ll13j7b 2:01f697b856de 21 if they so desire in order to come to a conclusion about the distances measured or an average
ll13j7b 1:f82359c58eda 22 */
ll13j7b 1:f82359c58eda 23 void plotGraph();
ll13j7b 1:f82359c58eda 24 /**
ll13j7b 2:01f697b856de 25 function is used to draw the axis on the graph in order for the data that is being sent from the sensor can be plotted on the
ll13j7b 2:01f697b856de 26 graph accurately to allow the user further experiment if they wish
ll13j7b 1:f82359c58eda 27 */
ll13j7b 1:f82359c58eda 28 void drawGraphAxis();
ll13j7b 1:f82359c58eda 29 /**
ll13j7b 2:01f697b856de 30 function allows the axis label for the y axis to be labelled horizontally in order to increase presentation towards the user
ll13j7b 1:f82359c58eda 31 */
ll13j7b 1:f82359c58eda 32 void drawBitmap();
ll13j7b 1:f82359c58eda 33 /**
ll13j7b 2:01f697b856de 34 function which uses the points that have been plotted on the graph and draws a line to improve the visual display for the user
ll13j7b 1:f82359c58eda 35 */
ll13j7b 1:f82359c58eda 36 void drawLines();
ll13j7b 2:01f697b856de 37 /**
ll13j7b 2:01f697b856de 38 function to allow the points that have been plotted to shift along the x axis in the negative direction to allow the new points
ll13j7b 2:01f697b856de 39 to be plotted and allow the user to see the updated measurements taken from the sensor as the user moves away or closer to the
ll13j7b 2:01f697b856de 40 set distance
ll13j7b 2:01f697b856de 41 */
ll13j7b 1:f82359c58eda 42 void drawLinesBackwards();
ll13j7b 1:f82359c58eda 43
ll13j7b 1:f82359c58eda 44
ll13j7b 1:f82359c58eda 45 // defining the private functions and variables
ll13j7b 1:f82359c58eda 46 private:
ll13j7b 1:f82359c58eda 47
ll13j7b 1:f82359c58eda 48 };
ll13j7b 1:f82359c58eda 49
ll13j7b 2:01f697b856de 50 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 51
ll13j7b 2:01f697b856de 52 // END OF CLASS //
ll13j7b 2:01f697b856de 53
ll13j7b 2:01f697b856de 54 //*****************************************************************************************************//
ll13j7b 1:f82359c58eda 55
ll13j7b 1:f82359c58eda 56 // members initialies outside of the class//
ll13j7b 2:01f697b856de 57
ll13j7b 2:01f697b856de 58 unsigned int i = 0; /*!< used global variable for iterations */
ll13j7b 1:f82359c58eda 59
ll13j7b 2:01f697b856de 60 unsigned int x = 20; /*!< variable used for the x axis on the graph allowing us to plot points for visual display to the user*/
ll13j7b 1:f82359c58eda 61
ll13j7b 2:01f697b856de 62 int points[7] = {10 ,20 , 30 ,40 ,50 ,60, 70}; /*!< array of 'x' points to plot on the graph to use for visual display to the user */
ll13j7b 2:01f697b856de 63
ll13j7b 2:01f697b856de 64 //
ll13j7b 1:f82359c58eda 65 const bool verticalWord_bitmap[46][7] = {
ll13j7b 1:f82359c58eda 66 { 0,0,0,1,1,0,0 },
ll13j7b 1:f82359c58eda 67 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 68 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 69 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 70 { 0,0,0,1,1,1,0 },
ll13j7b 1:f82359c58eda 71 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 72 { 0,0,0,0,0,1,0 },
ll13j7b 1:f82359c58eda 73 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 74 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 75 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 76 { 0,0,0,1,1,1,0 },
ll13j7b 1:f82359c58eda 77 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 78 { 0,0,0,1,1,1,1 },
ll13j7b 1:f82359c58eda 79 { 0,0,1,0,0,0,0 },
ll13j7b 1:f82359c58eda 80 { 0,0,1,0,0,0,0 },
ll13j7b 1:f82359c58eda 81 { 0,0,0,1,0,0,0 },
ll13j7b 1:f82359c58eda 82 { 0,0,1,1,1,1,1 },
ll13j7b 1:f82359c58eda 83 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 84 { 0,0,0,1,1,1,1 },
ll13j7b 1:f82359c58eda 85 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 86 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 87 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 88 { 0,0,0,0,0,1,0 },
ll13j7b 1:f82359c58eda 89 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 90 { 0,0,0,0,0,1,0 },
ll13j7b 1:f82359c58eda 91 { 0,0,0,0,0,0,1 },
ll13j7b 1:f82359c58eda 92 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 93 { 1,1,1,1,1,1,0 },
ll13j7b 1:f82359c58eda 94 { 0,0,1,0,0,0,0 },
ll13j7b 1:f82359c58eda 95 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 96 { 0,0,1,0,0,1,0 },
ll13j7b 1:f82359c58eda 97 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 98 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 99 { 0,0,1,0,1,0,1 },
ll13j7b 1:f82359c58eda 100 { 0,0,0,1,0,0,1 },
ll13j7b 1:f82359c58eda 101 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 102 { 0,0,0,0,0,0,1 },
ll13j7b 1:f82359c58eda 103 { 1,0,1,1,1,1,1 },
ll13j7b 1:f82359c58eda 104 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 105 { 0,0,0,0,0,0,0 },
ll13j7b 1:f82359c58eda 106 { 1,1,1,1,1,1,1 },
ll13j7b 1:f82359c58eda 107 { 0,0,0,1,0,0,1 },
ll13j7b 1:f82359c58eda 108 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 109 { 0,0,1,0,0,0,1 },
ll13j7b 1:f82359c58eda 110 { 0,0,0,1,1,1,0 },
ll13j7b 1:f82359c58eda 111 { 0,0,0,0,0,0,0 }
ll13j7b 2:01f697b856de 112 }; /*!< bitmap of the vertical word 'distance' displayed on the graph to increase aesthetic performance towards the user*/
ll13j7b 2:01f697b856de 113
ll13j7b 2:01f697b856de 114 #endif
ll13j7b 2:01f697b856de 115
ll13j7b 2:01f697b856de 116 /** @namespace DISTANCE_READINGS_DEQUE
ll13j7b 2:01f697b856de 117 @brief double ended queue for storing graph points
ll13j7b 2:01f697b856de 118 */
ll13j7b 2:01f697b856de 119 std::deque<int> DISTANCE_READINGS_DEQUE (6);
ll13j7b 2:01f697b856de 120
ll13j7b 2:01f697b856de 121 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 122
ll13j7b 2:01f697b856de 123 // END OF GLOBAL VARIABLES & FUNCTIONS //
ll13j7b 2:01f697b856de 124
ll13j7b 2:01f697b856de 125 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 126
ll13j7b 2:01f697b856de 127 // FUNCTION TO PLOT GRAPH OF DISTANCE VS. TIME
ll13j7b 2:01f697b856de 128 void Graph::plotGraph()
ll13j7b 2:01f697b856de 129 {
ll13j7b 2:01f697b856de 130 isInGraph = 1;
ll13j7b 2:01f697b856de 131 /// initiate the deque to all 0 values to avoid crashing when plotting a point that doesnt exist
ll13j7b 2:01f697b856de 132 DISTANCE_READINGS_DEQUE.assign(6,0);
ll13j7b 2:01f697b856de 133 lcd.clear();
ll13j7b 2:01f697b856de 134 while (isInGraph) {
ll13j7b 2:01f697b856de 135 goBackToMenu();
ll13j7b 2:01f697b856de 136 /// draw the graph axis on the screen
ll13j7b 2:01f697b856de 137 drawGraphAxis(); // draw the axis before the timer fires ( drawing the axis before the 5 second delay )
ll13j7b 2:01f697b856de 138 // normailise the distance range between amount of free pixels available
ll13j7b 2:01f697b856de 139 int average = 40 - getAverageReadingCm()/6.25;
ll13j7b 2:01f697b856de 140 // start the timer flag
ll13j7b 2:01f697b856de 141 if (g_timer_flag) {
ll13j7b 2:01f697b856de 142 g_timer_flag = 0; // reset the flag
ll13j7b 2:01f697b856de 143 // redrawing the graph axis ( crucial because the screen is cleared )
ll13j7b 2:01f697b856de 144 lcd.clear();
ll13j7b 2:01f697b856de 145 drawGraphAxis();
ll13j7b 2:01f697b856de 146 /// pushing in a new average value to the stack ( 6 elements in total, until if statement says otherwise [i] )
ll13j7b 2:01f697b856de 147 DISTANCE_READINGS_DEQUE.push_front(average);
ll13j7b 2:01f697b856de 148 // increasing i by 1 each timer fired
ll13j7b 2:01f697b856de 149 if(i<6) {
ll13j7b 2:01f697b856de 150 // drawing lines between the points from left hand side
ll13j7b 2:01f697b856de 151 drawLines();
ll13j7b 2:01f697b856de 152 i++;
ll13j7b 2:01f697b856de 153 } else {
ll13j7b 2:01f697b856de 154 drawLinesBackwards();
ll13j7b 2:01f697b856de 155 }
ll13j7b 2:01f697b856de 156 // increase the x axis each timer fired( moving the plotted point along the graph )
ll13j7b 2:01f697b856de 157 x += 10;
ll13j7b 2:01f697b856de 158 }
ll13j7b 2:01f697b856de 159 sleep(); // sleep before next interrupt
ll13j7b 2:01f697b856de 160 }
ll13j7b 2:01f697b856de 161 }
ll13j7b 1:f82359c58eda 162
ll13j7b 2:01f697b856de 163 // FUNCTION TO DRAW LINES BETWEEN POINTS ON THE GRAPH
ll13j7b 2:01f697b856de 164 void Graph::drawLines()
ll13j7b 2:01f697b856de 165 {
ll13j7b 2:01f697b856de 166 /// draws the lines from point to point in the forward direction
ll13j7b 2:01f697b856de 167 /// forward direction because the graph is filling up from the start
ll13j7b 2:01f697b856de 168 if (i != 0) {
ll13j7b 2:01f697b856de 169 for (int x = 0; x < i; x++) {
ll13j7b 2:01f697b856de 170 lcd.drawLine(points[x],DISTANCE_READINGS_DEQUE.at(i-x),points[x+1],DISTANCE_READINGS_DEQUE.at(i-x-1),1);
ll13j7b 2:01f697b856de 171 lcd.refresh();
ll13j7b 2:01f697b856de 172 }
ll13j7b 2:01f697b856de 173 }
ll13j7b 2:01f697b856de 174 }
ll13j7b 2:01f697b856de 175
ll13j7b 2:01f697b856de 176 // FUNCTION TO DRAW LINES BETWEEN POINTS ON THE GRAPH
ll13j7b 2:01f697b856de 177 void Graph::drawLinesBackwards()
ll13j7b 2:01f697b856de 178 {
ll13j7b 2:01f697b856de 179 /// draws the lines from point to point in the backward direction
ll13j7b 2:01f697b856de 180 /// backwards because when it is filled, the graph needs to be shifted backwards every new point made
ll13j7b 2:01f697b856de 181 for(int x = 0; x < 6 ; x++) {
ll13j7b 2:01f697b856de 182 lcd.drawLine(points[6-x],DISTANCE_READINGS_DEQUE.at(x),points[6-x-1],DISTANCE_READINGS_DEQUE.at(x+1),1);
ll13j7b 2:01f697b856de 183 lcd.refresh();
ll13j7b 2:01f697b856de 184 }
ll13j7b 2:01f697b856de 185 }
ll13j7b 2:01f697b856de 186
ll13j7b 2:01f697b856de 187 // DRAWING THE GRAPH AXIS AND LABELS
ll13j7b 2:01f697b856de 188 void Graph::drawGraphAxis()
ll13j7b 2:01f697b856de 189 {
ll13j7b 2:01f697b856de 190 /// draw the Y axis and the X axis
ll13j7b 2:01f697b856de 191 /// print the names of the axis
ll13j7b 2:01f697b856de 192 lcd.drawLine(9,6,9,39,1); // y-axis line
ll13j7b 2:01f697b856de 193 lcd.drawLine(9,39,80,39,1); // x-axis line
ll13j7b 2:01f697b856de 194 drawBitmap(); // drawing the bitmap of the vertical word
ll13j7b 2:01f697b856de 195 lcd.printString("time(s)",20,5); // printing the x axis
ll13j7b 2:01f697b856de 196 }
ll13j7b 2:01f697b856de 197
ll13j7b 2:01f697b856de 198 // FUNCTION TO DRAW THE BITMAP OF THE VERTICAL WORD
ll13j7b 2:01f697b856de 199 void Graph::drawBitmap()
ll13j7b 2:01f697b856de 200 {
ll13j7b 2:01f697b856de 201 /// draws the bitmap of the vertical word 'distance'
ll13j7b 2:01f697b856de 202 /// used a bitmap because horizontal text is not possible
ll13j7b 2:01f697b856de 203 for (int column = 0; column < 7; column++) { // loops through the columns
ll13j7b 2:01f697b856de 204 for (int row = 0; row < 46; row++) { // loops through the rows
ll13j7b 2:01f697b856de 205 if (verticalWord_bitmap[row][column]) { // if a bit is found true ( active 1 )
ll13j7b 2:01f697b856de 206 lcd.setPixel(column, 2 + row); // set the pixel at that location on the screen
ll13j7b 2:01f697b856de 207 }
ll13j7b 2:01f697b856de 208 }
ll13j7b 2:01f697b856de 209 }
ll13j7b 2:01f697b856de 210 }
ll13j7b 2:01f697b856de 211
ll13j7b 2:01f697b856de 212 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 213
ll13j7b 2:01f697b856de 214 // END OF FUNCTION IMPLEMENTATIONS //
ll13j7b 2:01f697b856de 215
ll13j7b 2:01f697b856de 216 //*****************************************************************************************************//
ll13j7b 2:01f697b856de 217
ll13j7b 2:01f697b856de 218
ll13j7b 2:01f697b856de 219
ll13j7b 2:01f697b856de 220
ll13j7b 2:01f697b856de 221