WIZwiki W7500 ADC Performance Meter

Dependencies:   SDFileSystem STATIC_COLORS WIZnetInterface mbed

Fork of WIZwiki-W7500_ADC_Sampling by FOURNET Olivier

/media/uploads/Fo170/wizwiki-w7500_adc_performancemeter.png

Files at this revision

API Documentation at this revision

Comitter:
Fo170
Date:
Sun Jul 10 15:23:53 2016 +0000
Parent:
3:cc86b144837b
Commit message:
WIZwiki W7500 ADC Performance Meter

Changed in this revision

WIZwiki_W7500_Interactivity_css.h Show diff for this revision Revisions of this file
WIZwiki_W7500_Interactivity_js.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/WIZwiki_W7500_Interactivity_css.h	Sun Jul 10 00:07:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,103 +0,0 @@
-
-void CSS(void)
-{
- printf_send_client("<style>\r\n");
- printf_send_client("* { padding: 0; margin: 0; vertical-align: top; }\r\n");
-
- printf_send_client("body {\r\n");
-    printf_send_client("background: url(background.png) repeat-x;\r\n");
-    printf_send_client("font: 18px/1.5em \"proxima-nova\", Helvetica, Arial, sans-serif;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("a { color: #069; }\r\n");
- printf_send_client("a:hover { color: #28b; }\r\n");
-
- printf_send_client("h2 {\r\n");
-    printf_send_client("margin-top: 15px;\r\n");
-    printf_send_client("font: normal 32px \"omnes-pro\", Helvetica, Arial, sans-serif;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("h3 {\r\n");
-    printf_send_client("margin-left: 30px;\r\n");
-    printf_send_client("font: normal 26px \"omnes-pro\", Helvetica, Arial, sans-serif;\r\n");
-    printf_send_client("color: #666;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("p {\r\n");
-    printf_send_client("margin-top: 10px;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("button {\r\n");
-    printf_send_client("font-size: 18px;\r\n");
-    printf_send_client("padding: 1px 7px;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("input {\r\n");
-    printf_send_client("font-size: 18px;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("input[type=checkbox] {\r\n");
-    printf_send_client("margin: 7px;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("#header {\r\n");
-    printf_send_client("position: relative;\r\n");
-    printf_send_client("width: 900px;\r\n");
-    printf_send_client("margin: auto;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("#header h2 {\r\n");
-    printf_send_client("margin-left: 10px;\r\n");
-    printf_send_client("vertical-align: middle;\r\n");
-    printf_send_client("font-size: 42px;\r\n");
-    printf_send_client("font-weight: bold;\r\n");
-    printf_send_client("text-decoration: none;\r\n");
-    printf_send_client("color: #000;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("#content {\r\n");
-    printf_send_client("width: 880px;\r\n");
-    printf_send_client("margin: 0 auto;\r\n");
-    printf_send_client("padding: 10px;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("#footer {\r\n");
-    printf_send_client("margin-top: 25px;\r\n");
-    printf_send_client("margin-bottom: 10px;\r\n");
-    printf_send_client("text-align: center;\r\n");
-    printf_send_client("font-size: 12px;\r\n");
-    printf_send_client("color: #999;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client(".demo-container {\r\n");
-    printf_send_client("box-sizing: border-box;\r\n");
-    printf_send_client("width: 850px;\r\n");
-    printf_send_client("height: 450px;\r\n");
-    printf_send_client("padding: 20px 15px 15px 15px;\r\n");
-    printf_send_client("margin: 15px auto 30px auto;\r\n");
-    printf_send_client("border: 1px solid #ddd;\r\n");
-    printf_send_client("background: #fff;\r\n");
-    printf_send_client("background: linear-gradient(#f6f6f6 0, #fff 50px);\r\n");
-    printf_send_client("background: -o-linear-gradient(#f6f6f6 0, #fff 50px);\r\n");
-    printf_send_client("background: -ms-linear-gradient(#f6f6f6 0, #fff 50px);\r\n");
-    printf_send_client("background: -moz-linear-gradient(#f6f6f6 0, #fff 50px);\r\n");
-    printf_send_client("background: -webkit-linear-gradient(#f6f6f6 0, #fff 50px);\r\n");
-    printf_send_client("box-shadow: 0 3px 10px rgba(0,0,0,0.15);\r\n");
-    printf_send_client("-o-box-shadow: 0 3px 10px rgba(0,0,0,0.1);\r\n");
-    printf_send_client("-ms-box-shadow: 0 3px 10px rgba(0,0,0,0.1);\r\n");
-    printf_send_client("-moz-box-shadow: 0 3px 10px rgba(0,0,0,0.1);\r\n");
-    printf_send_client("-webkit-box-shadow: 0 3px 10px rgba(0,0,0,0.1);\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client(".demo-placeholder {\r\n");
-    printf_send_client("width: 100%;\r\n");
-    printf_send_client("height: 100%;\r\n");
-    printf_send_client("font-size: 14px;\r\n");
-    printf_send_client("line-height: 1.2em;\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client(".legend table {\r\n");
-    printf_send_client("border-spacing: 5px;\r\n");
- printf_send_client("}\r\n");
- printf_send_client("</style>\r\n");
-}
\ No newline at end of file
--- a/WIZwiki_W7500_Interactivity_js.h	Sun Jul 10 00:07:14 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,110 +0,0 @@
-void init_WIZwiki_W7500_Interactivity_JS(void)
-{
-sprintf(httpHeader,"<link href=\"" __hebergement__ "electronique/e/WIZwiki-W7500/css/WIZwiki-W7500_Interactivity.css\" rel=\"stylesheet\" type=\"text/css\">\r\n");
-client.send(httpHeader,strlen(httpHeader));
-
-sprintf(httpHeader,"<!--[if lte IE 8]><script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "js/flot/excanvas.min.js\"></script><![endif]-->\r\n");
-client.send(httpHeader,strlen(httpHeader));
-
-sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "js/jquery.js\"></script>\r\n");
-client.send(httpHeader,strlen(httpHeader));
-
-sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "js/flot/jquery.flot.js\"></script>\r\n");
-client.send(httpHeader,strlen(httpHeader));
-/*
-sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_fct.js\"></script>");
-client.send(httpHeader,strlen(httpHeader));*/
-}
-
-void WIZwiki_W7500_Interactivity_div(void)
-{
- printf_send_client("<p>\r\n");
- printf_send_client("<div id=\"header\"><h2>Interactivity</h2></div>\r\n");
- printf_send_client("<div id=\"content\">\r\n");
- printf_send_client("<div class=\"demo-container\"><div id=\"placeholder\" class=\"demo-placeholder\"></div></div>\r\n");
- printf_send_client("<p>X : Time (s) , Y : Vmoy (V)<p>One of the goals of Flot is to support user interactions. Try pointing and clicking on the points.</p><p>\r\n");
- printf_send_client("<label><input id=\"enablePosition\" type=\"checkbox\" checked=\"checked\"></input>Show mouse position</label>\r\n");
- printf_send_client("<span id=\"hoverdata\"></span>\r\n");
- printf_send_client("<span id=\"clickdata\"></span>\r\n");
- printf_send_client("</p>\r\n");
- printf_send_client("<p>A tooltip is easy to build with a bit of jQuery code and the data returned from the plot.</p>\r\n");
- printf_send_client("<p><label><input id=\"enableTooltip\" type=\"checkbox\" checked=\"checked\"></input>Enable tooltip</label></p>\r\n");
- printf_send_client("</div>\r\n");
- printf_send_client("<div id=\"footer\">&nbsp;Copyright &copy; 2007 - 2014 IOLA and Ole Laursen</div>\r\n");
- printf_send_client("<p>\r\n");
-}
-
-void WIZwiki_W7500_Interactivity_JS(void)
-{
-/*
-sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_fct.js\"></script>");
-client.send(httpHeader,strlen(httpHeader));*/
-printf_send_client("<script language=\"javascript\" type=\"text/javascript\">\r\n");
-
- printf_send_client("$(function() {\r\n");
-
- printf_send_client("var plot = $.plot(\"#placeholder\",\r\n");
- printf_send_client("[\r\n");
- printf_send_client("{ data: array_value, label: label_Y, color: color_Y }\r\n");
- printf_send_client("],\r\n");
- printf_send_client("{\r\n");
- printf_send_client("series: {\r\n");
- printf_send_client("lines: { show: true },\r\n");
- printf_send_client("points: { show: true }\r\n");
- printf_send_client("},\r\n");
- printf_send_client("grid: { hoverable: true, clickable: true },\r\n");
- printf_send_client("xaxis: { min: x_min, max: x_max },\r\n");
- printf_send_client("yaxis: { min: y_min, max: y_max }\r\n");
- printf_send_client("});\r\n");
-
- printf_send_client("$(\"<div id='tooltip'></div>\").css({\r\n");
- printf_send_client("position: \"absolute\",\r\n");
- printf_send_client("border: \"1px solid #fdd\",\r\n");
- printf_send_client("padding: \"2px\",\r\n");
- printf_send_client("\"background-color\": \"#fee\",\r\n");
- printf_send_client("opacity: 0.80\r\n");
- printf_send_client("}).appendTo(\"body\");\r\n");
-
- printf_send_client("$(\"#placeholder\").bind(\"plothover\", function (event, pos, item)\r\n");
- printf_send_client("{\r\n");
-
- printf_send_client("if($(\"#enablePosition:checked\").length > 0)\r\n");
- printf_send_client("{\r\n");
- printf_send_client("var str = \"(\" + pos.x.toFixed(2) + \", \" + pos.y.toFixed(2) + \")\";\r\n");
- printf_send_client("$(\"#hoverdata\").text(str);\r\n");
- printf_send_client("}\r\n");
-
- printf_send_client("if($(\"#enableTooltip:checked\").length > 0)\r\n");
- printf_send_client("{\r\n");
- printf_send_client("if(item)\r\n");
- printf_send_client("{\r\n");
- printf_send_client("var x = item.datapoint[0].toFixed(2),\r\n");
- printf_send_client("y = item.datapoint[1].toFixed(2);\r\n");
-
- printf_send_client("$(\"#tooltip\").html(item.series.label + \" of \" + x + \" = \" + y)\r\n");
- printf_send_client(".css({top: item.pageY+5, left: item.pageX+5})\r\n");
- printf_send_client(".fadeIn(200);\r\n");
- printf_send_client("}\r\n");
- printf_send_client("else\r\n");
- printf_send_client("{\r\n");
- printf_send_client("$(\"#tooltip\").hide();\r\n");
- printf_send_client("}\r\n");
- printf_send_client("}\r\n");
- printf_send_client("});\r\n");
-
- printf_send_client("$(\"#placeholder\").bind(\"plotclick\", function (event, pos, item)\r\n");
- printf_send_client("{\r\n");
- printf_send_client("if(item)\r\n");
- printf_send_client("{\r\n");
- printf_send_client("$(\"#clickdata\").text(\" - click point \" + item.dataIndex + \" in \" + item.series.label);\r\n");
- printf_send_client("plot.highlight(item.series, item.datapoint);\r\n");
- printf_send_client("}\r\n");
- printf_send_client("});\r\n");
-
- printf_send_client("// Add the Flot version string to the footer\r\n");
-
- printf_send_client("$(\"#footer\").prepend(\"( http://www.flotcharts.org/flot/examples ) Flot \" + $.plot.version + \" &ndash;\");\r\n");
- printf_send_client("});\r\n");
- 
- printf_send_client("</script>\r\n");
-}
\ No newline at end of file
--- a/main.cpp	Sun Jul 10 00:07:14 2016 +0000
+++ b/main.cpp	Sun Jul 10 15:23:53 2016 +0000
@@ -4,28 +4,8 @@
 #include <stdio.h>
 #include <string.h>
 
-#define USE_DHCP    1
-/*
-MAC Address Details ( http://www.macvendorlookup.com/ )
-Company Wiznet Address
-Seoul 135-830
-Nonyhun, Kangnam
-KOREA, REPUBLIC OF
-Range : 00:08:DC:00:00:00 - 00:08:DC:FF:FF:FF
-Type : MA-L: IEEE MAC Address Large (24-bit block size)
-*/
-//#define MAC     "\x31\x41\x59\x26\x53\x58"
-#define MAC     "\x00\x08\xDC\x31\x41\x59"
-//#define MAC     "\x00\x08\xDC\x11\x34\x78"
-#define IP      "192.168.0.170"
-#define MASK    "255.255.255.0"
-#define GATEWAY "192.168.0.254"
-
-#define HTTPD_SERVER_PORT   80
-#define HTTPD_MAX_REQ_LENGTH   1023
-#define HTTPD_MAX_HDR_LENGTH   255
-#define HTTPD_MAX_FNAME_LENGTH   127
-#define HTTPD_MAX_DNAME_LENGTH   127
+// https://developer.mbed.org/users/chris/notebook/Getting-best-ADC-performance/
+// https://developer.mbed.org/users/chris/code/ADCPerformanceMeter/
 
 #if defined(TARGET_WIZwiki_W7500)
 Serial uart(USBTX, USBRX);
@@ -36,12 +16,9 @@
 // LED BLUE  : socket connecting status Busy
 #endif
 
-EthernetInterface eth;
-TCPSocketServer server;
-TCPSocketConnection client;
+#define HTTPD_MAX_FNAME_LENGTH   127
+#define HTTPD_MAX_DNAME_LENGTH   127
 
-char buffer[HTTPD_MAX_REQ_LENGTH+1];
-char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
 char *uristr;
@@ -53,424 +30,296 @@
 
 // Initialize a pins to perform analog input and digital output fucntions
 AnalogIn   ain0(A0);
-AnalogIn   ain1(A1);
-AnalogIn   ain2(A2);
-AnalogIn   ain3(A3);
 
-float a0_f, a1_f, a2_f, a3_f;
-
-#define __IP_LOCAL__           IP
-#define  __hebergement__                  "http://olivier.fournet.free.fr/"
-#define  __Time_between_page_refresh__    "1"
-//#include <hebergement.h>
-
-int refresh = 1; // 1 second refresh
-
-#define NB_SAMPLES_GPH    10
-unsigned long int Sample_gph = 0;
-float adc_samples[NB_SAMPLES_GPH];// = { 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.01,1.02,1.03,1.04 };
-float time_samples[NB_SAMPLES_GPH];// = { -0.1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
-float x_min = 0.0, x_max = 0.0;
-float y_min = 0.0, y_max = 0.0;
-float Seconds = 0.0;
-//float meas = 0.0;
-
-//------------
-#define      __Time_tic_in_Second__      0.1
-
-Ticker time_tic;
-
-//------------
-
-#define FREQUENCE_SECTEUR             50
-#define NB_SAMPLES_PAR_OSCILLATION    100
-#define NB_SAMPLES                    FREQUENCE_SECTEUR * NB_SAMPLES_PAR_OSCILLATION // 5000   // FREQUENCE_SECTEUR * NB_SAMPLES_PAR_OSCILLATION
-#define TIME_SAMPLES_us               1000000 / NB_SAMPLES                           // 200    // 1000000 / NB_SAMPLES
-unsigned long int Samples = 0;
-
-float time_between_two_measurement_ADC = 0.0;
-float meas, meas_sum, meas_moy, meas_min, meas_max;
-float vdc, vdc_min, vdc_max;
-float vac, pow2_vac, sum_pow2_vac, veff;
-
-Ticker time_measurement_ADC;
-
-void measurement_ADC(void)
-{
- float meas_moy_t, veff_t; // valeurs temporaires
+#define NUM_SAMPLES 500000 // size of sample series
+#define SAMPLE_BLOCKS 5
  
-    meas = 3.3 * ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
-    //wait_us(10);
-    meas_sum += meas;
-    if(meas_min > meas) meas_min = meas;
-    if(meas_max < meas) meas_max = meas;
-
-    vac = meas - 1.650; // 0V AC = 3,3V/2
-    pow2_vac = vac * vac; // valeur VAC au carré
-    sum_pow2_vac += pow2_vac; // somme des valeurs AC au carré
-
-    Samples++;
-
-    if( Samples == NB_SAMPLES ) 
-    {
-        // VDC
-        meas_moy_t = meas_sum;
-        meas_moy = meas_moy_t / (float)NB_SAMPLES;
-        // VAC
-        veff_t = sum_pow2_vac;
-        veff_t = veff_t / (float)NB_SAMPLES;
-        veff = sqrt(veff_t);
-
-        Samples = 0;
-        meas_sum = 0.0;
-        vdc_min = meas_min, vdc_max = meas_max;
-        meas_min = 3.3 , meas_max = 0.0;
-        sum_pow2_vac = 0.0;
-    }
-}
-
-void init_ADC_sampling(void)
-{
- //-------------
- meas_moy = 0.0;
- Samples = 0;
- meas_sum = 0.0;
- meas_min = 3.3 , meas_max = 0.0;
- sum_pow2_vac = 0.0;
- //-----------------
- // 50 Hz --> 20ms
- // 100 samples par oscillations = 20 / 100 = 0.2ms = 200µs
- // 100 samples * 50 oscillations = 5000 samples au total par secondes
- // interval: 200 micro seconds chaques samples
- time_measurement_ADC.attach_us(&measurement_ADC, TIME_SAMPLES_us);   
-}
-//------------
-
-void add_one_tic()
-{       
- int i;
- 
- Seconds = Seconds + (float)__Time_tic_in_Second__;
- 
- // mesures ADC  --> measurement_ADC();
- /*
-    meas = ain0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
-    meas = meas * 3.3; // Change the value to be in the 0 to 3300 range
-  */  
-    x_min = x_max = Seconds;
-    y_min = y_max = meas_moy; //meas;
-                    
-    for(i = 1 ; i < NB_SAMPLES_GPH ; i++)
-    {
-     time_samples[i-1] = time_samples[i];
-     adc_samples[i-1] = adc_samples[i];
-     if( time_samples[i] < x_min ) x_min = time_samples[i];
-     if( time_samples[i] > x_max ) x_max = time_samples[i];
-     if( adc_samples[i] < y_min ) y_min = adc_samples[i];
-     if( adc_samples[i] > y_max ) y_max = adc_samples[i];
-    }
-
-    adc_samples[NB_SAMPLES_GPH-1] = meas_moy; //meas;
-    time_samples[NB_SAMPLES_GPH-1] = Seconds;
-}
-//------------
+Timer t;
 
 Ticker ledTick;
 
 char *pch;
 char ext[5];
-char ext_gif[] = "gif";
-char ext_jpg[] = "jpg";
-char ext_png[] = "png";
-char ext_tiff[] = "tiff";
+
 int pos_ext;
 int extLen;
 
-void ledTickfunc()
-{
-    led_r = !led_r;
-}
-
-void printf_send_client(const char *str_c)
-{
- char http_send[HTTPD_MAX_REQ_LENGTH+1];   
- sprintf(http_send,str_c);
- client.send(http_send,strlen(http_send));
-}
-
-#include "WIZwiki_W7500_Interactivity_css.h"
-#include "WIZwiki_W7500_Interactivity_js.h"
+//---------------------------
+int samples = 0;
+int num_4     = 0;
+int num_8     = 0;
+int num_16    = 0;
+int num_32    = 0;
+int num_64    = 0;
+int num_128   = 0;
+int num_256   = 0;
+int num_512   = 0;
+int num_1024  = 0;
+int num_spike = 0; 
+   
+float AVERAGE = 0.5;
+float AVERAGE_MOY = 0.5;
+float average_min = 0.0;
+float average_max = 3.3;
+float average_error = 3.0303;  // Par défaut : 3.0303% sur la valeur centrale 1.65V
 
-void variables(void)
-{
- printf_send_client("<SCRIPT script language=\"javascript\" type=\"text/javascript\">\r\n");
- 
- a0_f = ain0.read()*3.3;
- sprintf(httpHeader,"A0 = %3.3f;\r\n", a0_f);
- client.send(httpHeader,strlen(httpHeader));
- 
- a1_f = ain1.read()*3.3;
- sprintf(httpHeader,"A1 = %3.3f;\r\n", a1_f);
- client.send(httpHeader,strlen(httpHeader));
- 
- a2_f = ain2.read()*3.3;
- sprintf(httpHeader,"A2 = %3.3f;\r\n", a2_f);
- client.send(httpHeader,strlen(httpHeader));
- 
- a3_f = ain3.read()*3.3;
- sprintf(httpHeader,"A3 = %3.3f;\r\n", a3_f);
- client.send(httpHeader,strlen(httpHeader));
- 
- printf_send_client("</SCRIPT>\r\n");
-}
-
-void ETAT(void)
+#define __AVERAGE_AUTO__    1
+        
+void PerformanceMeter(void)
 {
-  int i;
-  
-  // httpHeader
-  printf_send_client("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
-            
-  // Début page Web
-  printf_send_client("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n");
-  // meta_refresh
-  sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"" __Time_between_page_refresh__ ";url=http://%s/\">\r\n", eth.getIPAddress() );
-  //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"1;url=http://%s/\">\r\n", eth.getIPAddress() );
-  //sprintf(httpHeader,"<meta http-equiv=\"refresh\" content=\"%u;url=http://%s/\">\r\n", refresh, eth.getIPAddress() );
-  client.send(httpHeader,strlen(httpHeader));
-  
-  printf_send_client("<html><head>\r\n");
-  // title
-  printf_send_client("<title>WIZwiki-W7500 - Flot Examples: Interactivity</title>\r\n");
-  printf_send_client("<LINK REL=\"SHORTCUT ICON\" type=\"image/x-icon\" href=\"" __hebergement__ "favicon.ico\">\r\n<link rel=\"icon\" href=\"" __hebergement__ "favicon.ico\" type=\"image/x-icon\">\r\n");
-  // CSS
-  CSS();
-  // JavaScript Interactivity
-  init_WIZwiki_W7500_Interactivity_JS();
-  WIZwiki_W7500_Interactivity_JS(); 
-  /*sprintf(httpHeader,"<script language=\"javascript\" type=\"text/javascript\" src=\"" __hebergement__ "electronique/e/WIZwiki-W7500/js/WIZwiki-W7500_Interactivity_init.js\"></script>\r\n");
-  client.send(httpHeader,strlen(httpHeader));
-  */
-  //printf_send_client("<script language=\"javascript\" type=\"text/javascript\">init_WIZwiki_W7500_Interactivity();</script>\r\n");
-  
-  // Variables JavaScript
-  printf_send_client("<script language=\"javascript\" type=\"text/javascript\">\r\n");
-  printf_send_client("var color_Y = \"#FF0000\";\r\n");
-  printf_send_client("var label_Y = \"Meas Moy (V)\";\r\n");
-  // sprintf(httpHeader, "var x_min = -0.5, x_max =  14.5, y_min = -0.5, y_max =  1.5;\r\n"); // TEST
-  sprintf(httpHeader, "var x_min = %.1f, x_max = %.1f, y_min = %.1f, y_max = %.1f;\r\n", x_min, x_max, y_min, y_max);
-  client.send(httpHeader,strlen(httpHeader));
-  
-  // sprintf(httpHeader, "var array_value = [[-0.1,0.1],[2,0.2],[3,0.3],[4,0.4],[5,0.5],[6,0.6],[7,0.7],[8,0.8],[9,0.9],[10,1],[11,1.01],[12,1.02],[13,1.03],[14,1.04]];\r\n"); // TEST
-  // client.send(httpHeader,strlen(httpHeader));  // TEST
-  
-  if(Sample_gph > NB_SAMPLES_GPH)
+ float r = 0.0;
+ //---------------------------------
+ // Take the average over 500,000 samples
+ // This is because the bias to 1.65v has a tolerance
+ // adjust the analog input to 1.65V, if NO AUTO MODE
+ 
+ uart.printf("Taking an average over %d samples\r\n",NUM_SAMPLES);
+ 
+ if( __AVERAGE_AUTO__ ) 
+ {
+     COLOR(_BLUE_);
+     samples = 0;
+     average_error = 5.0; // 5% initiale
+     uart.printf("AVERAGE AUTO : error %f%%\r\n", average_error);
+     
+     while(samples < NUM_SAMPLES)
+     {
+      r = ain0.read();
+ 
+            AVERAGE_MOY += r;
+            samples++;
+     }
+     
+     AVERAGE_MOY /= NUM_SAMPLES;
+     average_min = AVERAGE_MOY - (AVERAGE_MOY * average_error / 100.0);
+     average_max = AVERAGE_MOY + (AVERAGE_MOY * average_error / 100.0);
+     uart.printf("Average Moyenne (for 5%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max);
+     samples = 0;
+     
+     while(samples < NUM_SAMPLES) 
+     {
+        r = ain0.read();
+ 
+        if((r > average_min) && (r < average_max))
+        {
+            AVERAGE += r;
+            samples++;
+        }
+     }
+    
+    AVERAGE /= NUM_SAMPLES;
+    average_min = AVERAGE - (AVERAGE * average_error / 100.0);
+    average_max = AVERAGE + (AVERAGE * average_error / 100.0);
+    uart.printf("Average (for 5%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max);
+    // étalonage à 1% --------------------
+    average_error = 1.0; // 1%
+    COLOR(_YELLOW_);
+    samples = 0;
+    
+    while(samples < NUM_SAMPLES) 
     {
-     printf_send_client("var array_value = [");
-
-     for(i = 0 ; i < NB_SAMPLES_GPH ; i++)
-     {
-      if(i < NB_SAMPLES_GPH-1) sprintf(httpHeader, "[%.1f,%.1f],", time_samples[i], adc_samples[i]);
-      else sprintf(httpHeader, "[%.1f,%.1f]", time_samples[i], adc_samples[i]);
-      client.send(httpHeader,strlen(httpHeader));
-     }
-     printf_send_client("];\r\n");
+        r = ain0.read();
+ 
+        if((r > average_min) && (r < average_max))
+        {
+            AVERAGE += r;
+            samples++;
+        }
     }
     
-    Sample_gph++;
-    printf_send_client("</script>\r\n");
+    AVERAGE /= NUM_SAMPLES;
+    average_min = AVERAGE - (AVERAGE * average_error / 100.0);
+    average_max = AVERAGE + (AVERAGE * average_error / 100.0);
+    uart.printf("Average (for 1%%) = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max); 
+ }
+ else
+ {
+  COLOR(_PURPLE_);
+  average_min = 1.65 - (1.65 * average_error / 100.0); // 0.45 --> -3,0303% of 1.65V --> 1.485V
+  average_max = 1.65 + (1.65 * average_error / 100.0); // 0.55 --> +3,0303% of 1.65V --> 1.815V
+  samples = 0;
+  
+  while(samples < NUM_SAMPLES) 
+    {
+        r = ain0.read();
+ 
+        if((r > average_min) && (r < average_max))  // 0.7425V à 0.9075
+        {
+            AVERAGE += r;
+            samples++;
+        }
+    }
+ 
+    AVERAGE /= NUM_SAMPLES;
+    uart.printf("Average = %f , Min = %f , Max = %f\r\n",3.3 * AVERAGE, 3.3 * average_min, 3.3 * average_max);
+ }
+ 
+   samples = 0;  
+    
+    // Now start sampling series of 500,000
+    // acculumating the errors seen in each range
+    uart.printf("Profiling %d samples\r\n",SAMPLE_BLOCKS*NUM_SAMPLES);
+    float a;
     
-    // <SCRIPT>
-    variables();
-    // <FIN SCRIPT>
-    // Fin Variable JavaScript
-    printf_send_client("</head><body><center>\r\n");
-            
-  sprintf(httpHeader,"<h2>WIZwiki-W7500 - mBED</h2> ( Compiled at : %s and %s )<p>\r\n",  __DATE__ , __TIME__);
-  client.send(httpHeader,strlen(httpHeader));
-   
-  printf_send_client("<p>(<a href=\"http://www.flotcharts.org/flot/examples/interacting/index.html\">Flot Examples: Interactivity</a>)<p>\r\n");
-  
-  printf_send_client("ETAT :<p>\r\n");
-  
-  sprintf(httpHeader,"IP: %s, MASK: %s, GW: %s<p>\r\n",
-                      eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
-  client.send(httpHeader,strlen(httpHeader));
-  
-  sprintf(httpHeader,"&tilde;A0 : %3.3fV , \r\n", a0_f);
-  client.send(httpHeader,strlen(httpHeader));
+    for(int j=0; j < SAMPLE_BLOCKS ; j++) 
+    {
+        t.reset();
+        t.start();
+        COLOR(_WHITE_);
+        //uart.printf("%d SAMPLE BLOCKS\r\n",j);
+        
+        for(int i = 0; i < NUM_SAMPLES ; i++) 
+        {
+            a = ain0.read();
+ 
+            if(a == 1.0) {
+                num_spike++;    // > 2048 lsb
+            } else if (a > (AVERAGE + 0.2500)) {
+                num_1024++;    // > 1024 lsb
+            } else if (a > (AVERAGE + 0.0625)) {
+                num_512++;    // > 512 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_256++;    // > 256 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_128++;    // > 128 lsb
+            } else if (a > (AVERAGE + 0.0156)) {
+                num_64++;    // > 64 lsb
+            } else if (a > (AVERAGE + 0.0078)) {
+                num_32++;    // > 32 lsb
+            } else if (a > (AVERAGE + 0.0039)) {
+                num_16++;    // > 16 lsb
+            } else if (a > (AVERAGE + 0.0019)) {
+                num_8++;    // > 8 lsb
+            } else if (a > (AVERAGE + 0.0009)) {
+                num_4++;    // > 8 lsb
+            }
+ 
+            samples++;
+        }
+        t.stop();
+        COLOR(_BLACK_);
+        if(j==0)
+        {
+            uart.printf("Samples\t");
+            uart.printf("4\t");
+            uart.printf("8\t");
+            uart.printf("16\t");
+            uart.printf("32\t");
+            uart.printf("64\t");
+            uart.printf("128\t");
+            uart.printf("256\t");
+            uart.printf("512\t");
+            uart.printf("1024\t");
+            uart.printf("Spikes\t");
+            uart.printf("Time\r\n");
+        }
+        
+        // Every 500,000 print the results
+        uart.printf("%d\t",samples);
+        uart.printf("%d\t",num_4);
+        uart.printf("%d\t",num_8);
+        uart.printf("%d\t",num_16);
+        uart.printf("%d\t",num_32);
+        uart.printf("%d\t",num_64);
+        uart.printf("%d\t",num_128);
+        uart.printf("%d\t",num_256);
+        uart.printf("%d\t",num_512);
+        uart.printf("%d\t",num_1024);
+        uart.printf("%d\t",num_spike);
+        uart.printf("%fs\r\n",t.read());
+    }
+ 
+    uart.printf("==== Test Complete ====\r\n");
+    COLOR(_GREEN_);
+    //------------------------------- 
+    // TEST continu
+    uart.printf("==== Test Continu avec %d Samples ====\r\n", NUM_SAMPLES / 10);
+    samples = 0;
+    
+    while(1) 
+    {
+            a = ain0.read();
+            samples++;
             
-  sprintf(httpHeader,"&tilde;A1 : %3.3fV , \r\n", a1_f);
-  client.send(httpHeader,strlen(httpHeader));
-            
-  sprintf(httpHeader,"&tilde;A2 : %3.3fV , \r\n", a2_f);
-  client.send(httpHeader,strlen(httpHeader));
+            if(a == 1.0) {
+                num_spike++;    // > 2048 lsb
+            } else if (a > (AVERAGE + 0.2500)) {
+                num_1024++;    // > 1024 lsb
+            } else if (a > (AVERAGE + 0.0625)) {
+                num_512++;    // > 512 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_256++;    // > 256 lsb
+            } else if (a > (AVERAGE + 0.0312)) {
+                num_128++;    // > 128 lsb
+            } else if (a > (AVERAGE + 0.0156)) {
+                num_64++;    // > 64 lsb
+            } else if (a > (AVERAGE + 0.0078)) {
+                num_32++;    // > 32 lsb
+            } else if (a > (AVERAGE + 0.0039)) {
+                num_16++;    // > 16 lsb
+            } else if (a > (AVERAGE + 0.0019)) {
+                num_8++;    // > 8 lsb
+            } else if (a > (AVERAGE + 0.0009)) {
+                num_4++;    // > 8 lsb
+            }
             
-  sprintf(httpHeader,"&tilde;A3 : %3.3fV<p>\r\n", a3_f);
-  client.send(httpHeader,strlen(httpHeader));
-  
-  sprintf(httpHeader, "ADC (A0) :<p>DC : %.3fVmoy ( sum : %.3f - Samples : %u )<br>\r\n", meas_moy, meas_sum, Samples);
-  client.send(httpHeader,strlen(httpHeader));
-    
-  sprintf(httpHeader, "DC (0 &agrave; 3,3V) : %.3fVmin,   %.3fVmax , Vdiff : %.0fmV<br>\r\n", vdc_min, vdc_max, 1000.0*(vdc_max - vdc_min));
-  client.send(httpHeader,strlen(httpHeader));
-    
-  sprintf(httpHeader, "AC (-1,65V &agrave; 1,65V) : %.3fVeff<p>\r\n", veff);
-  client.send(httpHeader,strlen(httpHeader));
-  
-  sprintf(httpHeader, "Time : %.1f Seconds - Sample Graphique : %u<p>\r\n", Seconds, Sample_gph);// diplays the human readable Seconds
-  client.send(httpHeader,strlen(httpHeader));
-  
-  WIZwiki_W7500_Interactivity_div();
-  //printf_send_client("<p><script language=\"javascript\" type=\"text/javascript\">WIZwiki_W7500_Interactivity();</script><p>\r\n");
-  
-  printf_send_client("<p><p><a href=\"..\">Root</a>\r\n");
-  
-  printf_send_client("</center></body></html>\r\n");               
+        if(samples == NUM_SAMPLES / 10)
+        {
+         uart.printf("Samples\t");
+         uart.printf("4\t");
+         uart.printf("8\t");
+         uart.printf("16\t");
+         uart.printf("32\t");
+         uart.printf("64\t");
+         uart.printf("128\t");
+         uart.printf("256\t");
+         uart.printf("512\t");
+         uart.printf("1024\t");
+         uart.printf("Spikes\r\n");
+         // Every 500,000 print the results
+         uart.printf("%d\t",samples);
+         uart.printf("%d\t",num_4);
+         uart.printf("%d\t",num_8);
+         uart.printf("%d\t",num_16);
+         uart.printf("%d\t",num_32);
+         uart.printf("%d\t",num_64);
+         uart.printf("%d\t",num_128);
+         uart.printf("%d\t",num_256);
+         uart.printf("%d\t",num_512);
+         uart.printf("%d\t",num_1024);
+         uart.printf("%d\r\n\r\n",num_spike);
+         num_4 = 0;
+         num_8 = 0;
+         num_16 = 0;
+         num_32 = 0;
+         num_64 = 0;
+         num_128 = 0;
+         num_256 = 0;
+         num_512 = 0;
+         num_1024 = 0;
+         num_spike = 0;
+         samples = 0;
+        }
+    } 
 }
 
 //--------------------------------------------
 
 int main(void)
 {
-    // initialisation des variables
- int i;
- 
- init_ADC_sampling();
- //-----------------
- 
- for(i = 0 ; i < NB_SAMPLES_GPH ; i++)
- {
-      time_samples[i] = 0;
-      adc_samples[i] = 0.0;
- }
- // Init the ticker with the address of the function (add_one_second) to be attached and the interval (1000 ms)
- time_tic.attach(&add_one_tic, __Time_tic_in_Second__);
- //--------------
-    ledTick.attach(&ledTickfunc,0.5);
-    // Serial Interface eth;
-    // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit
-    uart.baud(9600);
-    uart.format(8, SerialBase::None, 1);
-    uart.printf("Initializing\n");
-  wait(1.0);
-//    Check File System
-    uart.printf("Checking File System\n");
-    DIR *d = opendir("/sd/");
-    if(d != NULL) 
-    {
-        uart.printf("SD Card Present\n");
-    } 
-    else 
-    {
-        uart.printf("SD Card Root Directory Not Found\n");
-    }
-   wait(1.0);
-//    EthernetInterface eth;
-    uart.printf("Initializing Ethernet\n");
-    #if USE_DHCP
-    //eth.init Use DHCP
-    int ret = eth.init((uint8_t*)MAC);    // Use DHCP for WIZnetInterface
-    uart.printf("Connecting DHCP\n");
-    #else
-    int ret = eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
-    uart.printf("Connecting (IP,mask,Gateway)\n");
-    #endif
-    wait(1.0);
-    // Check Ethernet Link-Done
-    uart.printf("Check Ethernet Link\r\n");
+ // initialisation des variables
+
     
-    if(eth.link() == true) 
-    { 
-     uart.printf("- Ethernet PHY Link - Done\r\n");
-     //led_r = LED_ON;
-     COLOR(_RED_);
-    }
-    else 
-    {
-     uart.printf("- Ethernet PHY Link - Fail\r\n");
-     //led_r = LED_OFF;
-     COLOR(_BLACK_);
-    }
-    wait(1.0);
-    if(!ret) 
-    {
-     uart.printf("Initialized, MAC: %s\r\n", eth.getMACAddress());
-     ret = eth.connect();
-     
-     if(!ret) 
-     {
-            uart.printf("IP: %s, MASK: %s, GW: %s\r\n",
-                      eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
-      // led_b = LED_ON, led_g = LED_ON;
-      COLOR(_CYAN_);
-     } 
-     else 
-     {
-            uart.printf("Error ethernet.connect() - ret = %d\r\n", ret);
-      //led_b = LED_OFF;
-      COLOR(_BLUE_);
-      exit(0);
-     }
-    } 
-    else 
-    {
-        uart.printf("Error ethernet.init() - ret = %d\r\n", ret);
-     //led_b = LED_OFF;
-     COLOR(_BLACK_);
-     exit(0);
-    }    
-    wait(1.0);
-//    TCPSocketServer server;
-    server.bind(HTTPD_SERVER_PORT);
-    server.listen();
-    uart.printf("Server Listening\n");
-
-    while(true) 
-    {
-        uart.printf("\nWait for new connection...\r\n");
-        server.accept(client);
-        client.set_blocking(false, 1500); // Timeout after (1.5)s
-
-        uart.printf("Connection from: %s\r\n", client.get_address());
-        while(true) 
-        {
-            //led_g = LED_ON;
-            COLOR(_GREEN_);
-            int n = client.receive(buffer, sizeof(buffer));
-            if(n <= 0) break;
-            uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
-            if(n >= 1024) 
-            {
-                sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
-                client.send(httpHeader,strlen(httpHeader));
-                client.send(buffer,n);
-                break;
-            } 
-            else 
-            {
-                buffer[n]=0;
-            }
-            if(!strncmp(buffer, "GET ", 4)) 
-            {
-                uristr = buffer + 4;
-                eou = strstr(uristr, " ");
-                if(eou == NULL) 
-                {
-                    sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
-                    client.send(httpHeader,strlen(httpHeader));
-                    client.send(buffer,n);
-                } 
-                else 
-                {
-                    *eou = 0;
-                    //get_file(uristr);
-                    ETAT();
-                }
-            }
-        }
-        //led_g = LED_OFF;
-        COLOR(_BLACK_);
-        client.close();
-    }
+ //--------------
+ COLOR(_BLACK_);
+ // Serial Interface eth;
+ // Serial port configuration (valeurs par defaut) : 9600 baud, 8-bit data, no parity, stop bit
+ uart.baud(9600);
+ uart.format(8, SerialBase::None, 1);
+ COLOR(_RED_);
+ uart.printf("WIZwiki-W7500 - mBED ( Compiled at : %s and %s )\r\n",  __DATE__ , __TIME__);
+ uart.printf("Initializing\n\r");
+ wait(1.0);
+   
+ PerformanceMeter();
 }