1
1
#include "Button.h"
2
2
#include "Random.h"
3
+ #include "Configuration.h"
3
4
#include "Common.h"
4
5
#include "Settings.h"
5
6
6
- #define LONG_PRESS_TICK_COUNT 10
7
-
8
- static const char PROGMEM ButtonActionTable [][32 ] =
7
+ static const char PROGMEM ButtonActionTable [][BUTTON_NAME_MAX_LEN ] =
9
8
{
10
9
[BUTTON_ACTION_NONE ] = "CLOSED" ,
11
10
[BUTTON_ACTION_UID_RANDOM ] = "RANDOM_UID" ,
@@ -19,105 +18,109 @@ static const char PROGMEM ButtonActionTable[][32] =
19
18
20
19
void ButtonInit (void )
21
20
{
22
- BUTTON_PORT .DIRCLR = BUTTON_MASK ;
23
- BUTTON_PORT .BUTTON_PINCTRL = PORT_OPC_PULLUP_gc ;
21
+ BUTTON_PORT .DIRCLR = BUTTON_MASK ;
22
+ BUTTON_PORT .BUTTON_PINCTRL = PORT_OPC_PULLUP_gc ;
24
23
}
25
24
26
25
static void ExecuteButtonAction (ButtonActionEnum ButtonAction )
27
26
{
28
- uint8_t UidBuffer [32 ];
29
-
30
- if (ButtonAction == BUTTON_ACTION_UID_RANDOM ) {
31
-
32
- /* iceman, 2018, this random functionality could be more localized to the current cardtype in use.
33
- ie. for Ultralight based cards with 7byte uid, skip manufacturing byte
34
- */
35
-
36
- ApplicationGetUid (UidBuffer );
37
-
38
- /* skip manufacturing byte UID0 */
39
- for (uint8_t i = 1 ; i < ActiveConfiguration .UidSize - 1 ; i ++ ) {
40
- UidBuffer [i ] = RandomGetByte ();
41
- }
42
-
43
- ApplicationSetUid (UidBuffer );
44
- } else if (ButtonAction == BUTTON_ACTION_UID_LEFT_INCREMENT ) {
45
- ApplicationGetUid (UidBuffer );
46
- bool Carry = 1 ;
47
- uint8_t i ;
48
-
49
- for (i = 0 ; i < ActiveConfiguration .UidSize ; i ++ ) {
50
- if (Carry ) {
51
- if (UidBuffer [i ] == 0xFF ) {
52
- Carry = 1 ;
53
- } else {
54
- Carry = 0 ;
55
- }
56
-
57
- UidBuffer [i ] = (UidBuffer [i ] + 1 ) & 0xFF ;
58
- }
59
- }
60
-
61
- ApplicationSetUid (UidBuffer );
62
- } else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_INCREMENT ) {
63
- ApplicationGetUid (UidBuffer );
64
- bool Carry = 1 ;
65
- uint8_t i = ActiveConfiguration .UidSize ;
66
-
67
- while (i -- > 0 ) {
68
- if (Carry ) {
69
- if (UidBuffer [i ] == 0xFF ) {
70
- Carry = 1 ;
71
- } else {
72
- Carry = 0 ;
73
- }
74
-
75
- UidBuffer [i ] = (UidBuffer [i ] + 1 ) & 0xFF ;
76
- }
77
- }
78
-
79
- ApplicationSetUid (UidBuffer );
80
- } else if (ButtonAction == BUTTON_ACTION_UID_LEFT_DECREMENT ) {
81
- ApplicationGetUid (UidBuffer );
82
- bool Carry = 1 ;
83
- uint8_t i ;
84
-
85
- for (i = 0 ; i < ActiveConfiguration .UidSize ; i ++ ) {
86
- if (Carry ) {
87
- if (UidBuffer [i ] == 0x00 ) {
88
- Carry = 1 ;
89
- } else {
90
- Carry = 0 ;
91
- }
92
-
93
- UidBuffer [i ] = (UidBuffer [i ] - 1 ) & 0xFF ;
94
- }
95
- }
96
-
97
- ApplicationSetUid (UidBuffer );
98
- } else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_DECREMENT ) {
99
- ApplicationGetUid (UidBuffer );
100
- bool Carry = 1 ;
101
- uint8_t i = ActiveConfiguration .UidSize ;
102
-
103
- while (i -- > 0 ) {
104
- if (Carry ) {
105
- if (UidBuffer [i ] == 0x00 ) {
106
- Carry = 1 ;
107
- } else {
108
- Carry = 0 ;
109
- }
110
-
111
- UidBuffer [i ] = (UidBuffer [i ] - 1 ) & 0xFF ;
112
- }
113
- }
114
-
115
- ApplicationSetUid (UidBuffer );
116
- } else if (ButtonAction == BUTTON_ACTION_CYCLE_SETTINGS ) {
117
- SettingsCycle ();
118
- } else if (ButtonAction == BUTTON_ACTION_TOGGLE_READONLY ) {
119
- ActiveConfiguration .ReadOnly = !ActiveConfiguration .ReadOnly ;
120
- }
27
+ ConfigurationUidType UidBuffer ;
28
+
29
+ if (ButtonAction == BUTTON_ACTION_UID_RANDOM ) {
30
+ uint8_t startByte = 0 ;
31
+ ApplicationGetUid (UidBuffer );
32
+
33
+ #ifdef CONFIG_MF_ULTRALIGHT_SUPPORT
34
+ // Make RANDOM keep 1st byte safe for Ultralight types
35
+ ConfigurationEnum ActiveConfigurationId = GlobalSettings .ActiveSettingPtr -> Configuration ;
36
+ if ( (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT )
37
+ || (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT_EV1_80B )
38
+ || (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT_EV1_164B ) ) {
39
+ startByte = 1 ;
40
+ }
41
+ #endif
42
+ for ( ; startByte < ActiveConfiguration .UidSize ; startByte ++ ) {
43
+ UidBuffer [startByte ] = RandomGetByte ();
44
+ }
45
+
46
+ ApplicationSetUid (UidBuffer );
47
+ } else if (ButtonAction == BUTTON_ACTION_UID_LEFT_INCREMENT ) {
48
+ ApplicationGetUid (UidBuffer );
49
+ bool Carry = 1 ;
50
+ uint8_t i ;
51
+
52
+ for (i = 0 ; i < ActiveConfiguration .UidSize ; i ++ ) {
53
+ if (Carry ) {
54
+ if (UidBuffer [i ] == 0xFF ) {
55
+ Carry = 1 ;
56
+ } else {
57
+ Carry = 0 ;
58
+ }
59
+
60
+ UidBuffer [i ] = (UidBuffer [i ] + 1 ) & 0xFF ;
61
+ }
62
+ }
63
+
64
+ ApplicationSetUid (UidBuffer );
65
+ } else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_INCREMENT ) {
66
+ ApplicationGetUid (UidBuffer );
67
+ bool Carry = 1 ;
68
+ uint8_t i = ActiveConfiguration .UidSize ;
69
+
70
+ while (i -- > 0 ) {
71
+ if (Carry ) {
72
+ if (UidBuffer [i ] == 0xFF ) {
73
+ Carry = 1 ;
74
+ } else {
75
+ Carry = 0 ;
76
+ }
77
+
78
+ UidBuffer [i ] = (UidBuffer [i ] + 1 ) & 0xFF ;
79
+ }
80
+ }
81
+
82
+ ApplicationSetUid (UidBuffer );
83
+ } else if (ButtonAction == BUTTON_ACTION_UID_LEFT_DECREMENT ) {
84
+ ApplicationGetUid (UidBuffer );
85
+ bool Carry = 1 ;
86
+ uint8_t i ;
87
+
88
+ for (i = 0 ; i < ActiveConfiguration .UidSize ; i ++ ) {
89
+ if (Carry ) {
90
+ if (UidBuffer [i ] == 0x00 ) {
91
+ Carry = 1 ;
92
+ } else {
93
+ Carry = 0 ;
94
+ }
95
+
96
+ UidBuffer [i ] = (UidBuffer [i ] - 1 ) & 0xFF ;
97
+ }
98
+ }
99
+
100
+ ApplicationSetUid (UidBuffer );
101
+ } else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_DECREMENT ) {
102
+ ApplicationGetUid (UidBuffer );
103
+ bool Carry = 1 ;
104
+ uint8_t i = ActiveConfiguration .UidSize ;
105
+
106
+ while (i -- > 0 ) {
107
+ if (Carry ) {
108
+ if (UidBuffer [i ] == 0x00 ) {
109
+ Carry = 1 ;
110
+ } else {
111
+ Carry = 0 ;
112
+ }
113
+
114
+ UidBuffer [i ] = (UidBuffer [i ] - 1 ) & 0xFF ;
115
+ }
116
+ }
117
+
118
+ ApplicationSetUid (UidBuffer );
119
+ } else if (ButtonAction == BUTTON_ACTION_CYCLE_SETTINGS ) {
120
+ SettingsCycle ();
121
+ } else if (ButtonAction == BUTTON_ACTION_TOGGLE_READONLY ) {
122
+ ActiveConfiguration .ReadOnly = !ActiveConfiguration .ReadOnly ;
123
+ }
121
124
}
122
125
123
126
void ButtonTick (void )
@@ -130,28 +133,28 @@ void ButtonTick(void)
130
133
//LastButtonState = ThisButtonState;
131
134
132
135
if (ThisButtonState & BUTTON_MASK ) {
133
- /* Button is currently pressed */
134
- if (PressTickCounter < LONG_PRESS_TICK_COUNT ) {
135
- /* Count ticks while button is being pressed */
136
- PressTickCounter ++ ;
137
- } else if (PressTickCounter == LONG_PRESS_TICK_COUNT ) {
138
- /* Long button press detected execute button action and advance PressTickCounter
139
- * to an invalid state. */
140
- ExecuteButtonAction (GlobalSettings .ActiveSettingPtr -> ButtonLongAction );
141
- PressTickCounter ++ ;
142
- } else {
143
- /* Button is still pressed, ignore */
144
- }
136
+ /* Button is currently pressed */
137
+ if (PressTickCounter < BUTTON_LONG_PRESS_TICK_COUNT ) {
138
+ /* Count ticks while button is being pressed */
139
+ PressTickCounter ++ ;
140
+ } else if (PressTickCounter == BUTTON_LONG_PRESS_TICK_COUNT ) {
141
+ /* Long button press detected execute button action and advance PressTickCounter
142
+ * to an invalid state. */
143
+ ExecuteButtonAction (GlobalSettings .ActiveSettingPtr -> ButtonLongAction );
144
+ PressTickCounter ++ ;
145
+ } else {
146
+ /* Button is still pressed, ignore */
147
+ }
145
148
} else if (!(ThisButtonState & BUTTON_MASK )) {
146
- /* Button is currently not being pressed. Check if PressTickCounter contains
147
- * a recent short button press. */
148
- if ( (PressTickCounter > 0 ) && (PressTickCounter <= LONG_PRESS_TICK_COUNT ) ) {
149
- /* We have a short button press */
150
- ExecuteButtonAction (GlobalSettings .ActiveSettingPtr -> ButtonAction );
151
- }
152
-
153
- PressTickCounter = 0 ;
154
- }
149
+ /* Button is currently not being pressed. Check if PressTickCounter contains
150
+ * a recent short button press. */
151
+ if ( (PressTickCounter > 0 ) && (PressTickCounter <= BUTTON_LONG_PRESS_TICK_COUNT ) ) {
152
+ /* We have a short button press */
153
+ ExecuteButtonAction (GlobalSettings .ActiveSettingPtr -> ButtonAction );
154
+ }
155
+
156
+ PressTickCounter = 0 ;
157
+ }
155
158
}
156
159
157
160
void ButtonGetActionList (char * ListOut , uint16_t BufferSize )
@@ -185,47 +188,47 @@ void ButtonGetActionList(char* ListOut, uint16_t BufferSize)
185
188
186
189
void ButtonSetActionById (ButtonTypeEnum Type , ButtonActionEnum Action )
187
190
{
188
- #ifndef BUTTON_SETTING_GLOBAL
189
- if (Type == BUTTON_PRESS_SHORT ) {
190
- GlobalSettings .ActiveSettingPtr -> ButtonAction = Action ;
191
- } else if (Type == BUTTON_PRESS_LONG ) {
192
- GlobalSettings .ActiveSettingPtr -> ButtonLongAction = Action ;
193
- }
194
- #else
195
- /* Write button action to all settings when using global settings */
196
- for (uint8_t i = 0 ; i < SETTINGS_COUNT ; i ++ ) {
197
- if (Type == BUTTON_PRESS_SHORT ) {
198
- GlobalSettings .Settings [i ].ButtonAction = Action ;
199
- } else if (Type == BUTTON_PRESS_LONG ) {
200
- GlobalSettings .Settings [i ].ButtonLongAction = Action ;
201
- }
202
- }
203
- #endif
191
+ #ifndef BUTTON_SETTING_GLOBAL
192
+ if (Type == BUTTON_PRESS_SHORT ) {
193
+ GlobalSettings .ActiveSettingPtr -> ButtonAction = Action ;
194
+ } else if (Type == BUTTON_PRESS_LONG ) {
195
+ GlobalSettings .ActiveSettingPtr -> ButtonLongAction = Action ;
196
+ }
197
+ #else
198
+ /* Write button action to all settings when using global settings */
199
+ for (uint8_t i = 0 ; i < SETTINGS_COUNT ; i ++ ) {
200
+ if (Type == BUTTON_PRESS_SHORT ) {
201
+ GlobalSettings .Settings [i ].ButtonAction = Action ;
202
+ } else if (Type == BUTTON_PRESS_LONG ) {
203
+ GlobalSettings .Settings [i ].ButtonLongAction = Action ;
204
+ }
205
+ }
206
+ #endif
204
207
}
205
208
206
209
void ButtonGetActionByName (ButtonTypeEnum Type , char * ActionOut , uint16_t BufferSize )
207
210
{
208
- if (Type == BUTTON_PRESS_SHORT ) {
209
- strncpy_P (ActionOut , ButtonActionTable [GlobalSettings .ActiveSettingPtr -> ButtonAction ], BufferSize );
210
- } else if (Type == BUTTON_PRESS_LONG ) {
211
- strncpy_P (ActionOut , ButtonActionTable [GlobalSettings .ActiveSettingPtr -> ButtonLongAction ], BufferSize );
212
- } else {
213
- /* Should not happen (TM) */
214
- * ActionOut = '\0' ;
215
- }
211
+ if (Type == BUTTON_PRESS_SHORT ) {
212
+ strncpy_P (ActionOut , ButtonActionTable [GlobalSettings .ActiveSettingPtr -> ButtonAction ], BufferSize );
213
+ } else if (Type == BUTTON_PRESS_LONG ) {
214
+ strncpy_P (ActionOut , ButtonActionTable [GlobalSettings .ActiveSettingPtr -> ButtonLongAction ], BufferSize );
215
+ } else {
216
+ /* Should not happen (TM) */
217
+ * ActionOut = '\0' ;
218
+ }
216
219
}
217
220
218
221
bool ButtonSetActionByName (ButtonTypeEnum Type , const char * Action )
219
222
{
220
- uint8_t i ;
223
+ uint8_t i ;
221
224
222
- for (i = 0 ; i < BUTTON_ACTION_COUNT ; i ++ ) {
223
- if (strcmp_P (Action , ButtonActionTable [i ]) == 0 ) {
224
- ButtonSetActionById (Type , i );
225
- return true;
226
- }
227
- }
225
+ for (i = 0 ; i < BUTTON_ACTION_COUNT ; i ++ ) {
226
+ if (strcmp_P (Action , ButtonActionTable [i ]) == 0 ) {
227
+ ButtonSetActionById (Type , i );
228
+ return true;
229
+ }
230
+ }
228
231
229
- /* Button action not found */
230
- return false;
232
+ /* Button action not found */
233
+ return false;
231
234
}
0 commit comments