Skip to content

Commit

Permalink
Added fan control, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Noxet committed Sep 9, 2022
1 parent 73c693f commit 8145935
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
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 ".")
32 changes: 32 additions & 0 deletions firmware/main/fan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#include "fan.h"

#include "driver/ledc.h"

void Fan::init()
{
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};
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};
ESP_ERROR_CHECK(ledc_channel_config(&fan_channel));
}

void Fan::setSpeed(unsigned int val)
{
unsigned int duty = val / 100 * (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));
}
12 changes: 12 additions & 0 deletions firmware/main/fan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#pragma once

class Fan
{
public:
static void init();
/**
* Set the speed (duty cycle) between 0 - 100
*/
static void setSpeed(unsigned int val);
};
75 changes: 35 additions & 40 deletions firmware/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,24 @@
#include "routes.h"
#include "webserver.h"
#include "spi.h"
#include "fan.h"

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

#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 +76,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 @@ -135,13 +132,11 @@ void app_main()
* 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 +146,37 @@ 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);

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);
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 */
// );
}
29 changes: 17 additions & 12 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,20 @@ 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;

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
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;

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
}

ESP_ERROR_CHECK(spi_device_queue_trans(spi_handle_stop, &trans_desc_stop, portMAX_DELAY));
}

Fan::setSpeed(m_stopColor.r() / 255 * 100);
}

void TrafficLight::setGoColor(const Color &go)
{
Expand All @@ -64,9 +66,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));

Fan::setSpeed(m_goColor.r() / 255 * 100);
}

0 comments on commit 8145935

Please sign in to comment.