Kaleidoscope
Kaleidoscope.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Arduino.h>
4 
5 //end of add your includes here
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 void loop();
10 void setup();
11 #ifdef __cplusplus
12 } // extern "C"
13 #endif
14 
15 //add your function definitions for the project KeyboardIO here
16 
17 #define TS(X) //Serial.print(micros() );Serial.print("\t");Serial.println(X);
18 
19 #include <stdio.h>
20 #include <math.h>
21 
22 
23 #include KALEIDOSCOPE_HARDWARE_H
24 #include "key_events.h"
25 #include "kaleidoscope/hid.h"
26 #include "layers.h"
27 
28 #define HOOK_MAX 64
29 
31 
32 #ifndef VERSION
33 #define VERSION "locally-built"
34 #endif
35 
36 #define KEYMAP_SIZE (sizeof(keymaps) / ROWS / COLS / sizeof(Key))
37 
38 #define USE_PLUGINS(plugins...) Kaleidoscope.use(plugins)
39 
41  public:
42  virtual void begin(void) = 0;
43 };
44 
46  public:
47  Kaleidoscope_(void);
48 
49  void setup(const byte keymap_count) {
50  setup();
51  }
52  void setup(void);
53  void loop(void);
54 
55  // ---- Kaleidoscope.use() ----
56 
57  // First, we have the zero-argument version, which will satisfy the tail case.
58  inline void use() {
59  }
60 
61  // Then, the one-argument version, that gives us type safety for a single
62  // plugin.
63  inline void use(KaleidoscopePlugin *p) {
64  p->begin();
65  }
66 
67  // We have a no-op with a int argument, as a temporary hack until we remove
68  // the last instance of a NULL-terminated Kaleidoscope.use() call.
69  inline void use(int) {
70  }
71 
72  // And the magic is in the last one, a template. The first parameter is
73  // matched out by the compiler, and passed to one of the functions above. The
74  // rest of the parameter pack (which may be an empty set in a recursive case),
75  // are passed back to either ourselves, or the zero-argument version a few
76  // lines above.
77  template <typename... Plugins>
78  void use(KaleidoscopePlugin *first, Plugins&&... plugins) {
79  use(first);
80  use(plugins...);
81  }
82 
83  // ---- hooks ----
84 
85  /*
86  * In most cases, one only wants a single copy of a hook. On the other hand,
87  * plugins that depend on other plugins, may want to make it easier for the
88  * end-user to use the plugin, and call the setup function of the dependent
89  * plugins too. In case the end-user calls the same setup function, we'd end up
90  * with hooks registered multiple times.
91  *
92  * To avoid this, protection against double-registration has been introduced.
93  * The `event_handler_hook_use` and `loop_hook_use` functions will only allow
94  * one copy of the hook. The `event_handler_hook_append` and `loop_hook_append`
95  * functions will, on the other hand, just append the hooks, and not care about
96  * protection.
97  */
98  typedef Key(*eventHandlerHook)(Key mappedKey, byte row, byte col, uint8_t keyState);
99  static eventHandlerHook eventHandlers[HOOK_MAX];
100 
101  static void replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook);
102  static void appendEventHandlerHook(eventHandlerHook hook);
103  static void useEventHandlerHook(eventHandlerHook hook);
104 
105  typedef void (*loopHook)(bool postClear);
106  static loopHook loopHooks[HOOK_MAX];
107 
108  static void replaceLoopHook(loopHook oldHook, loopHook newHook);
109  static void appendLoopHook(loopHook hook);
110  static void useLoopHook(loopHook hook);
111 
112  static bool focusHook(const char *command);
113 };
114 
116 
117 #define FOCUS_HOOK_KALEIDOSCOPE FOCUS_HOOK(Kaleidoscope.focusHook, \
118  "layer.on\n" \
119  "layer.off\n" \
120  "layer.getState")
121 
122 /* -- DEPRECATED aliases; remove them when there are no more users. -- */
123 
124 #define event_handler_hook_use(hook) Kaleidoscope.useEventHandlerHook(hook);
125 #define event_handler_hook_append(hook) Kaleidoscope.appendEventHandlerHook(hook)
126 #define event_handler_hook_replace(oldHook, newHook) Kaleidoscope.replaceEventHandlerHook(oldHook, newHook)
127 
128 #define loop_hook_use(hook) Kaleidoscope.useLoopHook(hook)
129 #define loop_hook_append(hook) Kaleidoscope.appendLoopHook(hook)
130 #define loop_hook_replace(oldHook, newHook) Kaleidoscope.replaceLoopHook(oldHook, newHook)
Definition: Kaleidoscope.h:45
byte byte col
Definition: TapDance.cpp:229
void use(int)
Definition: Kaleidoscope.h:69
Definition: key_defs.h:13
Kaleidoscope_ Kaleidoscope
Definition: Kaleidoscope.cpp:126
#define HARDWARE_IMPLEMENTATION
Definition: Kaleidoscope-Hardware-Model01.h:5
void use(KaleidoscopePlugin *p)
Definition: Kaleidoscope.h:63
HARDWARE_IMPLEMENTATION KeyboardHardware
Definition: Kaleidoscope-Hardware-Model01.cpp:245
#define HOOK_MAX
Definition: Kaleidoscope.h:28
union Key_ Key
void use(KaleidoscopePlugin *first, Plugins &&... plugins)
Definition: Kaleidoscope.h:78
void use()
Definition: Kaleidoscope.h:58
Definition: Kaleidoscope.h:40
virtual void begin(void)=0
void loop()
byte row
Definition: TapDance.cpp:229
void setup()
uint8_t keyState
Definition: Kaleidoscope-Macros.cpp:5
void setup(const byte keymap_count)
Definition: Kaleidoscope.h:49