//////////////////////////////////////// // dc_motor_round.c // DCモーターの回転制御サンプルプログラム // // ↑を押している間DCモーターが回転する // // 環境: grace, FreeBSD8.3R // 2014/06/15 t.shimizu プログラム // s.minemura 回路設計 //////////////////////////////////////// #include "grobot.h" int main(void) { Display *d; Window w; KeySym key; XEvent e; char string[10]; int flg = 1, fd; u_int8_t val; fd = open("/dev/ppi0",O_RDWR, 0600);//おまじない d = XOpenDisplay(NULL); w = XCreateSimpleWindow(d, RootWindow(d, 0),300,300,400,200,2, BlackPixel(d,0), WhitePixel(d,0)); XSelectInput(d, w, KeyPressMask | KeyReleaseMask); XMapWindow(d, w); //ウインドウを画面に表示する while(flg){ //flg=1なので無限ループ XNextEvent(d, &e); XLookupString(&e, string,10, &key, NULL); if(key == XK_Up){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x01; //ピン2,3(H,L) CW ioctl(fd, PPISDATA, &val); printf("↑\n"); } } if(key == XK_Down){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x02; //ピン2,3(L,H) CCW ioctl(fd, PPISDATA, &val); printf("↓\n"); } } if(key == XK_Left){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x04; //ピン4,5(H,L) CW ioctl(fd, PPISDATA, &val); printf("←\n"); } } if(key == XK_Right){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x08; //ピン4,5(L,H) CCW ioctl(fd, PPISDATA, &val); printf("→\n"); } } if(key == XK_F12){ //停止 while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x00; ioctl(fd, PPISDATA, &val); } } if(key == XK_F1){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x10; ioctl(fd, PPISDATA, &val); } } if(key == XK_F2){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x20; ioctl(fd, PPISDATA, &val); } } if(key == XK_F3){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x40; ioctl(fd, PPISDATA, &val); } } if(key == XK_F4){ while(e.type == KeyPress){ XNextEvent(d, &e); val = 0x80; ioctl(fd, PPISDATA, &val); } } else if(key == XK_Escape){ printf("停止します\n"); val = 0x00; ioctl(fd, PPISDATA, &val); flg = 0; } else{ while(e.type == KeyPress){ system("set r off"); XNextEvent(d, &e); printf("以下のキーを入力してください\n"); } system("xset r on"); } } system("xset r on"); }