123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #include "toshiba_ir.h"
- #include <stdint.h>
- #include <stdio.h>
- #include "esp_log.h"
- #include "rxTimer.h"
- enum
- {
- UNKNOWN,
- T0,
- T1,
- T2,
- T3,
- TEMP1_OK,
- DONE
- };
- static uint8_t rx_state = UNKNOWN;
- static uint64_t sensor_data;
- static uint32_t rx_numBits;
- void Toshiba_ir_ResetDecoder()
- {
- sensor_data = 0;
- rx_numBits = 0;
- rx_state = UNKNOWN;
- }
- static void addBit(uint8_t value)
- {
- rx_numBits++;
- sensor_data = (sensor_data << 1) + (value & 0x01);
- rx_state = TEMP1_OK;
- }
- #define START_PULSE_MIN (3880-200)
- #define START_PULSE_MAX (4010+200)
- #define T0_PULSE_MIN (380-100)
- #define T0_PULSE_MAX (520+100)
- #define SHORT_PULSE_MIN (890-100)
- #define SHORT_PULSE_MAX (990+100)
- #define LONG_PULSE_MIN (1880-150)
- #define LONG_PULSE_MAX (1980+150)
- static int32_t rx_decode(uint32_t width)
- {
- switch (rx_state) {
- case UNKNOWN: // Start of frame
- if ( START_PULSE_MIN <= width && width <= START_PULSE_MAX )
- {
- rx_state = T0;
- }
- else
- {
- return -1; // error, reset
- }
- break;
- case T0: // First half of pulse : HIGH around 230us
- if (rx_numBits == 32)
- { // end of frame
- rx_state = DONE;
- sensor_data = (sensor_data >> 8); // Mask away some bits at the end
- return 1;
- }
- else if( T0_PULSE_MIN <= width && width <= T0_PULSE_MAX )
- {
- rx_state = T1;
- }
- else
- {
- if (rx_numBits == 0 && START_PULSE_MIN <= width && width <= START_PULSE_MAX )
- {
- rx_state = T0;
- }
- else
- {
- return -1; // error, reset
- }
- }
- break;
- case T1:
- if( SHORT_PULSE_MIN <= width && width <= SHORT_PULSE_MAX )
- {
- addBit(0);
- }
- else if( LONG_PULSE_MIN <= width && width <= LONG_PULSE_MAX )
- {
- addBit(1);
- }
- else
- {
- return -1; // error, reset
- }
- rx_state = T0;
- break;
- }
- return 0;
- }
- int64_t nextPulseToshiba_ir(uint32_t width)
- {
- static int64_t previous_data = 0;
- static uint32_t old_time=0;
- static uint32_t now;
- int64_t retVal = -1;
- if (width > 0)
- {
- if (rx_state != DONE)
- {
- switch (rx_decode(width))
- {
- case -1:
- Toshiba_ir_ResetDecoder();
- break;
- case 1:
- rx_state = DONE;
- break;
- }
- }
- }
- if (rx_state == DONE) {
- now = millis();
- if( sensor_data != previous_data || (now > (old_time+1000)) ) {
- previous_data = sensor_data;
- retVal = sensor_data;
-
- }
- old_time = now;
- Toshiba_ir_ResetDecoder();
- }
-
- return retVal;
- }
|