keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keybrd_library_developer_guide.md 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. keybrd Library Developer's Guide
  2. ================================
  3. This guide contains diagrams, naming conventions, and a style guide.
  4. This guide will help you design custom classes to interface with the keybrd library.
  5. The most common need for custom classes are:
  6. * Port classes for micro controller or I/O expanders
  7. * custom layer schemes for multi-layer keyboards
  8. * experimental features
  9. ## Who this guide is for
  10. This guide is for the maintainers, developers, and anyone that wants to extend the keybrd library.
  11. It is assumed the reader is familiar with C++ language including pointers, objects, classes, static class variables, composition, inheritance, polymorphism, and enum.
  12. Some classes use bit manipulation.
  13. ## Custom keybrd classes
  14. Please refer to tutorial_7a_using_someone_else's_keybrd_extension_library.md
  15. ## Class diagrams
  16. Keybrd library class inheritance diagram
  17. ```
  18. Matrix
  19. Row
  20. IOExpanderPort
  21. _______ RowPort _______
  22. / | \
  23. RowPort_AVR RowPort_MCP23018 RowPort_PCA9655E (one RowPort class for each type of IC)
  24. _______ ColPort _______
  25. / | \
  26. ColPort_AVR ColPort_MCP23018 ColPort_PCA9655E (one ColPort class for each type of IC)
  27. _____ LED ______
  28. / | \
  29. LED_AVR LED_MCP23018 LED_PCA9655E (one LED class for each type of IC)
  30. StateLayersInterface
  31. |
  32. StateLayers
  33. Key __
  34. | \
  35. | Key_LayeredKeysArray
  36. |
  37. Code
  38. |_____________________
  39. | \ \
  40. | Code_LayerLock Code_LayerHold
  41. |
  42. |___________________________
  43. | \ \
  44. | Code_LayeredScScBase Code_LayeredCodeScBase
  45. | | |
  46. | Code_LayeredScSc Code_LayeredCodeSc
  47. |
  48. |__________________________________________
  49. \ \ \ \
  50. Code_Sc Code_Shift Code_AutoShift Code_LockLED
  51. / | \
  52. Code_ScS Code_ScNS Code_ScNS_00
  53. ```
  54. ## Association diagrams
  55. single-layer Keybrd association diagram with LEDs
  56. ```
  57. keybrd[1]
  58. |
  59. matrix[1..*]
  60. |
  61. row[1..*]_____________________________
  62. | \ \ \
  63. rowPort[1] rowPin[1] colPort[1] keys[1]
  64. | |
  65. colPins[1..*] code[1..*]
  66. |
  67. LED[1]
  68. ```
  69. multi-layer Keybrd association diagram with LEDs and I/O Expander
  70. ```
  71. keybrd[1]
  72. |
  73. matrix[1..*]
  74. | stateLayers[1..*]
  75. row[1..*]_________________________________________/__ | \
  76. | \ \ \ / \ | \
  77. rowPort[1] rowPin[1] colPort[1] keys[1] / code_layer[1..*] LED[0..*]
  78. \ / \ | / /
  79. \ / colPins[1..*] key[1..*] /
  80. \ / | /
  81. \ / code[1..*] /
  82. \ / ______________________________________/
  83. IOExpanderPort[0..*]
  84. ```
  85. ## Class naming conventions
  86. Class names start with upper case letter.
  87. Most derived-class names start with the base class name followed by "_" and a name e.g.
  88. ```
  89. Code
  90. |
  91. Code_Sc
  92. ```
  93. This convention leads to class names that convey information about the classes inheritance.
  94. Underscore delineates base class name and sub name. Capital letters delineate words.
  95. ## Style guide
  96. Following the style guide makes it easier for the next programmer to understand your code.
  97. * For class names, see above section "Class Naming Conventions"
  98. * For member names, use camelCase starting with lowercase letter.
  99. * Use constants rather than macros, except for header guards.
  100. * For constant names that could be macros, use ALL_CAPS_AND_UNDERSCORE.
  101. * **ITEM_COUNT** is a constant number of items.
  102. * **itemCount** is a variable number of items.
  103. * Use header guards CLASS_NAME_H.
  104. * Prefix pointer name with "ptr" e.g. ptrRow = &row
  105. * Name arrays using the plural of elements e.g. Row* const = ptrsRows { &row0, &row1 };
  106. * Pass arrays using array notation rather than pointer notation. Use
  107. ```
  108. void printArray(char[] array);
  109. not
  110. void printArray( char* array);
  111. ```
  112. * In constructor's initialization list, use same names for fields and constructor parameters
  113. * Do not use new or malloc (to make memory leaks impossible).
  114. * If class has any non-[POD](http://en.wikipedia.org/wiki/Plain_old_data_structure) data members, [do not inline constructors and destructors](http://www.chromium.org/developers/coding-style/cpp-dos-and-donts).
  115. * Document class interface in .h file, above the class declaration.
  116. * Code should be self-documenting.
  117. The only comments should be things that may need clarification.
  118. A simple function with a good name needs no comment.
  119. http://stackoverflow.com/questions/2198241/best-practice-for-c-function-commenting
  120. * Code is automatically formated before being pushed to the keybrd repository
  121. The options file doc/astyle_cpp specifies the format:
  122. * Allman style indentation
  123. * indent 4 spaces
  124. * replace tabs with spaces
  125. * maximum code width of 100 columns
  126. ## keybrd sketches
  127. keybrd sketch naming convention is described in the keybrd_library_user_guide "Sample keybrd sketches".
  128. The head of sketch should contain latest version of keybrd lib that sketch was test on:
  129. tested on keybrd v1.1 by wolfv6
  130. ## trace of keybrd scan
  131. Arduino does not have a debugger; so here is a list of functions in the order that they are called.
  132. Refer to it like a table of contents while reading the keybrd library.
  133. ```
  134. Keybrd::scan() for each matrix
  135. Matrix::scan() for each row
  136. Row::process()
  137. Row::scan()
  138. RowPort_*::setActivePin*() strobe row on
  139. for each col port
  140. ColPort_*::read() read col port
  141. RowPort_*::setActivePin*() strobe row off
  142. Row::getRowState() for each col port
  143. for each connected col pin
  144. if key is pressed
  145. set rowState bit
  146. Row::debounce() debounce
  147. Row::detectEdge() detect edge
  148. Row::pressRelease() for each key in row
  149. if rising edge
  150. Key_*::press() scanCode->press()
  151. Code_*::press() Keyboard.press(scancode)
  152. ```
  153. ## The Arduino libraries
  154. The keybrd libraries compile on the Arduino IDE and make extensive use of the following [Arduino libraries](https://www.arduino.cc/en/Reference/Libraries):
  155. #include <Arduino.h>
  156. #include <Wire.h>
  157. #include <Keyboard.h>
  158. #include <Mouse.h>