Our server pr0vider carried out maintenance on 15 January 2020 but it corrupted some files in the process. If you notice anything out of the ordinary (partial files, pages not loading, that kind of shit) then let us know at:

kremap.ino 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <HID-Settings.h>
  2. #include <HID-Project.h>
  3. #include <hidboot.h>
  4. #include <usbhub.h>
  5. // Satisfy the IDE, which needs to see the include statment in the ino too.
  6. #ifdef dobogusinclude
  7. #include <spi4teensy3.h>
  8. #endif
  9. #include <SPI.h>
  10. // Keymaps are hideous and now in a header to more easily ignore
  11. #include "kbd_maps.h"
  12. enum Layouts {
  13. QWERTY = 0,
  14. COLEMAK = 1,
  15. };
  16. int sensePin = 3;
  17. int ledState = 0;
  18. uint8_t LAYOUT = COLEMAK;
  19. //uint8_t MAGIC_TOGGLE = KEY_ESC;
  20. uint8_t MAGIC_TOGGLE = KEY_MENU2;
  21. uint8_t CONSEC_MAGIC = 0;
  22. uint8_t MAGIC_THRESH = 1;
  23. uint8_t INTERRUPT_COUNT = 0;
  24. void isrsignal()
  25. {
  26. static unsigned long last_interrupt = 0;
  27. unsigned long interrupt_time = millis();
  28. if (interrupt_time - last_interrupt > 100)
  29. {
  30. INTERRUPT_COUNT = INTERRUPT_COUNT + 1;
  31. }
  32. last_interrupt = interrupt_time;
  33. digitalWrite(LED_BUILTIN, ledState ? HIGH : LOW);
  34. ledState = 1 - ledState;
  35. }
  36. KeyboardKeycode remap(uint8_t key)
  37. {
  38. return KeyboardKeycode(key);
  39. }
  40. class KbdRptParser : public KeyboardReportParser
  41. {
  42. void PrintKey(uint8_t mod, uint8_t key);
  43. protected:
  44. void OnControlKeysChanged(uint8_t before, uint8_t after);
  45. void OnKeyDown (uint8_t mod, uint8_t key);
  46. void OnKeyUp (uint8_t mod, uint8_t key);
  47. void OnKeyPressed(uint8_t key);
  48. };
  49. void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
  50. {
  51. MODIFIERKEYS mod;
  52. *((uint8_t*)&mod) = m;
  53. Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
  54. Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
  55. Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
  56. Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
  57. Serial.print(" >");
  58. PrintHex<uint8_t>(key, 0x80);
  59. Serial.print("< ");
  60. Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
  61. Serial.print((mod.bmRightShift == 1) ? "S" : " ");
  62. Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
  63. Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
  64. };
  65. void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
  66. {
  67. Serial.print("DN ");
  68. PrintKey(mod, key);
  69. if (LAYOUT == COLEMAK)
  70. {
  71. Keyboard.press(KeyboardKeycode(colemak_map[key]));
  72. } else {
  73. Keyboard.press(KeyboardKeycode(qwerty_map[key]));
  74. }
  75. uint8_t c = OemToAscii(mod, key);
  76. //if (c)
  77. // OnKeyPressed(c);
  78. }
  79. void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
  80. MODIFIERKEYS beforeMod;
  81. *((uint8_t*)&beforeMod) = before;
  82. MODIFIERKEYS afterMod;
  83. *((uint8_t*)&afterMod) = after;
  84. if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
  85. Serial.println("LeftCtrl changed");
  86. if (afterMod.bmLeftCtrl) {
  87. Keyboard.press(KEY_LEFT_CTRL);
  88. } else {
  89. Keyboard.release(KEY_LEFT_CTRL);
  90. }
  91. }
  92. if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
  93. Serial.println("LeftShift changed");
  94. if (afterMod.bmLeftShift) {
  95. Keyboard.press(KEY_LEFT_SHIFT);
  96. } else {
  97. Keyboard.release(KEY_LEFT_SHIFT);
  98. }
  99. }
  100. if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
  101. Serial.println("LeftAlt changed");
  102. if (afterMod.bmLeftAlt) {
  103. Keyboard.press(KEY_LEFT_ALT);
  104. } else {
  105. Keyboard.release(KEY_LEFT_ALT);
  106. }
  107. }
  108. if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
  109. Serial.println("LeftGUI changed");
  110. if (afterMod.bmLeftGUI) {
  111. Keyboard.press(KEY_LEFT_GUI);
  112. } else {
  113. Keyboard.release(KEY_LEFT_GUI);
  114. }
  115. }
  116. if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
  117. Serial.println("RightCtrl changed");
  118. if (afterMod.bmRightCtrl) {
  119. Keyboard.press(KEY_RIGHT_CTRL);
  120. } else {
  121. Keyboard.release(KEY_RIGHT_CTRL);
  122. }
  123. }
  124. if (beforeMod.bmRightShift != afterMod.bmRightShift) {
  125. Serial.println("RightShift changed");
  126. if (afterMod.bmRightShift) {
  127. Keyboard.press(KEY_RIGHT_SHIFT);
  128. } else {
  129. Keyboard.release(KEY_RIGHT_SHIFT);
  130. }
  131. }
  132. if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
  133. Serial.println("RightAlt changed");
  134. if (afterMod.bmRightAlt) {
  135. Keyboard.press(KEY_RIGHT_ALT);
  136. } else {
  137. Keyboard.release(KEY_RIGHT_ALT);
  138. }
  139. }
  140. if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
  141. Serial.println("RightGUI changed");
  142. if (afterMod.bmRightGUI) {
  143. Keyboard.press(KEY_RIGHT_GUI);
  144. } else {
  145. Keyboard.release(KEY_RIGHT_GUI);
  146. }
  147. }
  148. }
  149. void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
  150. {
  151. //Serial.print("UP ");
  152. //PrintKey(mod, key);
  153. if (LAYOUT == COLEMAK)
  154. {
  155. Keyboard.release(KeyboardKeycode(colemak_map[key]));
  156. } else {
  157. Keyboard.release(KeyboardKeycode(qwerty_map[key]));
  158. }
  159. if (key == MAGIC_TOGGLE) {
  160. CONSEC_MAGIC = CONSEC_MAGIC + 1;
  161. } else {
  162. CONSEC_MAGIC = 0;
  163. }
  164. if (CONSEC_MAGIC == MAGIC_THRESH) {
  165. CONSEC_MAGIC = 0;
  166. LAYOUT = 1 - LAYOUT;
  167. }
  168. if (INTERRUPT_COUNT > 0)
  169. {
  170. char buffer[50];
  171. MAGIC_TOGGLE = key;
  172. MAGIC_THRESH = INTERRUPT_COUNT;
  173. INTERRUPT_COUNT = 0;
  174. CONSEC_MAGIC = 0;
  175. sprintf(buffer, "New settings %d presses of %02x", MAGIC_THRESH, MAGIC_TOGGLE);
  176. Serial.println(buffer);
  177. }
  178. }
  179. void KbdRptParser::OnKeyPressed(uint8_t key)
  180. {
  181. Serial.print("ASCII: ");
  182. Serial.println((char)key);
  183. };
  184. USB Usb;
  185. //USBHub Hub(&Usb);
  186. HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
  187. KbdRptParser Prs;
  188. void setup()
  189. {
  190. Serial.begin( 115200 );
  191. #if !defined(__MIPSEL__)
  192. while (!Serial && millis()<5000); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  193. #endif
  194. Serial.println("Started");
  195. Keyboard.begin();
  196. if (Usb.Init() == -1)
  197. Serial.println("OSC did not start.");
  198. delay( 200 );
  199. pinMode(sensePin, INPUT_PULLUP);
  200. pinMode(LED_BUILTIN, OUTPUT);
  201. INTERRUPT_COUNT = 0;
  202. attachInterrupt(digitalPinToInterrupt(sensePin), isrsignal, FALLING);
  203. HidKeyboard.SetReportParser(0, &Prs);
  204. }
  205. void loop()
  206. {
  207. Usb.Task();
  208. }