![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Hackaday demo: Signal generator
Dependencies: SDFileSystem mbed
command.cpp@1:c229c3ceb909, 2015-09-15 (annotated)
- Committer:
- wd5gnr
- Date:
- Tue Sep 15 15:21:34 2015 +0000
- Revision:
- 1:c229c3ceb909
- Parent:
- 0:8baa10bd07de
Making sure all changes are ready for publication.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wd5gnr | 0:8baa10bd07de | 1 | #include "mbed.h" |
wd5gnr | 0:8baa10bd07de | 2 | #define _EXTERN_ extern |
wd5gnr | 0:8baa10bd07de | 3 | #include "main.h" |
wd5gnr | 0:8baa10bd07de | 4 | #include "command.h" |
wd5gnr | 0:8baa10bd07de | 5 | #include "sdcard.h" |
wd5gnr | 0:8baa10bd07de | 6 | |
wd5gnr | 0:8baa10bd07de | 7 | #include <cctype> |
wd5gnr | 0:8baa10bd07de | 8 | |
wd5gnr | 0:8baa10bd07de | 9 | // Commands (except SDCARD commands) |
wd5gnr | 0:8baa10bd07de | 10 | |
wd5gnr | 0:8baa10bd07de | 11 | // Print help |
wd5gnr | 0:8baa10bd07de | 12 | void help(void) |
wd5gnr | 0:8baa10bd07de | 13 | { |
wd5gnr | 1:c229c3ceb909 | 14 | pc.printf("\n\n\n\rPat Gen V1.0. Buffers size %d\r\n",BUFSIZE); |
wd5gnr | 0:8baa10bd07de | 15 | pc.printf("l=load decimal\r\nx=load hex\r\nf=load floating\r\ng=go\r\nt=timebase\r\nz=zap\r\nd=sd card dir\r\nr=sd read\r\nw=sd write\r\n"); |
wd5gnr | 0:8baa10bd07de | 16 | } |
wd5gnr | 0:8baa10bd07de | 17 | |
wd5gnr | 0:8baa10bd07de | 18 | // Fill memory with one or two values (helper function) |
wd5gnr | 0:8baa10bd07de | 19 | static void zapengine(unsigned int v1, unsigned int v2) |
wd5gnr | 0:8baa10bd07de | 20 | { |
wd5gnr | 0:8baa10bd07de | 21 | int i; |
wd5gnr | 0:8baa10bd07de | 22 | for (i=0; i<sizeof(buffer)/sizeof(buffer[0]); i++) { |
wd5gnr | 0:8baa10bd07de | 23 | buffer[i]=(i&1)?v1:v2; |
wd5gnr | 0:8baa10bd07de | 24 | } |
wd5gnr | 0:8baa10bd07de | 25 | |
wd5gnr | 0:8baa10bd07de | 26 | } |
wd5gnr | 0:8baa10bd07de | 27 | |
wd5gnr | 0:8baa10bd07de | 28 | // UI for fill memory (zap) |
wd5gnr | 0:8baa10bd07de | 29 | void zap(void) |
wd5gnr | 0:8baa10bd07de | 30 | { |
wd5gnr | 0:8baa10bd07de | 31 | unsigned int v1,v2; |
wd5gnr | 0:8baa10bd07de | 32 | pc.printf("First value (Esc to quit): "); |
wd5gnr | 0:8baa10bd07de | 33 | if (gethex(&v1)<=0) return; |
wd5gnr | 0:8baa10bd07de | 34 | pc.printf("\r\nSecond value (Enter for none): "); |
wd5gnr | 0:8baa10bd07de | 35 | v2=v1; |
wd5gnr | 0:8baa10bd07de | 36 | if (gethex(&v2)<0) return; |
wd5gnr | 0:8baa10bd07de | 37 | if (ibuf[0]) |
wd5gnr | 0:8baa10bd07de | 38 | sscanf(ibuf,"%x",&v2); |
wd5gnr | 0:8baa10bd07de | 39 | zapengine(v1,v2); |
wd5gnr | 0:8baa10bd07de | 40 | pc.printf("\r\nDone"); |
wd5gnr | 0:8baa10bd07de | 41 | } |
wd5gnr | 0:8baa10bd07de | 42 | |
wd5gnr | 0:8baa10bd07de | 43 | |
wd5gnr | 0:8baa10bd07de | 44 | |
wd5gnr | 0:8baa10bd07de | 45 | |
wd5gnr | 0:8baa10bd07de | 46 | // Load from PC |
wd5gnr | 0:8baa10bd07de | 47 | void load(int decflag) // -1 is float (-1,1), 0 is hex, 1 is dec |
wd5gnr | 0:8baa10bd07de | 48 | { |
wd5gnr | 0:8baa10bd07de | 49 | unsigned int i, n=0; |
wd5gnr | 0:8baa10bd07de | 50 | int rc; |
wd5gnr | 0:8baa10bd07de | 51 | float f=0.0; |
wd5gnr | 0:8baa10bd07de | 52 | pc.printf("Transfer file now (%d items Esc or ; to quit early)\r\n",sizeof(buffer)/sizeof(buffer[0])); |
wd5gnr | 0:8baa10bd07de | 53 | for (i=0; i<sizeof(buffer)/sizeof(buffer[0]); i++) { |
wd5gnr | 0:8baa10bd07de | 54 | if (decflag<0) { |
wd5gnr | 0:8baa10bd07de | 55 | rc=getfloat(&f); |
wd5gnr | 0:8baa10bd07de | 56 | if (f<-1.0f || f>1.0f) { |
wd5gnr | 0:8baa10bd07de | 57 | printf("Range error %f\r\n",f); |
wd5gnr | 0:8baa10bd07de | 58 | rc=-1; |
wd5gnr | 0:8baa10bd07de | 59 | } else |
wd5gnr | 0:8baa10bd07de | 60 | n=(unsigned int)((f+1.0f)*32767.0f+0.5f); |
wd5gnr | 0:8baa10bd07de | 61 | } else |
wd5gnr | 0:8baa10bd07de | 62 | rc=decflag?getdec(&n):gethex(&n); |
wd5gnr | 0:8baa10bd07de | 63 | if (rc<0) { |
wd5gnr | 0:8baa10bd07de | 64 | pc.printf("\r\nAborted at %d\r\n",i); |
wd5gnr | 0:8baa10bd07de | 65 | return; |
wd5gnr | 0:8baa10bd07de | 66 | } else buffer[i]=n; |
wd5gnr | 0:8baa10bd07de | 67 | } |
wd5gnr | 0:8baa10bd07de | 68 | pc.printf("\r\nDone\r\n"); |
wd5gnr | 0:8baa10bd07de | 69 | } |
wd5gnr | 0:8baa10bd07de | 70 | |
wd5gnr | 0:8baa10bd07de | 71 | // Set time base |
wd5gnr | 0:8baa10bd07de | 72 | void timebase() |
wd5gnr | 0:8baa10bd07de | 73 | { |
wd5gnr | 0:8baa10bd07de | 74 | float v; |
wd5gnr | 0:8baa10bd07de | 75 | pc.printf("Time/tick in uS: "); |
wd5gnr | 0:8baa10bd07de | 76 | if (getfloat(&v)>0) timebaseus=v; |
wd5gnr | 0:8baa10bd07de | 77 | } |
wd5gnr | 0:8baa10bd07de | 78 | |
wd5gnr | 0:8baa10bd07de | 79 | |
wd5gnr | 0:8baa10bd07de | 80 | |
wd5gnr | 0:8baa10bd07de | 81 | // Command parser |
wd5gnr | 0:8baa10bd07de | 82 | void command(void) |
wd5gnr | 0:8baa10bd07de | 83 | { |
wd5gnr | 0:8baa10bd07de | 84 | char cmd=' '; |
wd5gnr | 0:8baa10bd07de | 85 | zapengine(0xaaaa,0x5555); |
wd5gnr | 0:8baa10bd07de | 86 | help(); |
wd5gnr | 0:8baa10bd07de | 87 | while (1) { |
wd5gnr | 0:8baa10bd07de | 88 | pc.printf("\r\n? "); |
wd5gnr | 0:8baa10bd07de | 89 | // commands: ? = help, l=load CSV, g=go, t=set timebase |
wd5gnr | 0:8baa10bd07de | 90 | cmd=getachar(); |
wd5gnr | 0:8baa10bd07de | 91 | cmd=tolower(cmd); |
wd5gnr | 0:8baa10bd07de | 92 | pc.printf("\r\n"); |
wd5gnr | 0:8baa10bd07de | 93 | switch (cmd) { |
wd5gnr | 0:8baa10bd07de | 94 | case '?': |
wd5gnr | 0:8baa10bd07de | 95 | help(); |
wd5gnr | 0:8baa10bd07de | 96 | break; |
wd5gnr | 0:8baa10bd07de | 97 | case 'f': |
wd5gnr | 0:8baa10bd07de | 98 | load(-1); |
wd5gnr | 0:8baa10bd07de | 99 | break; |
wd5gnr | 0:8baa10bd07de | 100 | case 'l': |
wd5gnr | 0:8baa10bd07de | 101 | load(1); |
wd5gnr | 0:8baa10bd07de | 102 | break; |
wd5gnr | 0:8baa10bd07de | 103 | case 'x': |
wd5gnr | 0:8baa10bd07de | 104 | load(0); |
wd5gnr | 0:8baa10bd07de | 105 | break; |
wd5gnr | 0:8baa10bd07de | 106 | case 'g': |
wd5gnr | 0:8baa10bd07de | 107 | break; |
wd5gnr | 0:8baa10bd07de | 108 | case 't': |
wd5gnr | 0:8baa10bd07de | 109 | timebase(); |
wd5gnr | 0:8baa10bd07de | 110 | break; |
wd5gnr | 0:8baa10bd07de | 111 | case 'z': |
wd5gnr | 0:8baa10bd07de | 112 | zap(); |
wd5gnr | 0:8baa10bd07de | 113 | break; |
wd5gnr | 0:8baa10bd07de | 114 | case 'd': |
wd5gnr | 0:8baa10bd07de | 115 | dir(); |
wd5gnr | 0:8baa10bd07de | 116 | break; |
wd5gnr | 0:8baa10bd07de | 117 | case 'r': |
wd5gnr | 0:8baa10bd07de | 118 | sdread(); |
wd5gnr | 0:8baa10bd07de | 119 | break; |
wd5gnr | 0:8baa10bd07de | 120 | case 'w': |
wd5gnr | 0:8baa10bd07de | 121 | sdwrite(); |
wd5gnr | 0:8baa10bd07de | 122 | break; |
wd5gnr | 0:8baa10bd07de | 123 | case '\r': |
wd5gnr | 0:8baa10bd07de | 124 | break; |
wd5gnr | 0:8baa10bd07de | 125 | case '\n': |
wd5gnr | 0:8baa10bd07de | 126 | break; |
wd5gnr | 0:8baa10bd07de | 127 | default: |
wd5gnr | 0:8baa10bd07de | 128 | help(); |
wd5gnr | 0:8baa10bd07de | 129 | } |
wd5gnr | 0:8baa10bd07de | 130 | if (cmd=='g') break; // return to caller |
wd5gnr | 0:8baa10bd07de | 131 | } |
wd5gnr | 0:8baa10bd07de | 132 | } |
wd5gnr | 0:8baa10bd07de | 133 | |
wd5gnr | 0:8baa10bd07de | 134 |