Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15 fan control #16

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firmware/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
idf_component_register(SRCS "trafficlight.cpp" "color.cpp" "routes.cpp" "main.cpp" "wifi.cpp" "webserver.cpp" "webresponse.cpp" "spi.cpp"
idf_component_register(SRCS "trafficlight.cpp" "color.cpp" "routes.cpp" "main.cpp" "wifi.cpp" "webserver.cpp" "webresponse.cpp" "spi.cpp" "fan.cpp"
INCLUDE_DIRS ".")
51 changes: 51 additions & 0 deletions firmware/main/fan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include "fan.h"

#include "driver/ledc.h"

void Fan::init(int gpioNum, uint32_t freqHz, uint32_t duty)
{
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 = {};
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::setGoIntensity(unsigned int goIntensity)
{
m_accumGo = goIntensity;
setSpeed();
}

void Fan::setStopIntensity(unsigned int stopIntensity)
{
m_accumStop = stopIntensity;
setSpeed();
}

void Fan::setSpeed()
{
// 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;
37 changes: 37 additions & 0 deletions firmware/main/fan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#pragma once

#include "hal/ledc_types.h"

class Fan
{
public:
/**
* 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)
*/
void setSpeed();
};

extern Fan fanControl;
78 changes: 38 additions & 40 deletions firmware/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,26 @@
#include "routes.h"
#include "webserver.h"
#include "spi.h"
#include "fan.h"

#define STATIC 0
#define SHIFTING 1
#define SWEEP 2
#define RAINBOW 3
#include "driver/ledc.h"

#define STATIC 0
#define SHIFTING 1
#define SWEEP 2
#define RAINBOW 3

using namespace std;

extern "C" {
extern "C"
{
void app_main();
}

const char *TAG = "Traffic Light";


void go_driver(void *pvParameters);




/*
void go_driver(void *pvParameters)
{
Expand Down Expand Up @@ -79,7 +78,7 @@ void go_driver(void *pvParameters)
led_color(data, led, 0x00, 0x7f, 0x00); // rgb
}
break;

case SHIFTING:

color_time = 0xff & esp_log_timestamp();
Expand Down Expand Up @@ -130,18 +129,19 @@ 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.
*/
ESP_ERROR_CHECK(
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, WebServer::connectHandler, nullptr, nullptr)
);
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, WebServer::connectHandler, nullptr, nullptr));

ESP_ERROR_CHECK(
esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, WebServer::disconnectHandler,
nullptr, nullptr)
);
nullptr, nullptr));

/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
Expand All @@ -151,37 +151,35 @@ void app_main()

// add handlers for the URLs
g_webServer.addHandler(&g_root)
.addHandler(&g_echo)
.addHandler(&g_about);

.addHandler(&g_echo)
.addHandler(&g_about);

// 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);
STOP_LED_DATA_PIN, STOP_LED_CLK_PIN, STOP_LED_NUMBER);
ESP_LOGI(TAG, "Done configuring SPI for STOP sign.");

traffic_spi_init(VSPI_HOST, SPI_DMA_CH2, &spi_handle_go, &trans_desc_go, go_data,
GO_LED_DATA_PIN, GO_LED_CLK_PIN, GO_LED_NUMBER);
GO_LED_DATA_PIN, GO_LED_CLK_PIN, GO_LED_NUMBER);
ESP_LOGI(TAG, "Done configuring SPI for GO sign.");

// xTaskCreatePinnedToCore(
// stop_driver, /* Task's function. */
// "Stop sign driver", /* Name of the task. */
// 10000, /* Stack size of the task */
// nullptr, /* Parameter of the task */
// 2, /* Priority of the task */
// nullptr, /* Task handle to keep track of created task */
// 1 /* Pin task to core 1 */
// );


// xTaskCreatePinnedToCore(
// go_driver, /* Task's function. */
// "Go sign driver", /* Name of the task. */
// 10000, /* Stack size of the task */
// NULL, /* Parameter of the task */
// 3, /* Priority of the task */
// NULL, /* Task handle to keep track of created task */
// 1 /* Pin task to core 1 */
// );
// xTaskCreatePinnedToCore(
// stop_driver, /* Task's function. */
// "Stop sign driver", /* Name of the task. */
// 10000, /* Stack size of the task */
// nullptr, /* Parameter of the task */
// 2, /* Priority of the task */
// nullptr, /* Task handle to keep track of created task */
// 1 /* Pin task to core 1 */
// );

// xTaskCreatePinnedToCore(
// go_driver, /* Task's function. */
// "Go sign driver", /* Name of the task. */
// 10000, /* Stack size of the task */
// NULL, /* Parameter of the task */
// 3, /* Priority of the task */
// NULL, /* Task handle to keep track of created task */
// 1 /* Pin task to core 1 */
// );
}
30 changes: 19 additions & 11 deletions firmware/main/trafficlight.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "trafficlight.h"
#include "spi.h"
#include "fan.h"

#include <iostream>
#include <esp_log.h>
Expand All @@ -22,7 +23,6 @@ TrafficLight::TrafficLight() : m_stopColor(UniColor("ff0000")), m_goColor(UniCol
gpio_config(&io_conf);
}


void TrafficLight::setStopColor(const Color &stop)
{
ESP_LOGI(TAG, "setStopColor(%x, %x, %x)", stop.r(), stop.g(), stop.b());
Expand All @@ -35,18 +35,23 @@ void TrafficLight::setStopColor(const Color &stop)
stop_data[3] = 0x00;

// End frame
stop_data[(STOP_LED_NUMBER+1)*4 + 0] = 0xff;
stop_data[(STOP_LED_NUMBER+1)*4 + 1] = 0xff;
stop_data[(STOP_LED_NUMBER+1)*4 + 2] = 0xff;
stop_data[(STOP_LED_NUMBER+1)*4 + 3] = 0xff;
stop_data[(STOP_LED_NUMBER + 1) * 4 + 0] = 0xff;
stop_data[(STOP_LED_NUMBER + 1) * 4 + 1] = 0xff;
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++) {
Color::colorToLed(stop_data, led, m_stopColor.r(), m_stopColor.g(), m_stopColor.b()); // rgb
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));
}

// include fan.h
}

void TrafficLight::setGoColor(const Color &go)
{
Expand All @@ -64,9 +69,12 @@ void TrafficLight::setGoColor(const Color &go)
go_data[(GO_LED_NUMBER + 1) * 4 + 2] = 0xff;
go_data[(GO_LED_NUMBER + 1) * 4 + 3] = 0xff;

for (uint8_t led = 1; led <= GO_LED_NUMBER; led++) {
Color::colorToLed(go_data, led, m_goColor.r(), m_goColor.g(), m_goColor.b()); // rgb
for (uint8_t led = 1; led <= GO_LED_NUMBER; led++)
{
Color::colorToLed(go_data, led, m_goColor.r(), m_goColor.g(), m_goColor.b()); // rgb
}

ESP_ERROR_CHECK(spi_device_queue_trans(spi_handle_go, &trans_desc_go, portMAX_DELAY));

// include fan.h
}