#include <driver/rmt>
//#include "C:\Users\NUCi7\Documents\ArduinoData\packages\arduino\hardware\esp32\2.0.13\tools\sdk\esp32s3\include\driver\include\driver\rmt.h"
#define STEP_PIN GPIO_NUM_1
#define RMT_CHANNEL_FLAGS_INVERT_SIG (1 << 1)
// Reference https://esp-idf.readthedocs.io/en/v1.0/api/rmt.html
rmt_item32_t items[1];
const int pulsePin = 1; //HSYNCconst
int testsePin = 2;
const int redPin_0 = 4;
const int redPin_1 = 5;
const int redPin_2 = 6;
const int redPin_3 = 7;
const int redPin_4 = 8;
const int greenPin_0 = 9;
const int greenPin_1 = 10;
const int greenPin_2 = 11;
const int greenPin_3 = 12;
const int greenPin_4 = 13;
const int greenPin_5 = 14;
const int bluePin_0 = 15;
const int bluePin_1 = 16;
const int bluePin_2 = 17;
const int bluePin_3 = 18;
const int bluePin_4 = 21;
bool invert_signal = true; // Spécifie si le signal doit être inversé, ajustez selon vos besoins
bool oddEven=0; //0=even pair / 1=odd impair
int cpt = 0;
int increment = 1;
long cptColor = 1;
int redValue=0;
int greenValue=180;
int blueValue=0;
int a=4150; //4625 =
int b=59039; //59375 -
int c=2000; //2333 +
int d=29650; //29750 +
int e=27000; //27375 +
int f=4126; //4708 +
int nb_pulse=20;//305; //PAL 305
//int nb_pulse=??; //NTSC
static const char *TAG = "ppmGenerator";
static rmt_item32_t channelItems[] = {
{{{ 3000, 1, 300, 0 }}}, // channel 1 - variable pulse high followed by 300us pulse low.
{{{ 1000, 1, 300, 0 }}}, // channel 2
{{{ 1500, 1, 300, 0 }}}, // channel 3
{{{ 2000, 1, 300, 0 }}}, // channel 4
{{{ 1000, 1, 300, 0 }}}, // channel 5
{{{ 1500, 1, 300, 0 }}}, // channel 6
{{{ 2000, 1, 300, 0 }}}, // channel 7
{{{ 1000, 1, 300, 0 }}}, // channel 8
{{{ 7100, 1, 300, 0 }}} // sync pulse (22500 - sum of all channel pulses
};
//6-5-5 ODD IMPAIR
void genere_6_5_5(){
for (int i=1;i<=6;i++){genere_court();}
for (int i=1;i<=5;i++){genere_creux();}
for (int i=1;i<=5;i++){genere_court();}
}
//5-5-4 EVEN PAIR
void genere_5_5_4(){
for (int i=1;i<=5;i++){genere_court();}
for (int i=1;i<=5;i++){genere_creux();}
for (int i=1;i<=4;i++){genere_court();}
}
//A
void genere_long(){
items[0].duration0 = 1;
items[0].level0 = 1;
items[0].duration1 = 1; //(ajoute toujours 14uS
items[0].level1 = 0;
rmt_set_tx_loop_mode(RMT_CHANNEL_0, true);
rmt_write_items(RMT_CHANNEL_0, items, 1, false);
cpt=cpt+1;
}
//B
void genere_court(){
}
//C
void genere_creux(){
}
uint16_t item_count = sizeof(channelItems) / sizeof(channelItems[0]); // 9
uint16_t channels[] = {1000, 1100, 1200, 1400, 1500, 1700, 1900, 2000};
void setup() {
pinMode(testsePin, OUTPUT);
digitalWrite(testsePin, HIGH);
// put your setup code here, to run once:
rmt_config_t config;
config.rmt_mode = RMT_MODE_TX;
config.channel = RMT_CHANNEL_0;
config.gpio_num = STEP_PIN;//pulsePin;
config.mem_block_num = 1;
config.tx_config.loop_en = false;
config.tx_config.carrier_en = false;
config.tx_config.idle_output_en = 1;
//config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW; //RMT_IDLE_LEVEL_LOW;
//config.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;//RMT_CARRIER_LEVEL_HIGH
//config.tx_config.flags |= RMT_CHANNEL_FLAGS_INVERT_SIG;
config.clk_div = 80;//5; // 80MHx / 80 = 1MHz 0r 1uS per count
ESP_ERROR_CHECK(rmt_driver_install(RMT_CHANNEL_0, 0, 0)); // rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int rmt_intr_num)
ESP_ERROR_CHECK(rmt_config(&config));
//esp_err_t result = rmt_set_gpio(config.channel, config.rmt_mode, config.gpio_num, invert_signal);
rmt_set_tx_intr_en(RMT_CHANNEL_0, false); // turn off Tx interrupt
/*
items[0].duration0 = 1;
items[0].level0 = 1;
items[0].duration1 = 1;
items[0].level1 = 0;
*/
rmt_set_tx_loop_mode(RMT_CHANNEL_0, true); //
rmt_write_items(RMT_CHANNEL_0, channelItems, item_count, false); //write train
}
void loop() {
// esp_err_t rmt_write_items(rmt_channel_t channel, rmt_item32_t *rmt_item, int item_num, bool wait_tx_done)
//rmt_write_items(config.channel, items, 1, 0);
//delay(1);
//genere_long();
/*
if (cpt>=nb_pulse){
if (oddEven) {
genere_6_5_5();
}
else {
genere_5_5_4();
}
cpt=0;
oddEven=!oddEven;
}
//digitalWrite(testsePin, !digitalRead(testsePin));
*/
}