temp1.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "app.h"
  2. #include "nexa.h"
  3. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  4. static unsigned char temp1state = UNKNOWN;
  5. unsigned long temp1_x_data;
  6. static unsigned int temp1x_numBits;
  7. void temp1ResetDecoder () {
  8. temp1_x_data = 0;
  9. temp1x_numBits = 0;
  10. temp1state = UNKNOWN;
  11. }
  12. static boolean temp1isDone() { return temp1state == DONE; }
  13. static void temp1done () { temp1state = DONE; }
  14. static void temp1gotBit (char value) {
  15. // X
  16. temp1x_numBits++;
  17. temp1_x_data = (temp1_x_data << 1) + (value & 0x01);
  18. temp1state = OK;
  19. }
  20. static int temp1decode (unsigned int width) {
  21. switch (temp1state) {
  22. case UNKNOWN: // Start of frame
  23. if ( 3400 <= width && width <= 3800 ) {
  24. temp1state = T0;
  25. }
  26. else {
  27. return -1; // error, reset
  28. }
  29. break;
  30. case T0: // First half of pulse : HIGH around 230us
  31. if( temp1x_numBits == 32 ) { // end of frame
  32. temp1state = DONE;
  33. temp1_x_data = (temp1_x_data>>8); // Mask away some bits at the end
  34. return 1;
  35. }
  36. else if ( 340 <= width && width <= 480 ) {
  37. temp1state = T1;
  38. }
  39. else {
  40. if( temp1x_numBits == 0 && 3400 <= width && width <= 3800 ) {
  41. temp1state = T0;
  42. }
  43. else {
  44. return -1; // error, reset
  45. }
  46. }
  47. break;
  48. case T1:
  49. if ( 800 <= width && width <= 1000) {
  50. temp1gotBit(0);
  51. } else if ( 1600 <= width && width <= 1900 ) {
  52. temp1gotBit(1);
  53. } else {
  54. return -1; // error, reset
  55. }
  56. temp1state = T0;
  57. break;
  58. }
  59. return 0;
  60. }
  61. boolean nextPulseTemp1 (unsigned int width) {
  62. if( width > 0 ) {
  63. if (temp1state != DONE)
  64. switch (temp1decode(width)) {
  65. case -1:
  66. temp1ResetDecoder();
  67. break;
  68. case 1: temp1done(); break;
  69. }
  70. }
  71. return temp1isDone();
  72. }