Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tap_random_base64 and software timer info to Useful Functions doc #4360

Merged
merged 4 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions docs/faq_keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,3 @@ here real_mods lost state for 'physical left shift'.

weak_mods is ORed with real_mods when keyboard report is sent.
https://github.com/tmk/tmk_core/blob/master/common/action_util.c#L57

## Timer Functionality

It's possible to start timers and read values for time-specific events - here's an example:

```c
static uint16_t key_timer;
key_timer = timer_read();

if (timer_elapsed(key_timer) < 100) {
// do something if less than 100ms have passed
} else {
// do something if 100ms or more have passed
}
```

It's best to declare the `static uint16_t key_timer;` at the top of the file, outside of any code blocks you're using it in.
21 changes: 21 additions & 0 deletions docs/ref_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,24 @@ And to do so, add `reset_keyboard()` to your function or macro, and this will re
If you're having issues with Audio, RGB Underglow, backlighting or keys acting weird, then you can reset the EEPROM (persistent setting storage). Bootmagic is one way to do this, but if that isn't enabled, then you can use a custom macro to do so.

To wipe the EEPROM, run `eeconfig_init()` from your function or macro to reset most of the settings to default.

## Tap random key

If you want to send a random character to the host computer, you can use the `tap_random_base64()` function. This [pseudorandomly](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) selects a number between 0 and 63, and then sends a key press based on that selection. (0–25 is `A`–`Z`, 26–51 is `a`–`z`, 52–61 is `0`–`9`, 62 is `+` and 63 is `/`).

?> Needless to say, but this is _not_ a cryptographically secure method of generating random Base64 keys or passwords.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
?> Needless to say, but this is _not_ a cryptographically secure method of generating random Base64 keys or passwords.
?> Needless to say, this is _not_ a cryptographically secure method of generating random Base64 keys or passwords.

Woops, didn't proofread.


drashna marked this conversation as resolved.
Show resolved Hide resolved
## Software Timers

It's possible to start timers and read values for time-specific events. Here's an example:

```c
static uint16_t key_timer;
key_timer = timer_read();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably a bug: the timer will be reinitialized every time the function is called, effectively making the static declaration pointless (i.e. only the else branch will ever be executed). You should probably do static uint16_t key_timer = timer_read(); here instead, but I think that's a bit misleading too. Namely, this will make the timed event only ever trigger once, as timer_read() will only be called the first time the function is run.

A better alternative would be to set the timer in an if statement, then read and compare the elapsed time in the else branch, as is often done in keymaps:

static uint16_t key_timer;

if (record->event.pressed) {
  key_timer = timer_read();
} else {
  if (timer_elapsed(key_timer) < 100) {
    // Do something if less than 100 ms has passed
  } else {
    // Do something if 100 ms or more has passed
  }
}


if (timer_elapsed(key_timer) < 100) {
// do something if less than 100ms have passed
} else {
// do something if 100ms or more have passed
}
```