Advertisement
Guest User

makeyMate.cpp

a guest
Feb 28th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. /*
  2. makeyMate.cpp
  3. by: Jim Lindblom
  4. SparkFun Electronics
  5. date: October 9, 2012
  6. license: This code is beerware: feel free to use it, with or without attribution,
  7. in your own projects. If you find it helpful, buy me a beer next time you see me
  8. at the local pub.
  9.  
  10. Definition for makeyMateClass class.
  11. */
  12. #include "Arduino.h" // Needed for delay
  13. #include "makeyMate.h"
  14. #include <SoftwareSerial.h>
  15.  
  16. SoftwareSerial bluetooth(14, 16);
  17.  
  18. makeyMateClass::makeyMateClass()
  19. {
  20. for (int i=0; i<6; i++)
  21. {
  22. keyCodes[i] = 0x00;
  23. }
  24. modifiers = 0;
  25. }
  26.  
  27. uint8_t makeyMateClass::begin(char * name)
  28. {
  29. bluetooth.begin(9600);
  30.  
  31. /* The next few lines of code try to create a fresh start. We need to get the RN-42
  32. into command mode, from any of these 3 states:
  33. 1) Connected - Sending 0 will disconnect the module, then we'll put it into command mode
  34. 2) Disconnected - In this case, we'll just get it into command mode
  35. 3) Disconnected, already in Command Mode - Sending 0 makes the module unresponsive
  36. until an undefined \r\n's are sent. I can't find any reason for this in
  37. the RN-42 User manual
  38. */
  39. bluetooth.write((uint8_t) 0); // Disconnects, if connected
  40. delay(1000);
  41. bluetooth.println("$$$"); // Command mode string
  42. while (!bluetooth.available())
  43. bluetooth.write('\r');
  44. delay(100);
  45. bluetooth.flush();
  46.  
  47. while (!enterCommandMode())
  48. delay(100);
  49. delay(100);
  50. bluetooth.println("SA,1"); // enable authentication
  51. delay(100);
  52. bluetooth.println("SW,80A0"); // Deep sleep mode, 100ms sleeps - saves about 15mA when idling
  53. //bluetooth.println("SW,0000"); // No sleep mode, higher current
  54. delay(100);
  55. bluetooth.println("SQ,16"); // optimize for short bursts of data
  56. delay(100);
  57. bluetooth.println("SM,4"); // auto-connect (DTR) mode
  58. delay(100);
  59. bluetooth.println("ST,255"); // no config timer
  60. delay(100);
  61. bluetooth.println("S~,6"); //setHIDMode();
  62. delay(100);
  63. setKeyboardMouseMode();
  64. delay(100);
  65. setName(name);
  66. delay(100);
  67. reboot();
  68. delay(2000);
  69. }
  70.  
  71. void makeyMateClass::moveMouse(uint8_t b, uint8_t x, uint8_t y)
  72. {
  73. bluetooth.write(0xFD);
  74. bluetooth.write(5); // length
  75. bluetooth.write(2); // mouse
  76. bluetooth.write((byte) b); // buttons
  77. bluetooth.write((byte) x); // no x
  78. bluetooth.write((byte) y); // no y
  79. bluetooth.write((byte) 0); // no wheel
  80. }
  81.  
  82. uint8_t makeyMateClass::keyPress(uint8_t k)
  83. {
  84. uint8_t i;
  85.  
  86. if (k >= 136) // Non printing key
  87. {
  88. k = k - 136;
  89. }
  90. else if (k >= 128) // !!! Modifiers, todo !!!
  91. {
  92. }
  93. else
  94. {
  95. k = asciiToScanCode[k];
  96. if (!k)
  97. {
  98. return 0;
  99. }
  100.  
  101. if (k & 0x80)
  102. {
  103. modifiers |= 0x02;
  104. k &= 0x7F;
  105. }
  106. }
  107.  
  108. if (keyCodes[0] != k && keyCodes[1] != k &&
  109. keyCodes[2] != k && keyCodes[3] != k &&
  110. keyCodes[4] != k && keyCodes[5] != k) {
  111.  
  112. for (i=0; i<6; i++) {
  113. if (keyCodes[i] == 0x00) {
  114. keyCodes[i] = k;
  115. break;
  116. }
  117. }
  118. if (i == 6) { // Too many characters to send
  119. /// !!! Return an error !!!
  120. return 0;
  121. }
  122. }
  123.  
  124. bluetooth.write(0xFE); // Keyboard Shorthand Mode
  125. bluetooth.write(0x07); // Length
  126. bluetooth.write(modifiers); // Modifiers
  127. for (int j=0; j<6; j++)
  128. {
  129. bluetooth.write(keyCodes[j]);
  130. }
  131.  
  132. return 1;
  133. }
  134.  
  135. uint8_t makeyMateClass::keyRelease(uint8_t k)
  136. {
  137. uint8_t i;
  138.  
  139. if (k >= 136) // Non printing key
  140. {
  141. k = k - 136;
  142. }
  143. else if (k >= 128) // !!! Modifiers, todo !!!
  144. {
  145. }
  146. else
  147. {
  148. k = asciiToScanCode[k];
  149. if (!k)
  150. {
  151. return 0;
  152. }
  153.  
  154. if (k & 0x80)
  155. {
  156. modifiers &= ~0x02; // Clear the modifier
  157. k &= 0x7F;
  158. }
  159. }
  160.  
  161. for (i=0; i<6; i++)
  162. {
  163. if ((0 != k) && (keyCodes[i] == k))
  164. {
  165. keyCodes[i] = 0x00;
  166. }
  167. }
  168.  
  169. bluetooth.write(0xFE); // Keyboard Shorthand Mode
  170. bluetooth.write(0x07); // Length
  171. bluetooth.write(modifiers); // Modifiers
  172. for (int j=0; j<6; j++)
  173. {
  174. bluetooth.write(keyCodes[j]);
  175. }
  176.  
  177. return 1;
  178. }
  179.  
  180. uint8_t makeyMateClass::setName(char * name)
  181. {
  182. if (bluetooth.available())
  183. bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
  184.  
  185. bluetooth.print("SN,");
  186. for (int i=0; i<15; i++)
  187. {
  188. if (name[i] != '\r')
  189. bluetooth.write(name[i]);
  190. else
  191. break;
  192. }
  193. bluetooth.write('\r');
  194.  
  195. bluetoothReceive(rxBuffer);
  196.  
  197. return bluetoothCheckReceive(rxBuffer, "AOK", 3);
  198.  
  199. }
  200.  
  201. uint8_t makeyMateClass::connect()
  202. {
  203. while (!enterCommandMode())
  204. delay(100);
  205. delay(100);
  206. bluetooth.println("C");
  207. }
  208.  
  209. uint8_t makeyMateClass::connect(char * address)
  210. {
  211. if (enterCommandMode())
  212. {
  213. bluetooth.print("C,");
  214. for (int i=0; i<8; i++)
  215. bluetooth.write(address[i]);
  216. bluetooth.write('\r');
  217.  
  218. return 1;
  219. }
  220. else
  221. return 0;
  222. }
  223.  
  224. uint8_t makeyMateClass::reboot(void)
  225. {
  226. if (bluetooth.available())
  227. bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
  228.  
  229. bluetooth.print("R,1"); // reboot command
  230. bluetooth.write('\r');
  231.  
  232. bluetoothReceive(rxBuffer);
  233.  
  234. return bluetoothCheckReceive(rxBuffer, "Reboot!", 7);
  235. }
  236.  
  237. uint8_t makeyMateClass::bluetoothReceive(char * dest)
  238. {
  239. int timeout = 1000;
  240. char c;
  241. int i = 0;
  242.  
  243. while ((--timeout > 0) && (c != 0x0A))
  244. {
  245. if (bluetooth.available())
  246. {
  247. c = bluetooth.read();
  248. if (c != 0x0D)
  249. dest[i++] = c;
  250. timeout = 1000; // reset timeout
  251. }
  252. }
  253.  
  254. return timeout;
  255. }
  256.  
  257. uint8_t makeyMateClass::bluetoothCheckReceive(char * src, char * expected, int bufferSize)
  258. {
  259. int i = 0;
  260. char c;
  261.  
  262. while ((src[i] != 0x0A) || (i < bufferSize))
  263. {
  264. if (src[i] != expected[i])
  265. {
  266. return 0;
  267. }
  268. i++;
  269. }
  270.  
  271. return 1;
  272. }
  273.  
  274. uint8_t makeyMateClass::setKeyboardMouseMode(void)
  275. {
  276. if (bluetooth.available())
  277. bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
  278.  
  279. bluetooth.print("sh,0030");
  280. bluetooth.write('\r');
  281.  
  282. bluetoothReceive(rxBuffer);
  283.  
  284. return bluetoothCheckReceive(rxBuffer, "AOK", 3);
  285. }
  286.  
  287. uint8_t makeyMateClass::setHIDMode(void)
  288. {
  289. if (bluetooth.available())
  290. bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
  291.  
  292. bluetooth.print("S~,6"); // Bluetooth HID Mode
  293. bluetooth.write('\r');
  294.  
  295. bluetoothReceive(rxBuffer);
  296.  
  297. return bluetoothCheckReceive(rxBuffer, "AOK", 3);
  298. }
  299.  
  300. uint8_t makeyMateClass::enterCommandMode(void)
  301. {
  302. bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
  303. bluetooth.println("$$$"); // Command mode string
  304. bluetoothReceive(rxBuffer);
  305.  
  306. if (rxBuffer[0] == '?')
  307. {
  308. return 1;
  309. }
  310. else
  311. {
  312. return bluetoothCheckReceive(rxBuffer, "CMD", 3);
  313. }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement