Skip to content

Commit

Permalink
#15: Fan control working, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Noxet committed Oct 8, 2022
1 parent 8145935 commit 6e1acef
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
55 changes: 37 additions & 18 deletions firmware/main/fan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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));
}
}

Fan fanControl;
33 changes: 29 additions & 4 deletions firmware/main/fan.h
Original file line number Diff line number Diff line change
@@ -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);
};
void setSpeed();
};

extern Fan fanControl;
7 changes: 5 additions & 2 deletions firmware/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "spi.h"
#include "fan.h"

#include "driver/ledc.h"

#define STATIC 0
#define SHIFTING 1
#define SWEEP 2
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions firmware/main/trafficlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 6e1acef

Please sign in to comment.