//Line 82 static void wii_event(const struct xwii_event *event, int sockfd) { int n, reply[1]; //array that stores the states of key and acceleration data int arg[8] = {0}; //in event struct, v is an union that stores data payload //key struct continas the enumerated key number and key state //store key number in code and state as a bool pressed (0 false, 1 true) unsigned int code = event->v.key.code; bool pressed = event->v.key.state; //Use switch case to cycle through wiimote events switch(event->type) { case XWII_EVENT_KEY: //if the respective key are pressed, the state is set to 1, otherwise 0 if(pressed) { if (code == XWII_KEY_LEFT) arg[2] = 1; else arg[2] = 0; if (code == XWII_KEY_RIGHT) arg[3] = 1; else arg[3] = 0; if (code == XWII_KEY_UP) arg[4] = 1; else arg[4] = 0; if (code == XWII_KEY_DOWN) arg[5] = 1; else arg[5] = 0; } else { for(int i = 2; i < 6; i++) arg[i] = 0; } //key A and B act as switches for performing actions, the sleep() was used to pause the program while action is performed if (code == XWII_KEY_A) if(pressed) { arg[6] = -arg[6]; sleep(1); } if (code == XWII_KEY_B) if(pressed) { arg[7] = -arg[7]; sleep(1); } break; case XWII_EVENT_ACCEL: //abs struct contains absolute motion event payload, x stores roll, while y stores pitch if(arg[7] == 1) { arg[0] = event->v.abs[0].x*val2deg; arg[1] = event->v.abs[0].y*val2deg; } else { arg[0] = 0; arg[1] = 0; } break; } //send motion data to server using socket n = write(sockfd, arg, sizeof(arg)); if (n < 0) error("ERROR writing to socket"); bzero(reply,sizeof(reply)); n = read(sockfd,reply,sizeof(reply)); if (n < 0) error("ERROR reading from socket"); }