Skip to content

Commit

Permalink
Merge pull request #3149 from tonhuisman/feature/P008_hex_to_dec
Browse files Browse the repository at this point in the history
[P008] Add option to convert value to 'hex' (#3068), and updated docs
  • Loading branch information
TD-er authored Jul 19, 2020
2 parents c9cc98e + 02c2bdb commit 2d0dad2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
14 changes: 13 additions & 1 deletion docs/source/Plugin/P008.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. include:: ../Plugin/_plugin_substitutions_p00x.repl
.. include:: ../Plugin/_plugin_substitutions_p00x.repl
.. _P008_page:

|P008_typename|
Expand All @@ -21,6 +21,18 @@ Maintainer: |P008_maintainer|

Used libraries: |P008_usedlibraries|

Description
-----------

The Wiegand plugin reads the Wiegand protocol, either the 26 or 34 bit variant, transmitted over a 2-wire serial connection. The RFID reader or keypad has to be connected to two GPIO pins on the ESP module. The plugin uses an interrupt routine to decode the bitstream. A RFID reader will send a message containing the unique RFID Tag ID. A keypad will send the input after pressing the #, or another configured, confirmation key.

Data is transmitted in hexadecimal format.
For numeric keypads an option is provided to transform the hex value into a decimal representation, so that when entering value 1234# (# is the confirmation key here), not the result 4660 (0x1234) is made available, but actually 1234. This for easier processing/validating of the entered value. Any input of A-F is replaced by 0 when this option is enabled! It should not be enabled when using a RFID reader, as the Tag ID won't be correct.

The value is placed in the Tag variable (this name can be changed).

.. image:: P008_Settings.png

Supported hardware
------------------

Expand Down
Binary file added docs/source/Plugin/P008_Settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 48 additions & 2 deletions src/_P008_RFID.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
//################################# Plugin 008: Wiegand RFID Tag Reader #################################
//#######################################################################################################

/*
History:
2020-07-04 tonhuisman: Add checkbox for 'Present hex as decimal value' option (with note) so hexadecimal
value of f.e. a numeric keypad using the Wiegand protocol (hexadecimal data) will be cast to decimal.
When enabled entering 1234# will result in Tag = 1234 instead of 4660 (= 0x1234), any A-F
entered will result in a 0 in the output value.
-------------
No initial history available.
*/

#include "_Plugin_Helper.h"

#define PLUGIN_008
Expand All @@ -20,6 +30,26 @@ byte Plugin_008_WiegandSize = 26; // size of a tag via wiegand (26-bits

boolean Plugin_008_init = false;

/**
* Convert/cast a hexadecimal input to a decimal representation, so 0x1234 (= 4660) comes out as 1234.
*
* //FIXME Move to a more global place to also be used elsewhere?
*/
uint64_t castHexAsDec(uint64_t hexValue) {
uint64_t result = 0;
uint8_t digit;
for (int i = 0; i < 8; i++) {
digit = (hexValue & 0x0000000F);
if (digit > 10) digit = 0; // Cast by dropping any non-decimal input
if (digit > 0) { // Avoid 'expensive' pow operation if not used
result += (digit * static_cast<uint64_t>(pow(10, i)));
}
hexValue >>= 4;
if (hexValue == 0) break; // Stop when no more to process
}
return result;
}

boolean Plugin_008(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
Expand Down Expand Up @@ -125,6 +155,9 @@ boolean Plugin_008(byte function, struct EventStruct *event, String& string)

unsigned long old_key = ((uint32_t) UserVar[event->BaseVarIndex]) | ((uint32_t) UserVar[event->BaseVarIndex + 1])<<16;
bool new_key = false;
if (PCONFIG(1)) {
Plugin_008_keyBuffer = castHexAsDec(Plugin_008_keyBuffer);
}

if (old_key != Plugin_008_keyBuffer) {
UserVar[event->BaseVarIndex] = (Plugin_008_keyBuffer & 0xFFFF);
Expand Down Expand Up @@ -152,6 +185,16 @@ boolean Plugin_008(byte function, struct EventStruct *event, String& string)

if (new_key) sendData(event);
setPluginTaskTimer(500, event->TaskIndex, event->Par1);

// String info = "";
// uint64_t invalue = 0x1234;
// uint64_t outvalue = castHexAsDec(invalue);
// info.reserve(40);
// info += F("Test castHexAsDec(");
// info += (double)invalue;
// info += F(") => ");
// info += (double)outvalue;
// addLog(LOG_LEVEL_INFO, info);
}
}
break;
Expand All @@ -166,14 +209,17 @@ boolean Plugin_008(byte function, struct EventStruct *event, String& string)
optionValues[0] = 26;
optionValues[1] = 34;
addFormSelector(F("Wiegand Type"), F("p008_type"), 2, options, optionValues, choice);
bool presentHexToDec = PCONFIG(1);
addFormCheckBox(F("Present hex as decimal value"), F("p008_hexasdec"), presentHexToDec);
addFormNote(F("Useful only for numeric keypad input!"));
success = true;
break;
}

case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = web_server.arg(F("p008_type"));
PCONFIG(0) = plugin1.toInt();
PCONFIG(0) = getFormItemInt(F("p008_type"));
PCONFIG(1) = getFormItemInt(F("p008_hexasdec"));
success = true;
break;
}
Expand Down

0 comments on commit 2d0dad2

Please sign in to comment.