/ Published in: C
Created during August 2010 Superquest conference. Designed to show general I/O status on NXT screen.
Expand |
Embed | Plain Text
#if !defined(joystick) #include "JoystickDriver.c" #endif #if defined(NXT) || defined(TETRIX) #pragma autoStartTasks // Automatically start this task when the main user program starts. #endif #pragma systemFile // this eliminates warning for "unreferenced" functions //Global variables string sDataNXT[8]; //NXT display register for strings void dispjoy(int val, int line, int space = 4) { //Check data length and add string filler to keep alignment string sval = val; //Convert integer to string int length = strlen(sval); //get length of string data string holder; //Internal string holder for(int i=0 ; i<(space-length);i++) { strcat(holder," ");//Add spaces } strcat(holder,sval); //Add value to holder if(line >= 0 && line <= 7) { strcat(sDataNXT[line],holder); //Move data to global register } } task display() { /* //Sample display string sample[] = { "MA:XXXX", "MB:XXXX", "LT:X RT:X", "Li:XXX Sn:XXX", "JL x:XXXX y:XXXX", "JR x:XXXX y:XXXX", "B: 12345678", "Dpad : XX", }; */ int tibin1; //Integer registers string tsbin1; //String registers int trefresh = 100 ; //refresh rate int bitvalue[] = {1,2,4,8,16,32,64,128,256,512}; //Bit values used for buttons int ibutton[10]; //Boolean values for buttons. while(true) { // getJoystickSettings(joystick); //Poll joystick for current settings. //Line0 sDataNXT[0] = "MA: "; tsbin1 = motor[motorA]; //Convert motor A value to string strcat(sDataNXT[0],tsbin1); //Add Data to screen //Line1 sDataNXT[1] = "MB: "; tsbin1 = motor[motorB]; //Convert motor A value to string strcat(sDataNXT[1],tsbin1); //Add Data to screen //Line2 - Touch sDataNXT[2] = "LT: "; //Label tsbin1 = SensorValue(S2); //Convert sensor data to string strcat(sDataNXT[2], tsbin1); //Add sensor value to display array strcat(sDataNXT[2]," RT: "); //Add spacer tsbin1 = SensorValue(S3); strcat(sDataNXT[2],tsbin1); //Line3 - Light/Sonar sDataNXT[3] = "Li: "; tsbin1 = SensorValue(S1); strcat(sDataNXT[3], tsbin1); //Add sensor value to display array strcat(sDataNXT[3]," Sn: "); //Add spacer tsbin1 = SensorValue(S4); strcat(sDataNXT[3],tsbin1); //Line4 - Joystick Left stick sDataNXT[4] = "JL x:"; //Add label dispjoy(joystick.joy1_x1,4); // Add Right stick x axis strcat(sDataNXT[4]," y:"); //Add spacer dispjoy(joystick.joy1_y1,4); // Add Right stick y axis //Line5 - Joystick Right stick sDataNXT[5] = "JR x:"; //Add label dispjoy(joystick.joy1_x2,5); // Add Right stick x axis strcat(sDataNXT[5]," y:"); //Add spacer dispjoy(joystick.joy1_y2,5); // Add Right stick y axis //Line6 - Buttons sDataNXT[6] = "B: "; //Reset data field to get ready for next pass with a label. tibin1 = joystick.joy1_Buttons; //Get joystick data, buttons for(int j=(sizeof(bitvalue)/2)-1;j>=0;j--) { //Process joystick button value and set value to true in bitvalue array if(tibin1 >= bitvalue[j]) { ibutton[j] = 1; tibin1 = tibin1-bitvalue[j]; } else { ibutton[j] = 0; } } for(int j=0;j<(sizeof(ibutton)/2);j++) { //Process bitvalue to build display strings. if(ibutton[j] == 1) { tsbin1 = j+1; //Change Index into button number. strcat(sDataNXT[6],tsbin1); } else { strcat(sDataNXT[6]," "); } } //Line7 - Dpad tibin1 = joystick.joy1_TopHat; //Get joystick data sDataNXT[7] = "Dpad: "; //Reset data field to get ready for next pass with a label. //North South Check if (tibin1 == 0 || tibin1 == 1 || tibin1 == 7) { //North direction pressed strcat(sDataNXT[7],"N"); } else if (tibin1 == 3 || tibin1 == 4 || tibin1 == 5) { //South direction pressed strcat(sDataNXT[7],"S"); } else { //Nothing pressed strcat(sDataNXT[7]," "); } //East West check if (tibin1 >= 1 && tibin1 <= 3) { //East detected strcat(sDataNXT[7],"E"); } else if (tibin1 >= 5 && tibin1 <= 7) { //West detected strcat(sDataNXT[7],"W"); } else { //Nothing pressed strcat(sDataNXT[7]," "); } // Display data on NXT screen. eraseDisplay(); for(int d=0;d < (sizeof(sDataNXT)/20);d++) { nxtDisplayString(d,sDataNXT[d]); //Display data } wait1Msec(trefresh); } }
You need to login to post a comment.
