|
| 1 | +/* -*- mode: c++ -*- |
| 2 | + * Basic -- A very basic Kaleidoscope example |
| 3 | + * Copyright (C) 2018 Keyboard.io, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify it under |
| 6 | + * the terms of the GNU General Public License as published by the Free Software |
| 7 | + * Foundation, version 3. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 12 | + * details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along with |
| 15 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "Kaleidoscope.h" |
| 19 | + |
| 20 | + |
| 21 | +// This example demonstrates how a plugin can gather information about |
| 22 | +// the keymap at compile time, e.g. to adapt its behavior, safe resources, ... |
| 23 | + |
| 24 | +/* *INDENT-OFF* */ |
| 25 | +KEYMAPS( |
| 26 | + [0] = KEYMAP_STACKED |
| 27 | + ( |
| 28 | + Key_NoKey, Key_1, Key_1, Key_1, Key_4, Key_5, Key_NoKey, |
| 29 | + Key_Backtick, Key_Q, Key_W, Key_E, Key_R, Key_T, Key_Tab, |
| 30 | + Key_PageUp, Key_A, Key_S, Key_D, Key_F, Key_G, |
| 31 | + Key_PageDown, Key_Z, Key_X, Key_C, Key_V, Key_B, Key_Escape, |
| 32 | + |
| 33 | + Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, |
| 34 | + Key_NoKey, |
| 35 | + |
| 36 | + Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, |
| 37 | + Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, |
| 38 | + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, |
| 39 | + Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, |
| 40 | + |
| 41 | + Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, |
| 42 | + Key_NoKey |
| 43 | + ), |
| 44 | +) |
| 45 | +/* *INDENT-ON* */ |
| 46 | + |
| 47 | +using namespace kaleidoscope::sketch_exploration; |
| 48 | + |
| 49 | +class BPlugin : public kaleidoscope::Plugin {}; |
| 50 | +class CPlugin : public kaleidoscope::Plugin {}; |
| 51 | + |
| 52 | +// A simple plugin that defines just one hook. |
| 53 | +// |
| 54 | +class APlugin : public kaleidoscope::Plugin { |
| 55 | + |
| 56 | + public: |
| 57 | + APlugin() : has_key_1_{false} {} |
| 58 | + |
| 59 | + template<typename _Sketch> |
| 60 | + kaleidoscope::EventHandlerResult exploreSketch() { |
| 61 | + |
| 62 | + // Static keymap exploration |
| 63 | + |
| 64 | + typedef typename _Sketch::StaticKeymap K; |
| 65 | + |
| 66 | + // Important: Always make sure to call _Sketch::StaticKeymap's methods |
| 67 | + // in a constexpr context. This is done by |
| 68 | + // passing their value to a constexpr temporary variable. |
| 69 | + |
| 70 | + constexpr uint8_t n_key_1 = K::collect(NumKeysEqual{Key_1}); |
| 71 | + static_assert(n_key_1 == 3, "Error determining key count"); |
| 72 | + |
| 73 | + constexpr bool has_key_1 = K::collect(HasKey{Key_1}); |
| 74 | + static_assert(has_key_1, "Error querying key existence"); |
| 75 | + has_key_1_ = has_key_1; // Assign the temporary that was computed |
| 76 | + // at compile time. |
| 77 | + |
| 78 | + constexpr Key max_key = K::collect(MaxKeyRaw{}); |
| 79 | + static_assert(max_key.getRaw() > 0, ""); |
| 80 | + |
| 81 | + static_assert(K::getKey(0 /*layer*/, KeyAddr{2, 3}) == Key_D, |
| 82 | + "Key lookup failed"); |
| 83 | + |
| 84 | + constexpr auto n_layers = K::nLayers(); |
| 85 | + constexpr auto layer_size = K::layerSize(); |
| 86 | + |
| 87 | + // Plugin exploration |
| 88 | + // |
| 89 | + // Use macros ENTRY_TYPE, ENRTY_IS_LAST, PLUGIN_POSITION, |
| 90 | + // PLUGIN_IS_REGISTERED and NUM_OCCURRENCES to retreive information |
| 91 | + // about the plugins that are registered in the sketch. |
| 92 | + |
| 93 | + typedef typename _Sketch::Plugins P; |
| 94 | + |
| 95 | + static_assert(std::is_same<ENTRY_TYPE(P, 0), APlugin>::value, ""); |
| 96 | + static_assert(std::is_same<ENTRY_TYPE(P, 1), BPlugin>::value, ""); |
| 97 | + |
| 98 | + static_assert(P::size == 3, ""); |
| 99 | + |
| 100 | + static_assert(!ENRTY_IS_LAST(P, 0), ""); |
| 101 | + static_assert(!ENRTY_IS_LAST(P, 1), ""); |
| 102 | + static_assert(ENRTY_IS_LAST(P, 2), ""); |
| 103 | + |
| 104 | + static_assert(PLUGIN_POSITION(P, APlugin) == 0, ""); |
| 105 | + static_assert(PLUGIN_POSITION(P, BPlugin) == 1, ""); |
| 106 | + static_assert(PLUGIN_POSITION(P, CPlugin) == -1, ""); |
| 107 | + |
| 108 | + static_assert(PLUGIN_IS_REGISTERED(P, APlugin) == true, ""); |
| 109 | + static_assert(PLUGIN_IS_REGISTERED(P, BPlugin) == true, ""); |
| 110 | + static_assert(PLUGIN_IS_REGISTERED(P, CPlugin) == false, ""); |
| 111 | + |
| 112 | + static_assert(NUM_OCCURRENCES(P, APlugin) == 2, ""); |
| 113 | + static_assert(NUM_OCCURRENCES(P, BPlugin) == 1, ""); |
| 114 | + static_assert(NUM_OCCURRENCES(P, CPlugin) == 0, ""); |
| 115 | + |
| 116 | + return kaleidoscope::EventHandlerResult::OK; |
| 117 | + } |
| 118 | + |
| 119 | + private: |
| 120 | + |
| 121 | + bool has_key_1_; |
| 122 | +}; |
| 123 | + |
| 124 | +APlugin a_plugin1, a_plugin2; |
| 125 | +BPlugin b_plugin; |
| 126 | + |
| 127 | +KALEIDOSCOPE_INIT_PLUGINS( |
| 128 | + a_plugin1, |
| 129 | + b_plugin, |
| 130 | + a_plugin2 |
| 131 | +) |
| 132 | + |
| 133 | +void setup() { |
| 134 | + Kaleidoscope.setup(); |
| 135 | +} |
| 136 | + |
| 137 | +void loop() { |
| 138 | + Kaleidoscope.loop(); |
| 139 | +} |
0 commit comments