Hackaday demo: Signal generator

Dependencies:   SDFileSystem mbed

Revision:
0:8baa10bd07de
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cio.cpp	Fri Sep 11 02:32:43 2015 +0000
@@ -0,0 +1,115 @@
+#include "mbed.h"
+#define _EXTERN_ extern
+#include "main.h"
+#include "cio.h"
+#include <cctype>
+
+// Character I/O functions
+
+// Get a character but handle backspace
+// However, since we don't understand the start of a line
+// Don't print backspaces
+// Return is a non-zero character
+int getachar(void)
+{
+    int c=0;
+    while (!c) {
+        c=pc.getc();
+        if (c==0x7f) c=8;  // backspace
+        if (c!=8) pc.putc(c);
+        if (c=='\n') c='\r';
+    }
+    return c;
+}
+
+
+
+
+
+// Get a token (number)
+// basically we skip anything that doesn't look
+// like a hex number (even in decimal mode)
+// so we skip comma, quotes, etc. in CSV
+// Return: -1 - Exit via Esc, Semicolon or CNTL_Z
+// 0 - ok
+// 1 - long token
+// Odd input may cause odd results (for example floating point 1.2FF or something
+int gettoken(int type)
+{
+    int ip=0;
+    int c;
+    bool good;
+    do {
+        good=true;
+        c=getachar();  // get character
+        if (c==27 || c==';' || c==26) return -1;   // escape, semicolon, or ^Z returns -1 (stop)
+        if (c==8 && ip!=0) {
+            ip--;    // backspace not at first position
+            pc.putc(8);
+            pc.putc(' ');
+            pc.putc(8);
+        }
+        if (c==8) continue;  // any backspace
+        if (type==NUM_DEC && (c=='x' || c=='X')) type=NUM_HEX;  // decimal to hex if you see 0x
+
+        switch (type) {
+            case NUM_HEX:
+                good=isxdigit(c)||c=='x'||c=='X';  // hex digits (including 0x)
+                break;
+            case NUM_DEC:
+                good=isdigit(c);   // decimal digits
+                break;
+            case NUM_FLT:
+                good=isdigit(c)||c=='.'||c=='-'||c=='e'||c=='E';  // simple floating point
+                break;
+        }
+        if (!good) { // illegal character?
+            if (ip) {
+                ibuf[ip]='\0';   // got something
+                return 0;
+            } else
+                continue;   // just skip leading cruft
+        }
+        if (ip<sizeof(ibuf)-1) ibuf[ip++]=c;  // accumulate
+    } while (1);
+}
+
+
+// Get a token and convert to a number
+int getnum(unsigned int *v, int type)
+{
+    int val, rc, forcehex=0;
+    if (gettoken(type)<0) return -1;
+    if (ibuf[0]=='0' && (ibuf[1]=='x' || ibuf[1]=='X')) {
+        forcehex=2;
+        type=0;
+    }
+// Get number from input buffer (forcehex skips 0x)
+    rc=sscanf(ibuf+forcehex,type?"%d":"%x",&val);
+    if (rc && v) *v=val;
+    return rc==-1?0:rc;
+}
+
+// Get decimal number
+int getdec(unsigned int *v)
+{
+    return getnum(v,NUM_DEC);
+}
+
+
+// Get hex number
+int gethex(unsigned int *v)
+{
+    return getnum(v,NUM_HEX);
+}
+
+// Get float number
+int getfloat(float *v)
+{
+    int rv;
+    float vv;
+    if (gettoken(NUM_FLT)<0) return -1;
+    rv=sscanf(ibuf,"%f",&vv);
+    if (rv && v) *v=vv;
+    return rv;
+}
\ No newline at end of file