//绑定img为btn void imgbase::btn_bind(void *outitem,int min_alpha,void *data) { btn_map = new btnmap; //创建不规则按钮 if(min_alpha!=0) { mytex *btn_map_tex = new mytex; btn_map_tex->tex = 0; if(rendr_to_texture(btn_map_tex,btn_map->x_delta,btn_map->y_delta)) { btn_map->width = btn_map_tex->width; btn_map->height = btn_map_tex->height; getbtnmap(btn_map_tex,btn_map,min_alpha); } //释放btnmap纹理 if(btn_map_tex) SAFE_RELEASE(btn_map_tex->tex); delete btn_map_tex; btn_map_tex = 0; } btn_map->min_alpha = min_alpha; btn_map->outitem = outitem; //主程序中储存img数据的结构指针 btn_map->data = data; //绑定的数据,回调时返回 btn_enable = true; }
//当img发生变化(放缩,角度变化)时重新刷新btnmap void imgbase::refresh_map() { if(btn_map->min_alpha==0) return; mytex *btn_map_tex = new mytex; if(rendr_to_texture(btn_map_tex,btn_map->x_delta,btn_map->y_delta)) { //释放原有的map delete[] *(btn_map->btn_map); delete[] btn_map->btn_map; btn_map->width = btn_map_tex->width; btn_map->height = btn_map_tex->height; getbtnmap(btn_map_tex,btn_map,btn_map->min_alpha); } //释放btnmap纹理 SAFE_RELEASE(btn_map_tex->tex); delete btn_map_tex; btn_map_tex = 0; }
int main (int argc, char **argv) { FILE *fp; char fBuf[60]; int fd1; char buf2 [64] ; // int PinA = 0; // int PinB = 2; // int PinC = 3; int fd; int i; unsigned long curJsTime=0; unsigned long prevJsTime=0; int botDir=-1; unsigned char armDir=0; unsigned char axes = 2; unsigned char buttons = 2; int version = 0x000800; char name[NAME_LENGTH] = "Unknown"; uint16_t btnmap[BTNMAP_SIZE]; uint8_t axmap[AXMAP_SIZE]; int btnmapok = 1; int blevel; // char btaddress[18]="00:16:53:0e:c9:97"; //old NXT // char btaddress[18]="00:16:53:03:ca:76"; //new NXT char btaddress[18]; time_t curr_time, prev_time, diff_time; fp = fopen("/root/btslct.conf","r"); if(fp != NULL){ fBuf[0]=0; while(fgets(fBuf,sizeof fBuf, fp) != NULL){ strncpy(btaddress,&fBuf[0],18);//NXT BT addr } } fclose(fp); if (wiringPiSetup () == -1) exit (1) ; fd1 = lcdInit (2, 16, 4, 11, 10, 0,1,2,3,0,0,0,0) ; if (fd1 == -1) { printf ("lcdInit 1 failed\n") ; return 1 ; } pinMode (4, INPUT) ; // Pin 4 EncA pinMode (5, INPUT) ; // Pin 5 EncB pinMode (6, INPUT) ; // Pin 6 EncSw CurVal = digitalRead(4); PrevVal = CurVal; lcdPosition (fd1, 0, 0); lcdPuts (fd1, " GLXP MoonBots "); sleep(1); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Connecting to "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Robot "); //sleep(1); int waitForStart =0; if(init_bluetooth(btaddress) <0){ waitForStart =0; close(nxtSocket); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Not Connected "); sleep(1); return 1; } else waitForStart =1; lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, "Connected Via BT"); sleep(1); // digitalWrite(PinA, 0); printf("bluetooth connected to %s \n", btaddress); blevel = nxt_getbattery(); if(blevel<0){ waitForStart =0; close(nxtSocket); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Low Battery "); sleep(1); return 1; } else waitForStart =1; printf("battery level: %.2f\n",blevel/1000.00); // mlevel = nxt_move(); nxt_move_vr(0,0); /* if (argc < 2 || argc > 3 || !strcmp("--help", argv[1])) { puts(""); puts("Usage: jstest [<mode>] <device>"); puts(""); puts("Modes:"); puts(" --normal One-line mode showing immediate status"); puts(" --old Same as --normal, using 0.x interface"); puts(" --event Prints events as they come in"); puts(" --nonblock Same as --event, in nonblocking mode"); puts(" --select Same as --event, using select() call"); puts(""); return 1; } */ if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) { perror("jstest"); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Gamepad "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Not Found "); sleep(1); return 1; } // digitalWrite(PinB, 0); ioctl(fd, JSIOCGVERSION, &version); ioctl(fd, JSIOCGAXES, &axes); ioctl(fd, JSIOCGBUTTONS, &buttons); ioctl(fd, JSIOCGNAME(NAME_LENGTH), name); getaxmap(fd, axmap); getbtnmap(fd, btnmap); printf("Driver version is %d.%d.%d.\n", version >> 16, (version >> 8) & 0xff, version & 0xff); /* Determine whether the button map is usable. */ for (i = 0; btnmapok && i < buttons; i++) { if (btnmap[i] < BTN_MISC || btnmap[i] > KEY_MAX) { btnmapok = 0; break; } } if (!btnmapok) { /* btnmap out of range for names. Don't print any. */ puts("jstest is not fully compatible with your kernel. Unable to retrieve button map!"); printf("Joystick (%s) has %d axes ", name, axes); printf("and %d buttons.\n", buttons); } else { printf("Joystick (%s) has %d axes (", name, axes); for (i = 0; i < axes; i++) printf("%s%s", i > 0 ? ", " : "", axis_names[axmap[i]]); puts(")"); printf("and %d buttons (", buttons); for (i = 0; i < buttons; i++) { printf("%s%s", i > 0 ? ", " : "", button_names[btnmap[i] - BTN_MISC]); } puts(")."); } printf("Testing ... (interrupt to exit)\n"); int *axis; char *button; // int ii; struct js_event js; int apwr = 0; int preApwr=0; int mvpwr = 0; int preMvpwr = 0; int waitForRed=0; int motorNotSync = 0; axis = calloc(axes, sizeof(int)); button = calloc(buttons, sizeof(char)); fcntl(fd, F_SETFL, O_NONBLOCK);//js non-blocking mode, remove for event mode lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Set Gamepad "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Mode to Red? "); // while((!waitForRed)&&(read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event))) { while(!waitForRed){ read(fd, &js, sizeof(struct js_event)); if((js.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS){ axis[js.number] = js.value; if((axis[0]!=0)||(axis[1]!=0)||(axis[2]!=0)||(axis[3]!=0)||(axis[4]!=0)) waitForRed=0; else waitForRed=1; }//if fflush(stdout); } // sleep(2); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot Ready "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Gamepad Ready "); sleep(1); // digitalWrite(PinC, 0); // ii = 0; prev_time = time((time_t *)0); while(1){ while(waitForStart){ curr_time = time((time_t *)0); diff_time = curr_time - prev_time; if(diff_time >= 1){ blevel = nxt_getbattery(); if(blevel<0){ close(nxtSocket); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, "Connection Lost "); sleep(1); waitForStart=0; // return 1; }//blevel else{ // sprintf (buf2, "Battery: %.2fV ",blevel/1000.00); // lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); sprintf (buf2, "Connected: %.2fV",blevel/1000.00); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " GLXP MoonBots "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, buf2); } printf("battery level: %.2f\n",blevel/1000.00); // printf("Time diff %ld\n",diff_time); prev_time = curr_time; diff_time=0; }//diff_time while (read(fd, &js, sizeof(struct js_event)) == sizeof(struct js_event)) { if(!btBusy){ switch(js.type & ~JS_EVENT_INIT) { case JS_EVENT_BUTTON: // ii=0; button[js.number] = js.value; if(button[8]){ if(botDir==1) botDir=-1; else botDir=1; } if(button[9]){ if(armDir) armDir=0; else armDir=1; } if(button[0] && !button[2]) apwr = 30; else if(!button[0] && button[2]) apwr = -30; else apwr = 0; if(armDir){ apwr = -1* apwr; } if(apwr!=preApwr){ nxt_move_arm(apwr); preApwr=apwr; } if(button[6] && !button[4] && !button[7] && !button[5]) mvpwr = -20; else if(!button[6] && button[4] && !button[7] && !button[5]) mvpwr = 20; else if(!button[6] && !button[4] && button[7] && !button[5]) mvpwr = -100; else if(!button[6] && !button[4] && !button[7] && button[5]) mvpwr = 100; else mvpwr = 0; if(mvpwr!=preMvpwr){ nxt_move_sync(botDir*mvpwr,motorNotSync); motorNotSync=0; preMvpwr=mvpwr; } // if(button[0]) nxt_start_prog(); // if(button[2]) nxt_stop_prog(); break; case JS_EVENT_AXIS: if((js.number == 1)||(js.number==4)){//process only axis 1 and 4 curJsTime=js.time; // if(curJsTime != prevJsTime){ axis[js.number] = js.value; // printf("%d %d %d %d %d\n",axis[1],axis[2],axis[3], axis[4],js.number); if(js.number==1) { // nxt_move_x(botDir*axis[1],320,1); // nxt_move_b(botDir*axis[1],420); motorNotSync=1; nxt_move_b(botDir*axis[1],520); } if(js.number==4) { // nxt_move_x(botDir*axis[4],320,2); // nxt_move_c(botDir*axis[4],420); motorNotSync=1; nxt_move_c(botDir*axis[4],520); } // if(botDir) // nxt_move_vr((axis[1]/320),(axis[4]/320)); // else // nxt_move_vr(-1*(axis[1]/320),-1*(axis[4]/320)); // prevJsTime=curJsTime; // } } break; }//switch JS // if(botDir) // nxt_move_vr((axis[1]/320),(axis[4]/320)); // else // nxt_move_vr(-1*(axis[1]/320),-1*(axis[4]/320)); // ii++; // usleep(100000); }//if !btBusy // usleep(10000); }//while js read if(errno != EAGAIN){ perror("\njstest1: error reading"); return 1; }//if error // usleep(10000); }//waitforstart if(digitalRead(6) ==0){ if(init_bluetooth(btaddress) <0){ waitForStart =0; close(nxtSocket); lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Robot "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " Not Connected "); sleep(1); } else waitForStart =1; } else{ lcdPosition (fd1, 0, 0); lcdPuts (fd1, " Press Knob "); lcdPosition (fd1, 0, 1); lcdPuts (fd1, " to Reconnect "); } }//while(1) printf("jstest2: unknown mode: %s\n", argv[1]); close(nxtSocket); return -1; }