output.ino 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. static const char* bin2tristate(const char* bin);
  2. static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);
  3. void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
  4. const char* b = dec2binWzerofill(decimal, length);
  5. Serial.print("Decimal: ");
  6. Serial.print(decimal);
  7. Serial.print(" (");
  8. Serial.print( length );
  9. Serial.print("Bit) Binary: ");
  10. Serial.print( b );
  11. Serial.print(" Tri-State: ");
  12. Serial.print( bin2tristate( b) );
  13. Serial.print(" PulseLength: ");
  14. Serial.print(delay);
  15. Serial.print(" microseconds");
  16. Serial.print(" Protocol: ");
  17. Serial.println(protocol);
  18. Serial.print("Raw data: ");
  19. for (unsigned int i=0; i<= length*2; i++) {
  20. Serial.print(raw[i]);
  21. Serial.print(",");
  22. }
  23. Serial.println();
  24. Serial.println();
  25. }
  26. static const char* bin2tristate(const char* bin) {
  27. static char returnValue[50];
  28. int pos = 0;
  29. int pos2 = 0;
  30. while (bin[pos]!='\0' && bin[pos+1]!='\0') {
  31. if (bin[pos]=='0' && bin[pos+1]=='0') {
  32. returnValue[pos2] = '0';
  33. } else if (bin[pos]=='1' && bin[pos+1]=='1') {
  34. returnValue[pos2] = '1';
  35. } else if (bin[pos]=='0' && bin[pos+1]=='1') {
  36. returnValue[pos2] = 'F';
  37. } else {
  38. return "not applicable";
  39. }
  40. pos = pos+2;
  41. pos2++;
  42. }
  43. returnValue[pos2] = '\0';
  44. return returnValue;
  45. }
  46. static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
  47. static char bin[64];
  48. unsigned int i=0;
  49. while (Dec > 0) {
  50. bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
  51. Dec = Dec >> 1;
  52. }
  53. for (unsigned int j = 0; j< bitLength; j++) {
  54. if (j >= bitLength - i) {
  55. bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
  56. } else {
  57. bin[j] = '0';
  58. }
  59. }
  60. bin[bitLength] = '\0';
  61. return bin;
  62. }