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
SerialCom/sigbind.cpp
- Committer:
- aktk
- Date:
- 2019-10-23
- Revision:
- 1:b97639dad576
- Parent:
- 0:19a67422961f
File content as of revision 1:b97639dad576:
#include "sigbind.h"
namespace aktk_sigbind
{
// To avoid pressing key too much
enum Lock {UNLOCK, LOCK};
Lock aktk_keylock = UNLOCK;
//private functions
template<typename T>
int scanValue(T* arg_val);
void waitUnpress();
void setPulseParam();
void ArrowKeyBind(const char arg_c);
//void printStimType();
void printStatus(void);
void keyBind()
{
//stimulator.DAAD(0);
char sig = pc.getc();
if (aktk_keylock == UNLOCK) {
aktk_keylock = LOCK;
switch(sig) {
case 'w':
spacing = WIDE_CONDITION;
pc.printf("Wide condition\n");
waitUnpress();
break;
case 'n':
spacing = NARROW_CONDITION;
pc.printf("Narrow condition\n");
waitUnpress();
break;
case 'm':
switch(pc.getc()) {
case 'b':
smode = BIPOLAR;
pc.printf("BIPOLAR Stimulation\n");
break;
case 'a':
smode = ANODIC;
pc.printf("ANODIC Stimulation\n");
break;
case 'c':
smode = CATHODIC;
pc.printf("CATHODIC Stimulation\n");
break;
}
waitUnpress();
break;
case 'p':
printStatus();
waitUnpress();
break;
case 'v':
setPulseParam();
waitUnpress();
break;
case 'a': //switch mbed-pc communication on/off
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");
waitUnpress();
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) {
ArrowKeyBind(pc.getc());
pc.printf("Hight:%d\n",ph);//sc.getPH());
}
waitUnpress();
break;
default:
printKBManual();
pc.printf("Sent data was: %d(%c)\n", sig, sig);
waitUnpress();
break;
}
//aktk_keylock = UNLOCK;
} else if (sig == 0x1B) //esc
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 setPulseParam()
{
int lph, lpp, lpw;
lph = ph;//sc.getPH();
lpp = pp;//sc.getPP();
lpw = pw;//sc.getPW();
// read values
pc.printf("set pulse Height:");
if( scanValue<int>(&lph) ) goto BreakV;
pc.printf("set pulse period:");
if( scanValue<int>(&lpp) ) goto BreakV;
pc.printf("set pulse width :");
if( scanValue<int>(&lpw) ) goto BreakV;
// set values
ph = lph;
pp = lpp;
pw = lpw;
BreakV:
waitUnpress();
}
void ArrowKeyBind(const char arg_c)
{
switch(arg_c) {
case 0x41 : {
if(ph <= 4090) //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 <= 4045)//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;
}
}
}
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"
" w: Wide condition\n"
" n: Narrow condition\n"
" ---\n"
" mb: Bipolar stimulation\n"
" ma: Anodic Stimulation\n"
" mc: Cathodic Stimulation\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