Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SerialInputReactionHandler AMPulseTrain SwArr16MOSFET StrCommandHandler KajiLabES
Diff: SerialCom/sigbind.cpp
- Revision:
- 0:19a67422961f
- Child:
- 1:b97639dad576
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialCom/sigbind.cpp Tue Oct 23 15:11:18 2018 +0000
@@ -0,0 +1,254 @@
+#include "sigbind.h"
+namespace aktk_sigbind
+{
+// To avoid pressing key too much
+enum Lock {UNLOCK, LOCK};
+Lock aktk_keylock = UNLOCK;
+
+void waitUnpress()
+{
+ while (1) {
+ if(! pc.readable()) {
+ aktk_keylock = UNLOCK;
+ break;
+ }
+ }
+}
+template<typename T>
+int scanValue(T* arg_val)
+{
+ char c = 0;
+ char c_prevent = 0;
+ double tmp = 0.0;
+ double fig = 1.0;
+ bool dot_flag = false;
+ bool no_modified = true;
+
+ T arg_last_val = *arg_val;
+
+ do {
+ c = pc.getc();
+
+ if ('0' <= c && c <= '9') {
+ no_modified = false;
+ pc.putc(c);
+ if(! dot_flag) {
+ tmp *= 10.0;
+ tmp += (double)(c - '0');
+ } else {
+ fig *= 10.0;
+ tmp += (double)(c - '0') / fig;
+ }
+ } else if (c == '.') {
+ if(dot_flag == 0)pc.printf(".");
+ dot_flag = true;
+ } else if (c == 'k' || c == 'K') {
+ pc.printf("(keep present value)\n");
+ tmp = (double) arg_last_val;
+ break;
+ } else if (c == 0x10 || c == 0x08) {
+ pc.putc(0x08);
+ pc.putc(0x20);
+ pc.putc(0x08);
+ if(! dot_flag) {
+ c_prevent = (double)(((int)tmp) % 10);
+ tmp -= (double)(c_prevent);
+ tmp /= 10;
+ } else {
+ if(fig == 1.0)
+ dot_flag = false;
+ else {
+ c_prevent = (double)((int)(tmp * fig) % 10);
+ tmp -= (double)(c_prevent) / fig;
+ fig /= 10;
+ }
+ }
+ if(tmp == 0) no_modified = true;
+ } else if (c == 0x1b) {
+ pc.printf("CANSELED\n");
+ return -1;
+ } else if (c == 0x0A || c == 0x0D) {
+ pc.printf("\n");
+ break;
+ }
+ } while(1);
+
+ waitUnpress();
+
+ if(no_modified) *arg_val = arg_last_val;
+ else *arg_val = static_cast<T>(tmp);
+ pc.printf("tmp:%f\n",tmp);
+
+ return 0;
+}
+//int scanValue<int>(int*);
+//int scanValue<double>(double*);
+//int scanValue<unsigned short>(unsigned short*);
+
+void keyBind()
+{
+ char sig = pc.getc();
+ if (aktk_keylock == UNLOCK) {
+ aktk_keylock = LOCK;
+ switch(sig) {
+ case 'p':
+ printStatus();
+ waitUnpress();
+ break;
+ case 'v': {
+ int sv_state;
+ int lph, lpp, lpw;
+
+ //printStimType();
+ lph = ph;//sc.getPH();
+ lpp = pp;//sc.getPP();
+ lpw = pw;//sc.getPW();
+ for (int i = 0; i < 3; i++) {
+ switch (i) {
+ case 0:
+ pc.printf("set pulse Height:");
+ sv_state = scanValue<int>(&lph);
+ if(sv_state)goto BreakV;
+ break;
+ case 1:
+ pc.printf("set pulse period:");
+ sv_state = scanValue<int>(&lpp);
+ if(sv_state)goto BreakV;
+ break;
+ case 2:
+ pc.printf("set pulse width :");
+ sv_state = scanValue<int>(&lpw);
+ if(sv_state)goto BreakV;
+ break;
+ }
+ }
+ //sc.setWaveParam(lph,lpp,lpw);
+ ph = lph;
+ pp = lpp;
+ pw = lpw;
+BreakV:
+ waitUnpress();
+ break;
+ }
+
+
+ case 'a': //switch mbed-pc communication on/off
+ //pstate = OnOff(!pstate);
+ if (pstate == MAIN_ROUTINE) {
+ pstate = WAIT_A_CERTAIN_KEY;
+ pc.printf("stop\n");
+ } else if (pstate == WAIT_A_CERTAIN_KEY) {
+ pstate = MAIN_ROUTINE;
+ //printStimType();
+ pc.printf("start\n");
+ }
+ waitUnpress();
+ break;
+ case 'E':
+ pstate = TERMINATED;
+ pc.printf("TERMINATED");
+ break;
+
+ case 'S':// start
+ if (pstate == WAIT_A_CERTAIN_KEY) {
+ pstate = MAIN_ROUTINE;
+ //printStimType();
+ pc.printf("start\n");
+ }
+ waitUnpress();
+ break;
+
+ case 'P':// pause
+ pstate = WAIT_A_CERTAIN_KEY;
+ pc.printf("stop\n");
+ waitUnpress();
+ break;
+
+
+ case 0x1b: {
+ if(pc.getc() == 0x5b) {
+ switch(pc.getc()) {
+ case 0x41 : {
+ if(ph < 4095) //if(sc.getPH()<4000)
+ ph = ph + 5;//sc.setWaveParam(sc.getPH() + 5, sc.getPP(),sc.getPW());
+ else
+ ph = 4095;
+ pc.printf("up\n");
+ break;
+ }
+ case 0x42 : {
+ if(5 < ph) //if(5<sc.getPH())
+ ph = ph - 5;//sc.setWaveParam(sc.getPH() - 5, sc.getPP(),sc.getPW());
+ else
+ ph = 0;//sc.setWaveParam(0, sc.getPP(),sc.getPW());
+ pc.printf("down\n");
+ break;
+ }
+ case 0x43 : {
+ if(ph < 4095)//if(sc.getPH()<4000)
+ ph = ph + 50;//sc.setWaveParam(sc.getPH() + 50, sc.getPP(),sc.getPW());
+ else
+ ph = 4095;
+ pc.printf("shift up\n");
+ break;
+ }
+ case 0x44 : {
+ if(50 < ph)//if(50<sc.getPH())
+ ph = ph - 50;//sc.setWaveParam(sc.getPH() - 50, sc.getPP(),sc.getPW());
+ else
+ ph = 0;//sc.setWaveParam(0, sc.getPP(),sc.getPW());
+ pc.printf("shift down\n");
+ break;
+ }
+ default: {
+ //pc.printf("default\n");
+ break;
+ }
+ }
+ }
+ pc.printf("Hight:%d\n",ph);//sc.getPH());
+ waitUnpress();
+ break;
+ }
+
+ default:
+ printKBManual();
+ pc.printf("Sent data was: %d(%c)\n", sig, sig);
+
+ waitUnpress();
+ break;
+ }
+
+ } else if (sig == 0x1B) //esc
+ aktk_keylock = UNLOCK;
+}
+void printKBManual(void)
+{
+ pc.printf( "\n"
+ "keybind are following:\n"
+ " ---\n"
+ " v: set all paramater by arbitrary value\n"
+ " ---\n"
+ " UP: pulse hight + 5\n"
+ " Down: pulse hight - 5\n"
+ " right: pulse hight +50\n"
+ " left: pulse hight -50\n"
+ " ---\n"
+ " p: print wave parameter\n"
+ " ---\n"
+ " a: Start or Pause main loop\n"
+ " S: Start or Resume Stimulation\n"
+ " P: Pause Stimulation\n"
+ " E(shift+a): terminate the loop\n"
+ "\n");
+}
+void printStatus(void)
+{
+ pc.printf(
+ "PHeight:%05d "
+ "PPeriod:%05d "
+ "PWidth :%05d\n"
+ //,(int)sc.getPH(), (int)sc.getPP(), (int)sc.getPW()
+ ,ph,pp,pw);
+}
+}//end of name space
\ No newline at end of file