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

8 years ago
8 years ago
7 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
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_ShiftRegsRead
  21. PortWriteInterface
  22. / \
  23. PortInterface Port_ShiftRegs (Port class for MOSI shift registers)
  24. / \
  25. Port_PCA9655E Port_MCP23S17 (one Port class for each IOE type)
  26. LEDInterface
  27. / \
  28. LED_uC LED_Port
  29. DebouncerInterface
  30. |
  31. Debouncer_Samples
  32. ScanDelay
  33. LayerStateInterface
  34. |
  35. LayerState
  36. Key
  37. |____
  38. | \
  39. | Key_LayeredKeysBase
  40. | \____________________
  41. | / \
  42. | Key_LayeredKeys Key_LayeredKeys1
  43. |
  44. |___________________________
  45. | \ \
  46. | Key_LayeredScScBase Key_LayeredCodeScBase
  47. | | |
  48. | Key_LayeredScSc Key_LayeredCodeSc
  49. |
  50. Code
  51. |_____________________
  52. | \ \
  53. | Code_LayerLock Code_LayerHold
  54. |
  55. \________________________________________________________
  56. \ \ \ \ \
  57. Code_Sc Code_Shift Code_AutoShift Code_LEDLock Code_Null
  58. / \
  59. Code_ScS Code_ScNS
  60. ```
  61. Dependency diagrams
  62. -------------------
  63. Dependency diagram of example single-layer keyboard with LEDs
  64. ```
  65. ____ Row ______
  66. / | \
  67. Scanner_uC Debouncer Key ___
  68. | | \
  69. readPins Code Code_LEDLock
  70. |
  71. LED_PinNumber
  72. ```
  73. Dependency diagram of example multi-layer keyboard with layer LEDs
  74. ```
  75. LayerStates
  76. ___________ Row ________________/__ | \
  77. / / \ / \ | \
  78. Scanner_uC Debouncer Key_LayeredKeys / Code_Layer LED_PinNumber
  79. | \ /
  80. readPins Code
  81. ```
  82. Dependency diagram of example shift registers Row
  83. ```
  84. _______ Row _______
  85. / | \
  86. RowScanner_ShiftRegsPISO Debouncer Key
  87. |
  88. Code
  89. ```
  90. Dependency diagram of example I/O expander matrix with LEDs
  91. ```
  92. _________ Row ________
  93. / \ \
  94. __ Scanner_IOE __ Debouncer Key
  95. / | \ / \
  96. strobePin PortWrite PortRead Code Code_LEDLock
  97. | \ / \ |
  98. | PortIOE readPins LED_Port
  99. \___________________________/ \
  100. pin
  101. ```
  102. Class naming conventions
  103. ------------------------
  104. Class names start with upper case letter.
  105. Most derived-class names start with the base class name followed by "_" and a name e.g.
  106. ```
  107. Code
  108. |
  109. Code_LayerLock
  110. ```
  111. This convention leads to class names that convey information about the classes inheritance.
  112. Underscore delineates base class name and sub-class name. Capital letters delineate words.
  113. Interface class names end with "Interface".
  114. Except for Key, to reduce clutter because sketches define so many Key[] arrays.
  115. Layer-class naming conventions
  116. ------------------------------
  117. Layer classes are explained in [tutorial_3a_multi-layer_keyboard.md](../tutorials/tutorial_3a_multi-layer_keyboard.md).
  118. *Code_Layer* class names are concatenations of "Code_", "Layer" or layer name, and persistence.
  119. Example persistences are:
  120. * "Lock" - layer remains active after the layer key is released
  121. * "Hold" - layer is active for as long as layer key is held down
  122. Example Code_Layer class names:
  123. * Code_LayerHold
  124. * Code_LayerLock
  125. *LayerState* class names start with "LayerState" and end with a descriptive name.
  126. Example LayerState class names:
  127. * LayerState - basic LayerState class in keybrd library
  128. * LayerState_DH - main LayerState for the keybrd_DH library
  129. * LayerState_MF - LayerState for Mouse Function sub-layers in the keybrd_DH library
  130. *Key_Layered* class names start with "Key_Layered" and end with a descriptive name.
  131. Example Key_Layered class names:
  132. * Key_LayeredScSc
  133. * Key_LayeredKeys
  134. Style guide
  135. -----------
  136. Following the style guide makes it easier for the next programmer to understand your code.
  137. * For class names, see above section "Class naming conventions".
  138. * Member names use camelCase starting with lowercase letter.
  139. * Use constants rather than macros, except for header guards.
  140. * Global const names and static const names use ALL_CAPS_WITH_UNDERSCORE.
  141. * Header guards have _H suffix e.g. #ifndef FILE_NAME_H
  142. * Pointer names are prefixed with "ptr" e.g. ptrRow = &row;
  143. * Arrays names use the plural of the element name e.g. Row* const = ptrsRows { &row0, &row1 };
  144. * Pass arrays using array notation rather than pointer notation:
  145. ```
  146. void printArray(char[] array);
  147. not
  148. void printArray( char* array);
  149. ```
  150. * In constructor's initialization list, use same names for fields and constructor parameters.
  151. * Do not use new or malloc (make memory leaks impossible).
  152. * Document class interface in .h file, above the class declaration.
  153. * Code should be self-documenting. A simple function with a good name needs no comment.
  154. * Code is automatically formatted before being pushed to the keybrd repository.
  155. The [astyle_cpp](astyle_cpp) file specifies the format:
  156. * Allman style indentation
  157. * indent 4 spaces
  158. * replace tabs with spaces
  159. * maximum code width of 100 columns
  160. <!-- http://stackoverflow.com/questions/2198241/best-practice-for-c-function-commenting -->
  161. Trace of keybrd scan
  162. --------------------
  163. Arduino does not have a debugger.
  164. So here is a list of functions in the order that they are called.
  165. The trace is of a one-row single-layer keybrd scan.
  166. ```
  167. loop() for each row
  168. Row::process()
  169. Scanner_uC::scan() strobe row on
  170. for each readPin
  171. set readState bit
  172. strobe row off
  173. Debouncer_Samples::debounce() debounce
  174. Row::send() for each key in row
  175. if falling edge
  176. Key_*::release() scanCode->release()
  177. Code_*::release() Keyboard.release(scancode)
  178. if rising edge
  179. Key_*::press() scanCode->press()
  180. Code_*::press() Keyboard.press(scancode)
  181. scanDelay.delay();
  182. ```
  183. The Arduino libraries
  184. ---------------------
  185. The keybrd libraries compile on the Arduino IDE and make extensive use of the following [Arduino libraries](https://www.arduino.cc/en/Reference/Libraries):
  186. #include <Arduino.h>
  187. #include <Wire.h>
  188. #include <Keyboard.h>
  189. #include <Mouse.h>
  190. <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>.