temp2.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "app.h"
  2. #include "nexa.h"
  3. #include "temp2.h"
  4. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  5. static unsigned char temp2state = T0;
  6. unsigned long long temp2_x_data;
  7. static unsigned int temp2x_numBits;
  8. void temp2ResetDecoder () {
  9. temp2_x_data = 0;
  10. temp2x_numBits = 0;
  11. temp2state = T0;
  12. }
  13. static boolean temp2isDone() { return temp2state == DONE; }
  14. static void temp2done () { temp2state = DONE; }
  15. static void temp2gotBit (char value) {
  16. // X
  17. temp2x_numBits++;
  18. temp2_x_data = (temp2_x_data << 1) + (value & 0x01);
  19. temp2state = OK;
  20. }
  21. #define MARG 50
  22. static int temp2decode (unsigned int width) {
  23. switch (temp2state) {
  24. case T0: // First half of pulse : HIGH around 910
  25. if( temp2x_numBits == 48 ) { // end of frame
  26. temp2state = DONE;
  27. return 1;
  28. }
  29. else if ( (910-MARG) <= width && width <= (910+MARG) ) {
  30. temp2state = T1;
  31. }
  32. else {
  33. return -1; // error, reset
  34. }
  35. break;
  36. case T1:
  37. if ( (410-MARG) <= width && width <= (410+MARG)) { // 410
  38. temp2gotBit(1);
  39. } else if ( (1290-MARG) <= width && width <= (1290+MARG) ) { // 1290
  40. temp2gotBit(0);
  41. } else {
  42. return -1; // error, reset
  43. }
  44. temp2state = T0;
  45. break;
  46. }
  47. return 0;
  48. }
  49. boolean nextPulseTemp2 (unsigned int width) {
  50. if( width > 0 ) {
  51. if (temp2state != DONE)
  52. switch (temp2decode(width)) {
  53. case -1:
  54. temp2ResetDecoder();
  55. break;
  56. case 1: temp2done(); break;
  57. }
  58. }
  59. return temp2isDone();
  60. }