Converts the MBED in an oscilloscope by using the PC software from http://stampdock.com/ .. same as mbed_sccope, but the stampdock support code is put in a stampdock class. Also a debugged version of mbed_scope, as there was a problem with characters disappearing using putc after using getc which was tricky to find.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
apspijkerman
Date:
Thu Jul 28 11:08:04 2011 +0000
Commit message:

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
stampdock.cpp Show annotated file Show diff for this revision Revisions of this file
stampdock.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 28 11:08:04 2011 +0000
@@ -0,0 +1,618 @@
+
+
+#include "mbed.h"
+#include "stampdock.h"
+
+Stampdock stampdock;
+
+AnalogIn xa(p15);
+AnalogIn ya(p16);
+
+// mode0 = low , mode1 = high
+// returns values of 0-65535 ?
+
+int readadc(int p1, int p2, int p3,  int chan, int mode)
+{
+   int ret = 0;
+   
+   if (mode == 0){
+      if (chan == 0)  ret = xa * 4096;
+      if (chan == 1)  ret = ya * 4096;
+   }
+   if (mode == 1){
+      if (chan == 0)  ret = xa *  256;
+      if (chan == 1)  ret = ya *  256;
+   }
+
+   return ret;
+}
+
+int measure( int chan, int mode)
+{
+   int ret = 0;
+   
+   if (mode == 0){
+      if (chan == 0)  ret = xa * 4096;
+      if (chan == 1)  ret = ya * 4096;
+   }
+   if (mode == 1){
+      if (chan == 0)  ret = xa *  256;
+      if (chan == 1)  ret = ya *  256;
+   }
+
+   return ret;
+}
+#define boolean int
+
+  // ChanXMode    '0=off, 1=low, 2=high, 3=gnd ?
+ //  Mode          '0=scope,  1=XY,  2=record   (3=off ?)
+
+
+
+
+
+int main() {
+   boolean UseChanA = true,  UseChanB = true;        // Bit
+   boolean ready, grid = true, adc = false;    // Bit
+   int     ChanAMode = 0, ChanBMode = 0, mode = 0;       // Nib
+   int     PosA = 128, PosB = 128;
+
+   int     val, valb, ecnt = 0;       // Word
+   int     pval = 0, pvalb = 0;     // Bytes
+   int     x, s, cnt = 0, ptr, speed = 0;     // Byte
+
+   boolean trigMode = true, trigEdge = false, trigSrc = false;       // Bit
+   int     trigLevel = 127;   // Byte     (128 results in 127 !)
+   int  mem[100];  //  [] mem;
+
+   //StringBuffer serbuf = new StringBuffer(80);
+
+   //mem      = new int [100];
+
+   //InitStampdock();
+
+
+   //initpins();
+   //initadc(2,1,0);
+
+   //for (s = 0; s < 100; s++) System.out.print((char) 127 );
+   //System.out.print((char) 127 );
+   //System.out.print("scope running .. ");
+   stampdock.printf("scope running .. ");
+
+   // reset value of gui buttons if reset
+   stampdock.SetValue(1, mode);     // System.out.print("!i1,0;");    // mode, 0 is xt scope
+
+   stampdock.SetValue(102,0);       // System.out.print("!i102,1;");  // on/off switch on
+   if (grid)     stampdock.SetValue(103,1); else stampdock.SetValue(103,0);   // System.out.print("!i103,1;");  // grid switch on
+   if (adc)      stampdock.SetValue(104,1); else stampdock.SetValue(104,0);   // System.out.print("!i104,0;");  // ltc1298 off
+
+   stampdock.SetValue(30, speed);     // System.out.print("!i30,0;");    // speed switch 0
+   stampdock.SetValue(40, trigLevel); // System.out.print("!i40,128;");  // trigger level 128
+   if (trigMode) stampdock.SetValue(41,1); else stampdock.SetValue(41,0); // System.out.print("!i41,1;");  // trigger on
+   if (trigEdge) stampdock.SetValue(42,1); else stampdock.SetValue(42,0); // System.out.print("!i42,1;");  // trigger edge
+   if (trigSrc)  stampdock.SetValue(43,1); else stampdock.SetValue(43,0); // System.out.print("!i43,0;");  // trigger source
+
+   stampdock.SetValue(10, PosA);      // System.out.print("!i10,128;");  // pos chan a
+   stampdock.SetValue(12, ChanAMode); // System.out.print("!i12,0;");    // chan a mode
+   if (UseChanA) stampdock.SetValue(11,1); else stampdock.SetValue(11,0); // System.out.print("!i11,1;");    // channel a on
+
+   stampdock.SetValue(20, PosB);       // System.out.print("!i20,128;");  // pos chan b
+   stampdock.SetValue(22, ChanBMode);  // System.out.print("!i22,0;");    // chan b mode
+   if (UseChanB) stampdock.SetValue(21,1);  else stampdock.SetValue(21,0);  // System.out.print("!i21,1;");    // channel b on
+
+   while (true){
+      if (ecnt > 0){        // (ecnt > 1) test speed up
+         x = stampdock.GetEvent();
+         ecnt = 0;
+      }else{
+         ecnt++;
+         x = 0;
+      }
+
+      if (x != 0){      // do we have a gui click or event ?
+        ecnt = 0;            // just had an event, so max wait
+        s = stampdock.GetValue(x);     // get value of gui element x
+
+        // now handle GUI element x with value/state s
+        if (x == 1){      //mode 0=scope, 1=xy, 2=rec(,3 =off ?)
+           mode = s;
+           //stampdock.printf("#*");    // flush
+           // clear graph
+           //'DEBUG "!s100,100,200;!c;"    'set blue color , clear screen    17c
+           //DEBUG "!sb",3, 100,100,200, ";!c;"     'fast binary way to set color  11c
+           //System.out.print("!s100,100,200;!c;");
+           stampdock.SetColor(100,100,200);
+           stampdock.ClearScreen();
+        } else
+        if (x == 102){      // on/off switch ??
+           // mode = s;
+        } else
+        if (x == 100){     // turnknob for recorder
+           // mode = s;
+        } else
+        if (x == 103){     // grid on/off switch
+           //grid = s;
+           if (s == 0) grid = false; else grid = true;
+        } else
+        if (x == 104){     // ltc1298 on/off switch
+           //grid = s;
+           if (s == 0) adc = false; else adc = true;
+        } else
+        if (x == 30){      // speed 0,1,2,3
+           speed = s;
+        } else
+
+
+
+
+         if (x == 40){     // trigger level
+            trigLevel = 255 - s;
+         } else
+         if (x == 41){     // trigger on/off
+            //trigMode = s;
+            if (s == 0) trigMode = false; else trigMode = true;
+         } else
+         if (x == 42){    // trigger edge
+            //trigEdge = s;
+            if (s == 0) trigEdge = false; else trigEdge = true;
+         } else
+         if (x == 43){      // trigger source
+            //trigSrc = s;
+            if (s == 0) trigSrc = false; else trigSrc = true;
+         } else
+
+         if (x == 10){     // pos chan a
+            PosA = s;
+         } else
+         if (x == 11){    // chan a on/off
+            //UseChanA = s;
+            if (s == 0) UseChanA = false; else UseChanA = true;
+         } else
+         if (x == 12){    //  chan a mode (0=1.5v, 1=15v, 2=gnd)
+            ChanAMode = s;
+         } else
+
+         if (x == 20){     // pos chan b
+            PosB = s;
+         } else
+         if (x == 21){    // chan b on/off
+            //UseChanB = s;
+            if (s == 0) UseChanB = false; else UseChanB = true;
+         } else
+         if (x == 22){    //  chan b mode (0=1.5v, 1=15v, 2=gnd)
+            ChanBMode = s;
+         }
+      }  // if event
+
+
+      if (mode == 0){     // xt scope mode
+         // trigger
+         if (trigMode){
+
+            if (trigEdge == false){
+               pval = 255;
+            }else{
+               pval = 0;
+            }
+            cnt   = 0;
+            ready = false;
+            do {
+               cnt++;
+               if (cnt == 20) ready = true;
+
+               if (adc){   // ltc 1298 mode
+                  if (trigSrc == false){   // chan a
+                     val = readadc(2,1,0,  0, ChanAMode );
+                  }else{              // chan b
+                     val = readadc(2,1,0,  1, ChanBMode ); ;
+                  }
+               }else{   // opamp/transistor mode
+
+                  if (trigSrc == false){   // chan a
+                     val = measure(0, ChanAMode );
+                  }else{              // chan b
+                     val = measure(1, ChanBMode );
+                  }
+
+               }
+               //  now val is the value we want to check trigger on
+
+               if (trigEdge == false) {
+                  if (trigLevel < val && trigLevel >= pval) ready = true;
+               }else{
+                  if (trigLevel > val && trigLevel <= pval) ready = true;
+               }
+               pval = val;
+
+            }while (!ready);
+         } // trigMode
+
+         ptr = 0;    // mem counter
+         if (adc){   // ltc 1298 mode
+         for (cnt = 0; cnt <= 29; cnt++){
+            if (UseChanA){
+               //val = readadc(2,1,0,  0, ChanAMode );
+               //if (ChanAMode == 0){
+               //   val = val - (2048-128);
+               //}else{
+               //   val = val >> 4;
+               //}
+               //mem[ptr] = val;
+               mem[ptr] = readadc(2,1,0,  0, ChanAMode );
+            }
+            ptr++;
+
+            if (UseChanB){
+               //val = readadc(2,1,0,  1, ChanBMode );
+               //if (ChanBMode == 0){
+               //   val = val - (2048-128);
+               //}else{
+               //   val = val >> 4;
+               //}
+               //mem[ptr] = val;
+               mem[ptr] = readadc(2,1,0,  1, ChanBMode );
+            }
+            ptr++;
+
+            if (speed > 0){
+               if (speed == 1) wait(0.001); // CPU.delay(speed);
+               if (speed == 2) wait(0.01 ); // CPU.delay(speed);
+               if (speed == 3) wait(0.1  ); // CPU.delay(speed);
+            }
+         }     // for
+         }else{  // opamp/transistor mode
+         for (cnt = 0; cnt <= 29; cnt++){
+            if (UseChanA){
+               //val = measure(0, ChanAMode );
+               //mem[ptr] = val;
+               mem[ptr] = measure(0, ChanAMode );
+            }
+            ptr++;
+
+            if (UseChanB){
+               //val = measure(1, ChanBMode );
+               //mem[ptr] = val;
+               mem[ptr] = measure(1, ChanBMode );
+            }
+            ptr++;
+
+            if (speed > 0){
+               if (speed == 1) wait(0.001); // CPU.delay(speed);
+               if (speed == 2) wait(0.01 ); // CPU.delay(speed);
+               if (speed == 3) wait(0.1  ); // CPU.delay(speed);
+            }
+         }     // for
+         }  // !adc
+
+         //  ' clear graph
+         //  'DEBUG "!s100,100,200;!c;"    'set blue color , clear screen    17c
+         // DEBUG "!sb",3, 100,100,200, ";!c;"     'fast binary way to set color  11c
+         //System.out.print("!s100,100,200;!c;");
+         
+         //stampdock.print("#*");     // flush
+         //stampdock.SetColor(100,100,200);
+         stampdock.SetColor(100,100,200);
+         stampdock.ClearScreen();
+         
+         //grid = 0;
+         if (grid){
+            //'DEBUG "!s128,128,228;"            'set lighter color  14c
+            // DEBUG "!sb",3,128,128,228,";"      'fast binary way to set color  8c
+            //System.out.print("!s128,128,228;");
+            stampdock.SetColor(128,128,228);
+
+            //'DEBUG "!a20,20,29,19,55,12;"      'slow ascii way draw grid    20c
+            //DEBUG "!ab",6,20,20,29,19,55,12,";"     'fast binary way to draw grid  11c
+            //System.out.print("!a20,20,29,19,55,12;");
+            stampdock.DrawGrid(20,20,29,19,55,12);
+
+            if (trigMode){
+               cnt = trigLevel;
+               if (trigSrc == false){
+                  cnt = 255 - cnt + PosA - 128;
+               }else{
+                  cnt = 255 - cnt + PosB - 128;
+               }
+
+               // 'DEBUG "!l", DEC 1,",", DEC cnt,",", DEC 40,",", DEC cnt, ";"   'slow
+               //DEBUG "!lb", 4,1,cnt,40,cnt,";"    ' fast
+                //System.out.print("!l1,");
+                //System.out.print( cnt);
+                //System.out.print(",40,");
+                //System.out.print( cnt  );
+                //System.out.print(";");
+                stampdock.DrawLine(1,cnt,40,cnt);
+            } // end trigMode
+         }  // end grid
+
+//UseChanA = 0;
+//UseChanB = 0;
+
+         if (UseChanA){
+            //    draw channel a
+            // 'set color black
+            // 'DEBUG "!s0,0,0;"                     'slow ascii way to set color  8c
+            // DEBUG "!sb",3, 0,0,0, ";"         'fast binary way to set color   8c
+            //System.out.print("!s0,0,0;");
+            stampdock.SetColor(0,0,0);
+/*
+            ptr = 0;
+            for (cnt = 0; cnt <= 1; cnt++){       // <= 28
+               x = mem[ ptr ];
+               stampdock.printf("-%d", x );
+               ptr = ptr + 2;
+            }
+*/
+            ptr = 0;
+            s = mem[ ptr ];
+            s = 255 - s + PosA - 128;
+            ptr = ptr + 2;
+            s -= 60;
+
+            //if (s < -128) s = -128;
+            //if (s >  127) s =  127;
+            //if (s <    0) s =    0;
+            //if (s >  255) s =  255;
+            
+            //DEBUG "!Pb", 33,20, 0,60 , s     ' init poly line cmd in binary mode  38c
+            //' 33 is number of bytes we are going to send
+            //' 20=dx , 0=dy      (which means we only send y values)
+            //' 0,60 = start x,y
+
+            //System.out.print("!P20,0,60,");
+            //System.out.print( s );
+/*
+            serbuf.clear();
+            serbuf.append("!Pb");        // poly line, binary mode
+            serbuf.append((char)33);     // 33 values follow
+            serbuf.append((char)20);     // dx=20
+            serbuf.append((char) 0);     // dy=0 .. so only y values
+            serbuf.append((char)60);     // x start
+            serbuf.append((char) s);     // y start
+*/
+            stampdock.printf("!Pb");
+            stampdock.printf("%c%c%c%c%c", 33, 20, 0, 60, s);
+            //stampdock.printf("%d%d%d%d%d", 33, 20, 0, 60, s);
+            
+            for (cnt = 0; cnt <= 28; cnt++){
+               x = mem[ ptr ];
+               x = 255 - x + PosA - 128;
+               ptr = ptr + 2;
+
+               //'' --- slow line ---
+               //''val = cnt * 20
+               //''DEBUG "!l",DEC val,",",DEC s,",", DEC val+20,",", DEC x,";"    ' chars 18
+               //'' --- faster line ---
+               //''val = cnt * 8
+               //''DEBUG "!lb", 4 , val , s, val+8 , x , ";"                  'chars 8
+               //'' --- fastest line ---
+               //DEBUG x      'chars 1    (poly line cmd with constant dx)
+
+               //System.out.print( "," );
+               //System.out.print( x );
+
+               x -= 60;
+//               if (x > 127) x -= 256;  // int to char ?
+               //serbuf.append((char) x  );    // y value of next line
+
+               //if (x < -128) x = -128;
+               //if (x >  127) x =  127;
+               //if (x <    0) x =    0;
+               //if (x >  255) x =  255;
+               stampdock.printf("%c", x );    // y value of next line
+               //stampdock.printf("%d.", x );
+               //wait(0.01);
+
+               //s = x;
+            }
+            //System.out.print(";");
+            //serbuf.append(";");
+            //System.out.print(serbuf.toString());
+            stampdock.printf(";");
+            //stampdock.printf(" ");   // flush ?
+   
+         } // end UseChanA
+
+         if (UseChanB){
+            //' draw channel b
+            //'set color white
+            //'DEBUG "!s255,255,255;"                  'slow ascii way to set color
+            //DEBUG "!sb",3,255,255,255,";"     'fast binary way to set color
+            //System.out.print("!s255,255,255;");
+            stampdock.SetColor(255,255,255);
+
+            ptr = 1;
+            s = mem[ ptr ];
+            s = 255 - s + PosB - 128;
+            ptr = ptr + 2;
+            s -= 60;
+
+            //if (s < -128) s = -128;
+            //if (s >  127) s =  127;
+           // if (s <    0) s =    0;
+            //if (s >  255) s =  255;
+                        
+            //DEBUG "!Pb", 33,20, 0,60 , s     ' init poly line cmd in binary mode  38c
+            //' 33 is number of bytes we are going to send
+            //' 20=dx , 0=dy      (which means we only send y values)
+            //' 0,60 = start x,y
+
+            //System.out.print("!P20,0,60,");
+            //System.out.print( s );
+/*
+            serbuf.clear();
+            serbuf.append("!Pb");
+            serbuf.append((char)33);
+            serbuf.append((char)20);
+            serbuf.append((char) 0);
+            serbuf.append((char)60);
+            serbuf.append((char) s);
+*/
+            stampdock.printf("!Pb");
+            stampdock.printf("%c%c%c%c%c", 33, 20, 0, 60, s );
+            //stampdock.printf("%d%d%d%d%d", 33, 20, 0, 60, s );
+            
+            for (cnt = 0; cnt <= 28; cnt++){
+               x = mem[ ptr ];
+               x = 255 - x + PosB - 128;
+               ptr = ptr + 2;
+
+               //'' --- slow line ---
+               //''val = cnt * 20
+               //''DEBUG "!l",DEC val,",",DEC s,",", DEC val+20,",", DEC x,";"
+               //'' --- faster line ---
+               //''val = cnt * 8
+               //''DEBUG "!lb", 4 , val , s, val+8 , x , ";"
+               //'' --- fastest line ---
+               //DEBUG x      'chars 1          (poly line cmd with constant dx)
+
+               //System.out.print( "," );
+               //System.out.print( x );
+
+               x -= 60;
+//               if (x > 127) x -= 256;  // int to char ?
+               //serbuf.append((char) x  );
+               
+               //if (x < -128) x = -128;
+               //if (x >  127) x =  127;
+               //if (x <    0) x =    0;
+               //if (x >  255) x =  255;
+               stampdock.printf("%c", x );
+               //stampdock.printf("%d.", x );
+               //wait(0.01);
+               
+               //s = x;
+            }
+            //System.out.print(";");
+            //serbuf.append(";");
+            //System.out.print(serbuf.toString());
+            stampdock.printf(";");
+            //stampdock.printf(" ");   // flush ?
+            
+         } // end UseChanB
+
+         //CPU.delay(1);
+      }   // end xt scope mode
+
+      if (mode == 2){   // recorder mode
+         if (adc){   // ltc1298 mode
+            val  = readadc(2,1,0,  0, ChanAMode );
+            valb = readadc(2,1,0,  1, ChanBMode );
+         }else{
+            val  = measure(0, ChanAMode );
+            valb = measure(1, ChanBMode );
+         }
+
+         val  = val  + PosA - 128;        // offset
+         valb = valb + PosB - 128;
+
+         //' set plot color
+         //DEBUG "!s0,0,0;"
+         //System.out.print("!s0,0,0;");
+         stampdock.SetColor(0,0,0);
+
+         //' plot xy
+         //'DEBUG "!p", DEC 610,",", DEC val, ";"
+         //' line xy
+         //DEBUG "!l", DEC 610,",", DEC val,",", DEC 609,",", DEC pval ,";"
+
+         //System.out.print("!l610,");
+         //System.out.print( val  );
+         //System.out.print(",609,");
+         //System.out.print( pval );
+         //System.out.print(";");
+         stampdock.DrawLine(610,val,609,pval);
+
+         pval = val;
+
+         //' set plot color
+         //'DEBUG "!s250,250,250;"           'slow ascii way to set color
+         //DEBUG "!sb",3,250,250,250,";"     'fast binary way to set color
+         //System.out.print("!s250,250,250;");
+         stampdock.SetColor(250,250,250);
+
+         //' plot xy
+         //'DEBUG "!p", DEC 610,",", DEC valb, ";"
+         //' line xy
+         //DEBUG "!l", DEC 610,",", DEC valb,",", DEC 609,",", DEC pvalb ,";"
+
+         //System.out.print("!l610,");
+         //System.out.print( valb  );
+         //System.out.print(",609,");
+         //System.out.print( pvalb );
+         //System.out.print(";");
+         stampdock.DrawLine(610,valb,609,pvalb);
+
+         pvalb = valb;
+
+         // ' scrol 1 pixel to the left
+         //DEBUG "!G20,1;"
+         //System.out.print("!G20,1;"); 
+         stampdock.printf("!G20,1;");
+      }   // end recorder mode
+
+      if (mode == 1){    // xy scope mode
+         if (adc){    // ltc1298 mode
+            val  = readadc(2,1,0,  0, ChanAMode );
+            valb = readadc(2,1,0,  1, ChanBMode );
+         }else{
+            val  = measure(0, ChanAMode );
+            valb = measure(1, ChanBMode );
+         }
+
+         //' set plot color
+         //'DEBUG "!s200,200,200;"           'slow ascii way to set color
+         //DEBUG "!sb",3,200,200,200,";"     //fast binary way to set color
+         //System.out.print("!s,200,200,200;");
+         stampdock.SetColor(200,200,200);
+
+         //' plot xy
+         //DEBUG "!p", DEC val,",", DEC valb, ";"
+         //System.out.print("!p");
+         //System.out.print( val );
+         //System.out.print(",");
+         //System.out.print( valb );
+         //System.out.print(";");
+         stampdock.Plot(val,valb);
+
+         //' line xy
+         //DEBUG "!l", DEC val,",", DEC valb,",", DEC pval,",",DEC pvalb,  ";"
+
+         //System.out.print("!l");
+         //System.out.print( val  );
+         //System.out.print(",");
+         //System.out.print( valb );
+         //System.out.print(",");
+         //System.out.print( pval  );
+         //System.out.print(",");
+         //System.out.print( pvalb );
+         //System.out.print(";");
+         stampdock.DrawLine(val, valb, pval, pvalb);
+
+         pval  = val;
+         pvalb = valb;
+
+         cnt = cnt + 1;
+         if (cnt == 50){
+            //stampdock.printf("#*");  // flush
+            //' clear graph
+            //'DEBUG "!s100,100,200;!c;"    'set blue color , clear screen    17c
+            //DEBUG "!sb",3, 100,100,200, ";!c;"     'fast binary way to set color  11c
+            //System.out.print("!s,100,100,200;!c;");
+            stampdock.SetColor(100,100,200);
+            stampdock.ClearScreen();
+
+            cnt = 0;
+         }
+
+
+      } // end xy mode scope
+
+   }   // end while true
+ }       // end main
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jul 28 11:08:04 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stampdock.cpp	Thu Jul 28 11:08:04 2011 +0000
@@ -0,0 +1,805 @@
+
+#include "mbed.h"
+#include "stampdock.h"
+#include <stdarg.h>     // var_args
+
+Serial serial(USBTX, USBRX);   // tx, rx     default 9600 8N1
+
+//stampdock *stampdock::instance;
+
+// millisecs
+
+void qwait(int v)
+{
+#ifdef qmbed
+   // wait(1000*v);       // 0.001
+   wait_ms(v);
+#endif
+#ifdef qarduino
+
+#endif
+#ifdef qlpcxpresso
+   vTaskDelay(configTICK_RATE_HZ * v / 1000);
+#endif
+}
+
+
+// pinhigh, pinlow, togglepin
+// pinin, pinout, readpin
+// AnalogIn()
+
+Stampdock::Stampdock(void)
+{
+
+#ifdef qmbed
+   //serial.baud(9600);          // causes error
+   //serial.baud(19200);
+   //serial.baud(38400);
+   //serial.baud(57600);
+   //serial.baud(115200);
+   //serial.format(8,None,1);
+#endif
+#ifdef qarduino
+   serial.begin( 9600 );
+#endif
+
+}
+
+Stampdock::~Stampdock(void)
+{
+
+}
+
+
+int Stampdock::init(void)
+{
+   // set baud ?
+   return 0;
+}
+  
+int Stampdock::print(char *str)
+{
+/*
+   while (!serial.writeable()){
+      wait(0.001);
+   }
+   
+   serial.printf("%s", str); 
+*/
+   unsigned char *p;
+   int c, t, len, ready;
+   
+   len = strlen(str);
+   
+   p = (unsigned char *) &str[0]; 
+   //p = buffer;
+   t = 0;
+   ready = 0;
+
+   if (len == 0) ready = 1;
+
+   while (!ready){
+      if (!serial.writeable()){
+         wait(0.001);
+      }else{
+         c = *p++;         // (int) (char) ?
+         //if (c < 0) c += 256;
+         serial.putc( c );
+         t++;
+         if (t == len) ready = 1;
+      }
+   }
+   
+   return len;
+}
+
+int qprint(char *str);
+int qprint(char *str)
+{
+/*
+   while (!serial.writeable()){
+      wait(0.001);
+   }
+   
+   serial.printf("%s", str); 
+*/
+   unsigned char *p;
+   int c, t, len, ready;
+   
+   len = strlen(str);
+   
+   p = (unsigned char *) &str[0]; 
+   //p = buffer;
+   t = 0;
+   ready = 0;
+
+   if (len == 0) ready = 1;
+
+   while (!ready){
+      if (!serial.writeable()){
+         wait(0.001);
+      }else{
+         c = *p++;         // (int) (char) ?
+         //if (c < 0) c += 256;
+         serial.putc( c );
+         t++;
+         if (t == len) ready = 1;
+      }
+   }
+   
+   return len;
+}
+
+int Stampdock::printf(const char *fmt, ... )
+{
+   va_list var_args;
+   char string[80];  // char string[256];
+   unsigned char *p;
+   int len = 0, t, c, ready;
+
+   va_start(var_args, fmt);
+   len = vsprintf(string, fmt, var_args);
+ //len = vsprintf(buffer, fmt, var_args);
+   va_end(var_args);
+
+   while (!serial.writeable()){
+      wait(0.001);
+   }
+
+   //serial.printf( string );
+   //serial.printf( buffer );
+   //serial.printf( "%s", string );
+   //serial.printf( "%s", buffer );   
+   //wait(0.01);
+   
+      
+   p = (unsigned char *) &string[0]; 
+   //p = buffer;
+   t = 0;
+   ready = 0;
+
+   if (len == 0) ready = 1;
+   
+   while (!ready){
+      if (!serial.writeable()){
+         wait(0.001);
+      }else{
+         c = *p++;         // (int) (char) ?
+         //if (c < 0) c += 256;
+         serial.putc( c );
+         t++;
+         if (t == len) ready = 1;
+      }
+   }
+
+   //if (len > 60) serial.printf("-=%d.=-", len );
+
+   return len;
+}
+
+int qprintf(const char *fmt, ... );
+int qprintf(const char *fmt, ... )
+{
+   va_list var_args;
+   char string[80];  // char string[256];
+   unsigned char *p;
+   int len = 0, t, c, ready;
+
+   va_start(var_args, fmt);
+   len = vsprintf(string, fmt, var_args);
+ //len = vsprintf(buffer, fmt, var_args);
+   va_end(var_args);
+
+   while (!serial.writeable()){
+      wait(0.001);
+   }
+
+   //serial.printf( string );
+   //serial.printf( buffer );
+   //serial.printf( "%s", string );
+   //serial.printf( "%s", buffer );   
+   //wait(0.01);
+   
+      
+   p = (unsigned char *) &string[0]; 
+   //p = buffer;
+   t = 0;
+   ready = 0;
+
+   if (len == 0) ready = 1;
+   
+   while (!ready){
+      if (!serial.writeable()){
+         wait(0.001);
+      }else{
+         c = *p++;         // (int) (char) ?
+         //if (c < 0) c += 256;
+         serial.putc( c );
+         t++;
+         if (t == len) ready = 1;
+      }
+   }
+
+   //if (len > 60) serial.printf("-=%d.=-", len );
+
+   return len;
+}
+
+
+ int Stampdock::GetEvent(void)
+  {
+      int e = 0, c, ready = 0, cnt = 0;
+      char v;
+      //  DEBUG "?Q;"     'query for events .. also sync with PC
+      //  SERIN 16, Baud, [x]             '16468
+      //  PAUSE 1   ' millisecs .. let serial bus settle
+
+      //serial.printf("?Q;");     // query for event .. also sync with PC
+      qprint("?Q;");     // query for event .. also sync with PC
+//      qprint(" ");   // fix
+//wait_ms(175);
+
+      //qprint("?i1;");
+
+      do{
+      if (serial.readable()){
+         //v = Serial.read();
+/*
+         v = serial.getc();
+         e = v;
+         if (e < 0) e += 256;      // signed char to int ?
+         //e = (255-v) + 1;   // 2's complement
+*/
+         e = serial.getc();
+         if (e != 0) qprintf(".ID:%d.", e);
+
+         //serial.printf("EVENT:");
+         //serial.printf(e);
+         //serial.printf("<-");
+         ready = 1;
+      }else{
+         //serial.printf("no event");
+         wait(0.0001);
+         cnt++;
+         if (cnt == 1000) ready = 1;
+         //Blink(0);
+      }
+      }while(!ready);
+      //CPU.delay(10);
+
+      // .. flush out of sync events ..
+      //while (serial.readable()){
+      if (serial.readable()){
+         // Serial.read();
+         c = serial.getc();
+         if (e == 0 && c != 0) e = c;
+         if (e != 0) qprintf(".ID2:%d.", e);
+         //Blink(1);
+      }
+
+      if (serial.readable()){
+         // Serial.read();
+         c = serial.getc();
+         if (e == 0 && c != 0) e = c;
+         if (e != 0) qprintf(".ID3:%d.", e);
+         //Blink(2);
+      }
+      
+      qprint(" ");   // fix   
+      //wait_ms(175);   
+      return e;
+  }
+
+  // request the state of GUI element i
+
+int Stampdock::GetValue(int i)
+  {
+      int e = 0, ready = 0, cnt = 0;
+      char v;
+      //StringBuffer buf=new StringBuffer(12);
+
+      // DEBUG "?i",DEC x,";"      'get state of gui element x
+      // SERIN 16, Baud, [s]             '16468
+      // PAUSE 1   ' millisecs
+
+      //serial.printf("?i");     // query for event
+      //serial.printf(i);
+      //serial.printf(";");
+
+      //buf.clear();
+      //buf.append("?i");
+      //buf.append( i );
+      //buf.append(";");
+      //System.out.print(buf.toString());
+ 
+      //serial.printf("?i%d;", i );
+      qprintf("?i%d;", i );
+
+      //wait(0.1);         //CPU.delay(1);         // ??
+      do {
+      if (serial.readable()){
+         //v = Serial.read();
+/*
+         v = serial.getc();
+         e = v;
+         if (e < 0) e += 256;    // signed char to int ?
+         //e = (255-v) + 1;   // 2's complement
+*/
+         e = serial.getc();
+
+         ready = 1;
+      }else{
+         wait(0.0001);
+         cnt++;
+         if (cnt == 1000) ready = 1;
+      }
+      //CPU.delay(10);
+      }while(!ready);
+
+      qprint(" ");   // fix  
+      
+      return e;
+  }
+
+  // set the state (v) of gui element i
+
+ void Stampdock::SetValue(int i, int v)
+  {
+      //StringBuffer buf=new StringBuffer(16);
+
+      // DEBUG "?i",DEC x,";"      'get state of gui element x
+      // SERIN 16, Baud, [s]             '16468
+      // PAUSE 1   ' millisecs
+
+      //serial.printf("!i");     // set value
+      //serial.printf(i);
+      //serial.printf(",")
+      //serial.printf(v);
+      //serial.printf(";");
+
+      //buf.clear();      // slow ascii method to allow values > 255
+      if (i <= 255){
+         //buf.append("!ib");      // fast binary way
+         //buf.append((char) 2 );  // write 2 bytes
+         //buf.append((char) i );  // write id
+         //buf.append((char) v );  // always smaller als 256
+
+         //serial.printf("!ib%c%c%c;", 2 , i , v );
+         qprintf("!ib%c%c%c;", 2 , i , v );
+      }else{
+         //buf.append("!i");   // slow ascii way
+         //buf.append( i );    // in case ID greater as 255
+         //buf.append(",");
+         //buf.append( v );
+
+         //serial.printf("!i%d,%d;", i, v);
+         qprintf("!i%d,%d;", i, v);
+      }
+      //buf.append(";");
+      //System.out.print(buf.toString());
+
+      //CPU.delay(1);         // ??
+  }
+
+
+char Stampdock::ReadByte(void)
+  {
+     char v = 0, ready = 0, cnt = 0;
+
+     //wait(0.1);
+     do {
+     if (serial.readable()){
+         //v =  Serial.read();
+         v = serial.getc();
+
+         //e = v;
+         //if (e < 0) e += 256;    // signed char to int ?
+         ready = 1;
+     }else{
+        wait(0.0001);
+        cnt++;
+        if (cnt == 1000) ready = 1;
+     }
+     }while(!ready);
+     return v;
+  }
+
+
+void Stampdock::SetColor(int r, int g, int b)
+  {
+     // StringBuffer buf=new StringBuffer(12);
+
+    //serial.printf(" ");   // flush test
+    
+   //while (!serial.writeable()){
+   //   wait(0.001);
+   //}
+       
+    if (r == 0 && g == 0 && b == 0){
+      //serial.printf("!s;");      // black
+      qprint("!s;");      // black
+    }else{
+      //buf.clear();
+      //buf.append("!sb");     // fast binary method .. so values <= 255
+      //buf.append((char) 3 ); // write 3 bytes
+      //buf.append((char) r );
+      //buf.append((char) g );
+      //buf.append((char) b );
+      //buf.append(";"      );
+      //System.out.print(buf.toString());
+
+      //serial.printf("!s%d,%d,%d;",     r, g, b );          // slow ascii
+      //serial.printf("!sb%c%c%c%c;", 3, r, g, b );   // fast binary
+      qprintf("!sb%c%c%c%c;", 3, r, g, b );   // fast binary
+    }
+      //CPU.delay(1);         // ??
+  }
+
+
+void Stampdock::SetGrey(int g)
+  {
+     // StringBuffer buf=new StringBuffer(12);
+
+    // DEBUG "!fb", 1, dat, ";"
+
+    if (g == 0){
+      //System.out.print("!f;");      // black
+      //serial.printf("!f;");      // black
+      qprint("!f;");      // black
+    }else{
+      //buf.clear();
+      //buf.append("!fb");     // fast binary method .. so values <= 255
+      //buf.append((char) 1 ); // write 3 bytes
+      //buf.append((char) g );
+      //buf.append(";"      );
+      //System.out.print(buf.toString());
+
+      //serial.printf("!fb%c%c;", 1, g );
+      qprintf("!fb%c%c;", 1, g );
+    }
+      //CPU.delay(1);         // ??
+  }
+
+
+
+void Stampdock::SetLED(int i, int r, int g, int b)
+  {
+     // StringBuffer buf=new StringBuffer(12);
+
+      //buf.clear();
+      //buf.append("!jb");     // fast binary method .. so values <= 255
+      //buf.append((char) 4 ); // write 4 bytes
+      //buf.append((char) i );
+      //buf.append((char) r );
+      //buf.append((char) g );
+      //buf.append((char) b );
+      //buf.append(";"      );
+      //System.out.print(buf.toString());
+
+      //serial.printf("!jb%c%c%c%c%c;", 4, i, r, g, b );
+      qprintf("!jb%c%c%c%c%c;", 4, i, r, g, b );
+      
+      //CPU.delay(1);         // ??
+  }
+
+void Stampdock::DrawLine(int x1, int y1, int x2, int y2)
+  {
+      //StringBuffer buf=new StringBuffer(36);
+
+      //if (x1 < 0) x1 = 0;
+      //if (y1 < 0) y1 = 0;
+      //if (x2 < 0) x2 = 0;
+      //if (y2 < 0) y2 = 0;
+
+      //buf.clear();
+      if (x1 >= 0 && x1 <= 255 && y1 >= 0 && y1 <= 255 && x2 >= 0 && x2 <= 255 && y2 
+
+>= 0 && x2 <= 255){
+        //buf.append("!lb");        // fast binary method
+        //buf.append((char) 4  );   // write 4 bytes
+        //buf.append((char) x1 );
+        //buf.append((char) y1 );
+        //buf.append((char) x2 );
+        //buf.append((char) y2 );
+
+        //serial.printf("!lb%c%c%c%c%c;", 4, x1, y1, x2, y2 );
+        qprintf("!lb%c%c%c%c%c;", 4, x1, y1, x2, y2 );
+     }else{
+        //buf.append("!l");     // slow method .. so values > 255
+        //buf.append( x1 );
+        //buf.append("," );
+        //buf.append( y1 );
+        //buf.append("," );
+        //buf.append( x2 );
+        //buf.append("," );
+        //buf.append( y2 );
+
+        //serial.printf("!l%d,%d,%d,%d;", x1, y1, x2, y2);
+        qprintf("!l%d,%d,%d,%d;", x1, y1, x2, y2);
+      }
+      //buf.append(";" );
+      //System.out.print(buf.toString());
+
+      //CPU.delay(1);         // ??
+  }
+
+void Stampdock::Plot(int x, int y)
+  {
+      //StringBuffer buf=new StringBuffer(16);
+
+      //if (x < 0) x = 0;
+      //if (y < 0) y = 0;
+
+      //buf.clear();
+      if (x >= 0 && x <= 255 && y >= 0 && y <= 255){
+         //buf.append("!pb");       // fast method .. so values <= 255
+         //buf.append((char) 2  );  // write 2 bytes
+         //buf.append((char) x  );
+         //buf.append((char) y  );
+
+         //serial.printf("!pb%c%c%c;", 2, x, y);
+         qprintf("!pb%c%c%c;", 2, x, y);
+      }else{
+         //buf.append("!p");        // slow method .. so values > 255
+         //buf.append( x  );
+         //buf.append("," );
+         //buf.append( y  );
+
+         //serial.printf("!p%d,%d;", x, y);
+         qprintf("!p%d,%d;", x, y);
+      }
+      //buf.append(";" );
+      //System.out.print(buf.toString());
+
+      //CPU.delay(1);         // ??
+  }
+
+
+void Stampdock::DrawGrid(int a, int b, int c, int d, int e, int f)
+  {
+      //StringBuffer buf=new StringBuffer(36);
+
+      //buf.clear();
+      //buf.append("!ab");        // fast binary method
+      //buf.append((char) 6  );   // write 6 bytes
+      //buf.append((char) a  );   // xspace
+      //buf.append((char) b  );   // yspace
+      //buf.append((char) c  );   // xlines
+      //buf.append((char) d  );   // ylines
+      //buf.append((char) e  );   // xoffset
+      //buf.append((char) f  );   // yoffset
+      //buf.append(";" );
+      //System.out.print(buf.toString());
+
+      //serial.printf("!ab%c%c%c%c%c%c%c;", 6, a, b, c, d, e, f );
+      qprintf("!ab%c%c%c%c%c%c%c;", 6, a, b, c, d, e, f );
+      
+      //CPU.delay(1);         // ??
+  }
+
+void Stampdock::ClearScreen(void)
+  {
+      //serial.printf("!c;");
+      qprint("!c;");
+  }
+
+  // shortcut:     Cmd("!c;");  or Cmd("?O;");
+
+void Stampdock::Cmd(char *p)
+  {
+      //serial.printf(p);
+      qprintf(p);
+  }
+
+  // for "!nSpeech;" or "!Llog file;"
+
+void Stampdock::CmdStr(char cmd, char *p  )
+  {
+       //buf.clear();
+       //buf.append("!");
+       //buf.append( cmd  );
+       //buf.append( p    );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+
+       //serial.printf("!%c%s;", cmd, p);
+       qprintf("!%c%s;", cmd, p);
+   }
+
+  // cmd with no argument so  Cmd0('s');  = "!s;"
+
+void Stampdock::Cmd0(char cmd  )
+  {
+       //buf.clear();
+       //buf.append("!");
+       //buf.append( cmd  );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+
+       //serial.printf("!%c;", cmd);
+       qprintf("!%c;", cmd);
+  }
+
+  // request with no arguments
+  // Req0( 'm' );  =  "?m;"
+
+ void Stampdock::Req0(char req  )
+  {
+       //buf.clear();
+       //buf.append("?");
+       //buf.append( req  );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+
+       //serial.printf("?%c;", req );
+       qprintf("?%c;", req );
+  }
+
+  // cmd with 1 arg   Cmd1('a' , 1 );  =  "!a1;"
+
+void Stampdock::Cmd1(char cmd, int arg1 )
+  {
+     //buf.clear();
+     //buf.append("!");
+     //buf.append( cmd  );
+     if (arg1 < 256){
+       //buf.append( 'b'  );      // binary mode
+       //buf.append((char) 1  );  // 1 argument
+       //buf.append((char) arg1 );
+
+       //serial.printf("!%cb%c%c;", cmd, 1, arg1 );
+       qprintf("!%cb%c%c;", cmd, 1, arg1 );
+     }else{
+       //buf.append( arg1 );       // slow ascii mode
+
+       //serial.printf("!%c%d;", cmd, arg1 );
+       qprintf("!%c%d;", cmd, arg1 );
+    }
+    //buf.append(";" );
+    //System.out.print(buf.toString());
+  }
+
+  // cmd with 2 args    Cmd2('G', 20, 1);  = "!G20,1;"
+  // plot = Cmd2( 'p', x, y );
+
+void Stampdock::Cmd2(char cmd, int arg1, int arg2 )
+  {
+    //buf.clear();
+    //buf.append("!"   );       // !
+    //buf.append( cmd  );
+    if (arg1 < 255 && arg2 < 255){
+       //buf.append( 'b'  );      // binary mode
+       //buf.append((char) 2  );  // 2 arguments
+       //buf.append((char) arg1 );
+       //buf.append((char) arg2 );
+
+       //serial.printf("!%cb%c%c%c;", cmd, 2, arg1, arg2 );
+       qprintf("!%cb%c%c%c;", cmd, 2, arg1, arg2 );
+     }else{
+       //buf.append( arg1 );        // slow ascii mode
+       //buf.append(","   );
+       //buf.append( arg2 );
+ 
+       //serial.printf("!%c%d,%d;", cmd, arg1, arg2 );
+       qprintf("!%c%d,%d;", cmd, arg1, arg2 );
+     }
+     //buf.append(";"   );
+     //System.out.print(buf.toString());
+  }
+
+  // cmd with 3 args
+  // setcolor = Cmd3( 's', r, g, b );
+
+ void Stampdock::Cmd3(char cmd, int arg1, int arg2, int arg3 )
+  {
+    //buf.clear();
+    //buf.append("!"   );       // !
+    //buf.append( cmd  );
+    if (arg1 < 255 && arg2 < 255 && arg3 < 255){
+       //buf.append( 'b'  );      // binary mode
+       //buf.append((char) 3  );  // 3 arguments
+       //buf.append((char) arg1 );
+       //buf.append((char) arg2 );
+       //buf.append((char) arg3 );
+
+       //serial.printf("!%cb%c%c%c%c;", cmd, 3, arg1, arg2, arg3 );
+       qprintf("!%cb%c%c%c%c;", cmd, 3, arg1, arg2, arg3 );
+    }else{
+       //buf.append( arg1 );     // slow ascii mode
+       //buf.append(","   );
+       //buf.append( arg2 );
+       //buf.append(","   );
+       //buf.append( arg3 );
+
+       //serial.printf("!%c%d,%d,%d;", cmd, arg1, arg2, arg3 );
+       qprintf("!%c%d,%d,%d;", cmd, arg1, arg2, arg3 );
+     }
+     //buf.append(";"   );
+     //System.out.print(buf.toString());
+  }
+
+  // cmd with 4 args
+  // line = Cmd4( 'l', x1, y1, x2, y2);
+
+void Stampdock::Cmd4(char cmd, int arg1, int arg2, int arg3, int arg4 )
+  {
+    //buf.clear();
+    //buf.append("!"   );       // !
+    //buf.append( cmd  );
+    if (arg1 < 255 && arg2 < 255 && arg3 < 255 && arg4 < 255){
+       //buf.append( 'b'  );      // binary mode
+       //buf.append((char) 4  );  // 4 arguments
+       //buf.append((char) arg1 );
+       //buf.append((char) arg2 );
+       //buf.append((char) arg3 );
+       //buf.append((char) arg4 );
+
+       //serial.printf("!%cb%c%c%c%c%c;", cmd, 4, arg1, arg2, arg3, arg4 );
+       qprintf("!%cb%c%c%c%c%c;", cmd, 4, arg1, arg2, arg3, arg4 );
+    }else{
+       //buf.append( arg1 );    // slow ascii mode
+       //buf.append(","   );
+       //buf.append( arg2 );
+       //buf.append(","   );
+       //buf.append( arg3 );
+       //buf.append(","   );
+       //buf.append( arg4 );
+ 
+       //serial.printf("!%c%d,%d,%d,%d;", cmd, arg1, arg2, arg3, arg4 );
+       qprintf("!%c%d,%d,%d,%d;", cmd, arg1, arg2, arg3, arg4 );
+    }
+     //buf.append(";"   );
+     //System.out.print(buf.toString());
+  }
+
+  // play video, mp3 or picture
+
+void Stampdock::Media(char *p)
+  {
+       //buf.clear();
+       //buf.append("!V");
+       //buf.append( p );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+  
+       //serial.printf("!V%s;", p);
+       qprintf("!V%s;", p);
+  }
+
+void Stampdock::Speech(char *p)
+  {
+       //buf.clear();
+       //buf.append("!n");
+       //buf.append( p );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+ 
+       //serial.printf("!n%s;", p);
+       qprintf("!n%s;", p);
+  }
+
+void Stampdock::Log(char *p)
+  {
+       //buf.clear();
+       //buf.append("!L");
+       //buf.append( p );
+       //buf.append(";" );
+       //System.out.print(buf.toString());
+  
+       //serial.printf("!L%s;", p);
+       qprintf("!L%s;", p);
+ }
+
+ void Stampdock::ScrollLeft()
+ {
+     //serial.printf("!G20,1;");
+     qprintf("!G20,1;");
+ }
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stampdock.h	Thu Jul 28 11:08:04 2011 +0000
@@ -0,0 +1,81 @@
+
+#ifndef STAMPDOCK_H
+#define STAMPDOCK_H
+
+#include "mbed.h"
+
+// #define qarduino 1
+#define qmbed  1
+// #define qlpcxpresso  1
+
+class Stampdock {
+public:
+   Stampdock(void);
+  ~Stampdock(void);
+  
+   int init(void);
+  
+   int print(char *p);
+   int printf(const char *fmt, ... );
+  
+   int GetEvent(void);
+
+   // request the state of GUI element i
+   int GetValue(int i);
+
+   // set the state (v) of gui element i
+   void SetValue(int i, int v);
+
+   char ReadByte(void);
+
+   void SetColor(int r, int g, int b);
+
+   void SetGrey(int g);
+
+   void SetLED(int i, int r, int g, int b);
+
+   void DrawLine(int x1, int y1, int x2, int y2);
+
+   void Plot(int x, int y);
+
+   void DrawGrid(int a, int b, int c, int d, int e, int f);
+
+   void ClearScreen(void);
+   
+   void Cmd(char *p);   
+   
+   void CmdStr(char cmd, char *p  );
+
+   void Cmd0(char cmd  );
+
+   void Req0(char req  );
+
+   void Cmd1(char cmd, int arg1 );
+
+   void Cmd2(char cmd, int arg1, int arg2 );
+
+   void Cmd3(char cmd, int arg1, int arg2, int arg3 );
+
+   void Cmd4(char cmd, int arg1, int arg2, int arg3, int arg4 );
+
+   void Media(char *p);
+
+   void Speech(char *p);
+
+   void Log(char *p);
+   
+   void ScrollLeft(void);   
+   
+
+private:
+
+    //static stampdock *instance;
+    //Serial _serial;
+    char buffer[80];
+};
+
+
+
+
+#endif
+