Fix #4881 (#4884)
[opentx.git] / radio / src / haptic.cpp
blob4866f188767d09a7b237f904203bc72f17c577e9
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(PCBSKY9X) || defined(PCBX9DP) || defined(PCBX9E) || defined(PCBFLAMENCO) || defined(PCBHORUS)
42 // TODO define HAPTIC_PWM option
43 hapticOn(HAPTIC_STRENGTH() * 20);
44 #else
45 if (hapticTick-- > 0) {
46 hapticOn();
48 else {
49 hapticOff();
50 hapticTick = HAPTIC_STRENGTH();
52 #endif
54 else {
55 hapticOff();
56 if (buzzPause > 0) {
57 buzzPause--;
59 else if (t_queueRidx != t_queueWidx) {
60 buzzTimeLeft = queueHapticLength[t_queueRidx];
61 buzzPause = queueHapticPause[t_queueRidx];
62 if (!queueHapticRepeat[t_queueRidx]--) {
63 t_queueRidx = (t_queueRidx + 1) & (HAPTIC_QUEUE_LENGTH-1);
67 #endif // defined(SIMU)
70 void hapticQueue::play(uint8_t tLen, uint8_t tPause, uint8_t tFlags)
72 tLen = getHapticLength(tLen);
74 if ((tFlags & PLAY_NOW) || (!busy() && empty())) {
75 buzzTimeLeft = tLen;
76 buzzPause = tPause;
77 t_queueWidx = t_queueRidx;
79 else {
80 tFlags += 1;
83 tFlags &= 0x0f;
84 if (tFlags) {
85 uint8_t next_queueWidx = (t_queueWidx + 1) & (HAPTIC_QUEUE_LENGTH-1);
86 if (next_queueWidx != t_queueRidx) {
87 queueHapticLength[t_queueWidx] = tLen;
88 queueHapticPause[t_queueWidx] = tPause;
89 queueHapticRepeat[t_queueWidx] = tFlags-1;
90 t_queueWidx = next_queueWidx;
95 void hapticQueue::event(uint8_t e)
97 if (g_eeGeneral.hapticMode >= e_mode_nokeys || (g_eeGeneral.hapticMode >= e_mode_alarms && e <= AU_ERROR)) {
98 if (e <= AU_ERROR)
99 play(15, 3, PLAY_NOW);
100 else if (e <= AU_MIX_WARNING_3)
101 play(15, 3, PLAY_NOW);
102 else if (e >= AU_SPECIAL_SOUND_LAST && empty()) {
103 play(30, 10, 0);
104 play(10, 50-10*(e-AU_SPECIAL_SOUND_LAST), (e-AU_SPECIAL_SOUND_LAST));
109 hapticQueue haptic;