temp2.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Temperature sensor/hydrometer ProoveSmart TSS320 (Kjells)
  2. #include "app.h"
  3. #include "nexa.h"
  4. #include "temp2.h"
  5. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  6. static unsigned char temp2state = T0;
  7. unsigned long long temp2_x_data;
  8. static unsigned int temp2x_numBits;
  9. void temp2ResetDecoder () {
  10. temp2_x_data = 0;
  11. temp2x_numBits = 0;
  12. temp2state = T0;
  13. }
  14. static boolean temp2isDone() { return temp2state == DONE; }
  15. static void temp2done () { temp2state = DONE; }
  16. static void temp2gotBit (char value) {
  17. // X
  18. temp2x_numBits++;
  19. temp2_x_data = (temp2_x_data << 1) + (value & 0x01);
  20. temp2state = OK;
  21. }
  22. // Pulse timings
  23. #define START_PULSE 860
  24. #define LONG_PULSE 1380
  25. #define SHORT_PULSE 480
  26. #define MARG 100
  27. static int temp2decode (unsigned int width) {
  28. switch (temp2state) {
  29. case T0: // First half of pulse : HIGH around 910
  30. if( temp2x_numBits == 48 ) { // end of frame
  31. temp2state = DONE;
  32. return 1;
  33. }
  34. else if ( (START_PULSE-MARG) <= width && width <= (START_PULSE+MARG) ) {
  35. temp2state = T1;
  36. }
  37. else {
  38. return -1; // error, reset
  39. }
  40. break;
  41. case T1:
  42. if ( (SHORT_PULSE-MARG) <= width && width <= (SHORT_PULSE+MARG)) { // 410
  43. temp2gotBit(1);
  44. } else if ( (LONG_PULSE-MARG) <= width && width <= (LONG_PULSE+MARG) ) { // 1290
  45. temp2gotBit(0);
  46. } else {
  47. return -1; // error, reset
  48. }
  49. temp2state = T0;
  50. break;
  51. }
  52. return 0;
  53. }
  54. #pragma optimize=none
  55. boolean nextPulseTemp2 (unsigned int width) {
  56. static int result;
  57. if( width > 0 ) {
  58. if( temp2state != DONE ) {
  59. result = temp2decode(width);
  60. switch( result ) {
  61. case -1:
  62. temp2ResetDecoder();
  63. break;
  64. case 1:
  65. temp2done();
  66. break;
  67. default:
  68. case 0:
  69. break;
  70. }
  71. }
  72. }
  73. return temp2isDone();
  74. }