4x5macro-numpad.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. 4x5 Macropad
  3. if you are using the Numpad arrangement, use the numpad firmware
  4. */
  5. #ifndef USER_USB_RAM
  6. #error "Require USB RAM. Go Tools > USB Setting and pick the 2nd option in the dropdown list"
  7. #endif
  8. #include "src/userUsbHidMediaKeyboard/USBHIDMediaKeyboard.h"
  9. ////////////// HARDWARE CONFIG //////////
  10. //LED OUTPUT
  11. #define LED_OUT 14
  12. //SCAN LINES
  13. //Input Pins. Pull High (When reading is low = pressed)
  14. #define COL_1 15
  15. #define COL_2 16
  16. #define COL_3 17
  17. #define COL_4 11
  18. //Output Pins. Set to low to scan that line
  19. #define ROW_1 32
  20. #define ROW_2 31
  21. #define ROW_3 30
  22. #define ROW_4 34
  23. #define ROW_5 33
  24. ///////////////// RUNTIME ///////////////
  25. const int readColSeq[4] = {COL_4, COL_3, COL_2, COL_1};
  26. const int scanRowSeq[5] = {ROW_5, ROW_4, ROW_3, ROW_2, ROW_1};
  27. bool funcMode = false; //If Fn key is hold (Default func key: [0,1]
  28. //The layout is identical to the keypad when view from the front side (i.e. not the MCU side)
  29. int keyPressState[5][4] = {
  30. {false,false,false,false},
  31. {false,false,false,false},
  32. {false,false,false,false},
  33. {false,false,false,false},
  34. {false,false,false,false},
  35. };
  36. ///////////////// FUNC PROTOTYPES ///////////////
  37. void HandleKeyPressEvents(int row, int col, bool previousState, bool currentState);
  38. void PressNumericAsKeypad(char in);
  39. void UpdateLEDState();
  40. void setup() {
  41. delay(300);
  42. USBInit();
  43. //LED
  44. pinMode(LED_OUT, OUTPUT);
  45. //Sensing lines
  46. pinMode(COL_1, INPUT);
  47. pinMode(COL_2, INPUT);
  48. pinMode(COL_3, INPUT);
  49. pinMode(COL_4, INPUT);
  50. //Select lines
  51. pinMode(ROW_1, OUTPUT);
  52. pinMode(ROW_2, OUTPUT);
  53. pinMode(ROW_3, OUTPUT);
  54. pinMode(ROW_4, OUTPUT);
  55. pinMode(ROW_5, OUTPUT);
  56. //Set all lines to high (active low)
  57. digitalWrite(ROW_1, HIGH);
  58. digitalWrite(ROW_2, HIGH);
  59. digitalWrite(ROW_3, HIGH);
  60. digitalWrite(ROW_4, HIGH);
  61. digitalWrite(ROW_5, HIGH);
  62. }
  63. //Activate the row to be scanned
  64. void setScanningRow(int rowNumber) {
  65. //Hardcoding the sequence save some RAM and faster :)
  66. (rowNumber==0)?digitalWrite(scanRowSeq[0], LOW):digitalWrite(scanRowSeq[0], HIGH);
  67. (rowNumber==1)?digitalWrite(scanRowSeq[1], LOW):digitalWrite(scanRowSeq[1], HIGH);
  68. (rowNumber==2)?digitalWrite(scanRowSeq[2], LOW):digitalWrite(scanRowSeq[2], HIGH);
  69. (rowNumber==3)?digitalWrite(scanRowSeq[3], LOW):digitalWrite(scanRowSeq[3], HIGH);
  70. (rowNumber==4)?digitalWrite(scanRowSeq[4], LOW):digitalWrite(scanRowSeq[4], HIGH);
  71. //Disable all and enable the target one not sure why it is not working
  72. //Activate the target row
  73. //int targetRowPin = scanRowSeq[rowNumber];
  74. //digitalWrite(targetRowPin, LOW);
  75. //delay(50); //naive debouncing
  76. }
  77. //Read and return the key press status of a given column
  78. //Notes: keys are low activated. When not press, the keys are pull HIGH
  79. bool readKeyPress(int colNumber){
  80. int readPin = readColSeq[colNumber];
  81. return (digitalRead(readPin) == 0);
  82. }
  83. //Read all the switches states and update to the global array
  84. void updateSwitchStatus() {
  85. for (int i = 0; i < 5; i++) {
  86. //For each of the row, read each of the col switch
  87. setScanningRow(i);
  88. for (int j = 0; j < 4; j++) {
  89. bool isKeyDown = readKeyPress(j);
  90. bool prevState = keyPressState[i][j];
  91. if (isKeyDown != prevState){
  92. //Trigger on change events
  93. HandleKeyPressEvents(i,j,prevState,isKeyDown);
  94. delay(10); //resp time
  95. }
  96. keyPressState[i][j] = isKeyDown;
  97. }
  98. }
  99. }
  100. //Update the LED status
  101. void UpdateLEDState(){
  102. if (funcMode) {
  103. digitalWrite(LED_OUT, HIGH);
  104. } else {
  105. digitalWrite(LED_OUT, LOW);
  106. }
  107. }
  108. void loop() {
  109. //Read the mode of the keyboard
  110. updateSwitchStatus();
  111. //Update the NumLock LED
  112. UpdateLEDState();
  113. }