-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world_main.c
More file actions
198 lines (171 loc) · 6.21 KB
/
hello_world_main.c
File metadata and controls
198 lines (171 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
static const char *TAG = "led&hello";
#define RGBLED_GPIO (CONFIG_RGBLED_GPIO)
#define STEP_INTERVAL (CONFIG_LED_STEP_INTERVAL)
#define LED_MIN_INTENSITY (CONFIG_LED_MIN_INTENSITY)
#define LED_MAX_INTENSITY (CONFIG_LED_MAX_INTENSITY)
#define LED_TOTAL_CYCLES (CONFIG_LED_TOTAL_CYCLES)
#define RESTART_DELAY (CONFIG_RESTART_DELAY)
#define LED_START_0_3 (LED_MIN_INTENSITY)
#define LED_START_1_3 ((LED_MAX_INTENSITY-LED_MIN_INTENSITY) / 3u)
#define LED_START_2_3 (((LED_MAX_INTENSITY-LED_MIN_INTENSITY) * 2u) / 3u)
#define TIME_INTERVAL_1S (1000) // in [ms]
static led_strip_handle_t led_strip;
static void color_led(uint32_t red, uint32_t green, uint32_t blue);
static void color_led(uint32_t red, uint32_t green, uint32_t blue)
{
led_strip_set_pixel(led_strip, 0, red & 0xffu, green & 0xffu, blue & 0xffu);
led_strip_refresh(led_strip);
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = RGBLED_GPIO,
.max_leds = 1, // at least one LED on board
};
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
void app_main(void)
{
bool led_red_on;
bool led_green_on;
bool led_blue_on;
int cycles = LED_TOTAL_CYCLES;
printf("Hello world!\n");
printf("\nHey, I am imc and I am here in ESP32. Wait to see more from me!\n\n");
/* Print chip information */
esp_chip_info_t chip_info;
uint32_t flash_size;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
unsigned major_rev = chip_info.revision / 100;
unsigned minor_rev = chip_info.revision % 100;
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
printf("Get flash size failed");
return;
}
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
/* Configure the peripheral according to the LED type */
configure_led();
// blue sunrise, red sunset
uint32_t red, green, blue;
red = LED_START_0_3; green = LED_START_0_3; blue = LED_START_0_3;
bool redDir, greenDir, blueDir;
redDir = true; greenDir = true; blueDir = true;
led_red_on = false; led_green_on = false; led_blue_on = true;
ESP_LOGI(TAG,"========== Cycle %u begins", LED_TOTAL_CYCLES - (cycles - 1));
do {
color_led(red,green,blue);
if (led_red_on) {
if (redDir) {
++red;
if (red == (LED_MAX_INTENSITY)) {
redDir = false;
red = (LED_MAX_INTENSITY) - 1;
ESP_LOGI(TAG,"Red is fading down");
}
}
else {
--red;
if (red == (LED_MIN_INTENSITY)) {
if (cycles == 0) {
led_red_on = false;
}
else {
redDir = true;
ESP_LOGI(TAG,"Red is rising up");
}
}
}
}
if (led_green_on) {
if (greenDir) {
++green;
if (green == (LED_MAX_INTENSITY)) {
greenDir = false;
green = (LED_MAX_INTENSITY) - 1;
ESP_LOGI(TAG,"Green is fading down");
}
}
else {
--green;
if (green == (LED_MIN_INTENSITY)) {
if (cycles == 0) {
led_green_on = false;
}
else {
greenDir = true;
ESP_LOGI(TAG,"Green is rising up");
}
}
}
}
if (led_blue_on) {
if (blueDir) {
++blue;
if (!led_green_on && (blue == (LED_START_1_3))) {
led_green_on = true;
}
if (!led_red_on && (blue == (LED_START_2_3))) {
led_red_on = true;
}
if (blue == (LED_MAX_INTENSITY)) {
blueDir = false;
blue = (LED_MAX_INTENSITY) - 1;
ESP_LOGI(TAG,"Blue is fading down");
}
}
else {
--blue;
if (blue == (LED_MIN_INTENSITY)) {
if (--cycles == 0) {
led_blue_on = false;
}
else {
blueDir = true;
ESP_LOGI(TAG,"========== Cycle %u begins", LED_TOTAL_CYCLES - (cycles - 1));
ESP_LOGI(TAG,"Blue is rising up");
}
}
}
}
vTaskDelay((STEP_INTERVAL) / portTICK_PERIOD_MS);
} while(led_red_on || led_green_on || led_blue_on);
color_led(red,green,blue);
ESP_LOGI(TAG,"========== Lights are turned off. Good night.");
for (int i = RESTART_DELAY; i >= 0; i--) {
ESP_LOGI(TAG,"Restarting in %d seconds...", i);
vTaskDelay(TIME_INTERVAL_1S / portTICK_PERIOD_MS);
}
printf("Restarting now.\n");
fflush(stdout);
esp_restart();
}