int16_t spektrum_satellite_get_channel(uint8_t index) { int32_t i; if (time_keeper_get_millis()-last_update>20) { #ifdef KEYBOARD_ACTIVE get_keyboard_input(&joystick_axes); #endif get_joystick_status(joystick_filedescriptor, &joystick_axes, &joystick_buttons, 16, 16); for (i=0; i<16; i++) { if (joystick_axes[i]>joy_max[i]) joy_max[i]=joystick_axes[i]; if (joystick_axes[i]<joy_min[i]) joy_min[i]=joystick_axes[i]; } sat.channels[RC_ROLL] = joystick_axes[JOY_ROLL]*J_GAIN/ (joy_max[JOY_ROLL]-joy_min[JOY_ROLL]); sat.channels[RC_PITCH] = joystick_axes[JOY_PITCH]*J_GAIN/ (joy_max[JOY_PITCH]-joy_min[JOY_PITCH]); sat.channels[RC_YAW] = joystick_axes[JOY_YAW]*J_GAIN/ (joy_max[JOY_YAW]-joy_min[JOY_YAW]); sat.channels[RC_THROTTLE] = -joystick_axes[JOY_THROTTLE]*J_GAIN/ (joy_max[JOY_THROTTLE]-joy_min[JOY_THROTTLE]); sat.channels[RC_SAFETY] = joystick_axes[JOY_SAFETY] *J_GAIN/ (joy_max[JOY_SAFETY]-joy_min[JOY_SAFETY]); sat.channels[RC_ID_MODE] =joystick_axes[JOY_ID_MODE]*J_GAIN/ (joy_max[JOY_ID_MODE]-joy_min[JOY_ID_MODE]); last_update=time_keeper_get_millis(); } return sat.channels[index]; }
/*send a login request to server*/ void login(){ if(system("clear"));//unused warnning printf( "Hello, this is an IM program.\n" "You may pick a nickname and login.\n" "==========================================\n"); while(true){ while(true){ printf("Your name (no more than 15 characters):"); if(get_keyboard_input(username, 20) >= 3) break; else printf("It's too short.\n"); } /*construct a login request packet and send it*/ memset(sendbuf, 0, sizeof(sendbuf)); unsigned short data_size = 20; //request im packet data size construct_im_pkt_head((struct im_pkt_head *)sendbuf, TYPE_REQUEST, SERVICE_LOGIN, data_size); concat_im_pkt_data((struct im_pkt_head *)sendbuf, username); send(client_sock, sendbuf, IM_PKT_HEAD_SIZE + data_size, 0); /*get and parse the response packet*/ memset(recvbuf, 0 ,sizeof(recvbuf)); int n = readvrec(client_sock, recvbuf, BUF_SIZE); struct im_pkt_head *response_head = (struct im_pkt_head *)recvbuf; if( (n == 1) && (response_head -> service == SERVICE_LOGIN)){ //the response data size is 1 char *response_data = (char *)(response_head + 1);//right after the head bool login_success = (bool) response_data[0]; if(login_success){ query_online_all(); break; } else{ //login falied. name repeat printf( "\nSorry, that name seems to have been taken, pick another one!\n"); continue; } } printf("Sorry ,the server seems overloaded, wait a moment and try again.\n"); exit(-1); } }
void game_loop() { board_t a = {0}; board_t b = {0}; board_t *cur; board_t *next; int input; int AI_input; int score = 0; cur = &a; next = &b; board_rnd_gen_cell(*cur); while (1) { clear(); /* Draw the board */ board_dump(*cur, 4, 8); // stat_dump(); /* AI computation */ if (auto_play || suggestion) { AI_input = get_AI_input(*cur); const char *move_text[] = {"Left", "Right", "Up", "Down", "Game Over"}; mvprintw(1, 8, "Suggest: %s", move_text[AI_input]); } mvprintw(2, 8, "Score: %d", score); /* Update screen */ refresh(); /* Get input */ if (auto_play) { input = AI_input; } else { input = get_keyboard_input(); } int moved = 0; switch(input) { case UP: moved = move_up(*cur, *next); break; case LEFT: moved = move_left(*cur, *next); break; case DOWN: moved = move_down(*cur, *next); break; case RIGHT: moved = move_right(*cur, *next); break; case QUIT: return; default: continue; } if (!moved) continue; if (moved != 1) score += moved; /* Generate new cell */ board_rnd_gen_cell(*next); /* Switch cur and next */ board_t *temp = cur; cur = next; next = temp; board_clear(*next); } }
int main(void){ struct sockaddr_in server_addr;//server address /*client start preparations*/ if((client_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){ printf("error create socket\n"); exit(-1); } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERV_PORT);//the weather server port inet_pton(AF_INET, SERV_IP, &server_addr.sin_addr.s_addr);//weather server ip address if(connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1){ printf("error connect\n"); exit(-1); } online_friend_queue = init_user_queue(); //load login page login(); /*after login, the username and current online friends list will be settled down*/ /*create a thread to receive packets asynchronously */ pthread_t tid; int rc = pthread_create(&tid, NULL, recv_packet_thread, NULL); if(rc){ printf("ERROR creating thread, tid %d, return code %d\n", (int)tid, rc); exit(-1); } /*main thread loads the next page to get user's command*/ if(system("clear")) ; print_prompt_words(username); while(true){ printf("enter >> "); char command[20]; get_keyboard_input(command, 20); //printf("debug:%s\n", command); if(command[0] != '-'){ printf("input error, you can try again.\n"); continue; } if(strcmp(&command[1], "list") == 0) { assert(online_friend_queue -> front != NULL); print_online_friends(online_friend_queue); continue; } else if(strcmp(&command[1], "clear") == 0){ if(system("clear")) ; print_prompt_words(username); continue; } else if(strcmp(&command[1], "logout") == 0){ logout(); break;//this lead to the end of the program. } else if(strcmp(&command[1], "recent") == 0){ print_recent_messages(recent_messages, message_num); continue; }else if(strcmp(&command[1], "chat") == 0){ /*bug:if someone's name is "all", bug happens*/ char recipient[20]; printf("Who do you want to talk to?\n" "Type he/she's name or type \"all\":"); get_keyboard_input(recipient, 20); if(strcmp(recipient, username) == 0){ printf("it's funny to talk to yourself.\n"); } else if(strcmp(recipient, "all") == 0){ char text[100]; printf("Input your words to say to %s.\n" "Within 100 characters, end up with the Enter key):\n", recipient); get_keyboard_input(text, 100); /*build and send the packet*/ memset(sendbuf, 0, sizeof(sendbuf)); unsigned short data_size = 20 + 100; construct_im_pkt_head((struct im_pkt_head *)sendbuf, TYPE_REQUEST, SERVICE_MULTI_MESSAGE, data_size); strncpy(&sendbuf[IM_PKT_HEAD_SIZE], username, 20); strncpy(&sendbuf[IM_PKT_HEAD_SIZE + 20], text, 100); send(client_sock, sendbuf, IM_PKT_HEAD_SIZE + data_size, 0); } else if(find_user_by_name(online_friend_queue, recipient) != NULL){ //printf("debug:%s\n", recipient); char text[100]; printf("Input your words to say to %s.\n" "Within 100 characters, end up with the Enter key):\n", recipient); get_keyboard_input(text, 100); /*build and send the packet*/ memset(sendbuf, 0, sizeof(sendbuf)); unsigned short data_size = 20 + 20 + 100; construct_im_pkt_head((struct im_pkt_head *)sendbuf, TYPE_REQUEST, SERVICE_SINGLE_MESSAGE, data_size); strncpy(&sendbuf[IM_PKT_HEAD_SIZE], username, 20); strncpy(&sendbuf[IM_PKT_HEAD_SIZE + 20], recipient, 20); strncpy(&sendbuf[IM_PKT_HEAD_SIZE + 40], text, 100); send(client_sock, sendbuf, IM_PKT_HEAD_SIZE + data_size, 0); } else{ printf("sorry, %s seems not online now\n", recipient); print_online_friends(online_friend_queue); } continue; } else{ printf("input error, you can try again.\n"); continue; } } /*close socket and stop another thread before exit*/ pthread_cancel(tid); close(client_sock); return 0; }