Skip to content

Commit cdee266

Browse files
committed
Merge branch 'develop' into chibios
2 parents e49851a + 221663c commit cdee266

File tree

9 files changed

+198
-69
lines changed

9 files changed

+198
-69
lines changed

common/action_layer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static void default_layer_state_set(uint32_t state)
2222
debug("default_layer_state: ");
2323
default_layer_debug(); debug(" to ");
2424
default_layer_state = state;
25+
hook_default_layer_change(default_layer_state);
2526
default_layer_debug(); debug("\n");
2627
clear_keyboard_but_mods(); // To avoid stuck keys
2728
}

common/hook.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
#include "hook.h"
2020

2121
/* -------------------------------------------------
22-
* Definitions of hardware-independent default hooks
22+
* Definitions of default hooks
2323
* ------------------------------------------------- */
2424

25-
/* Called on layer state change event. */
26-
/* Default behaviour: do nothing. */
2725
__attribute__((weak))
28-
void hook_layer_change(uint8_t layer_state) {
29-
(void)layer_state;
26+
void hook_keyboard_loop(void) {}
27+
28+
__attribute__((weak))
29+
void hook_matrix_change(keyevent_t event) {
30+
(void)event;
3031
}
3132

32-
/* Called periodically from the matrix scan loop (very often!) */
33-
/* Default behaviour: do nothing. */
3433
__attribute__((weak))
35-
void hook_keyboard_loop(void) {}
34+
void hook_default_layer_change(uint32_t default_layer_state) {
35+
(void)default_layer_state;
36+
}
3637

37-
/* Called on matrix state change event (every keypress => often!) */
38-
/* Default behaviour: do nothing. */
3938
__attribute__((weak))
40-
void hook_matrix_change(keyevent_t event) {
41-
(void)event;
39+
void hook_layer_change(uint32_t layer_state) {
40+
(void)layer_state;
4241
}
4342

44-
/* Called on indicator LED update event (when reported from host). */
45-
/* Default behaviour: calls led_set (for compatibility). */
4643
__attribute__((weak))
4744
void hook_keyboard_leds_change(uint8_t led_status) {
4845
keyboard_set_leds(led_status);
4946
}
5047

51-
/* Called once, on checking the bootmagic combos. */
52-
/* Default behaviour: do nothing. */
5348
__attribute__((weak))
54-
void hook_bootmagic(void) {
55-
/* An example: */
56-
// #include "bootmagic.h"
57-
// #include "keymap.h"
58-
// if(bootmagic_scan_keycode(KC_W)) {
59-
// // do something
60-
// }
61-
}
49+
void hook_bootmagic(void) {}

common/hook.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include "led.h"
2323

2424
/* -------------------------------------
25-
* Hardware / one-off hooks
25+
* Protocol hooks
2626
* ------------------------------------- */
2727

28-
/* Called once, before initialising USB. */
28+
/* Called once, very early stage of initialization, just after processor startup. */
2929
/* Default behaviour: do nothing. */
3030
void hook_early_init(void);
3131

32-
/* Called once, after USB is connected and keyboard initialised. */
32+
/* Called once, very last stage of initialization, just before keyboard loop. */
3333
/* Default behaviour: do nothing. */
3434
void hook_late_init(void);
3535

@@ -47,12 +47,9 @@ void hook_usb_suspend_loop(void);
4747
* the "normal" indicator LED status by default. */
4848
void hook_usb_wakeup(void);
4949

50-
/* Called once, on checking the bootmagic combos. */
51-
/* Default behaviour: do nothing. */
52-
void hook_bootmagic(void);
5350

5451
/* -------------------------------------
55-
* Keyboard / periodic hooks
52+
* Keyboard hooks
5653
* ------------------------------------- */
5754

5855
/* Called periodically from the keyboard loop (very often!) */
@@ -63,12 +60,21 @@ void hook_keyboard_loop(void);
6360
/* Default behaviour: do nothing. */
6461
void hook_matrix_change(keyevent_t event);
6562

63+
/* Called on default layer state change event. */
64+
/* Default behaviour: do nothing. */
65+
void hook_default_layer_change(uint32_t default_layer_state);
66+
6667
/* Called on layer state change event. */
6768
/* Default behaviour: do nothing. */
68-
void hook_layer_change(uint8_t layer_state);
69+
void hook_layer_change(uint32_t layer_state);
6970

7071
/* Called on indicator LED update event (when reported from host). */
71-
/* Default behaviour: calls keyboard_set_leds (for compatibility). */
72+
/* Default behaviour: calls keyboard_set_leds. */
7273
void hook_keyboard_leds_change(uint8_t led_status);
7374

75+
/* Called once, on checking the bootmagic combos. */
76+
/* Default behaviour: do nothing. */
77+
void hook_bootmagic(void);
78+
79+
7480
#endif /* _HOOKS_H_ */

common/keymap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static action_t keycode_to_action(uint8_t keycode)
142142
case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
143143
action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
144144
break;
145-
case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
145+
case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
146146
action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
147147
break;
148148
case KC_MS_UP ... KC_MS_ACCEL2:

doc/hook.txt

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Hooks
2+
-----
3+
Hooks allow you to execute custom code at certain predefined points in the firmware execution. To use them, just define the hook function in your keymap file.
4+
5+
The following hooks are available available:
6+
7+
Hook function | Timing
8+
--------------------------------|-----------------------------------------------
9+
`hook_early_init(void)` | Early in the boot process, before the matrix is initialized and before a connection is made with the host. Thus, this hook has access to very few parameters, but it is a good place to define any custom parameters needed by other early processes.
10+
`hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized.
11+
`hook_bootmagic(void)` | During the Boot Magic window, after EEPROM and Bootloader checks are made, but before any other built-in Boot Magic checks are made.
12+
`hook_usb_wakeup(void)` | When the device wakes up from USB suspend state.
13+
`hook_usb_suspend_entry(void)` | When the device enters USB suspend state.
14+
`hook_usb_suspend_loop(void)` | Continuously, while the device is in USB suspend state. *Default action:* power down and periodically check the matrix, causing wakeup if needed.
15+
`hook_keyboard_loop(void)` | Continuously, during the main loop, after the matrix is checked.
16+
`hook_matrix_change(keyevent_t event)` | When a matrix state change is detected, before any other actions are processed.
17+
`hook_layer_change(uint32_t layer_state)` | When any layer is changed.
18+
`hook_default_layer_change(uint32_t default_layer_state)` | When any default layer is changed.
19+
`hook_keyboard_leds_change(uint8_t led_status)` | Whenever a change in the LED status is performed. *Default action:* call `keyboard_set_leds(led_status)`
20+
21+
22+
23+
24+
25+
### Hooks Examples
26+
27+
You can try these out by copying the code to your keymap file, or any .c file in the Makefile `SRC`.
28+
29+
#### Activate keymap layer 5 on startup
30+
31+
```C
32+
#include "action_layer.h"
33+
34+
void hook_late_init(void)
35+
{
36+
layer_on(5);
37+
print("Layer 5 enabled!");
38+
}
39+
```
40+
41+
#### Blink the Caps Lock LED every .5 seconds
42+
43+
```C
44+
#include "timer.h"
45+
#include "led.h"
46+
47+
bool my_led_status = 0;
48+
uint16_t my_led_timer;
49+
50+
void hook_keyboard_loop(void)
51+
{
52+
// check if we've reached 500 milliseconds yet...
53+
if (timer_elapsed(my_led_timer) > 500)
54+
{
55+
// we've reached 500 milliseconds!
56+
// reset the timer
57+
my_led_timer = timer_read();
58+
59+
// check the current LED state
60+
if (my_led_status)
61+
{
62+
// LED is on, so let's turn it off
63+
led_set(host_keyboard_leds() & ~(1<<USB_LED_CAPS_LOCK));
64+
my_led_status = 0;
65+
}
66+
else
67+
{
68+
// LED is off, so let's turn it on
69+
led_set(host_keyboard_leds() | (1<<USB_LED_CAPS_LOCK));
70+
my_led_status = 1;
71+
}
72+
}
73+
}
74+
```
75+
76+
#### Flash the Caps Lock LED for 20ms on every keypress
77+
```C
78+
include "timer.h"
79+
#include "led.h"
80+
81+
bool my_led_status = 0;
82+
uint16_t my_led_timer;
83+
84+
void hook_matrix_change(keyevent_t event)
85+
{
86+
// only flash LED for key press events, not key release events.
87+
if (event.pressed)
88+
{
89+
// check the current LED status and reverse it
90+
led_set(host_keyboard_leds() ^ (1<<USB_LED_CAPS_LOCK));
91+
92+
my_led_status = 1;
93+
my_led_timer = timer_read();
94+
}
95+
}
96+
97+
void hook_keyboard_loop(void)
98+
{
99+
if (my_led_status)
100+
{
101+
// check if we've reached 20 milliseconds yet...
102+
if (timer_elapsed(my_led_timer) > 50)
103+
{
104+
led_set(host_keyboard_leds());
105+
106+
my_led_status = 0;
107+
}
108+
}
109+
}
110+
111+
```

doc/keymap.md

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Keymap framework - how to define your keymap
33
***NOTE: This is not final version, may be inconsistent with source code and changed occasionally for a while.***
44

55
## 0. Keymap and layers
6-
**Keymap** is comprised of multiple layers of key layout, you can define **32 layers** at most.
7-
**Layer** is an array of **keycodes** to define **actions** for each physical keys.
8-
respective layers can be validated simultaneously. Layers are indexed with 0 to 31 and higher layer has precedence.
6+
The **keymap** is an array composed of one or more layers.
7+
Each **layer** is an array of **keycodes**, defining **actions** for each physical key.
8+
Layers can be activated and deactivated independently. Multiple layers may be active at once, resulting in the currently-active **layer state**. Each layer has an index between 0-31. As active layers are stacked together, higher layers take precedence over lower layers.
99

1010
Keymap: 32 Layers Layer: Keycode matrix
1111
----------------- ---------------------
@@ -21,31 +21,17 @@ respective layers can be validated simultaneously. Layers are indexed with 0 to
2121
1 /___________// | 1 `--------------------------
2222
0 /___________/ V low 0 `--------------------------
2323

24+
**Note:** The keymap array is limited to **32 layers**.
2425

2526

26-
### 0.1 Keymap status
27-
Keymap has its state in two parameters:
28-
**`default_layer`** indicates a base keymap layer(0-31) which is always valid and to be referred, **`keymap_stat`** is 16bit variable which has current on/off status of layers on its each bit.
2927

30-
Keymap layer '0' is usually `default_layer` and which is the only valid layer and other layers is initially off after boot up firmware, though, you can configured them in `config.h`.
31-
To change `default_layer` will be useful when you switch key layout completely, say you want Colmak instead of Qwerty.
28+
### 0.1 Layer state
29+
The current keymap layer state is determined by two parameters: the *default layer*, and the individual *layer states*. Changing the default layer is useful for switching key layouts completely; for example, switching to Dvorak, Colemak or Workman instead of QWERTY. Individual layer states, on the other hand, can be used to overlay the base layer with other functions such as navigation keys, function keys (F1-F12), media keys or other actions.
3230

33-
Initial state of Keymap Change base layout
34-
----------------------- ------------------
31+
Because the default layer is really just a special case affecting the overall layer state, it is important to first understand how the layer state is determined.
3532

36-
31 31
37-
30 30
38-
29 29
39-
: :
40-
: : ____________
41-
2 ____________ 2 / /
42-
1 / / ,->1 /___________/
43-
,->0 /___________/ | 0
44-
| |
45-
`--- default_layer = 0 `--- default_layer = 1
46-
layer_state = 0x00000001 layer_state = 0x00000002
47-
48-
On the other hand, you shall change `layer_state` to overlay base layer with some layers for feature such as navigation keys, function key(F1-F12), media keys or special actions.
33+
#### 0.1.1 The layer state
34+
The **layer state** indicates the current on/off status of all layers. It is defined in the firmware by a 32-bit integer, `layer_state`, which stores each layer's on/off status in a single bit: 0 for off, 1 for on. As layers are activated and deactivated, their respective bits are flipped, changing the value of `layer_state`.
4935

5036
Overlay feature layer
5137
--------------------- bit|status
@@ -62,28 +48,64 @@ On the other hand, you shall change `layer_state` to overlay base layer with som
6248
`--- default_layer = 1 |
6349
layer_state = 0x60000002 <-'
6450

51+
#### 0.1.2 The default layer
52+
The **default layer** is the base keymap layer (0-31) which is always active and considered the "bottom" of the stack. When the firmware boots, the default layer is the only active layer. It is set to layer 0 by default, though this can be changed ~~in *config.h*~~ via Boot Magic settings.
53+
54+
Initial state of Keymap Change base layout
55+
----------------------- ------------------
56+
57+
31 31
58+
30 30
59+
29 29
60+
: :
61+
: : ____________
62+
2 ____________ 2 / /
63+
1 / / ,->1 /___________/
64+
,->0 /___________/ | 0
65+
| |
66+
`--- default_layer = 0 `--- default_layer = 1
67+
layer_state = 0x00000001 layer_state = 0x00000002
68+
69+
Note that the `default_layer_state` variable only determines the lowest value to which `layer_state` may be set, and that `default_layer_state` is used by the core firmware when determining the starting value of `layer_state` before applying changes. In other words, the default layer will *always* be set to *on* in `layer_state`.
70+
71+
The default layer is defined in the firmware by the `default_layer_state` variable, which is identical in format to the `layer_state` variable exlpained above. The value may be changed using the following functions:
72+
73+
- `default_layer_state_set(state)` sets the state to the specified 32-bit integer value.
74+
- AND/OR/XOR functions set the state based on a boolean logic comparison between the current state and the specified 32-bit integer value:
75+
- `default_layer_state_and(state)`
76+
- `default_layer_state_or(state)`
77+
- `default_layer_state_xor(state)`
78+
79+
For example, to set layer 3 as the default layer:
80+
81+
```C
82+
// convert 3 to a 32-bit unsigned long value, and set the default layer
83+
default_layer_state_set(1UL<<3);
84+
```
85+
6586
6687
6788
### 0.2 Layer Precedence and Transparency
68-
Note that ***higher layer has higher priority on stack of layers***, namely firmware falls down from top layer to bottom to look up keycode. Once it spots keycode other than **`KC_TRNS`**(transparent) on a layer it stops searching and lower layers aren't referred.
89+
Note that ***higher layers have priority in the layer stack***. The firmware starts at the topmost active layer, and works down to the bottom to find the an active keycode. Once the search encounters any keycode other than **`KC_TRNS`** (transparent) on an active layer, the search is halted and the remaining lower layers aren't examined, even if they are active.
90+
91+
**Note:** a layer must be activated before it may be included in the stack search.
92+
93+
`KC_TRNS` is a special placeholder which can be used on overlay layers. This allows for the creation of "partial" layers which fall back on the lower layers, eliminating a good deal of repetition in keymap files.
6994
70-
You can place `KC_TRNS` on overlay layer changes just part of layout to fall back on lower or base layer.
71-
Key with `KC_TRANS` doesn't has its own keycode and refers to lower valid layers for keycode, instead.
72-
See example below.
7395
7496
7597
### 0.3 Keymap Example
76-
Keymap is **`keymaps[]`** C array in fact and you can define layers in it with **`KEYMAP()`** C macro and keycodes. To use complex actions you need to define `Fn` keycode in **`fn_actions[]`** array.
98+
The keymap is defined in the **`keymaps[]`** array, a 2-dimensional array of rows and columns corresponding to positions in the keyboard matrix. But most often the layers are defined using C macros to allow for easier reading and editing of the keymap files. To use complex actions you need to define `Fn` keycodes in the **`fn_actions[]`** array.
7799
78-
This is a keymap example for [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard.
79-
This example has three layers, 'Qwerty' as base layer, 'Cursor' and 'Mousekey'.
100+
This is a keymap example for the [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard.
101+
This example has three layers: the QWERTY base layer, and two overlay layers for cursor and mousekey control, respectively.
80102
In this example,
81103
82-
`Fn0` is a **momentary layer switching** key, you can use keys on Cursor layer while holding the key.
104+
`Fn0` is a **momentary layer switching** key--you can use keys on the Cursor layer while holding the key.
83105
84-
`Fn1` is a momentary layer switching key with tapping feature, you can get semicolon **';'** with taping the key and switch layers while holding the key. The word **'tap'** or **'tapping'** mean to press and release a key quickly.
106+
`Fn1` is a momentary layer switching key with tapping function--tapping the key as one would normally use it, sends the semicolon **';'** keycode, while holding the key down switches layers.
85107
86-
`Fn2` is a **toggle layer switch** key, you can stay switched layer after releasing the key unlike momentary switching.
108+
`Fn2` is a **toggle layer switch** key--pressing the key toggles the layer on until you press it again.
87109
88110
You can find other keymap definitions in file `keymap.c` located on project directories.
89111

protocol/vusb.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SRC += $(VUSB_DIR)/main.c \
1010

1111

1212
ifdef NO_UART
13+
OPT_DEFS += -DNO_UART
1314
SRC += $(COMMON_DIR)/sendchar_null.c
1415
else
1516
SRC += $(COMMON_DIR)/sendchar_uart.c \

protocol/vusb/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main(void)
4949
#endif
5050

5151
CLKPR = 0x80, CLKPR = 0;
52-
#ifndef PS2_USE_USART
52+
#ifndef NO_UART
5353
uart_init(UART_BAUD_RATE);
5454
#endif
5555

tool/chibios/chibios.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ endif
105105
include $(PLATFORM_MK)
106106

107107

108-
BOARD_MK = $(TARGET_DIR)/boards/$(BOARD)
108+
BOARD_MK = $(TARGET_DIR)/boards/$(BOARD)/board.mk
109109
ifeq ("$(wildcard $(BOARD_MK))","")
110110
BOARD_MK = $(CHIBIOS)/os/hal/boards/$(BOARD)/board.mk
111111
ifeq ("$(wildcard $(BOARD_MK))","")

0 commit comments

Comments
 (0)