macrokey_distribute.ino 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. Toby's Low Cost Macro Keyboard
  3. */
  4. #ifndef USER_USB_RAM
  5. #error "Require USB RAM. Go Tools > USB Setting and pick the 2nd option in the dropdown list"
  6. #endif
  7. #include "src/userUsbHidMediaKeyboard/USBHIDMediaKeyboard.h"
  8. #include "win-zh_util.h"
  9. ////////////// HARDWARE CONFIG //////////
  10. //Mode LED
  11. #define LED_A 34
  12. #define LED_B 33
  13. //RGB LED
  14. #define LED_Red 32
  15. #define LED_Green 31
  16. #define LED_Blue 30
  17. //Mode switch
  18. #define SWITCH 14
  19. //Button (Mechnical, left to right)
  20. #define BTN_1 15
  21. #define BTN_2 16
  22. #define BTN_3 17
  23. #define BTN_4 11
  24. ///////////////// RUNTIME ///////////////
  25. //Current Mode
  26. bool modeA = true;
  27. //Previous button states
  28. bool bt1ActiveState = false;
  29. bool bt2ActiveState = false;
  30. bool bt3ActiveState = false;
  31. bool bt4ActiveState = false;
  32. //Current button states
  33. bool bt1Active = false;
  34. bool bt2Active = false;
  35. bool bt3Active = false;
  36. bool bt4Active = false;
  37. ////////////// Special Hotkeys //////////////////
  38. //When Button 2 is hold in mode A, activate volume Mode to
  39. //allow button 3 and 4 to change volume instead of prev / next song
  40. bool volMode = false;
  41. void setup() {
  42. USBInit();
  43. pinMode(SWITCH, INPUT);
  44. //Modes LED
  45. pinMode(LED_A, OUTPUT);
  46. pinMode(LED_B, OUTPUT);
  47. //Bottom RGB LEDs
  48. //by defaults it follows the LED A or B
  49. pinMode(LED_Red, OUTPUT);
  50. pinMode(LED_Green, OUTPUT);
  51. pinMode(LED_Blue, OUTPUT);
  52. if (!digitalRead(SWITCH)){
  53. digitalWrite(LED_A, HIGH);
  54. digitalWrite(LED_B, LOW);
  55. digitalWrite(LED_Blue, HIGH);
  56. digitalWrite(LED_Red, LOW);
  57. }else{
  58. digitalWrite(LED_A, LOW);
  59. digitalWrite(LED_B, HIGH);
  60. digitalWrite(LED_Blue, LOW);
  61. digitalWrite(LED_Red, HIGH);
  62. }
  63. //LED G reserve for media controller Button 2 long press
  64. digitalWrite(LED_Green, LOW);
  65. }
  66. void loop() {
  67. //Read the mode of the keyboard
  68. modeA = !digitalRead(SWITCH);
  69. //Read the button states, default PULL HIGH (aka LOW Activate)
  70. bt1Active = !digitalRead(BTN_1);
  71. bt2Active = !digitalRead(BTN_2);
  72. bt3Active = !digitalRead(BTN_3);
  73. bt4Active = !digitalRead(BTN_4);
  74. if (modeA == HIGH){
  75. //Mode A
  76. //Media Controller
  77. //Button 1: Play / Pause
  78. if (bt1ActiveState != bt1Active){
  79. bt1ActiveState = bt1Active;
  80. if (bt1Active){
  81. Consumer_press(MEDIA_PLAY_PAUSE);
  82. }else{
  83. Consumer_release(MEDIA_PLAY_PAUSE);
  84. }
  85. }
  86. //Button 2: Toggle Playlist / Volumn Control Mode
  87. if (bt2ActiveState != bt2Active){
  88. bt2ActiveState = bt2Active;
  89. if (bt2Active){
  90. volMode = true;
  91. digitalWrite(LED_Green, HIGH);
  92. }else{
  93. volMode = false;
  94. digitalWrite(LED_Green, LOW);
  95. }
  96. }
  97. //Previous Song / Vol Down
  98. if (bt3ActiveState != bt3Active){
  99. bt3ActiveState = bt3Active;
  100. if (volMode){
  101. //Button 2 is held
  102. if (bt3Active){
  103. Consumer_press(MEDIA_VOLUME_DOWN);
  104. }else{
  105. Consumer_release(MEDIA_VOLUME_DOWN);
  106. }
  107. }else{
  108. //Default mode
  109. if (bt3Active){
  110. Consumer_press(MEDIA_PREV);
  111. }else{
  112. Consumer_release(MEDIA_PREV);
  113. }
  114. }
  115. }
  116. //Button 4: Next Song / Vol Up
  117. if (bt4ActiveState != bt4Active){
  118. bt4ActiveState = bt4Active;
  119. if (volMode){
  120. //Button 2 is held
  121. if (bt4Active){
  122. Consumer_press(MEDIA_VOLUME_UP);
  123. }else{
  124. Consumer_release(MEDIA_VOLUME_UP);
  125. }
  126. }else{
  127. //Default mode
  128. if (bt4Active){
  129. Consumer_press(MEDIA_NEXT);
  130. }else{
  131. Consumer_release(MEDIA_NEXT);
  132. }
  133. }
  134. }
  135. //Set the status LED
  136. digitalWrite(LED_A, HIGH);
  137. digitalWrite(LED_B, LOW);
  138. digitalWrite(LED_Blue, HIGH);
  139. digitalWrite(LED_Red, LOW);
  140. }else{
  141. //Mode B
  142. //For printing out information
  143. //Button 1
  144. if (bt1ActiveState != bt1Active){
  145. bt1ActiveState = bt1Active;
  146. if (bt1Active){
  147. //Open browser
  148. Consumer_press(CONSUMER_BROWSER_HOME);
  149. delay(100);
  150. Consumer_release(CONSUMER_BROWSER_HOME);
  151. delay(1500);
  152. //Type website
  153. Keyboard_write('h');
  154. delay(30);
  155. Keyboard_write('t');
  156. delay(30);
  157. Keyboard_write('t');
  158. delay(30);
  159. Keyboard_write('p');
  160. delay(30);
  161. Keyboard_write('s');
  162. delay(30);
  163. Keyboard_write(':');
  164. delay(30);
  165. Keyboard_write('/');
  166. delay(30);
  167. Keyboard_write('/');
  168. delay(30);
  169. Keyboard_write('i');
  170. delay(30);
  171. Keyboard_write('m');
  172. delay(30);
  173. Keyboard_write('u');
  174. delay(30);
  175. Keyboard_write('s');
  176. delay(30);
  177. Keyboard_write('l');
  178. delay(30);
  179. Keyboard_write('a');
  180. delay(30);
  181. Keyboard_write('b');
  182. delay(30);
  183. Keyboard_write('.');
  184. delay(30);
  185. Keyboard_write('c');
  186. delay(30);
  187. Keyboard_write('o');
  188. delay(30);
  189. Keyboard_write('m');
  190. delay(30);
  191. Keyboard_write(KEY_RETURN);
  192. delay(30);
  193. }
  194. }
  195. //Button 2
  196. if (bt2ActiveState != bt2Active){
  197. bt2ActiveState = bt2Active;
  198. if (bt2Active){
  199. typeChinese("45935"); //這
  200. typeChinese("44111"); //是
  201. Keyboard_write(' ');
  202. Keyboard_write('U');
  203. Keyboard_write('T');
  204. Keyboard_write('F');
  205. Keyboard_write('-');
  206. Keyboard_write('8');
  207. Keyboard_write(' ');
  208. typeChinese("43706"); //的
  209. typeChinese("42148"); //中
  210. typeChinese("42213"); //文
  211. typeChinese("49129"); //輸
  212. typeChinese("42328"); //出
  213. }
  214. }
  215. //Button 3
  216. if (bt3ActiveState != bt3Active){
  217. bt3ActiveState = bt3Active;
  218. if (bt3Active){
  219. typeChinese("36889"); //這
  220. typeChinese("26159"); //是
  221. Keyboard_write(' ');
  222. Keyboard_write('b');
  223. Keyboard_write('i');
  224. Keyboard_write('g');
  225. Keyboard_write('5');
  226. Keyboard_write(' ');
  227. typeChinese("30340"); //的
  228. typeChinese("20013"); //中
  229. typeChinese("25991"); //文
  230. typeChinese("36664"); //輸
  231. typeChinese("20986"); //出
  232. }
  233. }
  234. //Button 4
  235. if (bt4ActiveState != bt4Active){
  236. bt4ActiveState = bt4Active;
  237. if (bt4Active){
  238. Keyboard_write('S');
  239. Keyboard_write('e');
  240. Keyboard_write('e');
  241. Keyboard_write(' ');
  242. Keyboard_write('m');
  243. Keyboard_write('o');
  244. Keyboard_write('r');
  245. Keyboard_write('e');
  246. Keyboard_write(' ');
  247. Keyboard_write('o');
  248. Keyboard_write('n');
  249. Keyboard_write(' ');
  250. Keyboard_write('h');
  251. Keyboard_write('t');
  252. Keyboard_write('t');
  253. Keyboard_write('p');
  254. Keyboard_write('s');
  255. Keyboard_write(':');
  256. Keyboard_write('/');
  257. Keyboard_write('/');
  258. Keyboard_write('g');
  259. Keyboard_write('i');
  260. Keyboard_write('t');
  261. Keyboard_write('h');
  262. Keyboard_write('u');
  263. Keyboard_write('b');
  264. Keyboard_write('.');
  265. Keyboard_write('c');
  266. Keyboard_write('o');
  267. Keyboard_write('m');
  268. Keyboard_write('/');
  269. Keyboard_write('t');
  270. Keyboard_write('o');
  271. Keyboard_write('b');
  272. Keyboard_write('y');
  273. Keyboard_write('c');
  274. Keyboard_write('h');
  275. Keyboard_write('u');
  276. Keyboard_write('i');
  277. Keyboard_write('/');
  278. Keyboard_write('4');
  279. Keyboard_write('x');
  280. Keyboard_write('M');
  281. Keyboard_write('a');
  282. Keyboard_write('c');
  283. Keyboard_write('r');
  284. Keyboard_write('o');
  285. Keyboard_write('p');
  286. Keyboard_write('a');
  287. Keyboard_write('d');
  288. }
  289. }
  290. //Set the status LED
  291. digitalWrite(LED_A, LOW);
  292. digitalWrite(LED_B, HIGH);
  293. digitalWrite(LED_Blue, LOW);
  294. digitalWrite(LED_Red, HIGH);
  295. }
  296. delay(50); //naive debouncing
  297. }