datalogger with touch screen gui

Dependencies:   FT800_beyondtek SDFileSystem Brew_Panel_FT810 mbed

Fork of Brew_Panel_FT810 by The Best

Revision:
11:f6a146b62579
Parent:
10:c56b7ca4805f
Child:
12:c19239029172
Child:
13:8f23699b9c9f
diff -r c56b7ca4805f -r f6a146b62579 main.cpp
--- a/main.cpp	Fri Apr 01 19:56:18 2016 +0000
+++ b/main.cpp	Tue Apr 05 15:02:48 2016 +0000
@@ -1,18 +1,23 @@
 /*
 Title Block
-
 **     Project     : CLC Brew Panel
+**     Engineers   : Justin Montgomery and Cortland Ratliff
 **     Processor   : MK20
 **     Version     : 1.0
 **     Compiler    : mbed
 **     Date/Time   : 3/27/2016
-**     Abstract    : ?
-**
-**
+**     Abstract    : This program was written for a marketing panel called the Brew Panel. The purpose of this program is to control two ITVs that
+**                   regulate two different pressure lines that control two diaphragm pumps. The two pumps then pump and generate a flow rate for
+**                   two different colored liquids. Another purpose of this program is to also read back feedback from the two ITVs, a PF3W flow meter,
+**                   and a PSE pressure sensor. The program also interfaces with an HMI controlled by a FT810 LCD driver. The interface controls the hardware
+**                   and displays graphics. This program was designed for the Teensy and to control the Closed Loop Controller Brew Panel prototype board.
+**                  
+**     Notes       : The limitations of the VERTEX2II program forced us to create a bandage when trying to write images past pixel 511. The range of VERTEX2II is 0 to 511
+                     so in order to draw an image further away we had to use the translate command.
 **
-**     Todo :
+**     Todo        :
 */
-#include "mbed.h"
+#include "mbed.h"                               // library #includes
 #include "FT_Platform.h"
 #include "FT_color.h"
 #include "stdio.h"
@@ -20,113 +25,91 @@
 #include "SDFileSystem.h"
 #include "MCP4725.h"
 
-#define SAMAPP_DELAY_BTW_APIS (1000)
-#define SAMAPP_ENABLE_DELAY() Ft_Gpu_Hal_Sleep(SAMAPP_DELAY_BTW_APIS)
-#define SAMAPP_ENABLE_DELAY_VALUE(x) Ft_Gpu_Hal_Sleep(x)
-
-//#define Nucleo_F303K8
-#define K20
-// used add easy way to reprogram without using the phsyical button.
-#define Program_Button
-//#define Debug_Swipe
-
-#ifdef Nucleo_F303K8
-FT800 TFT(PB_5,PB_4,PB_3,PA_11,PA_8,PF_1); // the FT800 is connected to SPI 5-7, then we have CS, INT, PD
-#endif
+#define K20                                     // used to simplify defining for different microcontroller platforms
+#define Program_Button                          // used add easy way to reprogram without using the phsyical button.
+//#define Debug_Swipe                           // reads back touch data in real time on the screen
 
 #ifdef K20
-FT800 TFT(D11,D12,D13,D9,D8,D14);
-SDFileSystem sd(D11, D12, D13, D10, "sd");
-MCP4725 dac1(D18, D19, MCP4725::ADDRESS_2);
-MCP4725 dac2(D18, D19, MCP4725::ADDRESS_3);
+FT800 TFT(D11,D12,D13,D9,D8,D14);               // create object for the LCD, handles all of the data passing and slave select commands (SPI)
+SDFileSystem sd(D11, D12, D13, D10, "sd");      // create object for the SD card, handles all of the data passing and slave select commands (SPI)
+MCP4725 dac1(D18, D19, MCP4725::ADDRESS_2);     // create object for the first DAC board, handles all fo the data addressing (I2C)
+MCP4725 dac2(D18, D19, MCP4725::ADDRESS_3);     // create object for the second DAC board, handles all fo the data addressing (I2C)
 
-AnalogIn PSEpressure_raw(A9);
-AnalogIn PFWflowrate_raw(A8);
-AnalogIn ITVpressure1_feedback_raw(A3);
-AnalogIn ITVpressure2_feedback_raw(A2);
-DigitalIn card_present(D7);
+AnalogIn PSEpressure_raw(A9);                   // analog 9 pin connected to the PSE input
+AnalogIn PFWflowrate_raw(A8);                   // analog 9 pin connected to the PFW input
+AnalogIn ITVpressure1_feedback_raw(A3);         // analog 9 pin connected to the ITV1 input
+AnalogIn ITVpressure2_feedback_raw(A2);         // analog 9 pin connected to the ITV2 input
+DigitalIn card_present(D7);                     // analog 9 pin connected to the SD card detect pin input
 #endif
 
-// global Vars
-unsigned int r, b, g, CalVal0, CalVal1, CalVal2, CalVal3, CalVal4, curX, curY, pasX, pasY;
-float PSEpressure, PFWflowrate = 0.0;
-float ITVpressure1_feedback, ITVpressure2_feedback, ITVpressure1_user_input,  ITVpressure2_user_input = 0.0;
-float ITV1_pre, ITV1_fcal, ITV2_pre, ITV2_fcal, PFW_pre, PFW_fcal, PSE_pre, PSE_fcal = 0.0;
-static float avg1 = 0.05;
-static float avg2 = 0.95;
-uint16_t ITVpressure1_input_raw, ITVpressure2_input_raw, test = 0;
-int16_t velocity, distance, touched, location, paslocation ;
-char buffer[50];
-uint8_t tcal[24]; //Touch screen Calibration
-char tstr[40]; // Temp location for Touch Screen Calibration
-ft_int16_t x_size,y_size;
+// global variables
+unsigned int CalVal0, CalVal1, CalVal2, CalVal3, CalVal4, curX, curY, pasX, pasY;                                   // touchscreen calibration data and swiping variables.
+float PSEpressure, PFWflowrate = 0.0;                                                                               // variables to store data used in calculating flow and pressure
+float ITVpressure1_feedback, ITVpressure2_feedback, ITVpressure1_user_input,  ITVpressure2_user_input = 0.0;        // variables to store data thats used in calculating the output and input pressure for the ITVs
+float ITV1_pre, ITV1_fcal, ITV2_pre, ITV2_fcal, PFW_pre, PFW_fcal, PSE_pre, PSE_fcal = 0.0;                         // moving average filter variables
+static float avg1 = 0.05;                                                                                           // filter value for new samples
+static float avg2 = 0.95;                                                                                           // filter value previous samples
+uint16_t ITVpressure1_input_raw, ITVpressure2_input_raw = 0;                                                          // stores the raw 16 bit reads for the ITVs
+int16_t velocity, distance, touched, location, paslocation;                                                         // variables to handle the swiping and multiple pages
+char buffer[50];                                                                                                    // temporary buffer for writing characters to the LCD
+uint8_t tcal[24];                                                                                                   // touch screen Calibration
+char tstr[40];                                                                                                      // temp location for Touch Screen Calibration
+ft_int16_t x_size, y_size;                                                                                          // stores size data for loaded jpegs, not currently used
+unsigned int err[7] = {0,0,0,0,0,0,0};                                                                              // array to store returned values from Load_jpg function, possible future error handling
 
-unsigned int err[7];
-
-/***************************************************************************/
-/* Show a Screen with Text for 5 seconds                                   */
-/* A spinner shows the delay                                               */
-/***************************************************************************/
-
+/****************************************************************************/
+/* Serves as the bootup screen as the images are loaded from the SD card    */
+/* A spinner shows the delay                                                */
+/****************************************************************************/
 ft_void_t Start_Screen(ft_char8_t *str)
 {
-    TFT.DLstart();                                         // start a new display command list
-    TFT.DL(CLEAR_COLOR_RGB(255,255,255));      // set the clear color to white
-    TFT.DL(CLEAR(1,1,1));                      // clear buffers -> color buffer,stencil buffer, tag buffer
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.DLstart();                                             // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,255,255));                      // set the clear color to white
+    TFT.DL(CLEAR(1,1,1));                                      // clear buffers -> color buffer,stencil buffer, tag buffer
+    
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                         // generate border in SMC blue, all functions are in 1/16 pixel format
     TFT.DL(BEGIN(LINES));
     TFT.DL(LINE_WIDTH(8 * 16));
     TFT.DL(VERTEX2F(0*16,0*16));
     TFT.DL(VERTEX2F(799*16,0*16));
-
     TFT.DL(VERTEX2F(799*16,0*16));
     TFT.DL(VERTEX2F(799*16,479*16));
-
     TFT.DL(VERTEX2F(799*16,479*16));
     TFT.DL(VERTEX2F(0*16,479*16));
-
     TFT.DL(VERTEX2F(0*16,479*16));
     TFT.DL(VERTEX2F(0*16,0*16));
-
-    TFT.Text((TFT.DispWidth/2), TFT.DispHeight/2, 31, OPT_CENTERX, str); // draw Text with font 31
-    TFT.Text((TFT.DispWidth/2), 350, 31, OPT_CENTERX, "Brew Panel"); // draw Text with font 31
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));         // change current color
-    TFT.Spinner((TFT.DispWidth/2),TFT.DispHeight/4, 0,0);  // draw a animated spinner
+    TFT.DL(END());
 
-    TFT.DL(DISPLAY());                         // Display the image
-    TFT.Swap();                                            // Swap the current display list
-    TFT.Flush_Co_Buffer();                                 // Download the command list into fifo
+    TFT.Text((TFT.DispWidth/2), TFT.DispHeight/2, 31, OPT_CENTERX, str);        // draw Text with font 31
+    TFT.Text((TFT.DispWidth/2), 350, 31, OPT_CENTERX, "Brew Panel");            // draw Text with font 31
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                          // change current color
+    TFT.Spinner((TFT.DispWidth/2),TFT.DispHeight/4, 0,0);                       // draw a animated spinner
 
-    TFT.WaitCmdfifo_empty();                               // Wait till coprocessor completes the operation
-    TFT.Sleep(1000);                                       // Wait 5 s to show
+    TFT.DL(DISPLAY());                   // Display the image
+    TFT.Swap();                          // Swap the current display list
+    TFT.Flush_Co_Buffer();               // Download the command list into fifo
+    TFT.WaitCmdfifo_empty();             // Wait till coprocessor completes the operation
+    TFT.Sleep(1000);                     // Wait 1s to show
 }
 
-// construct the screen and download it to the LCD
-void screen_1()
+/****************************************************************************/
+/* Main screen function that writes the SD images and external hardware     */
+/* data to the screen, also controls the ITV pressure                       */
+/****************************************************************************/
+void Main_Screen()
 {
-    TFT.DLstart();                         // start a new display command list
-    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
-    TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer
-    #ifdef Program_Button
-        TFT.DL(TAG(200));                        // assign TAG value 200
-        TFT.Button(710, 10, 76, 22, 26, 0, "Reprogram");
-        TFT.DL(TAG(3));
-    #endif
+    TFT.DLstart();                                                      // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,255,255));                               // set the clear color to white
+    TFT.DL(CLEAR(1,1,1));                                               // clear buffers -> color buffer,stencil buffer, tag buffer
+#ifdef Program_Button
+    TFT.DL(TAG(200));                                                   // assign TAG value 200
+    TFT.Button(710, 10, 76, 22, 26, 0, "Reprogram");                    // generate reprogram button, only works for teensy
+    TFT.DL(TAG(3));
+#endif
+
     /***************************************************************************************************/
-    // some temp location holder for each page showing the transitions
-    /*TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
-    TFT.Text(300, 200, 31, 0, "Page 2");
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16+25600));
-    TFT.Text(300, 200, 31, 0, "Page 3");
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16+38400));
-    TFT.Text(300, 200, 31, 0, "Page 4");
-    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16));*/
-
     /*Border Creation*/
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                  // generate border in SMC blue, all functions are in 1/16 pixel format
     TFT.DL(BEGIN(LINES));
     TFT.DL(LINE_WIDTH(8 * 16));
     TFT.DL(VERTEX2F(0*16,0*16));
@@ -142,302 +125,278 @@
 
     /***************************************************************************************************/
     /*ITV 1 Screen Display*/
-    TFT.DL(COLOR_RGB(255,255,255));
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16));
-    
-    TFT.DL(BEGIN(BITMAPS));
-    TFT.DL(VERTEX2II(265,50,0,0));
-    TFT.DL(VERTEX2II(75,50,1,0));
-    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16));
-    TFT.DL(VERTEX2II(511,50,2,0));
-    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16));
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set background to white
+    TFT.DL(VERTEX_TRANSLATE_X(location* 16));                           // page 1
+
+    TFT.DL(BEGIN(BITMAPS));                                             // draw a bitmap that stores the first screens images
+    TFT.DL(VERTEX2II(265,50,0,0));                                      // draw logo image with bit handle 0
+    TFT.DL(VERTEX2II(75,50,1,0));                                       // draw ITV_1 image with bit handle 1
+    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16));                      // translate right 149 pixels for third image, location controls what page the screen is on
+    TFT.DL(VERTEX2II(511,50,2,0));                                      // draw ITV_2 image with bit handle 2
+    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16));                        // translate back to prevent the remaining commands from also being translated
     TFT.DL(END());
-    
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-    TFT.Text(265, 200, 31, 0, "ITV 1 Pressure");
-    TFT.DL(BEGIN(RECTS));
-    TFT.DL(VERTEX2F(300*16,265*16));
+
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                  // set color to SMC blue for the text
+    TFT.Text(265, 200, 31, 0, "ITV 1 Pressure");                        // title block for screen
+    TFT.DL(BEGIN(RECTS));                                               // draw rounded rectangle in SMC blue to act as background for where the feedback will be written
+    TFT.DL(VERTEX2F(300*16,265*16));                                    
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
-    TFT.DL(COLOR_RGB(255,255,255));
-    sprintf(buffer, "%.1f", ITVpressure1_feedback);
-    TFT.Text(325,275,31,0,buffer);
-    TFT.Text(415, 275, 31, 0, " PSI");
-    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - 9430) * (70.0) / (48430 - 9430) + 0;
-    ITV1_fcal = ITVpressure1_feedback;
-    ITV1_fcal *= avg1;
+    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - 9430) * (70.0) / (48430 - 9430) + 0;        // calibration and scaling math for the ITV feedback, 48430 = 20mA, 9430 = 4mA
+    ITV1_fcal = ITVpressure1_feedback;                                  // moving average filter                     
+    ITV1_fcal *= avg1;                                                 
     ITV1_fcal += ITV1_pre * avg2;
     ITV1_pre = ITV1_fcal;
-    ITVpressure1_feedback = ITV1_fcal;
-    TFT.DL(COLOR_RGB(255,255,255));
-    TFT.FgColor(COLOR_RGB(0,124,196));
-    TFT.BgColor(COLOR_RGB(0,124,196));
-    TFT.DL(TAG(1));                        // assign TAG value 1
-    TFT.Slider(225,375,350,30,0,ITVpressure1_user_input,2355);
-    TFT.DL(TAG(3));
+    ITVpressure1_feedback = ITV1_fcal;                                  // store the filtered value in the feedback variable
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set color to white for the text
+    sprintf(buffer, "%.1f", ITVpressure1_feedback);                     // write the feedback pressure to the screen
+    TFT.Text(325,275,31,0,buffer);
+    TFT.Text(415, 275, 31, 0, " PSI");                                  // write the units to the screen
+                                    
+    TFT.DL(COLOR_RGB(255,255,255));                                     // controls the color of the slider bar in RGB format
+    TFT.FgColor(COLOR_RGB(0,124,196));                                  // foreground color set to SMC blue in RGB format
+    TFT.BgColor(COLOR_RGB(0,124,196));                                  // background color set to SMC blue in RGB format
+    TFT.DL(TAG(1));                                                     // assign TAG value 1
+    TFT.Slider(225,375,350,30,0,ITVpressure1_user_input,2355);          // generate slider that controls the output pressure of ITV1
+    TFT.DL(TAG(3));                                                     // general tag assignment to prevent any tag assigning errors
     /***************************************************************************************************/
 
     /***************************************************************************************************/
     /*ITV 2 and PF3W Screen Display*/
-    TFT.DL(COLOR_RGB(255,255,255));
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+12800);
-    TFT.DL(BEGIN(BITMAPS));
-    //TFT.DL(VERTEX2II(265,50,0,0));
-    TFT.DL(VERTEX2II(30,75,3,0));
-    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16)+12800);
-    TFT.DL(VERTEX2II(411,75,4,0));
-    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+12800);
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set background color to white
+    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+12800);                     // translate screen writes by 12800 which is a 1/16th pixel format, this creates page 2 of the screen
+    TFT.DL(BEGIN(BITMAPS));                                             // draw a bitmap that stores the second screens images 
+    TFT.DL(VERTEX2II(30,75,3,0));                                       // write PF3W_1 image to the LCD
+    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16)+12800);                // translate right 149 pixels for third image, location controls what page the screen is on
+    TFT.DL(VERTEX2II(411,75,4,0));                                      // write PF3W_2 image to the LCD
+    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+12800);                  // translate back to 0 for remaining screen writes
     TFT.DL(END());
-    
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
-                  // Slide
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-    TFT.Text(265, 200, 31, 0, "ITV 2 Pressure");
-    TFT.DL(BEGIN(RECTS));
+
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                  // set text color to SMC blue, RGB format
+    TFT.Text(265, 200, 31, 0, "ITV 2 Pressure");                        // write title block to screen
+    TFT.DL(BEGIN(RECTS));                                               // draw rounded rectangle in SMC blue to act as background for where the feedback will be written
     TFT.DL(VERTEX2F(300*16,265*16));
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
-    TFT.DL(COLOR_RGB(255,255,255));
-    sprintf(buffer, "%.1f", ITVpressure2_feedback);
-    TFT.Text(325,275,31,0,buffer);
-    TFT.Text(415, 275, 31, 0, " PSI");
-    ITVpressure2_feedback = (ITVpressure2_feedback_raw.read_u16()- 9365) * (70.0) / (48420 - 9365) + 0;
-    ITV2_fcal = ITVpressure2_feedback;
+    
+    ITVpressure2_feedback = (ITVpressure2_feedback_raw.read_u16()- 9365) * (70.0) / (48420 - 9365) + 0; // calibration and scaling math for the ITV feedback, 48420 = 20mA, 9365 = 4mA
+    ITV2_fcal = ITVpressure2_feedback;                                  // moving average filter
     ITV2_fcal *= avg1;
     ITV2_fcal += ITV2_pre * avg2;
     ITV2_pre = ITV2_fcal;
-    ITVpressure2_feedback = ITV2_fcal;
-    TFT.DL(COLOR_RGB(255,255,255));
-    TFT.FgColor(COLOR_RGB(0,124,196));
-    TFT.BgColor(COLOR_RGB(0,124,196));
-    TFT.DL(TAG(2));                        // assign TAG value 2
-    TFT.Slider(225,375,350,30,0,ITVpressure2_user_input,2387);
-    TFT.DL(TAG(3));
-    
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-    TFT.Text(250, 20, 31, 0, "PF3W Flow Rate");
-    TFT.DL(BEGIN(RECTS));
+    ITVpressure2_feedback = ITV2_fcal;                                  // store filtered value in feedback variable
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set color for text to white
+    sprintf(buffer, "%.1f", ITVpressure2_feedback);                     // write feedback to screen in white
+    TFT.Text(325,275,31,0,buffer);
+    TFT.Text(415, 275, 31, 0, " PSI");                                  // write the units to the screen       
+    TFT.DL(COLOR_RGB(255,255,255));                                     // controls the color of the slider bar in RGB format
+    TFT.FgColor(COLOR_RGB(0,124,196));                                  // foreground color set to SMC blue in RGB format
+    TFT.BgColor(COLOR_RGB(0,124,196));                                  // background color set to SMC blue in RGB format
+    TFT.DL(TAG(2));                                                     // assign tag 2 to slider for ITV2
+    TFT.Slider(225,375,350,30,0,ITVpressure2_user_input,2387);          // generate slider that controls the output pressure of ITV2
+    TFT.DL(TAG(3));                                                     // general tag assignment to prevent any tag assigning errors
+
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                  // set color to SMC blue for text
+    TFT.Text(250, 20, 31, 0, "PF3W Flow Rate");                         // write title block to screen
+    TFT.DL(BEGIN(RECTS));                                               // draw rounded rectangle in SMC blue to act as background for where the feedback will be written
     TFT.DL(VERTEX2F(300*16,85*16));
     TFT.DL(VERTEX2F(505*16,150*16));
-    PFWflowrate = (PFWflowrate_raw.read_u16() - 10355) * (40.0) / (51920 - 10355) + 0;
-    PFW_fcal = PFWflowrate;
+    PFWflowrate = (PFWflowrate_raw.read_u16() - 10355) * (40.0) / (51920 - 10355) + 0; // calibration and scaling math for the PFW feedback, 51920 = 20mA, 10355 = 4mA
+    PFW_fcal = PFWflowrate;                                             // moving average filter
     PFW_fcal *= avg1;
     PFW_fcal += PFW_pre * avg2;
     PFW_pre = PFW_fcal;
-    PFWflowrate = PFW_fcal;
-    TFT.DL(COLOR_RGB(255,255,255));
-    sprintf(buffer, "%.1f", PFWflowrate);
+    PFWflowrate = PFW_fcal;                                             // store filtered value in feedback variable
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set text feedback text color to white
+    sprintf(buffer, "%.1f", PFWflowrate);                               // write feedback to LCD
     TFT.Text(285,95,31,0,buffer);
-    TFT.Text(385, 95, 31, 0, " L/min");
+    TFT.Text(385, 95, 31, 0, " L/min");                                 // write units to LCD
     /***************************************************************************************************/
 
     /***************************************************************************************************/
     /*ISE Screen Display*/
-    TFT.DL(COLOR_RGB(255,255,255));
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+25600);
-    TFT.DL(BEGIN(BITMAPS));
-    TFT.DL(TAG(3));
-    TFT.DL(VERTEX2II(265,50,0,0));
-    TFT.DL(VERTEX2II(75,50,5,0));
-    TFT.DL(VERTEX_TRANSLATE_X((99+location)*16)+25600);
-    TFT.DL(VERTEX2II(511,85,6,0));
-    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+25600);
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set the background for page 3 to white
+    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+25600);                     // translate screen writes by 25600 which is a 1/16th pixel format, this creates page 3 of the screen
+    TFT.DL(BEGIN(BITMAPS));                                             // create bitmap to draw images on LCD
+    TFT.DL(VERTEX2II(265,50,0,0));                                      // wrtie the SMC logo to the screen
+    TFT.DL(VERTEX2II(75,50,5,0));                                       // write PSE_1 image to the screen
+    TFT.DL(VERTEX_TRANSLATE_X((99+location)*16)+25600);                 // translate right by 99 pixels for the next image write
+    TFT.DL(VERTEX2II(511,85,6,0));                                      // write PSE_2 image to the screen
+    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+25600);                  // translate back to pixel 0 for the remaining screen writes
     TFT.DL(END());
-    
-    TFT.DL(VERTEX_TRANSLATE_X(location* 16+25600)); //page 3
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-    TFT.Text(280, 200, 31, 0, "PSE Pressure");
-    TFT.DL(BEGIN(RECTS));
+
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));                                  // set text color to SMC blue
+    TFT.Text(280, 200, 31, 0, "PSE Pressure");                          // write title block to screen
+    TFT.DL(BEGIN(RECTS));                                               // draw rounded rectangle in SMC blue to act as background for where the feedback will be written
     TFT.DL(VERTEX2F(295*16,265*16));
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
-    PSEpressure = (PSEpressure_raw.read_u16() - 10440) * (1.0) / (52350 - 10440) + 0;
-    PSE_fcal = PSEpressure;
+    PSEpressure = (PSEpressure_raw.read_u16() - 10440) * (1.0) / (52350 - 10440) + 0; // calibration and scaling math for the PFW feedback, 52350 = 20mA, 10440 = 4mA
+    PSE_fcal = PSEpressure;                                             // moving average filter
     PSE_fcal *= avg1;
     PSE_fcal += PSE_pre * avg2;
     PSE_pre = PSE_fcal;
-    PSEpressure = PSE_fcal;
-    TFT.DL(COLOR_RGB(255,255,255));
-    sprintf(buffer, "%.2f", PSEpressure);
-    //test = ISEpressure_raw.read_u16();
-    //sprintf(buffer, "%d",  test);
+    PSEpressure = PSE_fcal;                                             // store the filtered value in feedback variable
+    TFT.DL(COLOR_RGB(255,255,255));                                     // set text color to white
+    sprintf(buffer, "%.2f", PSEpressure);                               // write feedback to screen
     TFT.Text(300,275,31,0,buffer);
-    TFT.Text(405, 275, 31, 0, " MPa");
+    TFT.Text(405, 275, 31, 0, " MPa");                                  // write units to LCD
     /***************************************************************************************************/
 
-    /***************************************************************************************************/
-    /*PFM Screen Display*/
-    /*TFT.DL(VERTEX_TRANSLATE_X(location* 16+38400)); // page 4
-    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
-    TFT.Text(265, 200, 31, 0, "PFW Flow Rate");
-    TFT.DL(BEGIN(RECTS));
-    TFT.DL(VERTEX2F(300*16,265*16));
-    TFT.DL(VERTEX2F(505*16,330*16));
-    TFT.DL(END());
-    PFMflowrate = (PFMflowrate_raw.read_u16() - 10355) * (40.0) / (51920 - 10355) + 0;
-    TFT.DL(COLOR_RGB(255,255,255));
-    sprintf(buffer, "%.1f", PFMflowrate);
-    TFT.Text(295,275,31,0,buffer);
-    TFT.Text(385, 275, 31, 0, " L/min");*/
-    /***************************************************************************************************/
-
-
-    // add button to make for easy reprogramming
-
-
-#ifdef Debug_Swipe
-    TFT.DL(COLOR_RGB(0,0,0));
-    //sprintf(buffer, "%.2f", distance);
-    //TFT.Text(20, 450, 26, 0, buffer);
-    TFT.Number(20, 450, 26, 0, distance);
-    TFT.Number(80, 450, 26, 0, velocity);
+#ifdef Debug_Swipe                                   // writes touch data to screen
+    TFT.DL(COLOR_RGB(0,0,0));                        // set text color to black
+    TFT.Number(20, 450, 26, 0, distance);            // write distance value to screen
+    TFT.Number(80, 450, 26, 0, velocity);            // write velocity to screen
 #endif
-    // Debug the touch up load
-#ifdef Debug_Touch_File
-    TFT.DL(COLOR_RGB(0,0,0));
-    int i;
-    for (i = 0; i < 24; i++) {
-        TFT.Number(i*30+20, 450, 26, 0, tcal[i]);
-
+    
+#ifdef Debug_Touch_File                              // debug the touch up load
+    TFT.DL(COLOR_RGB(0,0,0));                        // set text color to black
+    int j;                                           // loop counter
+    for (j = 0; j < 24; j++) {                       // write file data to screen
+        TFT.Number(j*30+20, 450, 26, 0, tcal[j]);
     }
 #endif
 
-    TFT.DL(DISPLAY());                     // Display the image
-    TFT.Swap();                            // Swap the current display list
-    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
-    TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
+    TFT.DL(DISPLAY());                     // display the image
+    TFT.Swap();                            // swap the current display list
+    TFT.Flush_Co_Buffer();                 // download the command list into fifo
+    TFT.WaitCmdfifo_empty();               // wait till coprocessor completes the operation
 }
 
+/****************************************************************************/
+/* This function displays the error screen when an SD card isn't detected   */
+/* the function causes the screen to flash yellow and writes text to the LCD*/
+/****************************************************************************/
 void error_screen_SD()
 {
     TFT.DLstart();                         // start a new display command list
-    TFT.DL(CLEAR_COLOR_RGB(255,242,0));
+    TFT.DL(CLEAR_COLOR_RGB(255,242,0));    // clear the screen and set the background to yellow
     TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer
     TFT.DL(COLOR_RGB(0,0,0));
-    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!"); // draw Text with font 31
-    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!"); // draw Text with font 31
-    TFT.DL(DISPLAY());                     // Display the image
-    TFT.Swap();                            // Swap the current display list
-    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
-    TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
+    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!");            // draw Text with font 31
+    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!");   // draw Text with font 31
+    TFT.DL(DISPLAY());                                                      // display the image
+    TFT.Swap();                                                             // swap the current display list
+    TFT.Flush_Co_Buffer();                                                  // download the command list into fifo
+    TFT.WaitCmdfifo_empty();                                                // wait till coprocessor completes the operation
     TFT.Sleep(1000);
 
-    TFT.DLstart();                         // start a new display command list
-    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
-    TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer;
+    TFT.DLstart();                                                          // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,255,255));                                   // set clear color to white
+    TFT.DL(CLEAR(1,1,1));                                                   // clear buffers -> color buffer,stencil buffer, tag buffer;
     TFT.DL(COLOR_RGB(0,0,0));
-    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!"); // draw Text with font 31
-    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!"); // draw Text with font 31
-    TFT.DL(DISPLAY());                     // Display the image
-    TFT.Swap();                            // Swap the current display list
-    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
-    TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
+    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!");            // draw Text with font 31
+    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!");   // draw Text with font 31
+    TFT.DL(DISPLAY());                                                      // display the image
+    TFT.Swap();                                                             // swap the current display list
+    TFT.Flush_Co_Buffer();                                                  // download the command list into fifo
+    TFT.WaitCmdfifo_empty();                                                // wait till coprocessor completes the operation
     TFT.Sleep(1000);
 }
 
+/****************************************************************************/
+/* Checks for an SD card and reads/writes the touchscreen calibration data  */
+/* from the SD card                                                         */
+/****************************************************************************/
 void touchscreen_Calibration()
 {
-    int i = 0;
-    FILE *fp = fopen("/sd/TCal/TCalData.txt", "a");
+    int i = 0;                                                  // loop counter
+    while(card_present.read()) {                                // checks for SD card, if not present hold program until a SD card is inserted
+        error_screen_SD();                                      // write error screen to LCD
+    }
+    FILE *fp = fopen("/sd/TCal/TCalData.txt", "a");             // open touchscreen calibration data from SD card
     fp= fopen("/sd/TCal/TCalData.txt", "r");
-    if(!fp) {
-        //Could not open file for read
-        TFT.Calibrate();                  // calibrate the touch screen
-        TFT.read_calibrate(tcal);         // Read in cal data from screen
-        //Convert from Int to Char to save to SD card
-        /* no longer needed
-        for (i = 0; i < 25; i++) {
-            tstr[i] = tcal[i];
-        }
-        */
-        //Open file for to save calibration data
-        mkdir("/sd/TCal", 0777);
+    if(!fp) {                                                   // could not open file for read so generate data then store on SD card
+        TFT.Calibrate();                                        // calibrate the touch screen
+        TFT.read_calibrate(tcal);                               // read in cal data from screen
+        mkdir("/sd/TCal", 0777);                                // open file for to save calibration data
         FILE *fp = fopen("/sd/TCal/TCalData.txt", "w");
-        //Save integers one at a time to the file
-        for (i = 0; i < 25; i++) {
+        for (i = 0; i < 25; i++) {                              // save integers one at a time to the file
             fprintf(fp, "%c", tcal[i] );
         }
-        fclose(fp);
-    } else {
-        //Read in calibration Data
+        fclose(fp);                                             // close SD card file
+    } else {                                                    // file found, read in data
         fread(tstr,24,1,fp);
-        //Convert from Char to Int
-        for (i = 0; i < 24; i++) {
+        for (i = 0; i < 24; i++) {                              // convert from int to char
             tcal[i] = tstr[i];
         }
-        TFT.write_calibrate(tcal);         // write cal data to screen
+        TFT.write_calibrate(tcal);                              // write cal data to screen
     }
 }
 
+/****************************************************************************/
+/* Loads all of the images from the SD card and assigns them bitmap handles */
+/****************************************************************************/
 void initialize_Images()
 {
-    TFT.DLstart();
-    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
-    TFT.DL(CLEAR(1,1,1));
+    TFT.DLstart();                           // start a new display command list
+    TFT.DL(CLEAR(1,1,1));                    // clear buffers -> color buffer,stencil buffer, tag buffer
 
-    TFT.DL(BITMAP_HANDLE(0));
-    err[0] = TFT.Load_jpg("/sd/Logo.jpg",& x_size,& y_size, 0);
-    TFT.DL(BITMAP_HANDLE(1));
-    err[1] = TFT.Load_jpg("/sd/ITV_1.jpg",& x_size,& y_size, 45000);
-    TFT.DL(BITMAP_HANDLE(2));
-    err[2] = TFT.Load_jpg("/sd/ITV_2.jpg",& x_size,& y_size, 60000);
-    TFT.DL(BITMAP_HANDLE(3));
-    err[3] = TFT.Load_jpg("/sd/PF3W_1.jpg",& x_size,& y_size, 85000);
-    TFT.DL(BITMAP_HANDLE(4));
-    err[4] = TFT.Load_jpg("/sd/PF3W_2.jpg",& x_size,& y_size, 150000);
-    TFT.DL(BITMAP_HANDLE(5));
-    err[5] = TFT.Load_jpg("/sd/PSE_1.jpg",& x_size,& y_size, 230000);
-    TFT.DL(BITMAP_HANDLE(6));
-    err[6] = TFT.Load_jpg("/sd/PSE_2.jpg",& x_size,& y_size, 260000);
+    TFT.DL(BITMAP_HANDLE(0));                                               // assign bitmap handle 0 to logo.jpg
+    err[0] = TFT.Load_jpg("/sd/Logo.jpg",& x_size,& y_size, 0);             // load the jpg and write it to address 0 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(1));                                               // assign bitmap handle 1 to ITV_1.jpg
+    err[1] = TFT.Load_jpg("/sd/ITV_1.jpg",& x_size,& y_size, 45000);        // load the jpg and write it to address 45,000 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(2));                                               // assign bitmap handle 2 to ITV_2.jpg
+    err[2] = TFT.Load_jpg("/sd/ITV_2.jpg",& x_size,& y_size, 60000);        // load the jpg and write it to address 60,000 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(3));                                               // assign bitmap handle 3 to PF3W_1.jpg
+    err[3] = TFT.Load_jpg("/sd/PF3W_1.jpg",& x_size,& y_size, 85000);       // load the jpg and write it to address 85,000 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(4));                                               // assign bitmap handle 4 to PF3W_2.jpg
+    err[4] = TFT.Load_jpg("/sd/PF3W_2.jpg",& x_size,& y_size, 150000);      // load the jpg and write it to address 150,000 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(5));                                               // assign bitmap handle 5 to PSE_1.jpg
+    err[5] = TFT.Load_jpg("/sd/PSE_1.jpg",& x_size,& y_size, 230000);       // load the jpg and write it to address 230,000 in the FT810 RAM and store error result in array
+    TFT.DL(BITMAP_HANDLE(6));                                               // assign bitmap handle 6 to PSE_2.jpg
+    err[6] = TFT.Load_jpg("/sd/PSE_2.jpg",& x_size,& y_size, 260000);       // load the jpg and write it to address 260,000 in the FT810 RAM and store error result in array
 
-    TFT.DL(DISPLAY());                     // Display the image
-    TFT.Swap();                            // Swap the current display list
-    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
+    TFT.DL(DISPLAY());                     // display the image
+    TFT.Swap();                            // swap the current display list
+    TFT.Flush_Co_Buffer();                 // download the command list into fifo
     TFT.WaitCmdfifo_empty();
 }
 
+/****************************************************************************/
+/* Main function that tracks all of the touchscreen data and updates the    */
+/* screen accordingly                                                       */
+/****************************************************************************/
 int main()
 {
-    dac1.wakeup();
+    dac1.wakeup();                                                      // activate DAC boards
     dac2.wakeup();
-    dac1.write_u12(0);
-    dac2.write_u12(0);
-    ft_uint32_t TrackRegisterVal = 0;              // touch track
+    dac1.write_u12(0);                                                  // the natural DAC output value is 50% of its maximum value
+    dac2.write_u12(0);                                                  // the program has to immediately write a 0 to the DAC to turn off the outputs
+    ft_uint32_t TrackRegisterVal = 0;                                   // touch track variable
 
-    TFT.MemWrite(REG_ROTATE, 1);
+    TFT.MemWrite(REG_ROTATE, 1);                                        // rotate screen
     TFT.Rotate(1);
 
-    Start_Screen("Starting...");                // Show start screen
-
-    while(card_present.read()) {
-        error_screen_SD();
-    }
-
-    touchscreen_Calibration();
-
-    initialize_Images();
-    TFT.Track(200, 400, 375, 50, 1);
+    Start_Screen("Starting...");                                        // show start screen
+    touchscreen_Calibration();                                          // load touchscreen calibration data
+    initialize_Images();                                                // load and initialize images
+    
+    TFT.Track(200, 400, 375, 50, 1);                                    // track sliders
     TFT.Track(200, 400, 375, 50, 2);
-    TFT.Flush_Co_Buffer();                         // Download the commands into fifo
-    TFT.WaitCmdfifo_empty();  
-    /* the demo is updating the screen in a endless loop*/
+    TFT.Flush_Co_Buffer();                                              // download the command list into fifo
+    TFT.WaitCmdfifo_empty();                                            // wait till coprocessor completes the operation
+    
     while(1) {
-        // Touch position
-        curY = TFT.Rd32(REG_TOUCH_SCREEN_XY)& 0xffff;
+        curY = TFT.Rd32(REG_TOUCH_SCREEN_XY)& 0xffff;                   // touch positions
         curX = TFT.Rd32(REG_TOUCH_SCREEN_XY)>>16;
-        ft_uint8_t tagval = 0;
-        TrackRegisterVal = TFT.Rd32(REG_TRACKER);    // check if one of the two tracking fields is touched
-        tagval = TrackRegisterVal & 0xff;
-        if(1 == tagval) {
-            ITVpressure1_user_input = (TrackRegisterVal>>20) * (2356.0/4095);
-            ITVpressure1_input_raw = (TrackRegisterVal>>20) * (2356.0/4095) + 590;
-            dac1.write_u12(ITVpressure1_input_raw);
-        } else if(2 == tagval) {               // the slider is touched
-            ITVpressure2_user_input = (TrackRegisterVal>>20) * (2386.0/4095);
-            ITVpressure2_input_raw = (TrackRegisterVal>>20) * (2386.0/4095) + 585;
-            dac2.write_u12(ITVpressure2_input_raw);
-        } else if (200 == tagval) {
+        ft_uint8_t tagval = 0;                                          // initialize return tag value
+        TrackRegisterVal = TFT.Rd32(REG_TRACKER);                       // check if one of the two tracking fields is touched
+        tagval = TrackRegisterVal & 0xff;                               // store return tag value
+        // check what tag was touched
+        if(1 == tagval) {                                                                       // tag value for ITV1 slider touch                             
+            ITVpressure1_user_input = (TrackRegisterVal>>20) * (2356.0/4095);                   // calculate new slider value so that the slider updates graphically
+            ITVpressure1_input_raw = (TrackRegisterVal>>20) * (2356.0/4095) + 590;              // calculate raw 12 bit input number, includes calibration number to never go above 20mA
+            dac1.write_u12(ITVpressure1_input_raw);                                             // write 12 bit number to DAC to fire the ITV
+        } 
+        
+        else if(2 == tagval) {                                                                  // tag value for ITV2 slider touch
+            ITVpressure2_user_input = (TrackRegisterVal>>20) * (2386.0/4095);                   // calculate new slider value so that the slider updates graphically
+            ITVpressure2_input_raw = (TrackRegisterVal>>20) * (2386.0/4095) + 585;              // calculate raw 12 bit input number, includes calibration number to never go above 20mA
+            dac2.write_u12(ITVpressure2_input_raw);                                             // write 12 bit number to DAC to fire the ITV
+        } 
+        
+        else if (200 == tagval) {
             break;
         } else if(curX !=0x8000 && touched>0) {
             touched++;
@@ -445,25 +404,32 @@
             location = (int16_t)paslocation + distance;
 
 
-        } else if(curX !=0x8000 && !touched) {
+        } 
+        
+        else if(curX !=0x8000 && !touched) {
             touched++;
             pasX = curX;
             //
-        } else if(curX ==0x8000 && touched) {
+        } 
+        
+        else if(curX ==0x8000 && touched) {
             velocity = distance/touched;
             paslocation = location;
             touched=0;
 
 
 
-        } else {
+        } 
+        
+        else {
             //page 4 location
             if (location < - 2300 && location > -2500) {
                 if (velocity) {
                     velocity = (location + 2400)/-5;
                     location += (int16_t) velocity;
                     paslocation = location;
-                } else {
+                } 
+                else {
                     location = -2400;
                 }
 
@@ -474,7 +440,8 @@
                     velocity = (location + 1600)/-5;
                     location += (int16_t) velocity;
                     paslocation = location;
-                } else {
+                } 
+                else {
                     location = -1600;
                 }
 
@@ -485,17 +452,20 @@
                     velocity = (location + 800)/-5;
                     location += (int16_t) velocity;
                     paslocation = location;
-                } else {
+                } 
+                else {
                     location = -800;
                 }
                 //page 1
-            } else if (location > -100 && location < 100) {
+            } 
+            else if (location > -100 && location < 100) {
 
                 if (velocity) {
                     velocity = location/-5;
                     location += (int16_t) velocity;
                     paslocation = location;
-                } else {
+                } 
+                else {
                     location = 0;
                 }
                 //no page 0
@@ -504,24 +474,27 @@
                 location += (int16_t) velocity;
                 paslocation = location;
                 //no page 5
-            } else if (location <-1700) {
+            } 
+            else if (location <-1700) {
                 velocity = 10;
                 location += (int16_t) velocity;
                 paslocation = location;
                 //between pages
-            } else if (velocity) {
+            } 
+            else if (velocity) {
                 location += (int16_t) velocity;
                 paslocation = location;
                 //acceleration between pages
                 if (velocity >0) {
                     velocity+= 2;
-                } else {
+                } 
+                else {
                     velocity-= 2;
                 }
             }
         }
 
-        screen_1();                  // paint new screen
-        TFT.Sleep(10);               // wait 10ms for next time to repaint the screen
-    }  // end of display loop
+        Main_Screen();                  // paint new screen
+        TFT.Sleep(10);                  // wait 10ms for next time to repaint the screen
+    }                                   // end of display loop
 }
\ No newline at end of file