This commit is contained in:
j3d1 2021-11-03 19:14:18 +01:00
parent 1d4613e0d4
commit bce33fa836
3 changed files with 295 additions and 275 deletions

View file

@ -11,25 +11,27 @@
#include "esp/spi.h" #include "esp/spi.h"
#include "math.h" #include "math.h"
#include <time.h> #include <time.h>
#include <semphr.h>
#define DAYTIME(h,m,s) (h*3600+m*60+s) #define DAYTIME(h, m, s) (h*3600+m*60+s)
extern SemaphoreHandle_t time_available_semaphore;
struct { struct {
time_t sunrise_start = DAYTIME(5,30,0); time_t sunrise_start = DAYTIME(8, 30, 0);
time_t sunrise_end = DAYTIME(6,0,0); time_t sunrise_end = DAYTIME(9, 0, 0);
time_t sunrise_shutdown = DAYTIME(7,0,0); time_t sunrise_shutdown = DAYTIME(7, 0, 0);
time_t sunset_time = DAYTIME(22,0,0); time_t sunset_time = DAYTIME(22, 0, 0);
} settings; } settings;
struct { struct {
uint8_t r = 255; uint8_t r = 255;
uint8_t g = 160; uint8_t g = 160;
uint8_t b = 80; uint8_t b = 80;
} white; } white;
time_t day_seconds() { extern "C" time_t day_seconds() {
time_t t1, t2; time_t t1, t2;
struct tm tms; struct tm tms;
time(&t1); time(&t1);
@ -40,222 +42,249 @@ time_t day_seconds() {
t2 = mktime(&tms); t2 = mktime(&tms);
return t1 - t2; return t1 - t2;
} }
struct led_t{ struct led_t {
struct { struct {
unsigned int mod : 5, marker : 3; unsigned int mod: 5, marker: 3;
} __attribute__((packed)) global = {0x1F, 0x7}; } __attribute__((packed)) global = {0x1F, 0x7};
uint8_t b = 0; uint8_t b = 0;
uint8_t g = 0; uint8_t g = 0;
uint8_t r = 0; uint8_t r = 0;
}; };
led_t leds[4][10]; led_t leds[4][10];
float square(float x){ float square(float x) {
return x*x; return x * x;
} }
float lerp(float a, float b, float x){ float lerp(float a, float b, float x) {
return (1.-x)*a + x *b; return (1. - x) * a + x * b;
} }
float clamp(float x, float a, float b){ float clamp(float x, float a, float b) {
if(x<a) if(x < a)
return a; return a;
if(x>b) if(x > b)
return b; return b;
return x; return x;
} }
float fade(float x, float offset, float factor){ float fade(float x, float offset, float factor) {
float val = (x*factor-offset); float val = (x * factor - offset);
return lerp(square(clamp(val,0,1)),square(1-clamp(1-val,0,1)),val); return lerp(square(clamp(val, 0, 1)), square(1 - clamp(1 - val, 0, 1)), val);
} }
void write_leds(){ void write_leds() {
spi_transfer_32(1, 0x00000000); spi_transfer_32(1, 0x00000000);
for(int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t*)&leds[0][i]); spi_transfer_32(1, *(uint32_t *) &leds[0][i]);
for(int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t*)&leds[1][9-i]); spi_transfer_32(1, *(uint32_t *) &leds[1][9 - i]);
for(int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t*)&leds[2][i]); spi_transfer_32(1, *(uint32_t *) &leds[2][i]);
for(int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t*)&leds[3][9-i]); spi_transfer_32(1, *(uint32_t *) &leds[3][9 - i]);
spi_transfer_32(1, 0xFFFFFFFF); spi_transfer_32(1, 0xFFFFFFFF);
spi_transfer_32(1, 0xFFFFFFFF); spi_transfer_32(1, 0xFFFFFFFF);
} }
enum state_t{ BOOT_S, SUNRISE_S, MORNING_S, DAY_S, NIGHT_S }; enum state_t {
BOOT_S, SUNRISE_S, MORNING_S, DAY_S, NIGHT_S
};
state_t s = BOOT_S; volatile state_t s = BOOT_S;
bool maual_mode = false; volatile bool maual_mode = false;
extern "C" void manual_switch(){ extern "C" void manual_switch() {
maual_mode = !maual_mode; maual_mode = !maual_mode;
printf("%d\n", (int)maual_mode); printf("int %d\n", (int) maual_mode);
if(maual_mode){ if(maual_mode) {
if(s == NIGHT_S){ if(s == NIGHT_S) {
for(int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
for(int i = 0; i < 8; i+=2){ for (int i = 0; i < 8; i += 2) {
leds[j][i].global.mod = 2; leds[j][i].global.mod = 2;
leds[j][i].r = 128; leds[j][i].r = 128;
leds[j][i].g = 30; leds[j][i].g = 30;
leds[j][i].b = 15; leds[j][i].b = 15;
} }
}else{ } else {
for(int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
for(int i = 0; i < 10; i++){ for (int i = 0; i < 10; i++) {
leds[j][i].global.mod = 31; leds[j][i].global.mod = 31;
leds[j][i].r = white.r; leds[j][i].r = white.r;
leds[j][i].g = white.g; leds[j][i].g = white.g;
leds[j][i].b = white.b; leds[j][i].b = white.b;
} }
} }
write_leds(); write_leds();
}else{ } else {
for(int j = 0; j < 4; j++) for (int j = 0; j < 4; j++)
for(int i = 0; i < 10; i++){ for (int i = 0; i < 10; i++) {
leds[j][i].global.mod = 0; leds[j][i].global.mod = 0;
leds[j][i].r = 0; leds[j][i].r = 0;
leds[j][i].g = 0; leds[j][i].g = 0;
leds[j][i].b = 0; leds[j][i].b = 0;
} }
write_leds(); write_leds();
} }
} }
void loop(void *pvParameters) [[noreturn]] void loop(void *pvParameters) {
{ spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
while (!uxSemaphoreGetCount(time_available_semaphore))
while(1){ vTaskDelay(500 / portTICK_PERIOD_MS);
time_t rt = day_seconds(); maual_mode = false;
if(maual_mode){ for (int j = 0; j < 4; j++)
for (int i = 0; i < 10; i++) {
if(s == NIGHT_S){ leds[j][i].global.mod = 0;
leds[j][i].r = 0;
for(int j = 0; j < 4; j++) leds[j][i].g = 0;
for(int i = 0; i < 8; i+=2){ leds[j][i].b = 0;
leds[j][i].global.mod = 2; }
leds[j][i].r = 128; write_leds();
leds[j][i].g = 30;
leds[j][i].b = 15; vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}else{ printf("START>\n");
for(int j = 0; j < 4; j++) while (true) {
for(int i = 0; i < 10; i++){
leds[j][i].global.mod = 31; time_t rt = day_seconds();
leds[j][i].r = white.r;
leds[j][i].g = white.g; if(maual_mode) {
leds[j][i].b = white.b;
} if(s == NIGHT_S) {
}
for (int j = 0; j < 4; j++)
write_leds(); for (int i = 0; i < 8; i += 2) {
leds[j][i].global.mod = 2;
}else{ leds[j][i].r = 128;
leds[j][i].g = 30;
if( s == BOOT_S ) { leds[j][i].b = 15;
if( rt >= settings.sunrise_start ) { }
s = SUNRISE_S; } else {
printf("SUNRISE_S\n");
} for (int j = 0; j < 4; j++)
if( rt >= settings.sunrise_end ) { for (int i = 0; i < 10; i++) {
s = MORNING_S; leds[j][i].global.mod = 31;
printf("MORNING_S\n"); leds[j][i].r = white.r;
} leds[j][i].g = white.g;
} else if( s == SUNRISE_S ){ leds[j][i].b = white.b;
if( rt >= settings.sunrise_end ) { }
s = MORNING_S; }
printf("MORNING_S\n");
}else{ write_leds();
int steps = (settings.sunrise_end - settings.sunrise_start)*(1000/50);
int t = (rt - settings.sunrise_start)*(1000/50); } else {
for(; t < steps && !maual_mode; t++) {
if(s == BOOT_S) {
for(int j = 0; j < 4; j++) if(rt >= settings.sunrise_start) {
for(int i = 0; i < 10; i++){ s = SUNRISE_S;
float val = (-i*30.+t*3.*(4./5.))/(float)steps; printf("SUNRISE_S\n");
leds[j][i].global.mod = 8 + fade(val,0.,0.5)*23; }
leds[j][i].r = fade(val,0.01,1.) * white.r; if(rt >= settings.sunrise_end) {
leds[j][i].g = fade(val,0.1,0.5) * white.g; s = MORNING_S;
leds[j][i].b = fade(val,0.6,0.7) * white.b; printf("MORNING_S\n");
} }
if(rt >= settings.sunset_time || rt < settings.sunrise_start) {
write_leds(); s = NIGHT_S;
printf("NIGHT_S\n");
if ((t%25)==0) { }
printf("Time: %d%% %d %d/%d\n", (t*100)/steps, (int)day_seconds(), t, steps); printf("%lld | %lld %lld %lld %lld\n", rt,
printf("%d %d %d %d\n", settings.sunrise_start,
leds[0][0].global.mod, settings.sunrise_end,
leds[0][0].r, settings.sunset_time,
leds[0][0].g, settings.sunrise_shutdown);
leds[0][0].b); } else if(s == SUNRISE_S) {
} if(rt >= settings.sunrise_end) {
s = MORNING_S;
vTaskDelay(50/portTICK_PERIOD_MS); printf("MORNING_S\n");
} } else {
} int steps = (settings.sunrise_end - settings.sunrise_start) * (1000 / 50);
} else if( s == MORNING_S ) { int t = (rt - settings.sunrise_start) * (1000 / 50);
if( rt >= settings.sunrise_shutdown ) { for (; t < steps && !maual_mode; t++) {
s = DAY_S;
printf("DAY_S\n"); for (auto &led: leds)
}else{ for (int i = 0; i < 10; i++) {
for(int j = 0; j < 4; j++) float val = (-i * 30. + t * 3. * (4. / 5.)) / (float) steps;
for(int i = 0; i < 10; i++){ led[i].global.mod = 8 + fade(val, 0., 0.5) * 23;
leds[j][i].global.mod = 31; led[i].r = fade(val, 0.01, 1.) * white.r;
leds[j][i].r = white.r; led[i].g = fade(val, 0.1, 0.5) * white.g;
leds[j][i].g = white.g; led[i].b = fade(val, 0.6, 0.7) * white.b;
leds[j][i].b = white.b; }
}
write_leds(); write_leds();
}
} else if( s == DAY_S ) { if((t % 25) == 0) {
if( rt >= settings.sunset_time ) {
s = NIGHT_S; printf("Time: %d%% %d %d/%d\n", (t * 100) / steps, (int) day_seconds(), t, steps);
printf("NIGHT_S\n"); printf("%d %d %d %d\n",
}else{ leds[0][0].global.mod,
for(int j = 0; j < 4; j++) leds[0][0].r,
for(int i = 0; i < 10; i++){ leds[0][0].g,
leds[j][i].global.mod = 0; leds[0][0].b);
leds[j][i].r = 0; }
leds[j][i].g = 0;
leds[j][i].b = 0; vTaskDelay(50 / portTICK_PERIOD_MS);
} }
}
write_leds(); } else if(s == MORNING_S) {
} if(rt >= settings.sunrise_shutdown) {
} else if( s == NIGHT_S ) { s = DAY_S;
if( rt >= settings.sunrise_start && rt < settings.sunset_time) { printf("DAY_S\n");
s = SUNRISE_S; } else {
printf("SUNRISE_S\n"); for (int j = 0; j < 4; j++)
} for (int i = 0; i < 10; i++) {
} leds[j][i].global.mod = 31;
leds[j][i].r = white.r;
} leds[j][i].g = white.g;
leds[j][i].b = white.b;
vTaskDelay(1000/portTICK_PERIOD_MS); }
write_leds();
} }
} else if(s == DAY_S) {
if(rt >= settings.sunset_time) {
s = NIGHT_S;
printf("NIGHT_S\n");
} else {
for (int j = 0; j < 4; j++)
for (int i = 0; i < 10; i++) {
leds[j][i].global.mod = 0;
leds[j][i].r = 0;
leds[j][i].g = 0;
leds[j][i].b = 0;
}
write_leds();
}
} else if(s == NIGHT_S) {
if(rt >= settings.sunrise_start && rt < settings.sunset_time) {
s = SUNRISE_S;
printf("SUNRISE_S\n");
}
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} }
extern "C" void register_app(void) {
extern "C" void register_app(void) xTaskCreate(loop, "loop", 512, nullptr, 2, NULL);
{
xTaskCreate(loop, "loop", 1024, NULL, 2, NULL);
} }

View file

@ -36,45 +36,53 @@
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \ #define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org" "2.pool.ntp.org", "3.pool.ntp.org"
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS) #define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
#define UNUSED_ARG(x) (void)x #define UNUSED_ARG(x) (void)x
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG; const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
SemaphoreHandle_t time_available_semaphore;
time_t day_seconds();
void sntp_tsk(void *pvParameters)
{
const char *servers[] = {SNTP_SERVERS};
UNUSED_ARG(pvParameters);
/* Wait until we have joined AP and are assigned an IP */ void sntp_tsk(void *pvParameters) {
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) { const char *servers[] = {SNTP_SERVERS};
vTaskDelayMs(100); UNUSED_ARG(pvParameters);
}
/* Start SNTP */ while (!uxSemaphoreGetCount(wifi_available_semaphore)) {
printf("Starting SNTP... "); vTaskDelay(2000 / portTICK_PERIOD_MS);
/* SNTP will request an update each 5 minutes */ printf("waitfi\n");
sntp_set_update_delay(5*60000); }
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1*60, 1};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char*));
printf("DONE!\n");
/* Print date and time each 5 seconds */ /* Wait until we have joined AP and are assigned an IP */
while(1) { while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelayMs(5000); vTaskDelayMs(100);
//time_t ts = time(NULL); }
//int t = ts;
//printf("TIME: %d %d %s", t,(int) day_seconds(), ctime(&ts)); /* Start SNTP */
} printf("Starting SNTP... ");
/* SNTP will request an update each 5 minutes */
sntp_set_update_delay(5 * 60000);
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1 * 60, 1};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
printf("SNTP DONE!\n");
xSemaphoreGive(time_available_semaphore);
/* Print date and time each 5 seconds */
while (1) {
vTaskDelayMs(5000);
time_t ts = time(NULL);
int t = ts;
printf("TIME: %d %d %s", t, (int) day_seconds(), ctime(&ts));
}
} }
@ -111,41 +119,7 @@ void gpio_intr_handler(uint8_t gpio_num)
void register_app(void); void register_app(void);
void user_init(void) void user_init(void) {
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* required to call wifi_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
//netif_set_hostname(netif_default, "nachtlicht");
sdk_wifi_station_connect();
/* turn off LED */
gpio_enable(LED_PIN, GPIO_OUTPUT);
gpio_write(LED_PIN, true);
gpio_enable(SWITCH_PIN, GPIO_INPUT);
tsqueue = xQueueCreate(2, sizeof(uint32_t));
//xTaskCreate(buttonIntTask, "buttonIntTask", 256, &tsqueue, 2, NULL);
/* initialize tasks */
xTaskCreate(&httpd_task, "HTTP Daemon", 2048, NULL, 2, NULL);
xTaskCreate(&sntp_tsk, "SNTP", 512, NULL, 1, NULL);
register_app();
}
void user_init(void)
{
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version: %s\n", sdk_system_get_sdk_version()); printf("SDK version: %s\n", sdk_system_get_sdk_version());
@ -154,20 +128,33 @@ void user_init(void)
system_init_config(); system_init_config();
wifi_available_semaphore = xSemaphoreCreateBinary(); wifi_available_semaphore = xSemaphoreCreateBinary();
time_available_semaphore = xSemaphoreCreateBinary();
gpio_enable(SWITCH_PIN, GPIO_INPUT);
tsqueue = xQueueCreate(2, sizeof(uint32_t));
xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL); xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL);
xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); //xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL);
xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL); //xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
xTaskCreate(&sntp_tsk, "SNTP", 512, NULL, 1, NULL);
//xTaskCreate(buttonIntTask, "buttonIntTask", 256, &tsqueue, 2, NULL);
register_app();
gpio_enable(LED_PIN, GPIO_OUTPUT);
gpio_write(LED_PIN, true);
rboot_config conf = rboot_get_config(); rboot_config conf = rboot_get_config();
printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n", printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n",
conf.current_rom, conf.count); conf.current_rom, conf.count);
printf("Image addresses in flash:\r\n"); printf("Image addresses in flash:\r\n");
for(int i = 0; i <conf.count; i++) { for (int i = 0; i < conf.count; i++) {
printf("%c%d: offset 0x%08x\r\n", i == conf.current_rom ? '*':' ', i, conf.roms[i]); printf("%c%d: offset 0x%08x\r\n", i == conf.current_rom ? '*' : ' ', i, conf.roms[i]);
} }
printf("profit!\n"); printf("profit!\n");

View file

@ -358,6 +358,10 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_ap_ssid) free(wifi_ap_ssid); if(wifi_ap_ssid) free(wifi_ap_ssid);
if(wifi_ap_password) free(wifi_ap_password); if(wifi_ap_password) free(wifi_ap_password);
//gpio_enable(LED_PIN, GPIO_OUTPUT);
//gpio_write(LED_PIN, true);
xSemaphoreGive(wifi_available_semaphore); xSemaphoreGive(wifi_available_semaphore);
//monitor loop connection here //monitor loop connection here