1 #ifdef HAS_FIVE_WAY_BUTTON
2 #include "FiveWayButton.h"
4 #define KEY_DEBOUNCE_TIME 500
5 #define KEY_LONG_RPESS_TIME 1000
6 #define KEY_CHECK_OVER_TIME 2000
9 uint32_t key_debounce_start
= 0;
11 int key
= INPUT_KEY_NO_PRESS
;
14 #ifdef GPIO_PIN_JOYSTICK
22 static int16_t joyAdcValues
[] = JOY_ADC_VALUES
;
24 // Returns minimum difference between any pair
25 int findMinDiff(int16_t arr
[], int n
)
27 // Initialize difference as infinite
30 // Find the min diff by comparing difference
31 // of all possible pairs in given array
32 for (int i
=0; i
<n
-1; i
++)
33 for (int j
=i
+1; j
<n
; j
++)
34 if (abs(arr
[i
] - arr
[j
]) < diff
)
35 diff
= abs(arr
[i
] - arr
[j
]);
41 bool checkValue(int direction
){
42 int value
= analogRead(GPIO_PIN_JOYSTICK
);
43 if(value
< (joyAdcValues
[direction
] + 50) && value
> (joyAdcValues
[direction
] - 50)){
52 int fuzz
= (findMinDiff(joyAdcValues
, 6) /2 );
53 int value
= analogRead(GPIO_PIN_JOYSTICK
);
55 if(value
< (joyAdcValues
[0] + fuzz
) && value
> (joyAdcValues
[0] - fuzz
))
56 return INPUT_KEY_UP_PRESS
;
57 else if(value
< (joyAdcValues
[1] + fuzz
) && value
> (joyAdcValues
[1] - fuzz
))
58 return INPUT_KEY_DOWN_PRESS
;
59 else if(value
< (joyAdcValues
[2] + fuzz
) && value
> (joyAdcValues
[2] - fuzz
))
60 return INPUT_KEY_LEFT_PRESS
;
61 else if(value
< (joyAdcValues
[3] + fuzz
) && value
> (joyAdcValues
[3] - fuzz
))
62 return INPUT_KEY_RIGHT_PRESS
;
63 else if(value
< (joyAdcValues
[4] + fuzz
) && value
> (joyAdcValues
[4] - fuzz
))
64 return INPUT_KEY_OK_PRESS
;
66 return INPUT_KEY_NO_PRESS
;
70 void FiveWayButton::init()
72 #ifndef JOY_ADC_VALUES
73 pinMode(GPIO_PIN_FIVE_WAY_INPUT1
, INPUT
|PULLUP
);
74 pinMode(GPIO_PIN_FIVE_WAY_INPUT2
, INPUT
|PULLUP
);
75 pinMode(GPIO_PIN_FIVE_WAY_INPUT3
, INPUT
|PULLUP
);
78 key_state
= INPUT_KEY_NO_PRESS
;
80 isLongPressed
= false;
83 void FiveWayButton::handle()
87 #ifndef JOY_ADC_VALUES
88 int key_down
= digitalRead(GPIO_PIN_FIVE_WAY_INPUT1
) << 2 | digitalRead(GPIO_PIN_FIVE_WAY_INPUT2
) << 1 | digitalRead(GPIO_PIN_FIVE_WAY_INPUT3
);
90 // Hack, we know that INPUT_KEY_NO_PRESS = 7 so for analog we would have to have it equal idle
91 int key_down
= checkKey();
95 if(key_down
== INPUT_KEY_NO_PRESS
)
105 if((millis() - key_debounce_start
) > KEY_DEBOUNCE_TIME
)
113 if((millis() - key_debounce_start
) > KEY_LONG_RPESS_TIME
)
115 isLongPressed
= true;
121 #ifndef JOY_ADC_VALUES
122 key
= digitalRead(GPIO_PIN_FIVE_WAY_INPUT1
) << 2 | digitalRead(GPIO_PIN_FIVE_WAY_INPUT2
) << 1 | digitalRead(GPIO_PIN_FIVE_WAY_INPUT3
);
127 if(key
!= INPUT_KEY_NO_PRESS
)
130 isLongPressed
= false;
131 key_debounce_start
= millis();
136 void FiveWayButton::getKeyState(int *keyValue
, bool *keyLongPressed
)
138 *keyValue
= key_state
;
139 *keyLongPressed
= isLongPressed
;
140 if(key_state
!= INPUT_KEY_NO_PRESS
)
146 void FiveWayButton::clearKeyState()
149 isLongPressed
= false;
150 key_state
= INPUT_KEY_NO_PRESS
;