Skip to content

Commit f463cd6

Browse files
authored
Merge pull request #166 from shinhub/button_random_fix
Button random fix
2 parents 4a54102 + 4978115 commit f463cd6

File tree

2 files changed

+174
-168
lines changed

2 files changed

+174
-168
lines changed

Firmware/ChameleonMini/Button.c

+155-152
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include "Button.h"
22
#include "Random.h"
3+
#include "Configuration.h"
34
#include "Common.h"
45
#include "Settings.h"
56

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] =
98
{
109
[BUTTON_ACTION_NONE] = "CLOSED",
1110
[BUTTON_ACTION_UID_RANDOM] = "RANDOM_UID",
@@ -19,105 +18,109 @@ static const char PROGMEM ButtonActionTable[][32] =
1918

2019
void ButtonInit(void)
2120
{
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;
2423
}
2524

2625
static void ExecuteButtonAction(ButtonActionEnum ButtonAction)
2726
{
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+
}
121124
}
122125

123126
void ButtonTick(void)
@@ -130,28 +133,28 @@ void ButtonTick(void)
130133
//LastButtonState = ThisButtonState;
131134

132135
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+
}
145148
} 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+
}
155158
}
156159

157160
void ButtonGetActionList(char* ListOut, uint16_t BufferSize)
@@ -185,47 +188,47 @@ void ButtonGetActionList(char* ListOut, uint16_t BufferSize)
185188

186189
void ButtonSetActionById(ButtonTypeEnum Type, ButtonActionEnum Action)
187190
{
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
204207
}
205208

206209
void ButtonGetActionByName(ButtonTypeEnum Type, char* ActionOut, uint16_t BufferSize)
207210
{
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+
}
216219
}
217220

218221
bool ButtonSetActionByName(ButtonTypeEnum Type, const char* Action)
219222
{
220-
uint8_t i;
223+
uint8_t i;
221224

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+
}
228231

229-
/* Button action not found */
230-
return false;
232+
/* Button action not found */
233+
return false;
231234
}

Firmware/ChameleonMini/Button.h

+19-16
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@
1212
#include <avr/io.h>
1313
#include "Application/Application.h"
1414

15-
#define BUTTON_PORT PORTA
16-
#define BUTTON_MASK PIN6_bm
17-
#define BUTTON_PINCTRL PIN6CTRL
15+
#define BUTTON_PORT PORTA
16+
#define BUTTON_MASK PIN6_bm
17+
#define BUTTON_PINCTRL PIN6CTRL
18+
19+
#define BUTTON_NAME_MAX_LEN 20
20+
#define BUTTON_LONG_PRESS_TICK_COUNT 10
1821

1922
typedef enum {
20-
BUTTON_ACTION_NONE,
21-
BUTTON_ACTION_UID_RANDOM,
22-
BUTTON_ACTION_UID_LEFT_INCREMENT,
23-
BUTTON_ACTION_UID_RIGHT_INCREMENT,
24-
BUTTON_ACTION_UID_LEFT_DECREMENT,
25-
BUTTON_ACTION_UID_RIGHT_DECREMENT,
26-
BUTTON_ACTION_CYCLE_SETTINGS,
27-
BUTTON_ACTION_TOGGLE_READONLY,
28-
29-
/* This has to be last element */
30-
BUTTON_ACTION_COUNT
23+
BUTTON_ACTION_NONE,
24+
BUTTON_ACTION_UID_RANDOM,
25+
BUTTON_ACTION_UID_LEFT_INCREMENT,
26+
BUTTON_ACTION_UID_RIGHT_INCREMENT,
27+
BUTTON_ACTION_UID_LEFT_DECREMENT,
28+
BUTTON_ACTION_UID_RIGHT_DECREMENT,
29+
BUTTON_ACTION_CYCLE_SETTINGS,
30+
BUTTON_ACTION_TOGGLE_READONLY,
31+
32+
/* This has to be last element */
33+
BUTTON_ACTION_COUNT
3134
} ButtonActionEnum;
3235

3336
typedef enum {
34-
BUTTON_PRESS_SHORT,
35-
BUTTON_PRESS_LONG
37+
BUTTON_PRESS_SHORT,
38+
BUTTON_PRESS_LONG
3639
} ButtonTypeEnum;
3740

3841
void ButtonInit(void);

0 commit comments

Comments
 (0)