Skip to content

Commit

Permalink
employ a macro to compute element counts in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-zzo committed Oct 28, 2016
1 parent 046e289 commit ab3b4f2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Firmware/Chameleon-Mini/Application/Reader14443A.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static const CardIdentificationType PROGMEM CardIdentificationList[] = {
[CardType_Nokia_MIFARE_Classic_4k_emulated_6131] = { .ATQA=0x0008, .ATQARelevant=true, .SAK=0x38, .SAKRelevant=true, .ATSRelevant=false, .Manufacturer="Nokia", .Type="MIFARE Classic 4k - emulated (6131 NFC)" }
};

static CardType CardCandidates[sizeof(CardIdentificationList) / sizeof(CardIdentificationType)];
static CardType CardCandidates[ARRAY_COUNT(CardIdentificationList)];
static uint8_t CardCandidatesIdx = 0;

uint16_t addParityBits(uint8_t * Buffer, uint16_t BitCount)
Expand Down Expand Up @@ -549,7 +549,7 @@ uint16_t Reader14443AAppProcess(uint8_t* Buffer, uint16_t BitCount)
bool ISO14443_4A_compliant = IS_ISO14443A_4_COMPLIANT(Buffer);

uint8_t i;
for (i = 0; i < sizeof(CardIdentificationList)/sizeof(CardIdentificationType); i++)
for (i = 0; i < ARRAY_COUNT(CardIdentificationList); i++)
{
CardIdentificationType card;
memcpy_P(&card, &CardIdentificationList[i], sizeof(CardIdentificationType));
Expand Down
7 changes: 3 additions & 4 deletions Firmware/Chameleon-Mini/Button.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void ButtonTick(void)

void ButtonGetActionList(char* List, uint16_t BufferSize)
{
MapToString(ButtonActionMap, sizeof(ButtonActionMap)/sizeof(*ButtonActionMap), List, BufferSize);
MapToString(ButtonActionMap, ARRAY_COUNT(ButtonActionMap), List, BufferSize);
}

void ButtonSetActionById(ButtonTypeEnum Type, ButtonActionEnum Action)
Expand All @@ -256,16 +256,15 @@ void ButtonSetActionById(ButtonTypeEnum Type, ButtonActionEnum Action)

void ButtonGetActionByName(ButtonTypeEnum Type, char* Action, uint16_t BufferSize)
{
MapIdToText(ButtonActionMap, sizeof(ButtonActionMap)/sizeof(*ButtonActionMap),
MapIdToText(ButtonActionMap, ARRAY_COUNT(ButtonActionMap),
GlobalSettings.ActiveSettingPtr->ButtonActions[Type], Action, BufferSize);
}

bool ButtonSetActionByName(ButtonTypeEnum Type, const char* Action)
{
MapIdType Id;

if (MapTextToId(ButtonActionMap, sizeof(ButtonActionMap)/sizeof(*ButtonActionMap),
Action, &Id)) {
if (MapTextToId(ButtonActionMap, ARRAY_COUNT(ButtonActionMap), Action, &Id)) {
ButtonSetActionById(Type, Id);
return true;
} else {
Expand Down
3 changes: 3 additions & 0 deletions Firmware/Chameleon-Mini/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#define INLINE \
static inline __attribute__((always_inline))

#define ARRAY_COUNT(x) \
(sizeof(x) / sizeof(x[0]))

#define NIBBLE_TO_HEXCHAR(x) ( (x) < 0x0A ? (x) + '0' : (x) + 'A' - 0x0A )
#define HEXCHAR_TO_NIBBLE(x) ( (x) < 'A' ? (x) - '0' : (x) - 'A' + 0x0A )
#define VALID_HEXCHAR(x) ( ( (x) >= '0' && (x) <= '9' ) || ( (x) >= 'A' && (x) <= 'F' ) )
Expand Down
6 changes: 3 additions & 3 deletions Firmware/Chameleon-Mini/Configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ void ConfigurationSetById( ConfigurationEnum Configuration )

void ConfigurationGetByName(char* Configuration, uint16_t BufferSize)
{
MapIdToText(ConfigurationMap, sizeof(ConfigurationMap)/sizeof(*ConfigurationMap), GlobalSettings.ActiveSettingPtr->Configuration, Configuration, BufferSize);
MapIdToText(ConfigurationMap, ARRAY_COUNT(ConfigurationMap), GlobalSettings.ActiveSettingPtr->Configuration, Configuration, BufferSize);
}

bool ConfigurationSetByName(const char* Configuration)
{
MapIdType Id;

if (MapTextToId(ConfigurationMap, sizeof(ConfigurationMap)/sizeof(*ConfigurationMap), Configuration, &Id)) {
if (MapTextToId(ConfigurationMap, ARRAY_COUNT(ConfigurationMap), Configuration, &Id)) {
ConfigurationSetById(Id);
LogEntry(LOG_INFO_CONFIG_SET, Configuration, StringLength(Configuration, CONFIGURATION_NAME_LENGTH_MAX-1));
return true;
Expand All @@ -236,6 +236,6 @@ bool ConfigurationSetByName(const char* Configuration)

void ConfigurationGetList(char* List, uint16_t BufferSize)
{
MapToString(ConfigurationMap, sizeof(ConfigurationMap)/sizeof(*ConfigurationMap), List, BufferSize);
MapToString(ConfigurationMap, ARRAY_COUNT(ConfigurationMap), List, BufferSize);
}

8 changes: 4 additions & 4 deletions Firmware/Chameleon-Mini/LED.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void LEDTick(void)

void LEDGetFuncList(char* List, uint16_t BufferSize)
{
MapToString(LEDFunctionMap, sizeof(LEDFunctionMap)/sizeof(*LEDFunctionMap), List, BufferSize);
MapToString(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap), List, BufferSize);
}

void LEDSetFuncById(uint8_t Mask, LEDHookEnum Function)
Expand Down Expand Up @@ -146,10 +146,10 @@ void LEDSetFuncById(uint8_t Mask, LEDHookEnum Function)
void LEDGetFuncByName(uint8_t Mask, char* Function, uint16_t BufferSize)
{
if (Mask == LED_GREEN) {
MapIdToText(LEDFunctionMap, sizeof(LEDFunctionMap)/sizeof(*LEDFunctionMap),
MapIdToText(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap),
GlobalSettings.ActiveSettingPtr->LEDGreenFunction, Function, BufferSize);
} else if (Mask == LED_RED) {
MapIdToText(LEDFunctionMap, sizeof(LEDFunctionMap)/sizeof(*LEDFunctionMap),
MapIdToText(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap),
GlobalSettings.ActiveSettingPtr->LEDRedFunction, Function, BufferSize);
}
}
Expand All @@ -158,7 +158,7 @@ bool LEDSetFuncByName(uint8_t Mask, const char* Function)
{
MapIdType Id;

if (MapTextToId(LEDFunctionMap, sizeof(LEDFunctionMap)/sizeof(*LEDFunctionMap), Function, &Id)) {
if (MapTextToId(LEDFunctionMap, ARRAY_COUNT(LEDFunctionMap), Function, &Id)) {
LEDSetFuncById(Mask, Id);
return true;
} else {
Expand Down
7 changes: 3 additions & 4 deletions Firmware/Chameleon-Mini/Log.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ bool LogSetModeByName(const char* Mode)
{
MapIdType Id;

if (MapTextToId(LogModeMap, sizeof(LogModeMap)/sizeof(*LogModeMap),
Mode, &Id)) {
if (MapTextToId(LogModeMap, ARRAY_COUNT(LogModeMap), Mode, &Id)) {
LogSetModeById(Id);
return true;
}
Expand All @@ -183,13 +182,13 @@ bool LogSetModeByName(const char* Mode)

void LogGetModeByName(char* Mode, uint16_t BufferSize)
{
MapIdToText(LogModeMap, sizeof(LogModeMap)/sizeof(*LogModeMap),
MapIdToText(LogModeMap, ARRAY_COUNT(LogModeMap),
GlobalSettings.ActiveSettingPtr->LogMode, Mode, BufferSize);
}

void LogGetModeList(char* List, uint16_t BufferSize)
{
MapToString(LogModeMap, sizeof(LogModeMap)/sizeof(*LogModeMap), List, BufferSize);
MapToString(LogModeMap, ARRAY_COUNT(LogModeMap), List, BufferSize);
}

void LogSRAMToFRAM(void)
Expand Down
21 changes: 10 additions & 11 deletions Firmware/Chameleon-Mini/Terminal/CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ static const char* GetStatusMessageP(CommandStatusIdType StatusId)
{
uint8_t i;

for (i=0; i<(sizeof(StatusTable)/sizeof(*StatusTable)); i++) {
for (i = 0; i < ARRAY_COUNT(StatusTable); i++) {
if (pgm_read_byte(&StatusTable[i].Id) == StatusId)
return StatusTable[i].Message;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ static void DecodeCommand(void)
*pCommandDelimiter = '\0';

/* Search in command table */
for (i=0; i<(sizeof(CommandTable) / sizeof(*CommandTable)); i++) {
for (i = 0; i < ARRAY_COUNT(CommandTable); i++) {
if (strcmp_P(pTerminalBuffer, CommandTable[i].Command) == 0) {
/* Command found. Clear buffer, and call appropriate function */
char* pParam = ++pCommandDelimiter;
Expand Down Expand Up @@ -451,8 +451,7 @@ bool CommandLineProcessByte(uint8_t Byte) {
}

/* Prevent buffer overflow and account for '\0' */
if (BufferIdx
< (sizeof(TerminalBuffer) / sizeof(*TerminalBuffer) - 1)) {
if (BufferIdx < TERMINAL_BUFFER_SIZE - 1) {
TerminalBuffer[BufferIdx++] = Byte;
}
} else if (Byte == '\r') {
Expand Down Expand Up @@ -529,25 +528,25 @@ void CommandLineAppendData(void const * const Buffer, uint16_t Bytes)
char* pTerminalBuffer = (char*) TerminalBuffer;

uint16_t tmpBytes = Bytes;
if (Bytes > (sizeof(TerminalBuffer) / 2))
tmpBytes = sizeof(TerminalBuffer) / 2;
if (Bytes > (TERMINAL_BUFFER_SIZE / 2))
tmpBytes = TERMINAL_BUFFER_SIZE / 2;
Bytes -= tmpBytes;

BufferToHexString(pTerminalBuffer, sizeof(TerminalBuffer), Buffer, tmpBytes);
BufferToHexString(pTerminalBuffer, TERMINAL_BUFFER_SIZE, Buffer, tmpBytes);
TerminalSendString(pTerminalBuffer);

uint8_t i = 1;
while (Bytes > (sizeof(TerminalBuffer) / 2))
while (Bytes > (TERMINAL_BUFFER_SIZE / 2))
{
Bytes -= sizeof(TerminalBuffer) / 2;
BufferToHexString(pTerminalBuffer, sizeof(TerminalBuffer), Buffer + i * sizeof(TerminalBuffer) / 2, sizeof(TerminalBuffer));
Bytes -= TERMINAL_BUFFER_SIZE / 2;
BufferToHexString(pTerminalBuffer, TERMINAL_BUFFER_SIZE, Buffer + i * TERMINAL_BUFFER_SIZE / 2, TERMINAL_BUFFER_SIZE);
TerminalSendString(pTerminalBuffer);
i++;
}

if (Bytes > 0)
{
BufferToHexString(pTerminalBuffer, sizeof(TerminalBuffer), Buffer + i * sizeof(TerminalBuffer) / 2, Bytes);
BufferToHexString(pTerminalBuffer, TERMINAL_BUFFER_SIZE, Buffer + i * TERMINAL_BUFFER_SIZE / 2, Bytes);
TerminalSendString(pTerminalBuffer);
}

Expand Down

0 comments on commit ab3b4f2

Please sign in to comment.