Advertisement
Guest User

Handheld Wireless Code

a guest
Jan 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. //CONTROLLERS: (Uno)
  2.  
  3. //OLED libraries
  4. #include <Wire.h>
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_SSD1306.h>
  7.  
  8. #include <SparkFun_APDS9960.h>
  9.  
  10. //Wireless libraries
  11. #include <SPI.h>
  12. #include "nRF24L01.h"
  13. #include "RF24.h"
  14.  
  15. //Controller inputs
  16. #define button 4
  17. #define pressureButton A0
  18. #define touch 3
  19. #define APDS9960_INT 2
  20.  
  21. //The OLED display
  22. Adafruit_SSD1306 display(-1);
  23. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  24.  
  25. //The wireless signal radio
  26. RF24 radio(7, 8); // CE, CSN
  27. const byte pipe[6] = "00001";
  28. uint8_t buttonState, lastButton, pressure, lastPressure, lastGesture, isTouch = 0;
  29.  
  30. //The controller data that will be sent to the screen
  31. uint8_t controllerData[6] = {0, 0, 0, 0, 0, 0}; //PING -> Button -> Pressure -> PLAYERNUM -> Touch -> Gesture
  32.  
  33. const uint8_t DATA_PLAYER = 0, DATA_PING = 1, DATA_BUTTON = 2, DATA_PRESSURE = 3, DATA_TOUCH = 4, DATA_GESTURE = 5;
  34.  
  35. //Change this to 1 or 2 for the dedicated controllers
  36. const uint8_t CONTROLLER_P = 2;
  37.  
  38. //The APDS9960 Gesture sensor
  39. const uint8_t GESTURE_UP = 0, GESTURE_DOWN = 1, GESTURE_RIGHT = 3, GESTURE_LEFT = 2, GESTURE_NEAR = 4, GESTURE_FAR = 5;
  40.  
  41. //Used to track the connection
  42. uint8_t prevPing = 0, ping = 0;
  43.  
  44. //What does the OLED screen show? this int keeps track of the sequence of what should be displayed. You can tinker around with various things, while this number will say what will be shown.
  45. uint8_t screen_state = 0;
  46.  
  47. void setup(void)
  48. {
  49. Serial.begin(9600);
  50.  
  51. //The controller inputs
  52. pinMode(button, INPUT);
  53. pinMode(pressureButton, INPUT);
  54. pinMode(touch, INPUT);
  55.  
  56. //The OLED screen initialisation
  57. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  58. display.clearDisplay();
  59.  
  60. //Wireless radio initialisation
  61. radio.begin();
  62. radio.openWritingPipe(pipe);
  63.  
  64. //Assign player number
  65. controllerData[DATA_PLAYER] = CONTROLLER_P;
  66. }
  67.  
  68. unsigned long old_sec = 0;
  69. uint8_t gestureDel = 0;
  70.  
  71. void loop(void)
  72. {
  73. unsigned long current_time = millis();
  74.  
  75. //Update per second
  76. if (current_time - old_sec >= 1000)
  77. {
  78. old_sec = current_time;
  79. if (gestureDel > 0) gestureDel++;
  80. }
  81.  
  82. //Pressure pad
  83. pressure = analogRead(pressureButton);
  84. int force = 0;
  85. if (analogRead(pressureButton) > 290)
  86. {
  87. int calc = map(pressure, 85, 140, 0, 255);
  88. if (calc >= 0) force = calc;
  89. if (lastPressure != force)
  90. {
  91. lastPressure = force;
  92. controllerData[DATA_PRESSURE] = force;
  93. }
  94. }
  95.  
  96. //Button
  97. lastButton = buttonState;
  98. buttonState = digitalRead(button);
  99. controllerData[DATA_BUTTON] = buttonState;
  100.  
  101. //PING
  102. controllerData[DATA_PING] = random();
  103.  
  104. //Touch
  105. isTouch = clamp(digitalRead(touch), 0, 1);
  106. controllerData[DATA_TOUCH] = isTouch;
  107.  
  108. //Gestures
  109. handleGesture();
  110.  
  111. //Draw the input on the OLED screen of the controller
  112. if (screen_state > 0) drawControls();
  113.  
  114. //Write
  115. radio.write(&controllerData, sizeof(controllerData));
  116. }
  117.  
  118. void drawControls()
  119. {
  120. drawButton(20, 20);
  121. drawPressure(20, 10);
  122. drawTouch(80, 11);
  123. drawGesture(105, 20);
  124. display.display();
  125. }
  126.  
  127. void drawGesture(int x, int y)
  128. {
  129. int iconSize = 2;
  130. display.setFont(&Org_01);
  131. display.fillRect(x - 9, y - 9, 24, 14, BLACK);
  132. display.stopscroll();
  133. if (gestureDel > 0)
  134. {
  135. switch (lastGesture)
  136. {
  137. case DIR_UP:
  138. controllerData[DATA_GESTURE] = GESTURE_UP;
  139. writeArrow("U", x + 1, y - 1);
  140. break;
  141. case DIR_DOWN:
  142. controllerData[DATA_GESTURE] = GESTURE_DOWN;
  143. writeArrow("D", x + 1, y - 3);
  144. break;
  145. case DIR_LEFT:
  146. controllerData[DATA_GESTURE] = GESTURE_LEFT;
  147. writeArrow("L", x - 1, y);
  148. break;
  149. case DIR_RIGHT:
  150. controllerData[DATA_GESTURE] = GESTURE_RIGHT;
  151. writeArrow("R", x - 3, y);
  152. break;
  153. case DIR_NEAR:
  154. controllerData[DATA_GESTURE] = GESTURE_NEAR;
  155. writeScreen("O", x, y, iconSize, 0);
  156. break;
  157. case DIR_FAR:
  158. controllerData[DATA_GESTURE] = GESTURE_FAR;
  159. writeScreen("o", x + 2, y - 2, iconSize, 0);
  160. break;
  161. default:
  162. controllerData[DATA_GESTURE] = -1;
  163. writeScreen("*", x + 2, y - 1, iconSize, 0);
  164. }
  165. }
  166.  
  167. if (gestureDel >= 3)
  168. {
  169. gestureDel = 0;
  170. display.fillRect(x - 9, y - 9, 24, 14, BLACK);
  171. }
  172. }
  173.  
  174. void drawTouch(int x, int y)
  175. {
  176. int w = 12, h = 14;
  177. display.fillRect(x, y, w, h, BLACK);
  178. display.drawRect(x, y, w, h, WHITE);
  179. display.fillRect(x, y + (isTouch * (h / 2)), w, h / 2, WHITE);
  180. }
  181.  
  182. void drawButton(int x, int y)
  183. {
  184. display.fillRect(x - 2, y - 1, 10, 6, BLACK);
  185. if (buttonState > 0) display.fillRect(x - 2, y - 1, 10, 6, WHITE);
  186. else display.drawRect(x, y, 7, 4, WHITE);
  187. }
  188.  
  189. void drawPressure(int x, int y)
  190. {
  191. display.drawRect(x, y, 52, 8, WHITE);
  192. display.fillRect(x + 2, y + 2, 48, 4, BLACK);
  193. int fill = controllerData[DATA_PRESSURE] / 4;
  194. display.fillRect(x + 2, y + 2, clamp(fill, 0, 48), 4, WHITE);
  195. }
  196.  
  197. int clamp(int val, int mini, int maxi)
  198. {
  199. if (val < mini) return mini;
  200. else if (val > maxi) return maxi;
  201. return val;
  202. }
  203.  
  204. void writeScreen(String text, int x, int y, int textSize, int scrollDel)
  205. {
  206. display.setTextSize(textSize);
  207. display.setTextColor(WHITE);
  208. display.setCursor(x, y);
  209. display.print(text);
  210. display.display();
  211. delay(scrollDel);
  212. if (scrollDel > 0) display.startscrollleft(0x00, 0x0F);
  213. }
  214.  
  215. void writeArrow(String text, int x, int y)
  216. {
  217. display.setTextColor(WHITE);
  218. if (text == "U")
  219. {
  220. display.fillRect(x + 2, y - 7, 3, 3, WHITE);
  221. display.fillRect(x, y - 6, 7, 2, WHITE);
  222. display.fillRect(x - 2 - 1, y - 6 + 1, 3, 2, WHITE);
  223. display.fillRect(x + 5 + 2, y - 6 + 1, 3, 2, WHITE);
  224. display.fillRect(x + 2, y - 5, 3, 7, WHITE);
  225. }
  226. else if (text == "D")
  227. {
  228. display.fillRect(x + 2, y, 3, 3, WHITE);
  229. display.fillRect(x, y, 7, 2, WHITE);
  230. display.fillRect(x - 2 - 1, y - 1, 3, 2, WHITE);
  231. display.fillRect(x + 5 + 2, y - 1, 3, 2, WHITE);
  232. display.fillRect(x + 2, y - 5, 3, 7, WHITE);
  233. }
  234. else if (text == "R")
  235. {
  236. display.fillRect(x + 13, y - 5, 5, 2, WHITE);
  237. display.fillRect(x + 12, y - 7, 4, 6, WHITE);
  238. display.fillRect(x + 11, y - 8, 3, 2, WHITE);
  239. display.fillRect(x + 11, y - 2, 3, 2, WHITE);
  240. display.fillRect(x + 1, y - 5, 14, 2, WHITE);
  241. }
  242. else if (text == "L")
  243. {
  244. display.fillRect(x - 2, y - 5, 5, 2, WHITE);
  245. display.fillRect(x, y - 7, 4, 6, WHITE);
  246. display.fillRect(x + 2, y - 8, 3, 2, WHITE);
  247. display.fillRect(x + 2, y - 2, 3, 2, WHITE);
  248. display.fillRect(x + 1, y - 5, 14, 2, WHITE);
  249. }
  250. display.display();
  251. }
  252.  
  253. void handleGesture()
  254. {
  255. if (apds.isGestureAvailable()) lastGesture = apds.readGesture();
  256. gestureDel = 1;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement