temp1.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Pulse timings
  21. #define INIT_PULSE 3600
  22. #define START_PULSE 440
  23. #define SHORT_PULSE 850
  24. #define LONG_PULSE 1720
  25. #define MARG 200
  26. static int temp1decode (unsigned int width) {
  27. switch (temp1state) {
  28. case UNKNOWN: // Start of frame
  29. if ( (INIT_PULSE-MARG) <= width && width <= (INIT_PULSE+MARG) ) {
  30. temp1state = T0;
  31. }
  32. else {
  33. return -1; // error, reset
  34. }
  35. break;
  36. case T0: // First half of pulse : HIGH around 230us
  37. if( temp1x_numBits == 32 ) { // end of frame
  38. temp1state = DONE;
  39. temp1_x_data = (temp1_x_data>>8); // Mask away some bits at the end
  40. return 1;
  41. }
  42. else if ( (START_PULSE-MARG) <= width && width <= (START_PULSE+MARG) ) {
  43. temp1state = T1;
  44. }
  45. else {
  46. if( temp1x_numBits == 0 && 3400 <= width && width <= 3800 ) {
  47. temp1state = T0;
  48. }
  49. else {
  50. return -1; // error, reset
  51. }
  52. }
  53. break;
  54. case T1:
  55. if ( (SHORT_PULSE-MARG) <= width && width <= (SHORT_PULSE+MARG)) {
  56. temp1gotBit(0);
  57. } else if ( (LONG_PULSE-MARG) <= width && width <= (LONG_PULSE+MARG) ) {
  58. temp1gotBit(1);
  59. } else {
  60. return -1; // error, reset
  61. }
  62. temp1state = T0;
  63. break;
  64. }
  65. return 0;
  66. }
  67. boolean nextPulseTemp1 (unsigned int width) {
  68. if( width > 0 ) {
  69. if (temp1state != DONE)
  70. switch (temp1decode(width)) {
  71. case -1:
  72. temp1ResetDecoder();
  73. break;
  74. case 1: temp1done(); break;
  75. }
  76. }
  77. return temp1isDone();
  78. }