Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / targets / taranis / keys_driver.cpp
blobccaf607605b9ff020f37b68f8516d04dc116b910
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "opentx.h"
23 uint32_t readKeys()
25 uint32_t result = 0;
27 if (~KEYS_GPIO_REG_ENTER & KEYS_GPIO_PIN_ENTER)
28 result |= 1 << KEY_ENTER;
30 #if defined(KEYS_GPIO_PIN_MENU)
31 if (~KEYS_GPIO_REG_MENU & KEYS_GPIO_PIN_MENU)
32 result |= 1 << KEY_MENU;
33 #endif
35 #if defined(KEYS_GPIO_PIN_PAGE)
36 if (~KEYS_GPIO_REG_PAGE & KEYS_GPIO_PIN_PAGE)
37 result |= 1 << KEY_PAGE;
38 #endif
40 if (~KEYS_GPIO_REG_EXIT & KEYS_GPIO_PIN_EXIT)
41 result |= 1 << KEY_EXIT;
43 #if defined(KEYS_GPIO_PIN_PLUS)
44 if (~KEYS_GPIO_REG_PLUS & KEYS_GPIO_PIN_PLUS)
45 result |= 1 << KEY_PLUS;
46 if (~KEYS_GPIO_REG_MINUS & KEYS_GPIO_PIN_MINUS)
47 result |= 1 << KEY_MINUS;
48 #endif
50 #if defined(KEYS_GPIO_PIN_LEFT)
51 if (~KEYS_GPIO_REG_LEFT & KEYS_GPIO_PIN_LEFT)
52 result |= 1 << KEY_LEFT;
53 if (~KEYS_GPIO_REG_RIGHT & KEYS_GPIO_PIN_RIGHT)
54 result |= 1 << KEY_RIGHT;
55 if (~KEYS_GPIO_REG_UP & KEYS_GPIO_PIN_UP)
56 result |= 1 << KEY_UP;
57 if (~KEYS_GPIO_REG_DOWN & KEYS_GPIO_PIN_DOWN)
58 result |= 1 << KEY_DOWN;
59 #endif
61 #if defined(KEYS_GPIO_PIN_SHIFT)
62 if (~KEYS_GPIO_REG_SHIFT & KEYS_GPIO_PIN_SHIFT)
63 result |= 1 << KEY_SHIFT;
64 #endif
66 // if (result != 0) TRACE("readKeys(): result=0x%02x", result);
68 return result;
71 uint32_t readTrims()
73 uint32_t result = 0;
75 if (~TRIMS_GPIO_REG_LHL & TRIMS_GPIO_PIN_LHL)
76 result |= 0x01;
77 if (~TRIMS_GPIO_REG_LHR & TRIMS_GPIO_PIN_LHR)
78 result |= 0x02;
79 if (~TRIMS_GPIO_REG_LVD & TRIMS_GPIO_PIN_LVD)
80 result |= 0x04;
81 if (~TRIMS_GPIO_REG_LVU & TRIMS_GPIO_PIN_LVU)
82 result |= 0x08;
84 #if defined(PCBXLITE)
85 if (IS_SHIFT_PRESSED())
86 result = ((result & 0x03) << 6) | ((result & 0x0c) << 2);
87 #else
88 if (~TRIMS_GPIO_REG_RVD & TRIMS_GPIO_PIN_RVD)
89 result |= 0x10;
90 if (~TRIMS_GPIO_REG_RVU & TRIMS_GPIO_PIN_RVU)
91 result |= 0x20;
92 if (~TRIMS_GPIO_REG_RHL & TRIMS_GPIO_PIN_RHL)
93 result |= 0x40;
94 if (~TRIMS_GPIO_REG_RHR & TRIMS_GPIO_PIN_RHR)
95 result |= 0x80;
96 #endif
98 // TRACE("readTrims(): result=0x%02x", result);
100 return result;
103 bool trimDown(uint8_t idx)
105 return readTrims() & (1 << idx);
108 bool keyDown()
110 return readKeys() || readTrims();
113 /* TODO common to ARM */
114 void readKeysAndTrims()
116 uint8_t index = 0;
117 uint32_t keys_input = readKeys();
118 for (uint8_t i = 1; i != uint8_t(1 << TRM_BASE); i <<= 1) {
119 keys[index++].input(keys_input & i);
122 uint32_t trims_input = readTrims();
123 for (uint8_t i = 1; i != uint8_t(1 << 8); i <<= 1) {
124 keys[index++].input(trims_input & i);
127 #if defined(PWR_BUTTON_PRESS)
128 if ((keys_input || trims_input || pwrPressed()) && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) {
129 #else
130 if ((keys_input || trims_input) && (g_eeGeneral.backlightMode & e_backlight_mode_keys)) {
131 #endif
132 // on keypress turn the light on
133 backlightOn();
137 #if defined(PCBX9E)
138 #define ADD_2POS_CASE(x) \
139 case SW_S ## x ## 2: \
140 xxx = SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
141 break; \
142 case SW_S ## x ## 0: \
143 xxx = ~SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
144 break
145 #else
146 #define ADD_2POS_CASE(x) \
147 case SW_S ## x ## 0: \
148 xxx = SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
149 break; \
150 case SW_S ## x ## 2: \
151 xxx = ~SWITCHES_GPIO_REG_ ## x & SWITCHES_GPIO_PIN_ ## x ; \
152 break
153 #endif
155 #define ADD_3POS_CASE(x, i) \
156 case SW_S ## x ## 0: \
157 xxx = (SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H); \
158 if (IS_CONFIG_3POS(i)) { \
159 xxx = xxx && (~SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
161 break; \
162 case SW_S ## x ## 1: \
163 xxx = (SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H) && (SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
164 break; \
165 case SW_S ## x ## 2: \
166 xxx = (~SWITCHES_GPIO_REG_ ## x ## _H & SWITCHES_GPIO_PIN_ ## x ## _H); \
167 if (IS_CONFIG_3POS(i)) { \
168 xxx = xxx && (SWITCHES_GPIO_REG_ ## x ## _L & SWITCHES_GPIO_PIN_ ## x ## _L); \
170 break
172 #if !defined(BOOT)
173 uint32_t switchState(uint8_t index)
175 uint32_t xxx = 0;
177 switch (index) {
178 ADD_3POS_CASE(A, 0);
179 ADD_3POS_CASE(B, 1);
180 ADD_3POS_CASE(C, 2);
182 #if defined(PCBX9LITES)
183 ADD_2POS_CASE(D);
184 ADD_2POS_CASE(E);
185 ADD_2POS_CASE(F);
186 ADD_2POS_CASE(G);
187 #elif defined(PCBX9LITE)
188 ADD_2POS_CASE(D);
189 ADD_2POS_CASE(E);
190 #elif defined(PCBXLITES)
191 ADD_3POS_CASE(D, 3);
192 ADD_2POS_CASE(E);
193 ADD_2POS_CASE(F);
194 // no SWG and SWH on XLITES
195 #elif defined(PCBXLITE)
196 ADD_3POS_CASE(D, 3);
197 // no SWE, SWF, SWG and SWH on XLITE
198 #elif defined(PCBX7)
199 ADD_3POS_CASE(D, 3);
200 ADD_2POS_CASE(F);
201 ADD_2POS_CASE(H);
202 ADD_2POS_CASE(I);
203 ADD_2POS_CASE(J);
204 #else
205 ADD_3POS_CASE(D, 3);
206 ADD_3POS_CASE(E, 4);
207 ADD_2POS_CASE(F);
208 ADD_3POS_CASE(G, 6);
209 ADD_2POS_CASE(H);
210 #endif
212 #if defined(PCBX9DP) && PCBREV >= 2019
213 ADD_2POS_CASE(I);
214 #endif
216 #if defined(PCBX9E)
217 ADD_3POS_CASE(I, 8);
218 ADD_3POS_CASE(J, 9);
219 ADD_3POS_CASE(K, 10);
220 ADD_3POS_CASE(L, 11);
221 ADD_3POS_CASE(M, 12);
222 ADD_3POS_CASE(N, 13);
223 ADD_3POS_CASE(O, 14);
224 ADD_3POS_CASE(P, 15);
225 ADD_3POS_CASE(Q, 16);
226 ADD_3POS_CASE(R, 17);
227 #endif
229 default:
230 break;
233 // TRACE("switch %d => %d", index, xxx);
234 return xxx;
236 #endif
238 void keysInit()
240 GPIO_InitTypeDef GPIO_InitStructure;
241 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
242 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
243 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
244 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
246 #if defined(KEYS_GPIOA_PINS)
247 INIT_KEYS_PINS(GPIOA);
248 #endif
250 #if defined(KEYS_GPIOB_PINS)
251 INIT_KEYS_PINS(GPIOB);
252 #endif
254 #if defined(KEYS_GPIOC_PINS)
255 INIT_KEYS_PINS(GPIOC);
256 #endif
258 #if defined(KEYS_GPIOD_PINS)
259 INIT_KEYS_PINS(GPIOD);
260 #endif
262 #if defined(KEYS_GPIOE_PINS)
263 INIT_KEYS_PINS(GPIOE);
264 #endif
266 #if defined(KEYS_GPIOF_PINS)
267 INIT_KEYS_PINS(GPIOF);
268 #endif
270 #if defined(KEYS_GPIOG_PINS)
271 INIT_KEYS_PINS(GPIOG);
272 #endif