Captures digital signals and then displays them in a web browser using SVG.

Dependencies:   EthernetNetIf mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPServer.h"
00004 
00005 #define SAMPLE_COUNT 1000
00006 
00007 LocalFileSystem local("local");
00008 
00009 DigitalOut led1(LED1);
00010 DigitalIn sda(p9);
00011 DigitalIn scl(p10);
00012 
00013 char sdamem[SAMPLE_COUNT];
00014 char sclmem[SAMPLE_COUNT];
00015 
00016 EthernetNetIf eth;
00017 HTTPServer svr;
00018 
00019 void capture() {
00020     scl.mode(PullUp);
00021      while (scl) {
00022         wait_us(2);
00023     }
00024     for (int i = 0; i < SAMPLE_COUNT; i++) {
00025         sdamem[i] = sda;
00026         sclmem[i] = scl; 
00027         wait_us(1);
00028     }
00029 }
00030 
00031 void plot(FILE* fp, char * name, char * mem, int offset) {
00032     fprintf(fp, "<g transform='translate(100,%i)'>", offset);
00033     fprintf(fp, "<text x='10' y='40' style='fill:black;stroke:none;font-size:16pt;font-family:arial'>%s</text>", name);
00034     fprintf(fp, "<path d='M 50 50 ");
00035     for (int i = 0; i < SAMPLE_COUNT; i++) {
00036        fprintf(fp, "L %i %i %i %i ", 50+ 5*i, 50*mem[i], 55+5*i, 50*mem[i]);
00037     }
00038     fprintf(fp, "' />\n");
00039     fprintf(fp, "</g>");
00040 }
00041 
00042 void writeSVG() {
00043     FILE *fp = fopen("/local/out.svg", "w");  // Open "out.txt" on the local file system for writing
00044     fprintf(fp, "<svg xmlns='http://www.w3.org/2000/svg' width='1200' height='500'>\n");
00045     fprintf(fp, "<text x='500' y='50' style='fill:black;stroke:none;font-size:20pt;font-family:arial'>I2C bus activity</text>");
00046     fprintf(fp, "<g style='fill:none;stroke:black'>");
00047    
00048     plot(fp, "SDA", sdamem, 110);
00049     plot(fp, "SCL", sclmem, 210);
00050   
00051     fprintf(fp, "</g>");
00052     fprintf(fp, "</svg>");
00053     fclose(fp);
00054 }
00055 
00056 void startServer() {
00057 EthernetErr ethErr = eth.setup();
00058     if (ethErr) {
00059         printf("Error %d in setup.\n", ethErr);
00060         return;
00061     }
00062     printf("Server setup OK\n");
00063 
00064     FSHandler::mount("/local", "/"); //Mount /webfs path on web root path
00065     svr.addHandler<FSHandler>("/"); //Default handler
00066 
00067     svr.bind(80);
00068 
00069     printf("Listening...\n");
00070 
00071     Timer tm;
00072     tm.start();
00073     //Listen indefinitely
00074     while (true) {
00075         Net::poll();
00076         if (tm.read()>.5) {
00077             led1=!led1; //Show that we are alive
00078             tm.start();
00079         }
00080     }
00081 }
00082 
00083 
00084 int main() {
00085     led1 = 0;
00086     capture();
00087     writeSVG();
00088     startServer();
00089 }
00090 
00091 
00092