template.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ////////////// HARDWARE CONFIG //////////
  9. //Mode LED
  10. #define LED_A 34
  11. #define LED_B 33
  12. //RGB LED
  13. #define LED_Red 32
  14. #define LED_Green 31
  15. #define LED_Blue 30
  16. //Mode switch
  17. #define SWITCH 14
  18. //Button (Mechnical, left to right)
  19. #define BTN_1 15
  20. #define BTN_2 16
  21. #define BTN_3 17
  22. #define BTN_4 11
  23. ///////////////// RUNTIME ///////////////
  24. //Current Mode
  25. bool modeA = true;
  26. //Previous button states
  27. bool bt1ActiveState = false;
  28. bool bt2ActiveState = false;
  29. bool bt3ActiveState = false;
  30. bool bt4ActiveState = false;
  31. //Current button states
  32. bool bt1Active = false;
  33. bool bt2Active = false;
  34. bool bt3Active = false;
  35. bool bt4Active = false;
  36. void setup() {
  37. USBInit();
  38. pinMode(SWITCH, INPUT);
  39. //Modes LED
  40. pinMode(LED_A, OUTPUT);
  41. pinMode(LED_B, OUTPUT);
  42. //Bottom RGB LEDs
  43. //by defaults it follows the LED A or B
  44. pinMode(LED_Red, OUTPUT);
  45. pinMode(LED_Green, OUTPUT);
  46. pinMode(LED_Blue, OUTPUT);
  47. if (!digitalRead(SWITCH)){
  48. digitalWrite(LED_A, HIGH);
  49. digitalWrite(LED_B, LOW);
  50. digitalWrite(LED_Blue, HIGH);
  51. digitalWrite(LED_Red, LOW);
  52. }else{
  53. digitalWrite(LED_A, LOW);
  54. digitalWrite(LED_B, HIGH);
  55. digitalWrite(LED_Blue, LOW);
  56. digitalWrite(LED_Red, HIGH);
  57. }
  58. //LED G reserve for media controller Button 2 long press
  59. digitalWrite(LED_Green, LOW);
  60. }
  61. void loop() {
  62. //Read the mode of the keyboard
  63. modeA = !digitalRead(SWITCH);
  64. //Read the button states, default PULL HIGH (aka LOW Activate)
  65. bt1Active = !digitalRead(BTN_1);
  66. bt2Active = !digitalRead(BTN_2);
  67. bt3Active = !digitalRead(BTN_3);
  68. bt4Active = !digitalRead(BTN_4);
  69. if (modeA == HIGH){
  70. //Mode A
  71. //Button 1
  72. if (bt1ActiveState != bt1Active){
  73. bt1ActiveState = bt1Active;
  74. if (bt1Active){
  75. {{codeA1}}
  76. }
  77. }
  78. //Button 2
  79. if (bt2ActiveState != bt2Active){
  80. bt2ActiveState = bt2Active;
  81. if (bt2Active){
  82. {{codeA2}}
  83. }
  84. }
  85. //Button 3
  86. if (bt3ActiveState != bt3Active){
  87. bt3ActiveState = bt3Active;
  88. if (bt3Active){
  89. {{codeA3}}
  90. }
  91. }
  92. //Button 4
  93. if (bt4ActiveState != bt4Active){
  94. bt4ActiveState = bt4Active;
  95. if (bt4Active){
  96. {{codeA4}}
  97. }
  98. }
  99. //Set the status LED
  100. digitalWrite(LED_A, HIGH);
  101. digitalWrite(LED_B, LOW);
  102. digitalWrite(LED_Blue, HIGH);
  103. digitalWrite(LED_Red, LOW);
  104. }else{
  105. //Mode B
  106. //Button 1
  107. if (bt1ActiveState != bt1Active){
  108. bt1ActiveState = bt1Active;
  109. if (bt1Active){
  110. {{codeB1}}
  111. }
  112. }
  113. //Button 2
  114. if (bt2ActiveState != bt2Active){
  115. bt2ActiveState = bt2Active;
  116. if (bt2Active){
  117. {{codeB2}}
  118. }
  119. }
  120. //Button 3
  121. if (bt3ActiveState != bt3Active){
  122. bt3ActiveState = bt3Active;
  123. if (bt3Active){
  124. {{codeB3}}
  125. }
  126. }
  127. //Button 4
  128. if (bt4ActiveState != bt4Active){
  129. bt4ActiveState = bt4Active;
  130. if (bt4Active){
  131. {{codeB4}}
  132. }
  133. }
  134. //Set the status LED
  135. digitalWrite(LED_A, LOW);
  136. digitalWrite(LED_B, HIGH);
  137. digitalWrite(LED_Blue, LOW);
  138. digitalWrite(LED_Red, HIGH);
  139. }
  140. delay(50); //naive debouncing
  141. }