/**
This is starting point for this event.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include <max7219.h>
/***************************************************
MACROS
* ************************************************/
#define TAG "EXPLORER"
#define ONBOARD_LED GPIO_NUM_2
#define RED_LED GPIO_NUM_14
#define YELLOW_LED GPIO_NUM_12
#define GREEN_LED GPIO_NUM_13
/* Task configuration*/
#define TASK_STACK_SIZE 2048
#define TASK_PRIO_HIGH (configMAX_PRIORITIES - 1)
#define TASK_PRIO_LOW (configMAX_PRIORITIES - 5)
#define TASK_PRIO_SYS (configMAX_PRIORITIES - 10)
#define TASK_PERIOD_10MS 10
#define TASK_PERIOD_20MS 20
#define TASK_PERIOD_ALIVE 500
/* SPI configuration for MAX7219 */
/* SPI in use*/
#define HOST SPI3_HOST
/* PIN Configuration */
#define SPI_CONFIG_MOSI_PIN GPIO_NUM_19
#define SPI_CONFIG_CLK_PIN GPIO_NUM_18
#define SPI_CONFIG_CS_PIN GPIO_NUM_15
/* Number of 8x8 displays in array*/
#define CONFIG_EXAMPLE_CASCADE_SIZE 3
/***************************************************
Device configuration
* ************************************************/
/**
Max7219 device configuration
*/
max7219_t dev =
{
.cascade_size = CONFIG_EXAMPLE_CASCADE_SIZE,
.digits = 0,
.mirrored = true
};
/***************************************************
Function prototypes
* ************************************************/
/**
Function declarations
*/
static void system_init(void);
static void task_10ms_high_prio(void *pvParameters);
static void task_20ms_low_prio(void *pvParameters);
static void aliveness_task(void *pvParameters);
/**
Initialization of system LED
*/
void led_init(void)
{
ESP_LOGI(TAG, "LED initialization");
/* Configuration of the system aliveness LED*/
gpio_reset_pin(ONBOARD_LED);
gpio_reset_pin(RED_LED);
gpio_reset_pin(YELLOW_LED);
gpio_reset_pin(GREEN_LED);
/* Set the GPIO as a push/pull output */
gpio_set_direction(ONBOARD_LED, GPIO_MODE_OUTPUT);
gpio_set_direction(RED_LED, GPIO_MODE_OUTPUT);
gpio_set_direction(YELLOW_LED, GPIO_MODE_OUTPUT);
gpio_set_direction(GREEN_LED, GPIO_MODE_OUTPUT);
}
/**
Initialization of SPI communication for MAX7219 LED matrix
*/
void spi_max7219_init(void)
{
// Configure SPI bus
spi_bus_config_t cfg =
{
.mosi_io_num = SPI_CONFIG_MOSI_PIN,
.miso_io_num = -1,
.sclk_io_num = SPI_CONFIG_CLK_PIN,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
.flags = 0
};
ESP_ERROR_CHECK(spi_bus_initialize(HOST, &cfg, 1));
ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, SPI_CONFIG_CS_PIN));
ESP_ERROR_CHECK(max7219_init(&dev));
ESP_ERROR_CHECK(max7219_set_brightness(&dev, 10));
}
/***************************************************
RUNNABLES
* ************************************************/
/*
Initialization, called at system start
Place here all system initialisation
*/
static void system_init(void)
{
/* Initialize all LED */
led_init();
/* Initialization of matrix display */
spi_max7219_init();
}
/**
Runnable for system allivenes blinking
*/
void aliveness_led()
{
static uint8_t sys_led_state = 0x00;
gpio_set_level(ONBOARD_LED, sys_led_state);
/* Toggle LED state for the next call*/
sys_led_state = !sys_led_state;
}
void red_aliveness_led()
{
static uint8_t red_led_state = 0x00;
gpio_set_level(RED_LED, red_led_state);
/* Toggle LED state for the next call*/
red_led_state = !red_led_state;
}
/* enum traffic_light_state {
STATE_RED = 0,
STATE_RED_AND_YELLOW = 1,
STATE_GREEN = 2
STATE_GREEN_TRANSITION = 3,
STATE_YELLOW = 4
}*/
void traffic_light()
{
static uint8_t red_led_state = 0x00;
static uint8_t yellow_led_state = 0x00;
static uint8_t green_led_state = 0x00;
// static enum traffic_light_state state = STATE_RED;
static uint8_t traffic_light_count = 0x00;
if (traffic_light_count == 0) {
red_led_state = 0x01;
yellow_led_state = 0x00;
green_led_state = 0x00;
}
if (traffic_light_count == 6) {
red_led_state = 0x01;
yellow_led_state = 0x01;
green_led_state = 0x00;
}
if (traffic_light_count == 12) {
red_led_state = 0x00;
yellow_led_state = 0x00;
green_led_state = 0x01;
}
if (traffic_light_count >= 14 && traffic_light_count <= 19) {
red_led_state = 0x00;
yellow_led_state = 0x00;
green_led_state = !green_led_state;
}
if (traffic_light_count == 20) {
red_led_state = 0x00;
yellow_led_state = 0x01;
green_led_state = 0x00;
}
gpio_set_level(RED_LED, red_led_state);
gpio_set_level(YELLOW_LED, yellow_led_state);
gpio_set_level(GREEN_LED, green_led_state);
++traffic_light_count;
if (traffic_light_count == 24) {
traffic_light_count = 0x00;
}
}
/***************************************************
TASKS
* ************************************************/
/*
High priority task called every 10ms
*/
static void task_10ms_high_prio(void *pvParameters)
{
TickType_t task_called_time = xTaskGetTickCount();
static const uint64_t symbols[] =
{
0x383838fe7c381000,
0x3c4281a524242400,
};
while (1)
{
/* Call your runnables here*/
/* Set next call in 10ms */
vTaskDelayUntil(&task_called_time, pdMS_TO_TICKS(TASK_PERIOD_10MS));
}
}
/*
Low priority task called every 10ms
*/
static void task_20ms_low_prio(void *pvParameters)
{
TickType_t task_called_time = xTaskGetTickCount();
const uint64_t PANIC[] = {
0x3030201400221c00,
0x1818100a00110e00,
0x000018100a00110e,
0x000030201400221c,
0x0000604028004438,
0x0060402800443800
};
static uint8_t image_count2 = 0x00;
while (1)
{
/* Call your runnables here*/
if(image_count2 % 4 == 0) {
max7219_draw_image_8x8(&dev, 0x08, &PANIC[image_count2 % 6]);
}
++image_count2;
/* Set next call in 20ms */
vTaskDelayUntil(&task_called_time, pdMS_TO_TICKS(TASK_PERIOD_20MS));
}
}
/*
Task used for showing alivenes of the system
*/
static void aliveness_task(void *pvParameters)
{
TickType_t task_called_time = xTaskGetTickCount();
const uint64_t SMILE[] = {
0x00242424a581423c,
0x000474048581423c,
};
static uint8_t image_toggle = 0x00;
while (1)
{
/* Call your runnables here*/
aliveness_led();
// red_aliveness_led();
traffic_light();
if (image_toggle % 4 == 2 || image_toggle % 4 == 3) {
max7219_draw_image_8x8(&dev, 0x00, &SMILE);
} else {
max7219_draw_image_8x8(&dev, 0x00, &SMILE[1]);
}
++image_toggle;
/* Set next call in 500ms */
vTaskDelayUntil(&task_called_time, pdMS_TO_TICKS(TASK_PERIOD_ALIVE));
}
}
/***************************************************
MAIN
* ************************************************/
/*
Main function used for initialization of the system and task creation
*/
void app_main(void)
{
/* Start with initialization */
ESP_LOGI(TAG, "System is booting up \n");
system_init();
ESP_LOGI(TAG, "System should be initialized \n");
/* Create all tasks*/
xTaskCreate(task_10ms_high_prio, "high_prio_10ms", TASK_STACK_SIZE, NULL, TASK_PRIO_HIGH, NULL);
xTaskCreate(task_20ms_low_prio, "low_prio_20ms", TASK_STACK_SIZE, NULL, TASK_PRIO_LOW, NULL);
xTaskCreate(aliveness_task, "aliveness", TASK_STACK_SIZE, NULL, TASK_PRIO_SYS, NULL);
ESP_LOGI(TAG, "Tasks are created, system started \n");
}