Ok, I try to make the encoder for the Remote module from the ESP32:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int channel = (0);
#define MYLAPS_CLOCK_LOW_HIGH ( 80 / 5 / 2 ) // 80 Mhz base, then 5 Mhz for the carrier with 50% dutty
#define MYLAPS_CLOCK_PHASE_CHG ( MYLAPS_CLOCK_LOW_HIGH * 2 ) // 80 Mhz base, then 5 Mhz for the carrier for phase change
#define TELEGRAM_SIZE ( 12 * 8 * 4 ) /*!< Telegram has 12 bytes, so 96 bits */
#define RMT_TX_DATA_NUM 1 /*!< NEC tx test data number */
#define msg_len 12
#define msg_count 8
#define TRANS_COUNT 1
#define true ( 1 == 1 )
#define false ( 1 == 0 )
typedef struct {
union {
struct {
unsigned int duration0 :15;
unsigned int level0 :1;
unsigned int duration1 :15;
unsigned int level1 :1;
};
unsigned int val;
};
} rmt_item32_t;
// 2351957
static unsigned char radio_msg [msg_count*TRANS_COUNT][msg_len] =
{
{ 0XF9, 0X16, 0XE1, 0XCB, 0X12, 0X1C, 0XC9, 0XD6, 0XC3, 0XE0, 0XFF, 0X0F }, // Transponder ID Telegram
{ 0XF9, 0X16, 0XDA, 0XE7, 0X94, 0X77, 0XE9, 0X3C, 0X91, 0XD7, 0XC3, 0XCC }, // Status Telegram
{ 0XF9, 0X16, 0XEC, 0X50, 0X55, 0X92, 0XE2, 0X23, 0X61, 0XD4, 0XF0, 0X0C },
{ 0XF9, 0X16, 0X36, 0X58, 0X15, 0X1B, 0XC8, 0XC3, 0X62, 0X14, 0X3C, 0X00 },
{ 0XF9, 0X16, 0X0E, 0X29, 0XBA, 0XE0, 0X3E, 0XE3, 0X62, 0XDB, 0XC0, 0XC3 },
{ 0XF9, 0X16, 0X36, 0X55, 0X57, 0X09, 0XFB, 0X3F, 0X91, 0X27, 0X00, 0XF0 },
{ 0XF9, 0X16, 0X0E, 0XFE, 0XF0, 0X8A, 0X22, 0X3C, 0X52, 0X1B, 0X3F, 0XF3 },
{ 0XF9, 0X16, 0XD7, 0XA8, 0X10, 0X77, 0XD1, 0X23, 0XA2, 0XD7, 0XC3, 0X3C }
};
static int MyLaps_CreateTelegram(int channel, rmt_item32_t* item, int item_num, unsigned char *msg)
{
int i = 0,
j = 0;
int last_bit = ((0b010000000) & (msg[0])) ? true : false; // initialise the n-1 bits
int phase = false;
if(item_num < TELEGRAM_SIZE) // Check if we have space....
{
return -1;
}
for(j = 0; j < TELEGRAM_SIZE / 4 ; j++)
{
int current_bit = ((0b010000000 >> ( j % 8 )) & msg[ j / 8 ])? true : false;
printf("> %8d %8d %8d\t",current_bit,last_bit,j,j%8,j/8);
for( int b=0;b<8;b++)
{
printf("%d:%d ",current_bit,last_bit);
if( current_bit == last_bit ) // test if previsous bit is the same
{ // the and four cycles
if( i % 2 )
{
item[i/2].duration0 = MYLAPS_CLOCK_LOW_HIGH;
item[i/2].level0 = 0;
}
else
{
item[i/2].duration1 = MYLAPS_CLOCK_LOW_HIGH;
item[i/2].level1 = 1;
}
i++;
printf(" \t");
}
else // we have a phase change...
{
printf("<PC>\t");
phase = ! phase; // invert the phase
last_bit = current_bit; // copy the last_bit....
int l = i-1;
if( i > 0 ) // only to be sure we are not on start....
{
if( l % 2 )
{
item[l/2].duration0 = MYLAPS_CLOCK_PHASE_CHG;
}
else
{
item[l/2].duration1 = MYLAPS_CLOCK_PHASE_CHG;
}
}
else
printf("ERROR \n");
if( b )
printf("Something wrong at: %d,%d,%d - %d;%d ** ", i,b,j,current_bit,last_bit);
}
}
printf("\n");
last_bit = current_bit ; // copy the last_bit....
}
return i;
}
void rmt_write_items(int channel, rmt_item32_t *item, int item_num, int flag)
{
printf("\n");
int t = 0;
for( int i = 0; i<item_num; i ++ )
{
int d,l;
if( i % 2 )
{
d = item[i/2].duration0;
l = item[i/2].level0;
}
else
{
d = item[i/2].duration1;
l = item[i/2].level1;
}
if( d == 0 )
{
printf("\nEnd at %d ( %d,%d)",i,d,l);
break;
}
t += d;
if( d != MYLAPS_CLOCK_LOW_HIGH )
{
printf("%s", l ? "X X":"_ _" );
}
else
{
printf("%s", l ? "X":"_" );
}
if( ( t % ( MYLAPS_CLOCK_LOW_HIGH * 4 * 2 ) ) == 0 )
printf(" ");
}
printf("\n");
}
int main( int argc,char **argv)
{
size_t size = (sizeof(rmt_item32_t) * TELEGRAM_SIZE );
//each item represent a cycle of waveform.
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
int item_num = TELEGRAM_SIZE ;
int i,
offset = 0;
memset((void*) item, 0, size);
printf("Creating radio :\n");
i = MyLaps_CreateTelegram(channel, item , item_num,&radio_msg[0][0]);
printf(" inserted %d items \n",i);
rmt_write_items(channel, item, item_num, true);
return 0;
}