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

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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 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. Custom Row classes
  14. ------------------
  15. Row classes are central to the keybrd library.
  16. Row is an abstract base class that allows flexibility for designing derived Row classes:
  17. * Row functions can be overridden in a derived class
  18. * choice of Debouncers
  19. * choice of Scanners
  20. This example illustrates the custom Row classes for a fictional keybrd_Ext extension library.
  21. The keybrd_Ext library is for a split keyboard with sticky keys and a matrix on each hand.
  22. Row_Ext::keyWasPressed() overrides Row::keyWasPressed()<br>
  23. Row_Ext::keyWasPressed() is used to unstick sticky keys
  24. Row_Ext_uC and Row_Ext_ShiftRegisters are a custom classes composed of stock keybrd library classes.<br>
  25. Row_Ext_uC uses Scanner_uC to scan the primary matrix.<br>
  26. Row_Ext_ShiftRegisters uses Scanner_ShiftRegs74HC165 to scan the secondary matrix.
  27. Class inheritance diagram
  28. ```
  29. Row
  30. |
  31. Row_Ext (override Row::keyWasPressed() )
  32. / \
  33. Row_Ext_uC Row_Ext_ShiftRegisters (inherit Row_Ext::keyWasPressed() )
  34. Scanner_uC Scanner_ShiftRegs74HC165
  35. ```
  36. Dependency diagram
  37. ```
  38. ________ Row_Ext_uC[1] ______________
  39. / | \
  40. Scanner_uC[1] Debouncer_Samples[1] Key[1..*]
  41. / |
  42. strobePin[1] Code[1..*]
  43. _________ Row_Ext_ShiftRegisters[1] ________
  44. / \ \
  45. Scanner_ShiftRegs74HC165[1] Debouncer_Samples[1] Key[1..*]
  46. | |
  47. strobePin[1] Code[1..*]
  48. ```
  49. Class inheritance diagrams
  50. --------------------------
  51. Keybrd library class inheritance diagram
  52. ```
  53. ________ Row ___________
  54. / | \
  55. Row_uC Row_ShiftRegisters Row_IOE (to be added)
  56. Scanner_uC Scanner_Port Scanner_ShiftRegs74HC165
  57. PortIOE
  58. PortWrite
  59. |
  60. PortWrite_PCA9655E (one PortWrite class for each IOE type)
  61. PortRead
  62. |
  63. PortRead_PCA9655E (one PortRead class for each IOE type)
  64. _ LED _
  65. / \
  66. LED_uC LED_PCA9655E
  67. DebouncerInterface
  68. |
  69. Debouncer_Samples
  70. ScanDelay
  71. LayerStateInterface
  72. |
  73. LayerState
  74. Key __
  75. | \
  76. | Key_LayeredKeysArray
  77. |
  78. Code
  79. |_____________________
  80. | \ \
  81. | Code_LayerLock Code_LayerHold
  82. |
  83. |___________________________
  84. | \ \
  85. | Code_LayeredScScBase Code_LayeredCodeScBase
  86. | | |
  87. | Code_LayeredScSc Code_LayeredCodeSc
  88. |
  89. |__________________________________________
  90. \ \ \ \
  91. Code_Sc Code_Shift Code_AutoShift Code_LEDLock
  92. / \
  93. Code_ScS Code_ScNS
  94. ```
  95. Dependency diagrams
  96. -------------------
  97. Dependency diagram of example single-layer keyboard with LEDs
  98. ```
  99. _ Row_uC[1..*] _
  100. / | \
  101. Scanner_uC Debouncer Keys[1..*] __
  102. | \
  103. Code[1..*] Code_LEDLock[1..*]
  104. |
  105. LED_PinNumber[1]
  106. ```
  107. Dependency diagram of example multi-layer keyboard with layer LEDs
  108. ```
  109. LayerStates[1..*]
  110. ________ Row_uC[1..*] ___________/__ | \
  111. / | \ / \ | \
  112. Scanner_uC[1] Debouncer[1] Keys[1..*] / Code_Layer[1..*] LED_PinNumber[0..*]
  113. | /
  114. Code[1..*]
  115. ```
  116. Dependency diagram of example secondary matrix with shift registers
  117. ```
  118. Row_ShiftRegisters[1..*]
  119. / \ \
  120. RowScanner_ShiftRegisters Debouncer Keys[1..*]
  121. |
  122. Code[1..*]
  123. ```
  124. Dependency diagram of example secondary matrix with I/O Expander and LEDs
  125. ```
  126. ___ Row_IOE[1..*] _________
  127. / \ \
  128. _ Scanner_Port[1] _ Debouncer[1] Keys[1..*] __
  129. / | \ | \
  130. PortWrite[1] RowPin[1] PortRead[1] Code[1..*] Code_LEDLock[1..*]
  131. \ / \ |
  132. \ / ColPins[1..*] LED[1]
  133. \ /
  134. PortIOE[0..*]
  135. ```
  136. Class naming conventions
  137. ------------------------
  138. Class names start with upper case letter.
  139. Most derived-class names start with the base class name followed by "_" and a name e.g.
  140. ```
  141. Code
  142. |
  143. Code_LayerLock
  144. ```
  145. This convention leads to class names that convey information about the classes inheritance.
  146. Underscore delineates base class name and sub-class name. Capital letters delineate words.
  147. Layer-class naming conventions
  148. ------------------------------
  149. *Code_Layer* class names are concatenations of "Code_", "Layer" or layer name, and persistence.
  150. Example persistences are:
  151. * "Lock" - layer remains active after the layer key is released
  152. * "Hold" - layer is active for as long as layer key is held down
  153. Example Code_Layer class names:
  154. * Code_LayerHold
  155. * Code_LayerLock
  156. *LayerState* class names start with "LayerState" and end with a descriptive name.
  157. Example LayerState class names:
  158. * LayerState - basic LayerState class in keybrd library
  159. * LayerState_DH - main LayerState for the keybrd_DH library
  160. * LayerState_MF - LayerState for Mouse Function sub-layers in the keybrd_DH library
  161. *Code_Layered* class names start with "Code_Layered" and end with a descriptive name.
  162. Example Code_Layered class names:
  163. * Code_LayeredScSc
  164. * Key_LayeredKeysArray
  165. Style guide
  166. -----------
  167. Following the style guide makes it easier for the next programmer to understand your code.
  168. * For class names, see above section "Class naming conventions".
  169. * Member names use camelCase starting with lowercase letter.
  170. * Use constants rather than macros, except for header guards.
  171. * Global const names and static const names use ALL_CAPS_WITH_UNDERSCORE.
  172. * Macros use ALL_CAPS_WITH_UNDERSCORE and have _MACRO suffix e.g. SAMPLE_COUNT_MACRO
  173. * Header guards have _H suffix e.g. #ifndef FILE_NAME_H
  174. * Pointer names are prefixed with "ptr" e.g. ptrRow = &row;
  175. * Arrays names use the plural of the element name e.g. Row* const = ptrsRows { &row0, &row1 };
  176. * Pass arrays using array notation rather than pointer notation:
  177. ```
  178. void printArray(char[] array);
  179. not
  180. void printArray( char* array);
  181. ```
  182. * In constructor's initialization list, use same names for fields and constructor parameters.
  183. * Do not use new or malloc (make memory leaks impossible).
  184. * Document class interface in .h file, above the class declaration.
  185. * Code should be self-documenting. A simple function with a good name needs no comment.
  186. * Code is automatically formatted before being pushed to the keybrd repository.
  187. The [astyle_cpp](astyle_cpp) file specifies the format:
  188. * Allman style indentation
  189. * indent 4 spaces
  190. * replace tabs with spaces
  191. * maximum code width of 100 columns
  192. <!-- http://stackoverflow.com/questions/2198241/best-practice-for-c-function-commenting -->
  193. Trace of keybrd scan
  194. --------------------
  195. Arduino does not have a debugger.
  196. So here is the next best thing; a list of functions in the order that they are called.
  197. The trace is of a one-row single-layer keybrd scan.
  198. Refer to it like a table of contents while reading the keybrd library.
  199. ```
  200. loop() for each row
  201. Row::process()
  202. Scanner_uC::scan() strobe row on
  203. for each readPin
  204. set readState bit
  205. strobe row off
  206. Debouncer_Samples::debounce() debounce
  207. Row::send() for each key in row
  208. if falling edge
  209. Key_*::release() scanCode->release()
  210. Code_*::release() Keyboard.release(scancode)
  211. if rising edge
  212. Key_*::press() scanCode->press()
  213. Code_*::press() Keyboard.press(scancode)
  214. scanDelay.delay();
  215. ```
  216. The Arduino libraries
  217. ---------------------
  218. The keybrd libraries compile on the Arduino IDE and make extensive use of the following [Arduino libraries](https://www.arduino.cc/en/Reference/Libraries):
  219. #include <Arduino.h>
  220. #include <Wire.h>
  221. #include <Keyboard.h>
  222. #include <Mouse.h>
  223. <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>.