Posted By

hsdhunterd on 08/05/10


Tagged

display Sensor RobotC Superquest NXT Joystick


Versions (?)

GenDisplay v1


 / Published in: C
 

Created during August 2010 Superquest conference. Designed to show general I/O status on NXT screen.

  1. #if !defined(joystick)
  2. #include "JoystickDriver.c"
  3. #endif
  4.  
  5. #if defined(NXT) || defined(TETRIX)
  6. #pragma autoStartTasks // Automatically start this task when the main user program starts.
  7. #endif
  8. #pragma systemFile // this eliminates warning for "unreferenced" functions
  9.  
  10. //Global variables
  11. string sDataNXT[8]; //NXT display register for strings
  12.  
  13. void dispjoy(int val, int line, int space = 4)
  14. {
  15. //Check data length and add string filler to keep alignment
  16. string sval = val; //Convert integer to string
  17. int length = strlen(sval); //get length of string data
  18. string holder; //Internal string holder
  19. for(int i=0 ; i<(space-length);i++)
  20. {
  21. strcat(holder," ");//Add spaces
  22. }
  23. strcat(holder,sval); //Add value to holder
  24. if(line >= 0 && line <= 7)
  25. {
  26. strcat(sDataNXT[line],holder); //Move data to global register
  27. }
  28. }
  29.  
  30. task display()
  31. {
  32. /*
  33.   //Sample display
  34.  
  35.   string sample[] = {
  36.   "MA:XXXX",
  37.   "MB:XXXX",
  38.   "LT:X RT:X",
  39.   "Li:XXX Sn:XXX",
  40.   "JL x:XXXX y:XXXX",
  41.   "JR x:XXXX y:XXXX",
  42.   "B: 12345678",
  43.   "Dpad : XX",
  44.   };
  45.   */
  46.  
  47. int tibin1; //Integer registers
  48. string tsbin1; //String registers
  49. int trefresh = 100 ; //refresh rate
  50.  
  51. int bitvalue[] = {1,2,4,8,16,32,64,128,256,512}; //Bit values used for buttons
  52. int ibutton[10]; //Boolean values for buttons.
  53.  
  54.  
  55. while(true)
  56. {
  57. // getJoystickSettings(joystick); //Poll joystick for current settings.
  58.  
  59. //Line0
  60. sDataNXT[0] = "MA: ";
  61. tsbin1 = motor[motorA]; //Convert motor A value to string
  62. strcat(sDataNXT[0],tsbin1); //Add Data to screen
  63.  
  64. //Line1
  65. sDataNXT[1] = "MB: ";
  66. tsbin1 = motor[motorB]; //Convert motor A value to string
  67. strcat(sDataNXT[1],tsbin1); //Add Data to screen
  68.  
  69. //Line2 - Touch
  70. sDataNXT[2] = "LT: "; //Label
  71. tsbin1 = SensorValue(S2); //Convert sensor data to string
  72. strcat(sDataNXT[2], tsbin1); //Add sensor value to display array
  73. strcat(sDataNXT[2]," RT: "); //Add spacer
  74. tsbin1 = SensorValue(S3);
  75. strcat(sDataNXT[2],tsbin1);
  76.  
  77. //Line3 - Light/Sonar
  78. sDataNXT[3] = "Li: ";
  79. tsbin1 = SensorValue(S1);
  80. strcat(sDataNXT[3], tsbin1); //Add sensor value to display array
  81. strcat(sDataNXT[3]," Sn: "); //Add spacer
  82. tsbin1 = SensorValue(S4);
  83. strcat(sDataNXT[3],tsbin1);
  84.  
  85. //Line4 - Joystick Left stick
  86. sDataNXT[4] = "JL x:"; //Add label
  87. dispjoy(joystick.joy1_x1,4); // Add Right stick x axis
  88. strcat(sDataNXT[4]," y:"); //Add spacer
  89. dispjoy(joystick.joy1_y1,4); // Add Right stick y axis
  90.  
  91. //Line5 - Joystick Right stick
  92. sDataNXT[5] = "JR x:"; //Add label
  93. dispjoy(joystick.joy1_x2,5); // Add Right stick x axis
  94. strcat(sDataNXT[5]," y:"); //Add spacer
  95. dispjoy(joystick.joy1_y2,5); // Add Right stick y axis
  96.  
  97. //Line6 - Buttons
  98. sDataNXT[6] = "B: "; //Reset data field to get ready for next pass with a label.
  99. tibin1 = joystick.joy1_Buttons; //Get joystick data, buttons
  100.  
  101. for(int j=(sizeof(bitvalue)/2)-1;j>=0;j--)
  102. {
  103. //Process joystick button value and set value to true in bitvalue array
  104. if(tibin1 >= bitvalue[j])
  105. {
  106. ibutton[j] = 1;
  107. tibin1 = tibin1-bitvalue[j];
  108. }
  109. else
  110. {
  111. ibutton[j] = 0;
  112. }
  113. }
  114. for(int j=0;j<(sizeof(ibutton)/2);j++)
  115. {
  116. //Process bitvalue to build display strings.
  117. if(ibutton[j] == 1)
  118. {
  119. tsbin1 = j+1; //Change Index into button number.
  120. strcat(sDataNXT[6],tsbin1);
  121. }
  122. else
  123. {
  124. strcat(sDataNXT[6]," ");
  125. }
  126. }
  127.  
  128. //Line7 - Dpad
  129. tibin1 = joystick.joy1_TopHat; //Get joystick data
  130. sDataNXT[7] = "Dpad: "; //Reset data field to get ready for next pass with a label.
  131.  
  132. //North South Check
  133. if (tibin1 == 0 || tibin1 == 1 || tibin1 == 7)
  134. {
  135. //North direction pressed
  136. strcat(sDataNXT[7],"N");
  137. }
  138. else if (tibin1 == 3 || tibin1 == 4 || tibin1 == 5)
  139. {
  140. //South direction pressed
  141. strcat(sDataNXT[7],"S");
  142. }
  143. else
  144. {
  145. //Nothing pressed
  146. strcat(sDataNXT[7]," ");
  147. }
  148.  
  149. //East West check
  150. if (tibin1 >= 1 && tibin1 <= 3)
  151. {
  152. //East detected
  153. strcat(sDataNXT[7],"E");
  154. }
  155. else if (tibin1 >= 5 && tibin1 <= 7)
  156. {
  157. //West detected
  158. strcat(sDataNXT[7],"W");
  159. }
  160. else
  161. {
  162. //Nothing pressed
  163. strcat(sDataNXT[7]," ");
  164. }
  165.  
  166. // Display data on NXT screen.
  167. eraseDisplay();
  168. for(int d=0;d < (sizeof(sDataNXT)/20);d++)
  169. {
  170. nxtDisplayString(d,sDataNXT[d]); //Display data
  171. }
  172.  
  173. wait1Msec(trefresh);
  174. }
  175. }

Report this snippet  

You need to login to post a comment.