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 9.0KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. keybrd Library Developer's Guide
  2. ================================
  3. This guide if for maintaining and writing new classes for the keybrd library and its extension libraries.
  4. The most common reason for adding new classes are:
  5. * I/O expander classes
  6. * custom layer schemes for multi-layer keyboards
  7. * experimental features
  8. Who this guide is for
  9. ---------------------
  10. This guide is for the maintainers and developers of the keybrd library and it's extensions.
  11. It is assumed the reader is familiar with the C++ language including pointers, objects, classes, static class variables, composition, aggregation, inheritance, polymorphism, and enum.
  12. Row, Scanner, and Debouncer classes use bit manipulation.
  13. Class inheritance diagrams
  14. --------------------------
  15. Keybrd library class inheritance diagram
  16. ```
  17. Row
  18. ___ ScannerInterface ___
  19. / | \
  20. Scanner_uC Scanner_IOE Scanner_ShiftRegsPISO
  21. PortIOE
  22. PortWriteInterface
  23. / \
  24. PortWrite_PCA9655E PortWrite_MCP23S17 (one PortWrite class for each IOE type)
  25. PortReadInterface
  26. / \
  27. PortRead_PCA9655E PortRead_MCP23S17 (one PortRead class for each IOE type)
  28. _ LED _
  29. / \
  30. LED_uC LED_PCA9655E
  31. DebouncerInterface
  32. |
  33. Debouncer_Samples
  34. ScanDelay
  35. LayerStateInterface
  36. |
  37. LayerState
  38. Key
  39. |____
  40. | \
  41. | Key_LayeredKeysBase
  42. | \____________________
  43. | / \
  44. | Key_LayeredKeys Key_LayeredKeys1
  45. |
  46. |___________________________
  47. | \ \
  48. | Key_LayeredScScBase Key_LayeredCodeScBase
  49. | | |
  50. | Key_LayeredScSc Key_LayeredCodeSc
  51. |
  52. Code
  53. |_____________________
  54. | \ \
  55. | Code_LayerLock Code_LayerHold
  56. |
  57. \________________________________________________________
  58. \ \ \ \ \
  59. Code_Sc Code_Shift Code_AutoShift Code_LEDLock Code_Null
  60. / \
  61. Code_ScS Code_ScNS
  62. ```
  63. Dependency diagrams
  64. -------------------
  65. Dependency diagram of example single-layer keyboard with LEDs
  66. ```
  67. ____ Row ______
  68. / | \
  69. Scanner_uC Debouncer Key ___
  70. | | \
  71. readPins Code Code_LEDLock
  72. |
  73. LED_PinNumber
  74. ```
  75. Dependency diagram of example multi-layer keyboard with layer LEDs
  76. ```
  77. LayerStates
  78. ___________ Row ________________/__ | \
  79. / / \ / \ | \
  80. Scanner_uC Debouncer Key_LayeredKeys / Code_Layer LED_PinNumber
  81. | \ /
  82. readPins Code
  83. ```
  84. Dependency diagram of example shift registers Row
  85. ```
  86. _______ Row _______
  87. / | \
  88. RowScanner_ShiftRegsPISO Debouncer Key
  89. |
  90. Code
  91. ```
  92. Dependency diagram of example I/O expander matrix with LEDs
  93. ```
  94. _________ Row ________
  95. / \ \
  96. __ Scanner_IOE __ Debouncer Key
  97. / | \ / \
  98. strobePin PortWrite PortRead Code Code_LEDLock
  99. | \ / \ |
  100. | PortIOE readPins LED
  101. \___________________________/ \
  102. pin
  103. ```
  104. Class naming conventions
  105. ------------------------
  106. Class names start with upper case letter.
  107. Most derived-class names start with the base class name followed by "_" and a name e.g.
  108. ```
  109. Code
  110. |
  111. Code_LayerLock
  112. ```
  113. This convention leads to class names that convey information about the classes inheritance.
  114. Underscore delineates base class name and sub-class name. Capital letters delineate words.
  115. Interface class names end with "Interface".
  116. Except for Key, because sketches look nicer with short names defining Key[] arrays.
  117. Layer-class naming conventions
  118. ------------------------------
  119. Layer classes are explained in [tutorial_3a_multi-layer_keyboard.md](../tutorials/tutorial_3a_multi-layer_keyboard.md).
  120. *Code_Layer* class names are concatenations of "Code_", "Layer" or layer name, and persistence.
  121. Example persistences are:
  122. * "Lock" - layer remains active after the layer key is released
  123. * "Hold" - layer is active for as long as layer key is held down
  124. Example Code_Layer class names:
  125. * Code_LayerHold
  126. * Code_LayerLock
  127. *LayerState* class names start with "LayerState" and end with a descriptive name.
  128. Example LayerState class names:
  129. * LayerState - basic LayerState class in keybrd library
  130. * LayerState_DH - main LayerState for the keybrd_DH library
  131. * LayerState_MF - LayerState for Mouse Function sub-layers in the keybrd_DH library
  132. *Key_Layered* class names start with "Key_Layered" and end with a descriptive name.
  133. Example Key_Layered class names:
  134. * Key_LayeredScSc
  135. * Key_LayeredKeys
  136. Style guide
  137. -----------
  138. Following the style guide makes it easier for the next programmer to understand your code.
  139. * For class names, see above section "Class naming conventions".
  140. * Member names use camelCase starting with lowercase letter.
  141. * Use constants rather than macros, except for header guards.
  142. * Global const names and static const names use ALL_CAPS_WITH_UNDERSCORE.
  143. * Macros use ALL_CAPS_WITH_UNDERSCORE and have _MACRO suffix e.g. SAMPLE_COUNT_MACRO
  144. * Header guards have _H suffix e.g. #ifndef FILE_NAME_H
  145. * Pointer names are prefixed with "ptr" e.g. ptrRow = &row;
  146. * Arrays names use the plural of the element name e.g. Row* const = ptrsRows { &row0, &row1 };
  147. * Pass arrays using array notation rather than pointer notation:
  148. ```
  149. void printArray(char[] array);
  150. not
  151. void printArray( char* array);
  152. ```
  153. * In constructor's initialization list, use same names for fields and constructor parameters.
  154. * Do not use new or malloc (make memory leaks impossible).
  155. * Document class interface in .h file, above the class declaration.
  156. * Code should be self-documenting. A simple function with a good name needs no comment.
  157. * Code is automatically formatted before being pushed to the keybrd repository.
  158. The [astyle_cpp](astyle_cpp) file specifies the format:
  159. * Allman style indentation
  160. * indent 4 spaces
  161. * replace tabs with spaces
  162. * maximum code width of 100 columns
  163. <!-- http://stackoverflow.com/questions/2198241/best-practice-for-c-function-commenting -->
  164. Trace of keybrd scan
  165. --------------------
  166. Arduino does not have a debugger.
  167. So here is a list of functions in the order that they are called.
  168. The trace is of a one-row single-layer keybrd scan.
  169. ```
  170. loop() for each row
  171. Row::process()
  172. Scanner_uC::scan() strobe row on
  173. for each readPin
  174. set readState bit
  175. strobe row off
  176. Debouncer_Samples::debounce() debounce
  177. Row::send() for each key in row
  178. if falling edge
  179. Key_*::release() scanCode->release()
  180. Code_*::release() Keyboard.release(scancode)
  181. if rising edge
  182. Key_*::press() scanCode->press()
  183. Code_*::press() Keyboard.press(scancode)
  184. scanDelay.delay();
  185. ```
  186. The Arduino libraries
  187. ---------------------
  188. The keybrd libraries compile on the Arduino IDE and make extensive use of the following [Arduino libraries](https://www.arduino.cc/en/Reference/Libraries):
  189. #include <Arduino.h>
  190. #include <Wire.h>
  191. #include <Keyboard.h>
  192. #include <Mouse.h>
  193. <a rel="license" href="https://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://licensebuttons.net/l/by/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">keybrd guide</span> by <a xmlns:cc="https://creativecommons.org/ns" href="https://github.com/wolfv6/keybrd" property="cc:attributionName" rel="cc:attributionURL">Wolfram Volpi</a> is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.<br />Permissions beyond the scope of this license may be available at <a xmlns:cc="https://creativecommons.org/ns" href="https://github.com/wolfv6/keybrd/issues/new" rel="cc:morePermissions">https://github.com/wolfv6/keybrd/issues/new</a>.