Skip to content

Commit

Permalink
Merge pull request #166 from shinhub/button_random_fix
Browse files Browse the repository at this point in the history
Button random fix
  • Loading branch information
iceman1001 authored Nov 7, 2019
2 parents 4a54102 + 4978115 commit f463cd6
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 168 deletions.
307 changes: 155 additions & 152 deletions Firmware/ChameleonMini/Button.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "Button.h"
#include "Random.h"
#include "Configuration.h"
#include "Common.h"
#include "Settings.h"

#define LONG_PRESS_TICK_COUNT 10

static const char PROGMEM ButtonActionTable[][32] =
static const char PROGMEM ButtonActionTable[][BUTTON_NAME_MAX_LEN] =
{
[BUTTON_ACTION_NONE] = "CLOSED",
[BUTTON_ACTION_UID_RANDOM] = "RANDOM_UID",
Expand All @@ -19,105 +18,109 @@ static const char PROGMEM ButtonActionTable[][32] =

void ButtonInit(void)
{
BUTTON_PORT.DIRCLR = BUTTON_MASK;
BUTTON_PORT.BUTTON_PINCTRL = PORT_OPC_PULLUP_gc;
BUTTON_PORT.DIRCLR = BUTTON_MASK;
BUTTON_PORT.BUTTON_PINCTRL = PORT_OPC_PULLUP_gc;
}

static void ExecuteButtonAction(ButtonActionEnum ButtonAction)
{
uint8_t UidBuffer[32];

if (ButtonAction == BUTTON_ACTION_UID_RANDOM) {

/* iceman, 2018, this random functionality could be more localized to the current cardtype in use.
ie. for Ultralight based cards with 7byte uid, skip manufacturing byte
*/

ApplicationGetUid(UidBuffer);

/* skip manufacturing byte UID0 */
for (uint8_t i=1; i<ActiveConfiguration.UidSize-1; i++) {
UidBuffer[i] = RandomGetByte();
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_LEFT_INCREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i;

for (i=0; i<ActiveConfiguration.UidSize; i++) {
if (Carry) {
if (UidBuffer[i] == 0xFF) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] + 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_INCREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i = ActiveConfiguration.UidSize;

while(i-- > 0) {
if (Carry) {
if (UidBuffer[i] == 0xFF) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] + 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_LEFT_DECREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i;

for (i=0; i<ActiveConfiguration.UidSize; i++) {
if (Carry) {
if (UidBuffer[i] == 0x00) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] - 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_DECREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i = ActiveConfiguration.UidSize;

while(i-- > 0) {
if (Carry) {
if (UidBuffer[i] == 0x00) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] - 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_CYCLE_SETTINGS) {
SettingsCycle();
} else if (ButtonAction == BUTTON_ACTION_TOGGLE_READONLY) {
ActiveConfiguration.ReadOnly = !ActiveConfiguration.ReadOnly;
}
ConfigurationUidType UidBuffer;

if (ButtonAction == BUTTON_ACTION_UID_RANDOM) {
uint8_t startByte = 0;
ApplicationGetUid(UidBuffer);

#ifdef CONFIG_MF_ULTRALIGHT_SUPPORT
// Make RANDOM keep 1st byte safe for Ultralight types
ConfigurationEnum ActiveConfigurationId = GlobalSettings.ActiveSettingPtr->Configuration;
if( (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT)
|| (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT_EV1_80B)
|| (ActiveConfigurationId == CONFIG_MF_ULTRALIGHT_EV1_164B) ) {
startByte = 1;
}
#endif
for( ; startByte < ActiveConfiguration.UidSize; startByte++) {
UidBuffer[startByte] = RandomGetByte();
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_LEFT_INCREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i;

for (i=0; i<ActiveConfiguration.UidSize; i++) {
if (Carry) {
if (UidBuffer[i] == 0xFF) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] + 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_INCREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i = ActiveConfiguration.UidSize;

while(i-- > 0) {
if (Carry) {
if (UidBuffer[i] == 0xFF) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] + 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_LEFT_DECREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i;

for (i=0; i<ActiveConfiguration.UidSize; i++) {
if (Carry) {
if (UidBuffer[i] == 0x00) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] - 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_UID_RIGHT_DECREMENT) {
ApplicationGetUid(UidBuffer);
bool Carry = 1;
uint8_t i = ActiveConfiguration.UidSize;

while(i-- > 0) {
if (Carry) {
if (UidBuffer[i] == 0x00) {
Carry = 1;
} else {
Carry = 0;
}

UidBuffer[i] = (UidBuffer[i] - 1) & 0xFF;
}
}

ApplicationSetUid(UidBuffer);
} else if (ButtonAction == BUTTON_ACTION_CYCLE_SETTINGS) {
SettingsCycle();
} else if (ButtonAction == BUTTON_ACTION_TOGGLE_READONLY) {
ActiveConfiguration.ReadOnly = !ActiveConfiguration.ReadOnly;
}
}

void ButtonTick(void)
Expand All @@ -130,28 +133,28 @@ void ButtonTick(void)
//LastButtonState = ThisButtonState;

if (ThisButtonState & BUTTON_MASK) {
/* Button is currently pressed */
if (PressTickCounter < LONG_PRESS_TICK_COUNT) {
/* Count ticks while button is being pressed */
PressTickCounter++;
} else if (PressTickCounter == LONG_PRESS_TICK_COUNT) {
/* Long button press detected execute button action and advance PressTickCounter
* to an invalid state. */
ExecuteButtonAction(GlobalSettings.ActiveSettingPtr->ButtonLongAction);
PressTickCounter++;
} else {
/* Button is still pressed, ignore */
}
/* Button is currently pressed */
if (PressTickCounter < BUTTON_LONG_PRESS_TICK_COUNT) {
/* Count ticks while button is being pressed */
PressTickCounter++;
} else if (PressTickCounter == BUTTON_LONG_PRESS_TICK_COUNT) {
/* Long button press detected execute button action and advance PressTickCounter
* to an invalid state. */
ExecuteButtonAction(GlobalSettings.ActiveSettingPtr->ButtonLongAction);
PressTickCounter++;
} else {
/* Button is still pressed, ignore */
}
} else if (!(ThisButtonState & BUTTON_MASK)) {
/* Button is currently not being pressed. Check if PressTickCounter contains
* a recent short button press. */
if ( (PressTickCounter > 0) && (PressTickCounter <= LONG_PRESS_TICK_COUNT) ) {
/* We have a short button press */
ExecuteButtonAction(GlobalSettings.ActiveSettingPtr->ButtonAction);
}

PressTickCounter = 0;
}
/* Button is currently not being pressed. Check if PressTickCounter contains
* a recent short button press. */
if ( (PressTickCounter > 0) && (PressTickCounter <= BUTTON_LONG_PRESS_TICK_COUNT) ) {
/* We have a short button press */
ExecuteButtonAction(GlobalSettings.ActiveSettingPtr->ButtonAction);
}

PressTickCounter = 0;
}
}

void ButtonGetActionList(char* ListOut, uint16_t BufferSize)
Expand Down Expand Up @@ -185,47 +188,47 @@ void ButtonGetActionList(char* ListOut, uint16_t BufferSize)

void ButtonSetActionById(ButtonTypeEnum Type, ButtonActionEnum Action)
{
#ifndef BUTTON_SETTING_GLOBAL
if (Type == BUTTON_PRESS_SHORT) {
GlobalSettings.ActiveSettingPtr->ButtonAction = Action;
} else if (Type == BUTTON_PRESS_LONG) {
GlobalSettings.ActiveSettingPtr->ButtonLongAction = Action;
}
#else
/* Write button action to all settings when using global settings */
for (uint8_t i=0; i<SETTINGS_COUNT; i++) {
if (Type == BUTTON_PRESS_SHORT) {
GlobalSettings.Settings[i].ButtonAction = Action;
} else if (Type == BUTTON_PRESS_LONG) {
GlobalSettings.Settings[i].ButtonLongAction = Action;
}
}
#endif
#ifndef BUTTON_SETTING_GLOBAL
if (Type == BUTTON_PRESS_SHORT) {
GlobalSettings.ActiveSettingPtr->ButtonAction = Action;
} else if (Type == BUTTON_PRESS_LONG) {
GlobalSettings.ActiveSettingPtr->ButtonLongAction = Action;
}
#else
/* Write button action to all settings when using global settings */
for (uint8_t i=0; i<SETTINGS_COUNT; i++) {
if (Type == BUTTON_PRESS_SHORT) {
GlobalSettings.Settings[i].ButtonAction = Action;
} else if (Type == BUTTON_PRESS_LONG) {
GlobalSettings.Settings[i].ButtonLongAction = Action;
}
}
#endif
}

void ButtonGetActionByName(ButtonTypeEnum Type, char* ActionOut, uint16_t BufferSize)
{
if (Type == BUTTON_PRESS_SHORT) {
strncpy_P(ActionOut, ButtonActionTable[GlobalSettings.ActiveSettingPtr->ButtonAction], BufferSize);
} else if (Type == BUTTON_PRESS_LONG) {
strncpy_P(ActionOut, ButtonActionTable[GlobalSettings.ActiveSettingPtr->ButtonLongAction], BufferSize);
} else {
/* Should not happen (TM) */
*ActionOut = '\0';
}
if (Type == BUTTON_PRESS_SHORT) {
strncpy_P(ActionOut, ButtonActionTable[GlobalSettings.ActiveSettingPtr->ButtonAction], BufferSize);
} else if (Type == BUTTON_PRESS_LONG) {
strncpy_P(ActionOut, ButtonActionTable[GlobalSettings.ActiveSettingPtr->ButtonLongAction], BufferSize);
} else {
/* Should not happen (TM) */
*ActionOut = '\0';
}
}

bool ButtonSetActionByName(ButtonTypeEnum Type, const char* Action)
{
uint8_t i;
uint8_t i;

for (i=0; i<BUTTON_ACTION_COUNT; i++) {
if (strcmp_P(Action, ButtonActionTable[i]) == 0) {
ButtonSetActionById(Type, i);
return true;
}
}
for (i=0; i<BUTTON_ACTION_COUNT; i++) {
if (strcmp_P(Action, ButtonActionTable[i]) == 0) {
ButtonSetActionById(Type, i);
return true;
}
}

/* Button action not found */
return false;
/* Button action not found */
return false;
}
35 changes: 19 additions & 16 deletions Firmware/ChameleonMini/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,30 @@
#include <avr/io.h>
#include "Application/Application.h"

#define BUTTON_PORT PORTA
#define BUTTON_MASK PIN6_bm
#define BUTTON_PINCTRL PIN6CTRL
#define BUTTON_PORT PORTA
#define BUTTON_MASK PIN6_bm
#define BUTTON_PINCTRL PIN6CTRL

#define BUTTON_NAME_MAX_LEN 20
#define BUTTON_LONG_PRESS_TICK_COUNT 10

typedef enum {
BUTTON_ACTION_NONE,
BUTTON_ACTION_UID_RANDOM,
BUTTON_ACTION_UID_LEFT_INCREMENT,
BUTTON_ACTION_UID_RIGHT_INCREMENT,
BUTTON_ACTION_UID_LEFT_DECREMENT,
BUTTON_ACTION_UID_RIGHT_DECREMENT,
BUTTON_ACTION_CYCLE_SETTINGS,
BUTTON_ACTION_TOGGLE_READONLY,

/* This has to be last element */
BUTTON_ACTION_COUNT
BUTTON_ACTION_NONE,
BUTTON_ACTION_UID_RANDOM,
BUTTON_ACTION_UID_LEFT_INCREMENT,
BUTTON_ACTION_UID_RIGHT_INCREMENT,
BUTTON_ACTION_UID_LEFT_DECREMENT,
BUTTON_ACTION_UID_RIGHT_DECREMENT,
BUTTON_ACTION_CYCLE_SETTINGS,
BUTTON_ACTION_TOGGLE_READONLY,

/* This has to be last element */
BUTTON_ACTION_COUNT
} ButtonActionEnum;

typedef enum {
BUTTON_PRESS_SHORT,
BUTTON_PRESS_LONG
BUTTON_PRESS_SHORT,
BUTTON_PRESS_LONG
} ButtonTypeEnum;

void ButtonInit(void);
Expand Down

0 comments on commit f463cd6

Please sign in to comment.