
Interference current stimulation program with kaji-lab ES device.
Dependencies: mbed SerialInputReactionHandler AMPulseTrain SwArr16MOSFET StrCommandHandler KajiLabES
Revision 0:19a67422961f, committed 2018-10-23
- Comitter:
- aktk
- Date:
- Tue Oct 23 15:11:18 2018 +0000
- Child:
- 1:b97639dad576
- Commit message:
- used in UIST2018 DEMO
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KajiLabES.lib Tue Oct 23 15:11:18 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/aktk/code/KajiLabES/#9b21e855a577
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PMRC16ch.lib Tue Oct 23 15:11:18 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/aktk/code/PMRC4ch/#fa067e2a30f2
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialCom/sigbind.h Tue Oct 23 15:11:18 2018 +0000 @@ -0,0 +1,24 @@ +#ifndef SIG_BIND_H +#define SIG_BIND_H +#include "mbed.h" + + +namespace aktk_sigbind +{ +// prototype +void keyBind(); +void printKBManual(void); +//void printStimType(); +void printStatus(void); +} + +extern uint32_t pw, pp, ph; + +// Mbed Specails +extern Serial pc; // tx, rx + +// - For Procedure state +enum ProcessState {WAIT_A_CERTAIN_KEY, MAIN_ROUTINE, TERMINATED}; +extern ProcessState pstate; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 23 15:11:18 2018 +0000 @@ -0,0 +1,58 @@ +#include "mbed.h" +#include "KajiLabES.h" +#include "PMRC16ch.h" +#include "sigbind.h" +#include <iostream> +#include <bitset> +ProcessState pstate; +Serial pc(USBTX, USBRX); // tx, rx + +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); +KajiLabES stimulator; +PMRC16ch pmrc(16); +uint32_t pw;//pulse width +uint32_t pp;//pulse period +uint32_t ph;//pulse hight + +int main() +{ + stimulator.DAADinit(); + myled1 = 1; + pmrc.allHiZ(); + myled2 = 1; + { + using namespace aktk_sigbind; + pc.baud(921600); + pc.attach(&keyBind); + pw = 100; + pp = 5000; + ph = 0; + printKBManual(); + wait(.5); + } + while(1) { + while (pstate == WAIT_A_CERTAIN_KEY) { + myled3 = (!myled3); + wait(0.5); + } + myled3 = 1; + pmrc.setPol(PMRC16ch::Cathodic); + while (pstate == MAIN_ROUTINE) { + for(int i = 0; i < 2; i++) { + //pmrc.setTwin(i+1, (i+4)%8+1); + pmrc.setTwin(i+1, (i+1)%8+1); + //pmrc.setOvsO(i+1); + wait_us(pp-pw); + stimulator.DAAD(ph); + wait_us(pw); + stimulator.DAAD(0); + //pmrc.allHiZ(); + //wait_us(pw); + } + if (pstate == TERMINATED) break; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 23 15:11:18 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/5aab5a7997ee \ No newline at end of file