Files
edgetx-gx12/radio/src/targets/Horus/keys_driver.cpp
T
2015-10-14 23:47:27 +02:00

242 lines
6.5 KiB
C++

/*
* Authors (alphabetical order)
* - Andre Bernet <bernet.andre@gmail.com>
* - Andreas Weitl
* - Bertrand Songis <bsongis@gmail.com>
* - Bryan J. Rentoul (Gruvin) <gruvin@gmail.com>
* - Cameron Weeks <th9xer@gmail.com>
* - Erez Raviv
* - Gabriel Birkus
* - Jean-Pierre Parisy
* - Karl Szmutny
* - Michael Blandford
* - Michal Hlavinka
* - Pat Mackenzie
* - Philip Moss
* - Rob Thomson
* - Romolo Manfredini <romolo.manfredini@gmail.com>
* - Thomas Husterer
*
* opentx is based on code named
* gruvin9x by Bryan J. Rentoul: http://code.google.com/p/gruvin9x/,
* er9x by Erez Raviv: http://code.google.com/p/er9x/,
* and the original (and ongoing) project by
* Thomas Husterer, th9x: http://code.google.com/p/th9x/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*
*/
#include "../../opentx.h"
uint32_t readKeys()
{
uint32_t result = 0;
if (~KEYS_GPIO_REG_MENU & KEYS_GPIO_PIN_MENU)
result |= 1 << KEY_MENU;
if (~KEYS_GPIO_REG_EXIT & KEYS_GPIO_PIN_EXIT)
result |= 1 << KEY_EXIT;
if (~KEYS_GPIO_REG_ENTER & KEYS_GPIO_PIN_ENTER)
result |= 1 << KEY_ENTER;
if (~KEYS_GPIO_REG_UP & KEYS_GPIO_PIN_UP)
result |= 1 << KEY_UP;
if (~KEYS_GPIO_REG_DOWN & KEYS_GPIO_PIN_DOWN)
result |= 1 << KEY_DOWN;
if (~KEYS_GPIO_REG_RIGHT & KEYS_GPIO_PIN_RIGHT)
result |= 1 << KEY_RIGHT;
if (~KEYS_GPIO_REG_LEFT & KEYS_GPIO_PIN_LEFT)
result |= 1 << KEY_LEFT;
// TRACE("readKeys(): %x", result);
return result;
}
uint32_t readTrims()
{
uint32_t result = 0;
if (~TRIMS_GPIO_REG_LHL & TRIMS_GPIO_PIN_LHL)
result |= 0x01;
if (~TRIMS_GPIO_REG_LHR & TRIMS_GPIO_PIN_LHR)
result |= 0x02;
if (~TRIMS_GPIO_REG_LVD & TRIMS_GPIO_PIN_LVD)
result |= 0x04;
if (~TRIMS_GPIO_REG_LVU & TRIMS_GPIO_PIN_LVU)
result |= 0x08;
if (~TRIMS_GPIO_REG_RVD & TRIMS_GPIO_PIN_RVD)
result |= 0x10;
if (~TRIMS_GPIO_REG_RVU & TRIMS_GPIO_PIN_RVU)
result |= 0x20;
if (~TRIMS_GPIO_REG_RHL & TRIMS_GPIO_PIN_RHL)
result |= 0x40;
if (~TRIMS_GPIO_REG_RHR & TRIMS_GPIO_PIN_RHR)
result |= 0x80;
#if 0
if (~TRIMS_GPIO_REG_LSD & TRIMS_GPIO_PIN_LSD)
result |= 0x100;
if (~TRIMS_GPIO_REG_LSU & TRIMS_GPIO_PIN_LSU)
result |= 0x200;
if (~TRIMS_GPIO_REG_RSD & TRIMS_GPIO_PIN_RSD)
result |= 0x400;
if (~TRIMS_GPIO_REG_RSU & TRIMS_GPIO_PIN_RSU)
result |= 0x800;
#endif
// TRACE("readTrims(): result=0x%04x", result);
return result;
}
uint8_t trimDown(uint8_t idx)
{
return readTrims() & (1 << idx);
}
uint8_t keyDown()
{
return readKeys();
}
rotenc_t rotencValue;
void checkRotaryEncoder()
{
static uint32_t Rotary_position;
register uint32_t dummy ;
dummy = ENC_GPIO->IDR ; // Read Rotary encode
dummy >>= 10; // quick & dirty!
dummy &= 0x03 ; // pick out the two bits
if ( dummy != ( Rotary_position & 0x03 ) ) {
if ( ( Rotary_position & 0x01 ) ^ ( ( dummy & 0x02) >> 1 ) ) {
--rotencValue;
}
else {
++rotencValue;
}
Rotary_position &= ~0x03 ;
Rotary_position |= dummy ;
}
}
/* TODO common to ARM */
void readKeysAndTrims()
{
register uint32_t i;
uint8_t enuk = 0;
uint32_t in = readKeys();
for (i = 0; i < TRM_BASE; i++) {
keys[enuk++].input(in & (1 << i));
}
static rotenc_t rePreviousValue;
rotenc_t reNewValue = (rotencValue / 2);
int8_t scrollRE = reNewValue - rePreviousValue;
if (scrollRE) {
rePreviousValue = reNewValue;
if (scrollRE < 0) {
putEvent(EVT_ROTARY_LEFT);
}
else {
putEvent(EVT_ROTARY_RIGHT);
}
}
in = readTrims();
for (i = 1; i < 4096; i <<= 1) {
keys[enuk++].input(in & i);
}
}
#define ADD_2POS_CASE(x) \
case SW_S ## x ## 0: \
xxx = SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
break; \
case SW_S ## x ## 2: \
xxx = ~SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
break
#define ADD_3POS_CASE(x, i) \
case SW_S ## x ## 0: \
xxx = (SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H); \
if (IS_3POS(i)) { \
xxx = xxx && (~SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
} \
break; \
case SW_S ## x ## 1: \
xxx = (SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H) && (SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
break; \
case SW_S ## x ## 2: \
xxx = (~SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H); \
if (IS_3POS(i)) { \
xxx = xxx && (SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
} \
break
#if !defined(BOOT)
bool switchState(EnumKeys enuk)
{
register uint32_t xxx = 0;
if (enuk < (int) DIM(keys)) return keys[enuk].state() ? 1 : 0;
switch ((uint8_t) enuk) {
ADD_3POS_CASE(A, 0);
ADD_3POS_CASE(B, 1);
ADD_3POS_CASE(C, 2);
ADD_3POS_CASE(D, 3);
ADD_3POS_CASE(E, 4);
ADD_2POS_CASE(F);
ADD_3POS_CASE(G, 6);
ADD_2POS_CASE(H);
default:
break;
}
// TRACE("switch %d => %d", enuk, xxx);
return xxx;
}
#endif
void keysInit()
{
RCC_AHB1PeriphClockCmd(KEYS_RCC_AHB1Periph_GPIO, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOB_PINS;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOC_PINS;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOD_PINS;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOE_PINS;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOG_PINS;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOH_PINS;
GPIO_Init(GPIOH, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOI_PINS;
GPIO_Init(GPIOI, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = KEYS_GPIOJ_PINS;
GPIO_Init(GPIOJ, &GPIO_InitStructure);
}