Decrease the timer precision to seconds

While we use milliseconds internally, the end-user will not need higher
precision than a second, therefore, store the settings with second-precision.
This is much friendlier towards the user, and also uses less space in EEPROM, by
about six bytes.

Addresses a part of #8, thanks @gedankenexperimenter!

Signed-off-by: Gergely Nagy <kaleidoscope@gergo.csillger.hu>
pull/389/head
Gergely Nagy 7 years ago
parent d7302d04dd
commit 4850787b65

@ -40,27 +40,27 @@ void setup (void) {
## Plugin methods
The plugin provides a single object, `TypingBreaks`, with the following
properties. All times are in milliseconds.
properties. All times are in seconds.
### `.settings.idle_time_limit`
> The amount of time that can pass between two pressed keys, before the plugin
> considers it a new session, and starts all timers and counters over.
>
> Defaults to 10000 (10 seconds).
> Defaults to 10 seconds.
### `.settings.lock_time_out`
> The length of the session, after which the keyboard will be locked.
>
> Defaults to 2700000 (45 minutes).
> Defaults to 2700 seconds (45 minutes).
### `.settings.lock_length`
> The length until the keyboard lock is held. Any key pressed while the lock is
> active, will be discarded.
>
> Defaults to 300000 (5 minutes).
> Defaults to 300 seconds (5 minutes).
### `.settings.left_hand_max_keys`

@ -23,9 +23,9 @@
namespace kaleidoscope {
TypingBreaks::settings_t TypingBreaks::settings = {
.idle_time_limit = 10000, // 10s
.lock_time_out = 2700000, // 45m
.lock_length = 300000, // 5m
.idle_time_limit = 10, // 10s
.lock_time_out = 2700, // 45m
.lock_length = 300, // 5m
.left_hand_max_keys = 0,
.right_hand_max_keys = 0
};
@ -45,14 +45,18 @@ void TypingBreaks::begin(void) {
}
Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
uint32_t lock_length = settings.lock_length * 1000;
uint32_t idle_time_limit = settings.idle_time_limit * 1000;
uint32_t lock_time_out = settings.lock_time_out * 1000;
// If we are locked, and didn't time out yet, no key has to be pressed.
if (lock_start_time_ && (millis() - lock_start_time_ <= settings.lock_length))
if (lock_start_time_ && (millis() - lock_start_time_ <= lock_length))
return Key_NoKey;
// If we are locked...
if (lock_start_time_) {
// ...and the lock has not expired yet
if (millis() - lock_start_time_ <= settings.lock_length)
if (millis() - lock_start_time_ <= lock_length)
return Key_NoKey; // remain locked
// ...otherwise clear the lock
@ -66,7 +70,7 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k
// Any other case, we are not locked yet! (or we just unlocked)
// Are we still in the same session?
if (last_key_time_ && (millis() - last_key_time_) >= settings.idle_time_limit) {
if (last_key_time_ && (millis() - last_key_time_) >= idle_time_limit) {
// No, we are not. Clear timers and start over.
lock_start_time_ = 0;
left_hand_keys_ = right_hand_keys_ = 0;
@ -87,9 +91,9 @@ Key TypingBreaks::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t k
return Key_NoKey;
}
if (settings.lock_time_out) {
if (lock_time_out) {
// Is the session longer than lock_time_out?
if (millis() - session_start_time_ >= settings.lock_time_out) {
if (millis() - session_start_time_ >= lock_time_out) {
// Yeah, it is.
lock_start_time_ = millis();
TypingBreak(true);

@ -32,9 +32,9 @@ class TypingBreaks : public KaleidoscopePlugin {
static bool focusHook(const char *command);
typedef struct settings_t {
uint32_t idle_time_limit;
uint32_t lock_time_out;
uint32_t lock_length;
uint16_t idle_time_limit;
uint16_t lock_time_out;
uint16_t lock_length;
uint16_t left_hand_max_keys;
uint16_t right_hand_max_keys;
} settings_t;

Loading…
Cancel
Save