Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / haptic.cpp
blob760637ec962ad3a20af7b80967ea7a589666d4bf
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 hapticQueue::hapticQueue()
25 buzzTimeLeft = 0;
26 buzzPause = 0;
28 t_queueRidx = 0;
29 t_queueWidx = 0;
31 hapticTick = 0;
34 void hapticQueue::heartbeat()
36 #if defined(SIMU)
37 return;
38 #else
39 if (buzzTimeLeft > 0) {
40 buzzTimeLeft--; // time gets counted down
41 #if defined(HAPTIC_PWM)
42 hapticOn(HAPTIC_STRENGTH() * 20);
43 #else
44 if (hapticTick-- > 0) {
45 hapticOn();
47 else {
48 hapticOff();
49 hapticTick = HAPTIC_STRENGTH();
51 #endif
53 else {
54 hapticOff();
55 if (buzzPause > 0) {
56 buzzPause--;
58 else if (t_queueRidx != t_queueWidx) {
59 buzzTimeLeft = queueHapticLength[t_queueRidx];
60 buzzPause = queueHapticPause[t_queueRidx];
61 if (!queueHapticRepeat[t_queueRidx]--) {
62 t_queueRidx = (t_queueRidx + 1) & (HAPTIC_QUEUE_LENGTH-1);
66 #endif // defined(SIMU)
69 void hapticQueue::play(uint8_t tLen, uint8_t tPause, uint8_t tFlags)
71 tLen = getHapticLength(tLen);
73 if ((tFlags & PLAY_NOW) || (!busy() && empty())) {
74 buzzTimeLeft = tLen;
75 buzzPause = tPause;
76 t_queueWidx = t_queueRidx;
78 else {
79 tFlags += 1;
82 tFlags &= 0x0f;
83 if (tFlags) {
84 uint8_t next_queueWidx = (t_queueWidx + 1) & (HAPTIC_QUEUE_LENGTH-1);
85 if (next_queueWidx != t_queueRidx) {
86 queueHapticLength[t_queueWidx] = tLen;
87 queueHapticPause[t_queueWidx] = tPause;
88 queueHapticRepeat[t_queueWidx] = tFlags-1;
89 t_queueWidx = next_queueWidx;
94 void hapticQueue::event(uint8_t e)
96 if (g_eeGeneral.hapticMode >= e_mode_nokeys || (g_eeGeneral.hapticMode >= e_mode_alarms && e <= AU_ERROR)) {
97 if (e <= AU_ERROR)
98 play(15, 3, PLAY_NOW);
99 else if (e <= AU_MIX_WARNING_3)
100 play(15, 3, PLAY_NOW);
101 else if (e >= AU_SPECIAL_SOUND_LAST && empty()) {
102 play(30, 10, 0);
103 play(10, 50-10*(e-AU_SPECIAL_SOUND_LAST), (e-AU_SPECIAL_SOUND_LAST));
108 hapticQueue haptic;