/* ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio. This file is part of ChibiOS. ChibiOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ChibiOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * @file chbsem.h * @brief Binary semaphores structures and macros. * * @addtogroup binary_semaphores * @details Binary semaphores related APIs and services. *

Operation mode

* Binary semaphores are implemented as a set of inline functions * that use the existing counting semaphores primitives. The * difference between counting and binary semaphores is that the * counter of binary semaphores is not allowed to grow above the * value 1. Repeated signal operation are ignored. A binary * semaphore can thus have only two defined states: * - Taken, when its counter has a value of zero or lower * than zero. A negative number represent the number of threads * queued on the binary semaphore. * - Not taken, when its counter has a value of one. * . * Binary semaphores are different from mutexes because there is no * concept of ownership, a binary semaphore can be taken by a * thread and signaled by another thread or an interrupt handler, * mutexes can only be taken and released by the same thread. Another * difference is that binary semaphores, unlike mutexes, do not * implement the priority inheritance protocol.
* In order to use the binary semaphores APIs the * @p CH_CFG_USE_SEMAPHORES option must be enabled in @p chconf.h. * @{ */ #ifndef CHBSEM_H #define CHBSEM_H #if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__) /*===========================================================================*/ /* Module constants. */ /*===========================================================================*/ /*===========================================================================*/ /* Module pre-compile time settings. */ /*===========================================================================*/ /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ /*===========================================================================*/ /* Module data structures and types. */ /*===========================================================================*/ /** * @extends semaphore_t * * @brief Binary semaphore type. */ typedef struct { semaphore_t sem; } binary_semaphore_t; /*===========================================================================*/ /* Module macros. */ /*===========================================================================*/ /** * @brief Data part of a static semaphore initializer. * @details This macro should be used when statically initializing a semaphore * that is part of a bigger structure. * * @param[in] name the name of the semaphore variable * @param[in] taken the semaphore initial state */ #define _BSEMAPHORE_DATA(name, taken) \ {_SEMAPHORE_DATA(name.sem, ((taken) ? 0 : 1))} /** * @brief Static semaphore initializer. * @details Statically initialized semaphores require no explicit * initialization using @p chBSemInit(). * * @param[in] name the name of the semaphore variable * @param[in] taken the semaphore initial state */ #define BSEMAPHORE_DECL(name, taken) \ binary_semaphore_t name = _BSEMAPHORE_DATA(name, taken) /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ /*===========================================================================*/ /* Module inline functions. */ /*===========================================================================*/ /** * @brief Initializes a binary semaphore. * * @param[out] bsp pointer to a @p binary_semaphore_t structure * @param[in] taken initial state of the binary semaphore: * - @a false, the initial state is not taken. * - @a true, the initial state is taken. * . * * @init */ static inline void chBSemObjectInit(binary_semaphore_t *bsp, bool taken) { chSemObjectInit(&bsp->sem, taken ? (cnt_t)0 : (cnt_t)1); } /** * @brief Wait operation on the binary semaphore. * * @param[in] bsp pointer to a @p binary_semaphore_t structure * @return
/* Copyright 2016 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef LEADER_ENABLE

#include "process_leader.h"

#ifndef LEADER_TIMEOUT
  #define LEADER_TIMEOUT 300
#endif

__attribute__ ((weak))
void leader_start(void) {}

__attribute__ ((weak))
void leader_end(void) {}

// Leader key stuff
bool leading = false;
uint16_t leader_time = 0;

uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
uint8_t leader_sequence_size = 0;

void qk_leader_start(void) {
  if (leading) { return; }
  leader_start();
  leading = true;
  leader_time = timer_read();
  leader_sequence_size = 0;
  leader_sequence[0] = 0;
  leader_sequence[1] = 0;
  leader_sequence[2] = 0;
  leader_sequence[3] = 0;
  leader_sequence[4] = 0;
}

bool process_leader(uint16_t keycode, keyrecord_t *record) {
  // Leader key set-up
  if (record->event.pressed) {
    if (leading) {
      if (timer_elapsed(leader_time) < LEADER_TIMEOUT) {
#ifndef LEADER_KEY_STRICT_KEY_PROCESSING
        if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
          keycode = keycode & 0xFF;
        }
#endif // LEADER_KEY_STRICT_KEY_PROCESSING
        leader_sequence[leader_sequence_size] = keycode;
        leader_sequence_size++;
#ifdef LEADER_PER_KEY_TIMING
        leader_time = timer_read();
#endif
        return false;
      }
    } else {
      if (keycode == KC_LEAD) {
        qk_leader_start();
      }
    }
  }
  return true;
}

#endif