diff --git a/firmware/main/fan.cpp b/firmware/main/fan.cpp index 8233a53..3cbeb13 100644 --- a/firmware/main/fan.cpp +++ b/firmware/main/fan.cpp @@ -3,30 +3,49 @@ #include "driver/ledc.h" -void Fan::init() +void Fan::init(int gpioNum, uint32_t freqHz, uint32_t duty) { - ledc_timer_config_t fan_timer = { - .speed_mode = LEDC_LOW_SPEED_MODE, - .duty_resolution = LEDC_TIMER_12_BIT, - .timer_num = LEDC_TIMER_0, - .freq_hz = 100, - .clk_cfg = LEDC_AUTO_CLK}; + ledc_timer_config_t fan_timer = {}; + fan_timer.speed_mode = LEDC_LOW_SPEED_MODE; + fan_timer.duty_resolution = LEDC_TIMER_12_BIT; + fan_timer.timer_num = LEDC_TIMER_0; + fan_timer.freq_hz = freqHz; + fan_timer.clk_cfg = LEDC_USE_APB_CLK; ESP_ERROR_CHECK(ledc_timer_config(&fan_timer)); - ledc_channel_config_t fan_channel = { - .gpio_num = 16, - .speed_mode = LEDC_LOW_SPEED_MODE, - .channel = LEDC_CHANNEL_0, - .intr_type = LEDC_INTR_DISABLE, - .timer_sel = LEDC_TIMER_0, - .duty = 0, // Set duty to 0% - .hpoint = 0}; + ledc_channel_config_t fan_channel = {}; + fan_channel.gpio_num = gpioNum; + fan_channel.speed_mode = LEDC_LOW_SPEED_MODE; + fan_channel.channel = LEDC_CHANNEL_0; + fan_channel.intr_type = LEDC_INTR_DISABLE; + fan_channel.timer_sel = LEDC_TIMER_0; + fan_channel.duty = duty; + ESP_ERROR_CHECK(ledc_channel_config(&fan_channel)); } -void Fan::setSpeed(unsigned int val) +void Fan::setGoIntensity(unsigned int goIntensity) +{ + m_accumGo = goIntensity; + setSpeed(); +} + +void Fan::setStopIntensity(unsigned int stopIntensity) +{ + m_accumStop = stopIntensity; + setSpeed(); +} + +void Fan::setSpeed() { - unsigned int duty = val / 100 * (1 << 12); + // Normalized intensity, 121 pixels in total, 8-bit per pixel + float totalIntensity = (m_accumGo + m_accumStop) * 0.001080f; + int fanIntensity = static_cast(totalIntensity); + if (fanIntensity >= 98) fanIntensity = 100; // might as well.. + + unsigned int duty = fanIntensity / 100.0 * (1 << 12); ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, duty)); ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); -} \ No newline at end of file +} + +Fan fanControl; \ No newline at end of file diff --git a/firmware/main/fan.h b/firmware/main/fan.h index e32719a..a7e0817 100644 --- a/firmware/main/fan.h +++ b/firmware/main/fan.h @@ -1,12 +1,37 @@ #pragma once +#include "hal/ledc_types.h" + class Fan { public: - static void init(); /** - * Set the speed (duty cycle) between 0 - 100 + * Initializes the fan controller to default values: + * - GPIO: 16 + * - CLK: 10 kHz + * - Duty: 0 + */ + void init(int gpioNum = 16, uint32_t freqHz = 10000, uint32_t duty = 0); + + /** + * Go intensity between 0 - 46665 (61 pixels) + */ + void setGoIntensity(unsigned int goIntensity); + + /** + * Stop intensity between 0 - 45900 (60 pixels) + */ + void setStopIntensity(unsigned int stopIntensity); + +private: + unsigned int m_accumGo; + unsigned int m_accumStop; + + /** + * Sets the speed based on sign intensities (duty: 0-100) */ - static void setSpeed(unsigned int val); -}; \ No newline at end of file + void setSpeed(); +}; + +extern Fan fanControl; \ No newline at end of file diff --git a/firmware/main/main.cpp b/firmware/main/main.cpp index c45ff4d..44e749a 100644 --- a/firmware/main/main.cpp +++ b/firmware/main/main.cpp @@ -21,6 +21,8 @@ #include "spi.h" #include "fan.h" +#include "driver/ledc.h" + #define STATIC 0 #define SHIFTING 1 #define SWEEP 2 @@ -127,6 +129,9 @@ void app_main() ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); + fanControl.init(); + fanControl.setGoIntensity(10000); + /** * Register two events and bind the callbacks to the WiFi object. * We want to know when we have an IP, and when we disconnet from the WiFi. @@ -149,8 +154,6 @@ void app_main() .addHandler(&g_echo) .addHandler(&g_about); - Fan::init(); - // initialize SPI for stop and go sign traffic_spi_init(HSPI_HOST, SPI_DMA_CH1, &spi_handle_stop, &trans_desc_stop, stop_data, STOP_LED_DATA_PIN, STOP_LED_CLK_PIN, STOP_LED_NUMBER); diff --git a/firmware/main/trafficlight.cpp b/firmware/main/trafficlight.cpp index 759f617..2649e29 100644 --- a/firmware/main/trafficlight.cpp +++ b/firmware/main/trafficlight.cpp @@ -40,14 +40,17 @@ void TrafficLight::setStopColor(const Color &stop) stop_data[(STOP_LED_NUMBER + 1) * 4 + 2] = 0xff; stop_data[(STOP_LED_NUMBER + 1) * 4 + 3] = 0xff; + unsigned int accumIntensity = 0; + for (uint8_t led = 1; led <= STOP_LED_NUMBER; led++) { + accumIntensity += m_stopColor.r() + m_stopColor.g() + m_goColor.b(); Color::colorToLed(stop_data, led, m_stopColor.r(), m_stopColor.g(), m_stopColor.b()); // rgb } ESP_ERROR_CHECK(spi_device_queue_trans(spi_handle_stop, &trans_desc_stop, portMAX_DELAY)); - Fan::setSpeed(m_stopColor.r() / 255 * 100); + // include fan.h } void TrafficLight::setGoColor(const Color &go) @@ -73,5 +76,5 @@ void TrafficLight::setGoColor(const Color &go) ESP_ERROR_CHECK(spi_device_queue_trans(spi_handle_go, &trans_desc_go, portMAX_DELAY)); - Fan::setSpeed(m_goColor.r() / 255 * 100); + // include fan.h }