Skip to content

Commit 8aa03e7

Browse files
committed
added Tx and Rx examples
1 parent fc39193 commit 8aa03e7

File tree

3 files changed

+258
-6
lines changed

3 files changed

+258
-6
lines changed

libraries/ESP32/examples/RMTLoopback/RMTLoopback.ino renamed to libraries/ESP32/examples/RMT/RMTLoopback/RMTLoopbakc.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@ void setup()
2626
Serial.println("init receiver failed\n");
2727
}
2828

29+
float realTick = rmtSetTick(rmt_send, 100);
30+
printf("real tick set to: %fns\n", realTick);
2931

30-
float realTick = rmtSetTick(rmt_send, 400);
31-
Serial.printf("real tick set to: %f\n", realTick);
32-
realTick = rmtSetTick(rmt_recv, 400);
33-
Serial.printf("real tick set to: %f\n", realTick);
34-
35-
events = xEventGroupCreate();
3632
}
3733

3834
void loop()
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 LED_MATRIX_SIZE 8*4*24
9+
10+
//
11+
// Note: This example uses Neopixel LED board, 32 LEDs chained one
12+
// after another, each RGB LED has its 24 bit value
13+
// for color configuration (8b for each color)
14+
//
15+
// Bits encoded as pulses as follows:
16+
//
17+
// "0":
18+
// ---+ +--------------+
19+
// | | |
20+
// | | |
21+
// | | |
22+
// | | |
23+
// +-------+ +--
24+
// | 0.4us | 0.85 0us |
25+
//
26+
// "1":
27+
// ---+ +-------+
28+
// | | |
29+
// | | |
30+
// | | |
31+
// | | |
32+
// +-------------+ +--
33+
// | 0.8us | 0.4us |
34+
35+
rmt_data_t led_data[LED_MATRIX_SIZE];
36+
37+
rmt_obj_t* rmt_send = NULL;
38+
39+
static EventGroupHandle_t events;
40+
41+
void setup()
42+
{
43+
Serial.begin(115200);
44+
45+
if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL)
46+
{
47+
Serial.println("init sender failed\n");
48+
}
49+
50+
float realTick = rmtSetTick(rmt_send, 100);
51+
Serial.printf("real tick set to: %fns\n", realTick);
52+
53+
}
54+
55+
int color[] = { 0x55, 0x11, 0x77 }; // RGB value
56+
int led_index = 0;
57+
58+
void loop()
59+
{
60+
// Init data with only one led ON
61+
int i;
62+
int item, bit, inx;
63+
for (i=0; i<LED_MATRIX_SIZE; i++) {
64+
65+
item = i/24;
66+
67+
bit = i%8;
68+
inx = i/8;
69+
70+
if ( (color[inx%3] & (1<<(8-bit))) && (item == led_index) )
71+
led_data[i].val = 0x80070006;
72+
else
73+
led_data[i].val = 0x80040008;
74+
}
75+
// make the led travel in the pannel
76+
if ((++led_index)>=LED_MATRIX_SIZE) {
77+
led_index = 0;
78+
}
79+
80+
// Send the data
81+
rmtWrite(rmt_send, led_data, LED_MATRIX_SIZE);
82+
83+
delay(100);
84+
}

0 commit comments

Comments
 (0)