|
| 1 | +#include "freertos/FreeRTOS.h" |
| 2 | +#include "freertos/task.h" |
| 3 | +#include "freertos/event_groups.h" |
| 4 | +#include "Arduino.h" |
| 5 | + |
| 6 | +#include "esp32-hal-rmt.h" |
| 7 | + |
| 8 | +#define XJT_VALID(i) (i->level0 && !i->level1 && i->duration0 >= 8 && i->duration0 <= 11) |
| 9 | + |
| 10 | + |
| 11 | +rmt_obj_t* rmt_recv = NULL; |
| 12 | + |
| 13 | +static uint32_t *s_channels; |
| 14 | + |
| 15 | +static uint32_t channels[16]; |
| 16 | +static uint8_t xjt_flags = 0x0; |
| 17 | +static uint8_t xjt_rxid = 0x0; |
| 18 | + |
| 19 | + |
| 20 | +typedef union { |
| 21 | + struct { |
| 22 | + uint8_t head;//0x7E |
| 23 | + uint8_t rxid;//Receiver Number |
| 24 | + uint8_t flags;//Range:0x20, Bind:0x01 |
| 25 | + uint8_t reserved0;//0x00 |
| 26 | + union { |
| 27 | + struct { |
| 28 | + uint8_t ch0_l; |
| 29 | + uint8_t ch0_h:4; |
| 30 | + uint8_t ch1_l:4; |
| 31 | + uint8_t ch1_h; |
| 32 | + }; |
| 33 | + uint8_t bytes[3]; |
| 34 | + } channels[4]; |
| 35 | + uint8_t reserved1;//0x00 |
| 36 | + uint8_t crc_h; |
| 37 | + uint8_t crc_l; |
| 38 | + uint8_t tail;//0x7E |
| 39 | + }; |
| 40 | + uint8_t buffer[20]; |
| 41 | +} xjt_packet_t; |
| 42 | + |
| 43 | +static bool xjtReceiveBit(size_t index, bool bit){ |
| 44 | + static xjt_packet_t xjt; |
| 45 | + static uint8_t xjt_bit_index = 8; |
| 46 | + static uint8_t xht_byte_index = 0; |
| 47 | + static uint8_t xht_ones = 0; |
| 48 | + |
| 49 | + if(!index){ |
| 50 | + xjt_bit_index = 8; |
| 51 | + xht_byte_index = 0; |
| 52 | + xht_ones = 0; |
| 53 | + } |
| 54 | + |
| 55 | + if(xht_byte_index > 19){ |
| 56 | + //fail! |
| 57 | + return false; |
| 58 | + } |
| 59 | + if(bit){ |
| 60 | + xht_ones++; |
| 61 | + if(xht_ones > 5 && xht_byte_index && xht_byte_index < 19){ |
| 62 | + //fail! |
| 63 | + return false; |
| 64 | + } |
| 65 | + //add bit |
| 66 | + xjt.buffer[xht_byte_index] |= (1 << --xjt_bit_index); |
| 67 | + } else if(xht_ones == 5 && xht_byte_index && xht_byte_index < 19){ |
| 68 | + xht_ones = 0; |
| 69 | + //skip bit |
| 70 | + return true; |
| 71 | + } else { |
| 72 | + xht_ones = 0; |
| 73 | + //add bit |
| 74 | + xjt.buffer[xht_byte_index] &= ~(1 << --xjt_bit_index); |
| 75 | + } |
| 76 | + if ((!xjt_bit_index) || (xjt_bit_index==1 && xht_byte_index==19) ) { |
| 77 | + xjt_bit_index = 8; |
| 78 | + if(!xht_byte_index && xjt.buffer[0] != 0x7E){ |
| 79 | + //fail! |
| 80 | + return false; |
| 81 | + } |
| 82 | + xht_byte_index++; |
| 83 | + if(xht_byte_index == 20){ |
| 84 | + //done |
| 85 | + if(xjt.buffer[19] != 0x7E){ |
| 86 | + //fail! |
| 87 | + return false; |
| 88 | + } |
| 89 | + //check crc? |
| 90 | + |
| 91 | + xjt_flags = xjt.flags; |
| 92 | + xjt_rxid = xjt.rxid; |
| 93 | + for(int i=0; i<4; i++){ |
| 94 | + uint16_t ch0 = xjt.channels[i].ch0_l | ((uint16_t)(xjt.channels[i].ch0_h & 0x7) << 8); |
| 95 | + ch0 = ((ch0 * 2) + 2452) / 3; |
| 96 | + uint16_t ch1 = xjt.channels[i].ch1_l | ((uint16_t)(xjt.channels[i].ch1_h & 0x7F) << 4); |
| 97 | + ch1 = ((ch1 * 2) + 2452) / 3; |
| 98 | + uint8_t c0n = i*2; |
| 99 | + if(xjt.channels[i].ch0_h & 0x8){ |
| 100 | + c0n += 8; |
| 101 | + } |
| 102 | + uint8_t c1n = i*2+1; |
| 103 | + if(xjt.channels[i].ch1_h & 0x80){ |
| 104 | + c1n += 8; |
| 105 | + } |
| 106 | + s_channels[c0n] = ch0; |
| 107 | + s_channels[c1n] = ch1; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +void parseRmt(rmt_data_t* items, size_t len, uint32_t* channels){ |
| 115 | + size_t chan = 0; |
| 116 | + bool valid = false; |
| 117 | + rmt_data_t* it = NULL; |
| 118 | + |
| 119 | + if (!channels) { |
| 120 | + log_e("Please provide data block for storing channel info"); |
| 121 | + return; |
| 122 | + } |
| 123 | + s_channels = channels; |
| 124 | + |
| 125 | + valid = false; |
| 126 | + it = &items[0]; |
| 127 | + for(size_t i = 0; i<len; i++){ |
| 128 | + if(!valid){ |
| 129 | + break; |
| 130 | + } |
| 131 | + it = &items[i]; |
| 132 | + if(XJT_VALID(it)){ |
| 133 | + if(it->duration1 >= 5 && it->duration1 <= 8){ |
| 134 | + valid = xjtReceiveBit(i, false); |
| 135 | + } else if(it->duration1 >= 13 && it->duration1 <= 16){ |
| 136 | + valid = xjtReceiveBit(i, true); |
| 137 | + } else { |
| 138 | + valid = false; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +extern "C" void receive_data(uint32_t *data, size_t len) |
| 145 | +{ |
| 146 | + parseRmt((rmt_data_t*) data, len, channels); |
| 147 | +} |
| 148 | + |
| 149 | +void setup() |
| 150 | +{ |
| 151 | + Serial.begin(115200); |
| 152 | + |
| 153 | + // Initialize the channel to capture up to 192 items |
| 154 | + if ((rmt_recv = rmtInit(21, false, RMT_MEM_192)) == NULL) |
| 155 | + { |
| 156 | + Serial.println("init receiver failed\n"); |
| 157 | + } |
| 158 | + |
| 159 | + // Setup 1us tick |
| 160 | + float realTick = rmtSetTick(rmt_recv, 1000); |
| 161 | + printf("real tick set to: %fns\n", realTick); |
| 162 | + |
| 163 | + // Ask to start reading |
| 164 | + rmtRead(rmt_recv, receive_data); |
| 165 | +} |
| 166 | + |
| 167 | +void loop() |
| 168 | +{ |
| 169 | + // printout some of the channels |
| 170 | + Serial.printf("%04x %04x %04x %04x\n", channels[0], channels[1], channels[2], channels[3]); |
| 171 | + delay(500); |
| 172 | +} |
0 commit comments