/** SEBURY Wiegand 26 send data in BINary
if FATEK we have code saved in memory in HEX FFFFFFFF
for compae coe from FATEK nad SEBURY we need
covert DEC to HEX and switch lower and higher byte
- thema - how to covert string value to bin value?
*/
//
int buttonReset = 0;
int button0 = 0;
int button1 = 0;
int slideSwitch = 0;
int lastSlideSwitch = 0;
String outBytes = "";
String inBinary = "00000001001000111100111101011010"; //0123cf5a -> cf5a0123
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT); //switch byte
pinMode(10, INPUT_PULLUP); //0
pinMode(11, INPUT_PULLUP); //1
pinMode(12, INPUT_PULLUP); //reset intBinar
showValues();
}
//return HEX from BIN 32 switched low and high bytes for FATEK
void bin2hex(String sample_str, bool swtch = false){
uint32_t result = 0;
for (unsigned int i = 0; i < sample_str.length(); ++i){
result = result << 1;
result = result | (sample_str[i] & 1);
}
String hexCod = String(result, HEX);
int len = hexCod.length();
String lowBytes = String(&hexCod[len - 4]);
//delka celeho - 4 je co zbyde pokud je mensi nez 4 doplnime 0
String highBytes = hexCod.substring(0,len-4);
if (len-4< 4){
String prevHB = highBytes;
switch (len-4){
case 3:
highBytes = "0";
highBytes.concat(prevHB);
break;
case 2:
highBytes = "00";
highBytes.concat(prevHB);
break;
case 1:
highBytes = "000";
highBytes.concat(prevHB);
break;
case 0:
highBytes = "0000";
highBytes.concat(prevHB);
break;
}
}
//Switch low and high bytes if set
if (swtch){
outBytes = lowBytes;
outBytes.concat(highBytes);
} else {
outBytes = hexCod;
}
return outBytes;
}
/*
void printHex(int num, int precision){
Serial.println(num);
char tmp[16];
char format[32];
sprintf(format, "%%.%dX", precision);
sprintf(tmp, format, num);
Serial.println(tmp);
}
*/
/*
void crPrintHEX(unsigned long DATA, unsigned char numChars) {
unsigned long mask = 0x0000000F;
mask = mask << 4*(numChars-1);
for (unsigned int i=numChars; i>0; --i) {
Serial.print(((DATA & mask) >> (i-1)*4),HEX);
mask = mask >> 4;
}
Serial.print(" ");
}
*/
void loop() {
button0 = digitalRead(10);
button1 = digitalRead(11);
buttonReset = digitalRead(12);
if(buttonReset == LOW){
Serial.println("Reset");
inBinary = "";
delay(100);
}
slideSwitch = digitalRead(2);
if(slideSwitch != lastSlideSwitch){
lastSlideSwitch = slideSwitch;
showValues();
delay(100);
}
if (inBinary.length() < 34){
if(button0 == LOW){
inBinary.concat(0);
showValues();
delay(150);
}
if(button1 == LOW){
inBinary.concat(1);
showValues();
delay(150);
}
}
}
void showValues(){
//const char* chInBinary = inBinary.c_str();
// inBinaryLow;
// inBinaryHigh
//Serial.println( inBinary, HEX );
/*
uint16_t resultt = 0;
for (unsigned int i = 0; i < inBinaryLow.length(); ++i)
{
resultt = resultt << 1;
resultt = resultt | (inBinaryLow[i] & 1);
}
String hexCod = String(resultt, HEX);
*/
//int n = sprintf(inBinaryLow, "%16", 1);
//Serial.print( n );
//Serial.print(" ");
slideSwitch = digitalRead(2);
bool stat = false;
if (slideSwitch == HIGH){
stat = true;
}
bin2hex( inBinary, stat );
Serial.println(inBinary);
Serial.println(outBytes);
Serial.println(" ");
}