From 1b08f64cbc4edc038eef39640875e24dada823a2 Mon Sep 17 00:00:00 2001 From: aleigh Date: Tue, 26 Apr 2022 17:24:09 -0700 Subject: [PATCH] leigh-modcon-controller: Working PWM example --- leigh-modcon-controller/include/mecha.h | 1 + leigh-modcon-controller/include/pwm.h | 1 + leigh-modcon-controller/src/pwm.c | 46 +++++++++++++++++++++++++ leigh-modcon-controller/src/sacn.c | 4 +++ 4 files changed, 52 insertions(+) create mode 100644 leigh-modcon-controller/include/pwm.h create mode 100644 leigh-modcon-controller/src/pwm.c diff --git a/leigh-modcon-controller/include/mecha.h b/leigh-modcon-controller/include/mecha.h index 3ede9979d..6cd5fa569 100644 --- a/leigh-modcon-controller/include/mecha.h +++ b/leigh-modcon-controller/include/mecha.h @@ -2,5 +2,6 @@ #define _MECHA_H void netFmc_listener(void (*pkt_handler)(const char *buf, size_t buf_len)); +void pwmFset(float left_duty_fraction, float right_duty_fraction); #endif \ No newline at end of file diff --git a/leigh-modcon-controller/include/pwm.h b/leigh-modcon-controller/include/pwm.h new file mode 100644 index 000000000..b22ce6844 --- /dev/null +++ b/leigh-modcon-controller/include/pwm.h @@ -0,0 +1 @@ +void pwmFinit(void); diff --git a/leigh-modcon-controller/src/pwm.c b/leigh-modcon-controller/src/pwm.c new file mode 100644 index 000000000..2b8578c3a --- /dev/null +++ b/leigh-modcon-controller/src/pwm.c @@ -0,0 +1,46 @@ +#include +#include "driver/ledc.h" +#include "esp_err.h" + +#define RIGHT_PWM_PIN GPIO_NUM_12 +#define LEFT_PWM_PIN GPIO_NUM_13 + +void pwmFinit(void) +{ + ledc_channel_config_t ledc_channel_left = {0}, ledc_channel_right = {0}; + ledc_channel_left.gpio_num = LEFT_PWM_PIN; + ledc_channel_left.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_channel_left.channel = LEDC_CHANNEL_1; + ledc_channel_left.intr_type = LEDC_INTR_DISABLE; + ledc_channel_left.timer_sel = LEDC_TIMER_1; + ledc_channel_left.duty = 0; + + ledc_channel_right.gpio_num = RIGHT_PWM_PIN; + ledc_channel_right.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_channel_right.channel = LEDC_CHANNEL_2; + ledc_channel_right.intr_type = LEDC_INTR_DISABLE; + ledc_channel_right.timer_sel = LEDC_TIMER_1; + ledc_channel_right.duty = 0; + + ledc_timer_config_t ledc_timer = {0}; + ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE; + ledc_timer.bit_num = LEDC_TIMER_10_BIT; + ledc_timer.timer_num = LEDC_TIMER_1; + ledc_timer.freq_hz = 25000; + + ESP_ERROR_CHECK( ledc_channel_config(&ledc_channel_left) ); + ESP_ERROR_CHECK( ledc_channel_config(&ledc_channel_right) ); + ESP_ERROR_CHECK( ledc_timer_config(&ledc_timer) ); +} + +void pwmFset(float left_duty_fraction, float right_duty_fraction) { + uint32_t max_duty = (1 << LEDC_TIMER_10_BIT) - 1; + uint32_t left_duty = 512; + uint32_t right_duty = 512; + + ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1, left_duty) ); + ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_1) ); + + ESP_ERROR_CHECK( ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2, right_duty) ); + ESP_ERROR_CHECK( ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_2) ); +} \ No newline at end of file diff --git a/leigh-modcon-controller/src/sacn.c b/leigh-modcon-controller/src/sacn.c index 68d3f97d5..f5170fd3d 100644 --- a/leigh-modcon-controller/src/sacn.c +++ b/leigh-modcon-controller/src/sacn.c @@ -3,6 +3,7 @@ #include "sacn.h" #include "mecha.h" +#include "pwm.h" static const char *TAG = "sacn"; static void sacnFpkt_handler(const char *buf, size_t buf_len); @@ -16,6 +17,7 @@ static void sacnFpkt_handler(const char *buf, size_t buf_len); */ void sacnFtask(void (*sacn_handler)(const sacnTe131_packet *pkt)) { + pwmFinit(); netFmc_listener(sacnFpkt_handler); } @@ -38,4 +40,6 @@ static void sacnFpkt_handler(const char *buf, size_t buf_len) pkt->dmp.prop_val[2], pkt->dmp.prop_val[3], pkt->dmp.prop_val[4]); + + pwmFset(.5,.5); } \ No newline at end of file -- GitLab