control the laser with the MBED

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
bwang
Date:
Mon Nov 11 03:38:40 2019 +0000
Parent:
0:5d2320fc9350
Commit message:
added command line

Changed in this revision

CommandProcessor/CommandProcessor.cpp Show annotated file Show diff for this revision Revisions of this file
CommandProcessor/CommandProcessor.h Show annotated file Show diff for this revision Revisions of this file
CommandProcessor/cmd_helpers.cpp Show annotated file Show diff for this revision Revisions of this file
CommandProcessor/cmd_set_get.cpp Show annotated file Show diff for this revision Revisions of this file
CommandProcessor/cmd_sys.cpp Show annotated file Show diff for this revision Revisions of this file
defaults.h Show annotated file Show diff for this revision Revisions of this file
globals.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
prefs.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5d2320fc9350 -r d42ef49f54df CommandProcessor/CommandProcessor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommandProcessor/CommandProcessor.cpp	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include "CommandProcessor.h"
+
+void processCmd(Serial *pc, char *buf) {
+    char *tokens[10];
+    int len = tokenize(buf, tokens, 10);
+    
+    switch (len) {
+    case 1:
+        if (strcmp(tokens[0], "ls") == 0) cmd_ls(pc);
+        else if (strcmp(tokens[0], "defaults") == 0) cmd_defaults(pc);
+        else if (strcmp(tokens[0], "clear") == 0) cmd_clear(pc);
+        else pc->printf("%s\n", "Bad command");
+        break;
+    case 2:
+        if (strcmp(tokens[0], "ls") == 0) cmd_ls2(pc, tokens[1]);
+        else if (strcmp(tokens[0], "get") == 0) cmd_ls2(pc, tokens[1]);
+        else pc->printf("%s\n", "Bad command");
+        break;
+    case 3:
+        if (strcmp(tokens[0], "set") == 0) cmd_set(pc, tokens[1], tokens[2]);
+        else pc->printf("%s\n", "Bad command");
+        break;
+    default:
+        pc->printf("%s\n", "Bad command");
+        break;
+    }
+}
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df CommandProcessor/CommandProcessor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommandProcessor/CommandProcessor.h	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,22 @@
+#ifndef __COMMAND_PROCESSOR_H
+#define __COMMAND_PROCESSOR_H
+
+#include "mbed.h"
+
+void processCmd(Serial *pc, char *buf);
+
+/*---variable loading, setting, and flashing---*/
+void cmd_ls(Serial *pc);
+void cmd_ls2(Serial *pc, char *buf);
+void cmd_set(Serial *pc, char *buf, char *val);
+void cmd_defaults(Serial *pc);
+
+/*---system commands---*/
+void cmd_clear(Serial *pc);
+
+/*---internal functions---*/
+int tokenize(char *buf, char **out, int max);
+float *checkf(char *s);
+int *checkn(char *s);
+
+#endif
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df CommandProcessor/cmd_helpers.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommandProcessor/cmd_helpers.cpp	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+
+#include "CommandProcessor.h"
+#include "prefs.h"
+
+int tokenize(char *buf, char **out, int max) {
+    char* tok;
+    int k = 0;
+    
+    tok = strtok(buf, " ");
+    
+    while(tok != NULL && k < max) {
+        out[k] = tok;
+        k++;
+        tok = strtok(NULL, " ");
+    }
+    return k;
+}
+
+#define __check(x) if(strcmp(s, #x) == 0) return &_##x
+#define __check2(x) if (strcmp(s, #x) == 0) return &x
+
+float* checkf(char *s) {
+    return NULL;
+}
+
+int* checkn(char *s) {
+    __check(PULSE_WIDTH);
+    __check(PULSE_FREQ);
+    return NULL;
+}
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df CommandProcessor/cmd_set_get.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommandProcessor/cmd_set_get.cpp	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "CommandProcessor.h"
+
+#include "defaults.h"
+#include "prefs.h"
+#include "globals.h"
+
+void cmd_ls(Serial *pc) {
+    DPRINT(PULSE_WIDTH);
+    DPRINT(PULSE_FREQ);
+}
+
+#define ls_specialf(a) if (strcmp(buf, #a) == 0) {pc->printf("%s: %f\n", #a, a); return;}
+#define ls_speciald(a) if (strcmp(buf, #a) == 0) {pc->printf("%s: %d\n", #a, a); return;}
+void cmd_ls2(Serial *pc, char *buf) {    
+    float *fptr = checkf(buf);
+    if (fptr != NULL) pc->printf("%s: %f\n", buf, *fptr);
+    int *nptr = NULL;
+    if (fptr == NULL) nptr = checkn(buf);
+    if (nptr != NULL) pc->printf("%s: %d\n", buf, *nptr);
+    if (nptr == NULL && fptr == NULL) pc->printf("%s\n", "No Such Parameter");
+}
+
+void cmd_defaults(Serial *pc) {
+    DEFAULT(PULSE_WIDTH);
+    DEFAULT(PULSE_FREQ);
+    pc->printf("Defaults Loaded\n");
+}
+
+void cmd_set(Serial *pc, char *buf, char *val) {
+    float *fptr = checkf(buf);
+    if (fptr != NULL) *fptr = (float) (atof(val));
+    int *nptr = NULL;
+    if (fptr == NULL) nptr = checkn(buf);
+    if (nptr != NULL) *nptr = (int) (atoi(val));
+    if (nptr != NULL || fptr != NULL) cmd_ls2(pc, buf);
+    if (nptr == NULL && fptr == NULL) pc->printf("%s\n", "No Such Parameter");
+    
+    if (strcmp(buf, "PULSE_FREQ") == 0) {
+        out.period_us(1000 / _PULSE_FREQ);
+    }
+}
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df CommandProcessor/cmd_sys.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CommandProcessor/cmd_sys.cpp	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,6 @@
+#include "mbed.h"
+#include "CommandProcessor.h"
+
+void cmd_clear(Serial *pc) {
+    pc->printf("\e[1;1H\e[2J");
+}
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df defaults.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/defaults.h	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,8 @@
+#ifndef __DEFAULTS_H
+#define __DEFAULTS_H
+
+/*---pulsewidth/frequency---*/
+#define PULSE_WIDTH 2
+#define PULSE_FREQ 100
+
+#endif
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df globals.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globals.h	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,6 @@
+#ifndef __GLOBALS_H
+#define __GLOBALS_H
+
+extern PwmOut out;
+
+#endif
\ No newline at end of file
diff -r 5d2320fc9350 -r d42ef49f54df main.cpp
--- a/main.cpp	Mon Nov 11 02:53:52 2019 +0000
+++ b/main.cpp	Mon Nov 11 03:38:40 2019 +0000
@@ -1,4 +1,9 @@
 #include "mbed.h"
+#include "CommandProcessor.h"
+#include "prefs.h"
+
+float __float_reg[64];
+int __int_reg[64];
 
 DigitalOut led(LED1);
 InterruptIn in(PA_10);
@@ -6,8 +11,38 @@
 
 Serial pc(USBTX, USBRX);
 
+char linebuf[128];
+int index = 0;
+void rxCallback() {
+    while (pc.readable()) {
+        char c = pc.getc();
+        if (c != 127 && c != 8 && c != '\r' && c != '\t') {
+            linebuf[index] = c;
+            if (index < 127) index++;
+            if (c < 128) pc.putc(c);
+        } else if (c == 127 || c == 8) {
+            if (index > 0) {
+                index--;
+                //BS (8) should delete previous char
+                pc.putc(127);
+            }
+        } else if (c == '\r') {
+            linebuf[index] = 0;
+            if (index > 0) {
+                pc.putc(c);
+                processCmd(&pc, linebuf);
+                pc.putc('>');
+            } else {
+                pc.putc(c);
+                pc.putc('>');
+            }
+            index = 0;
+        }
+    }
+}
+
 void turn_on() {
-    out.pulsewidth_us(2);
+    out.pulsewidth_us(_PULSE_WIDTH);
     led = 1;
 }
 
@@ -17,9 +52,15 @@
 }
 
 int main() {
+    pc.baud(115200);
+    pc.attach(rxCallback);
     pc.printf("PYROFLEX\n");
+    cmd_clear(&pc);
+    cmd_defaults(&pc);
+    pc.printf(">");
+    
     out.period_us(10);
-    out.pulsewidth_us(2);
+    out.pulsewidth_us(_PULSE_WIDTH);
     
     in.rise(turn_on);
     in.fall(turn_off);
diff -r 5d2320fc9350 -r d42ef49f54df prefs.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/prefs.h	Mon Nov 11 03:38:40 2019 +0000
@@ -0,0 +1,21 @@
+#ifndef __CONFIG_H
+#define __CONFIG_H
+
+#include "mbed.h"
+
+/*---pulsewidth/frequency---*/
+#define _PULSE_WIDTH __int_reg[0]
+#define _PULSE_FREQ __int_reg[1]
+
+/*internal variables and macros*/
+
+extern float __float_reg[];
+extern int __int_reg[];
+
+#define DEFAULT(a) _##a = a
+#define FPRINT(a) pc->printf("%s: %f\n", #a, _##a)
+#define DPRINT(a) pc->printf("%s: %d\n", #a, _##a)
+#define FPRINT2(a) pc->printf("%s: %f\n", #a, a);
+#define DPRINT2(a) pc->printf("%s: %d\n", #a, a)
+
+#endif
\ No newline at end of file