Compare commits

...

16 commits

Author SHA1 Message Date
f9b13d4c06 stash
Some checks failed
continuous-integration/drone/push Build is failing
2021-11-21 00:37:10 +01:00
875b3a8999 stash 2021-11-21 00:37:10 +01:00
084161eba9 stash 2021-11-21 00:37:10 +01:00
bce33fa836 stash 2021-11-21 00:37:07 +01:00
1d4613e0d4 stash 2021-11-21 00:35:56 +01:00
f6681d54de stash
Some checks failed
continuous-integration/drone/push Build is failing
2021-11-21 00:35:27 +01:00
d96504d3eb stash 2021-11-21 00:35:27 +01:00
e2e3b249bf stash 2021-11-21 00:35:27 +01:00
874ca62eee websocket goes brrrrrrrrrrrrrrr 2021-11-21 00:35:27 +01:00
22b240a472 stash
All checks were successful
continuous-integration/drone/push Build is passing
2021-11-21 00:34:53 +01:00
4b23e5bc09 stash 2021-11-21 00:34:53 +01:00
b72426b9c7 stash 2021-11-21 00:34:53 +01:00
7f422a2ddd generic output channel 2021-11-21 00:34:53 +01:00
769351684b stash 2021-11-21 00:34:53 +01:00
78934b85c2 stash 2021-11-21 00:34:53 +01:00
e6d6757982 stash 2021-11-21 00:34:53 +01:00
12 changed files with 1173 additions and 32 deletions

7
firmware/config.sample Normal file
View file

@ -0,0 +1,7 @@
sunrise_start: time
sunrise_end: time
sunrise_shutdown: time
sunrise_color: color
sunrise_fade: linear, ease, ease_in, ease_out
sunset_time: time
night_color: color

View file

@ -1,18 +1,63 @@
#include "system.h"
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
#include "lux.h"
#include <esp8266.h>
#include <esp/uart.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <task.h>
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include "system.h"
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
#include "ntp.h"
#include "lux.h"
void user_init(void)
#include <espressif/esp_common.h>
#include "rboot-api.h"
#define LED_PIN 2
#define SWITCH_PIN 2
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
time_t day_seconds();
void gpio_intr_handler(uint8_t gpio_num);
void manual_switch(void);
_Noreturn void buttonIntTask(void *pvParameters) {
printf("Waiting for button press interrupt on gpio %d...\r\n", SWITCH_PIN);
QueueHandle_t *tsqueue = (QueueHandle_t *) pvParameters;
gpio_set_interrupt(SWITCH_PIN, int_type, gpio_intr_handler);
uint32_t last = 0;
while (1) {
uint32_t button_ts;
xQueueReceive(*tsqueue, &button_ts, portMAX_DELAY);
button_ts *= portTICK_PERIOD_MS;
if(last < button_ts-200) {
manual_switch();
//printf("Button interrupt fired at %dms\r\n", button_ts);
last = button_ts;
}
}
}
static QueueHandle_t tsqueue;
void gpio_intr_handler(uint8_t gpio_num)
{
uint32_t now = xTaskGetTickCountFromISR();
xQueueSendToBackFromISR(tsqueue, &now, NULL);
}
void register_app(void);
void user_init(void) {
uart_set_baud(0, 115200);
printf("SDK version: %s\n", sdk_system_get_sdk_version());
@ -21,10 +66,38 @@ void user_init(void)
system_init_config();
wifi_available_semaphore = xSemaphoreCreateBinary();
wan_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(&httpd_task, "httpd_task", 1024, NULL, 2, NULL);
xTaskCreate(&httpd_task, "httpd_task", 2048, NULL, 2, NULL);
xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
//xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
xTaskCreate(&sntp_task, "sntp_task", 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();
printf("\r\n\r\nOTA Basic demo.\r\nCurrently running on flash slot %d / %d.\r\n\r\n",
conf.current_rom, conf.count);
printf("Image addresses in flash:\r\n");
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("profit!\n");
*/
}

View file

@ -9,24 +9,297 @@
#include <task.h>
#include <esp/spi.h>
#include <ws2812_i2s/ws2812_i2s.h>
const int signal_led_pin = 2;
extern "C" {
#include <sysparam.h>
}
const int cs0 = 15;
const int gpio4 = 4;
const int gpio5 = 5;
struct apa10xx_pixel_t {
struct {
unsigned int mod: 5, marker: 3;
} __attribute__((packed)) global = {0x1F, 0x7};
uint8_t b = 0;
uint8_t g = 0;
uint8_t r = 0;
};
static ws2812_pixel_t next_colour(int i) {
ws2812_pixel_t colour = {{0, 0, 0, 0}};
colour.red = 0;
colour.green = 0;
colour.blue = 0;
colour.white = 8;
return colour;
}
static apa10xx_pixel_t next_color(int i) {
apa10xx_pixel_t colour;
colour.global.mod = 8;
colour.r = 16;
colour.g = 16;
colour.b = 16;
return colour;
}
namespace fiatlux {
struct hal_error_t {
constexpr hal_error_t() = default;
hal_error_t(const char *) {}
hal_error_t(const char *, hal_error_t *cause) {}
};
constexpr hal_error_t empty_error;
enum class hal_module_t {
NONE, SIGNAL, RELAIS, SPI_DIMMER, WS28X, APA10X
};
namespace ports {
hal_module_t spi = hal_module_t::NONE;
hal_module_t uart = hal_module_t::NONE;
hal_module_t gpio2 = hal_module_t::NONE;
hal_module_t gpio4 = hal_module_t::NONE;
hal_module_t gpio5 = hal_module_t::NONE;
hal_module_t gpio15 = hal_module_t::NONE;
}
namespace signal {
void write_data(bool data) {
gpio_write(2, !data);
}
void setup() {
gpio_enable(2, GPIO_OUTPUT);
}
}
namespace relais {
void write_data(bool a, bool b) {
gpio_write(4, a);
gpio_write(5, b);
}
void write_data(uint8_t data[2]) {
write_data((bool) data[0], (bool) data[1]);
}
void setup() {
gpio_enable(4, GPIO_OUTPUT);
gpio_enable(5, GPIO_OUTPUT);
}
}
namespace spi_dimmer {
constexpr int cs0 = 15;
void write_data(uint16_t data[6]) {
for (int i = 0; i < 6; ++i) {
int dac_val = (data[i] << 2) & 0x3FFC;
spi_transfer_8(1, ~(0x00));
gpio_write(cs0, true);
gpio_write(cs0, false);
spi_transfer_8(1, ~(0x01 << i));
gpio_write(cs0, true);
gpio_write(cs0, false);
spi_transfer_16(1, dac_val);
spi_transfer_8(1, ~(0x00));
gpio_write(cs0, true);
gpio_write(cs0, false);
spi_transfer_8(1, ~(0x01 << i));
gpio_write(cs0, true);
gpio_write(cs0, false);
}
}
void setup() {
gpio_enable(cs0, GPIO_OUTPUT);
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
}
}
namespace ws28x {
void write_data(ws2812_pixel_t *data) {
ws2812_i2s_update(data, PIXEL_RGBW);
}
void setup(size_t len) {
ws2812_i2s_init(len, PIXEL_RGBW);
}
}
namespace apa10x {
void write_data(apa10xx_pixel_t *data, size_t len) {
spi_transfer_32(1, 0x00000000);
for (size_t i = 0; i < len; i++)
spi_transfer_32(1, *(uint32_t *) &data[i]);
//spi_transfer_32(1, *(uint32_t *) &data[len - 1]); // dunno maybe this helps
//spi_transfer_32(1, *(uint32_t *) &data[len - 1]); // dunno maybe this helps
spi_transfer_32(1, 0xFFFFFFFF);
spi_transfer_32(1, 0xFFFFFFFF);
}
void setup() {
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
}
}
hal_error_t write_channel(uint8_t *data, size_t count, size_t stride, hal_module_t mod) {
if(mod == hal_module_t::SIGNAL) {
if(count != 1)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
signal::write_data(data[0]);
} else if(mod == hal_module_t::RELAIS) {
if(count != 2)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
relais::write_data(data);
} else if(mod == hal_module_t::SPI_DIMMER) {
if(count != 6)
return "unsupported value for count";
if(stride != 2)
return "unsupported value for stride";
spi_dimmer::write_data((uint16_t *) data);
} else if(mod == hal_module_t::WS28X) {
if(stride != 4)
return "unsupported value for stride";
ws28x::write_data((ws2812_pixel_t *) data);
} else if(mod == hal_module_t::APA10X) {
if(stride != 4)
return "unsupported value for stride";
apa10x::write_data((apa10xx_pixel_t *) data, count);
} else {
return "unsupported module";
}
return empty_error;
}
hal_error_t setup_channel(size_t count, size_t stride, hal_module_t mod) {
/*if(mod == hal_module_t::SIGNAL) {
if(count != 1)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
signal::write_data(data[0]);
} else if(mod == hal_module_t::RELAIS) {
if(count != 2)
return "unsupported value for count";
if(stride != 1)
return "unsupported value for stride";
relais::write_data(data);
} else if(mod == hal_module_t::SPI_DIMMER) {
if(count != 6)
return "unsupported value for count";
if(stride != 2)
return "unsupported value for stride";
spi_dimmer::write_data((uint16_t *) data);
} else if(mod == hal_module_t::WS28X) {
if(stride != 4)
return "unsupported value for stride";
ws28x::write_data((ws2812_pixel_t *) data, count);
} else if(mod == hal_module_t::APA10X) {
if(stride != 4)
return "unsupported value for stride";
apa10x::write_data((apa10xx_pixel_t *) data, count);
} else {
return "unsupported module";
}*/
return empty_error;
}
}
//ws2812_pixel_t **pixels_ptr;
extern "C" void signal_led(bool state) {
fiatlux::signal::write_data(state);
}
extern "C" void white_led(bool state) {
gpio_write(signal_led_pin, !state);
}
extern "C" void lux_task(void *pvParameters) {
gpio_enable(signal_led_pin, GPIO_OUTPUT);
gpio_enable(cs0, GPIO_OUTPUT);
gpio_enable(gpio4, GPIO_OUTPUT);
gpio_enable(gpio5, GPIO_OUTPUT);
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
*
*/
extern "C" [[noreturn]] void lux_task(void *pvParameters) {
vTaskDelete(nullptr);
int32_t lux_ws2812_number = 40;
auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number);
if(ret != SYSPARAM_OK)
lux_ws2812_number = 40;
int32_t lux_apa10xx_number = 40;
ret = sysparam_get_int32("lux_apa10xx_number", &lux_apa10xx_number);
if(ret != SYSPARAM_OK)
lux_apa10xx_number = 40;
ws2812_pixel_t pixels[lux_ws2812_number];
ws2812_i2s_init(lux_ws2812_number, PIXEL_RGBW);
memset(pixels, 0, sizeof(ws2812_pixel_t) * lux_ws2812_number);
apa10xx_pixel_t leds[lux_apa10xx_number];
//lux_apa102c_number
//gpio_enable(9, GPIO_INPUT);
//gpio_enable(10, GPIO_INPUT);
//fiatlux::spi_dimmer::setup();
fiatlux::signal::setup();
fiatlux::relais::setup();
fiatlux::apa10x::setup();
while (true) {
/*for (int j = 0; j < 64; j++) {
for (int i = 0; i < 8; i++)
spi_dac(i, 64 * j);
//printf("> %d\n", 64*j);
vTaskDelay(100 / portTICK_PERIOD_MS);
}*/
/*gpio_write(gpio4, 1);
vTaskDelay(200 / portTICK_PERIOD_MS);
gpio_write(gpio4, 0);
for (int i = 0; i < 8; i++)
spi_dac(i, 0);
gpio_write(gpio5, 1);
vTaskDelay(200 / portTICK_PERIOD_MS);
gpio_write(gpio5, 0);*/
fiatlux::signal::write_data(false);
for (int c = 8; c >= 0; c--) {
for (auto &pixel: pixels) {
pixel = next_colour(c);
}
for (auto &led: leds) {
led = next_color(c);
}
ws2812_i2s_update(pixels, PIXEL_RGBW);
fiatlux::write_channel((uint8_t *) &leds[0], lux_apa10xx_number, 4, fiatlux::hal_module_t::APA10X);
vTaskDelay(200 / portTICK_PERIOD_MS);
}
fiatlux::relais::write_data(true, false);
vTaskDelay(200 / portTICK_PERIOD_MS);
fiatlux::relais::write_data(false, true);
vTaskDelay(200 / portTICK_PERIOD_MS);
fiatlux::relais::write_data(false, false);
fiatlux::signal::write_data(true);
vTaskDelay(200 / portTICK_PERIOD_MS);
}
}

296
firmware/noctis.cpp Normal file
View file

@ -0,0 +1,296 @@
/* Example SPI transfert
*
* This sample code is in the public domain.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp8266.h"
#include <stdio.h>
#include "esp/spi.h"
#include "math.h"
#include <time.h>
#include <semphr.h>
#include "ntp.h"
#include "log.h"
#define DAYTIME(h, m, s) (h*3600+m*60+s)
struct {
time_t sunrise_start = DAYTIME(8, 30, 0);
time_t sunrise_end = DAYTIME(9, 0, 0);
time_t sunrise_shutdown = DAYTIME(7, 0, 0);
time_t sunset_time = DAYTIME(22, 0, 0);
} settings;
struct {
uint8_t r = 255;
uint8_t g = 160;
uint8_t b = 80;
} white;
extern "C" time_t day_seconds() {
time_t t1, t2;
struct tm tms;
time(&t1);
localtime_r(&t1, &tms);
tms.tm_hour = 0;
tms.tm_min = 0;
tms.tm_sec = 0;
t2 = mktime(&tms);
return t1 - t2;
}
struct led_t {
struct {
unsigned int mod: 5, marker: 3;
} __attribute__((packed)) global = {0x1F, 0x7};
uint8_t b = 0;
uint8_t g = 0;
uint8_t r = 0;
};
led_t leds[4][10];
float square(float x) {
return x * x;
}
float lerp(float a, float b, float x) {
return (1. - x) * a + x * b;
}
float clamp(float x, float a, float b) {
if(x < a)
return a;
if(x > b)
return b;
return x;
}
float fade(float x, float offset, float factor) {
float val = (x * factor - offset);
return lerp(square(clamp(val, 0, 1)), square(1 - clamp(1 - val, 0, 1)), val);
}
void write_leds() {
spi_transfer_32(1, 0x00000000);
for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t *) &leds[0][i]);
for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t *) &leds[1][9 - i]);
for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t *) &leds[2][i]);
for (int i = 0; i < 10; i++)
spi_transfer_32(1, *(uint32_t *) &leds[3][9 - i]);
spi_transfer_32(1, 0xFFFFFFFF);
spi_transfer_32(1, 0xFFFFFFFF);
}
enum state_t {
BOOT_S, SUNRISE_S, MORNING_S, DAY_S, NIGHT_S
};
volatile state_t s = BOOT_S;
volatile bool maual_mode = false;
extern "C" void manual_switch() {
maual_mode = !maual_mode;
printf("int %d\n", (int) maual_mode);
if(maual_mode) {
if(s == NIGHT_S) {
for (int j = 0; j < 4; j++)
for (int i = 0; i < 8; i += 2) {
leds[j][i].global.mod = 2;
leds[j][i].r = 128;
leds[j][i].g = 30;
leds[j][i].b = 15;
}
} else {
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;
}
}
write_leds();
} 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();
}
}
[[noreturn]] void loop(void *pvParameters) {
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_LITTLE_ENDIAN, false);
while (!uxSemaphoreGetCount(time_available_semaphore))
vTaskDelay(500 / portTICK_PERIOD_MS);
maual_mode = false;
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();
vTaskDelay(1000 / portTICK_PERIOD_MS);
syslog("START>\n");
while (true) {
time_t rt = day_seconds();
if(maual_mode) {
if(s == NIGHT_S) {
for (int j = 0; j < 4; j++)
for (int i = 0; i < 8; i += 2) {
leds[j][i].global.mod = 2;
leds[j][i].r = 128;
leds[j][i].g = 30;
leds[j][i].b = 15;
}
} else {
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;
}
}
write_leds();
} else {
if(s == BOOT_S) {
if(rt >= settings.sunrise_start) {
s = SUNRISE_S;
syslog("SUNRISE_S\n");
}
if(rt >= settings.sunrise_end) {
s = MORNING_S;
syslog("MORNING_S\n");
}
if(rt >= settings.sunset_time || rt < settings.sunrise_start) {
s = NIGHT_S;
syslog("NIGHT_S\n");
}
printf("%ld | %ld %ld %ld %ld\n", rt,
settings.sunrise_start,
settings.sunrise_end,
settings.sunset_time,
settings.sunrise_shutdown);
} else if(s == SUNRISE_S) {
if(rt >= settings.sunrise_end) {
s = MORNING_S;
syslog("MORNING_S\n");
} else {
int steps = (settings.sunrise_end - settings.sunrise_start) * (1000 / 50);
int t = (rt - settings.sunrise_start) * (1000 / 50);
for (; t < steps && !maual_mode; t++) {
for (auto &led: leds)
for (int i = 0; i < 10; i++) {
float val = (-i * 30. + t * 3. * (4. / 5.)) / (float) steps;
led[i].global.mod = 8 + fade(val, 0., 0.5) * 23;
led[i].r = fade(val, 0.01, 1.) * white.r;
led[i].g = fade(val, 0.1, 0.5) * white.g;
led[i].b = fade(val, 0.6, 0.7) * white.b;
}
write_leds();
if((t % 25) == 0) {
printf("Time: %d%% %d %d/%d\n", (t * 100) / steps, (int) day_seconds(), t, steps);
printf("%d %d %d %d\n",
leds[0][0].global.mod,
leds[0][0].r,
leds[0][0].g,
leds[0][0].b);
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
} else if(s == MORNING_S) {
if(rt >= settings.sunrise_shutdown) {
s = DAY_S;
syslog("DAY_S\n");
} else {
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;
}
write_leds();
}
} else if(s == DAY_S) {
if(rt >= settings.sunset_time) {
s = NIGHT_S;
syslog("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;
syslog("SUNRISE_S\n");
}
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
/*time_t ts = time(nullptr);
int t = ts;
printf("TIME: %d %d %s", t, (int) day_seconds(), ctime(&ts));*/
}
}
extern "C" void register_app(void) {
xTaskCreate(loop, "loop", 512, nullptr, 2, NULL);
}

53
firmware/ntp.cpp Normal file
View file

@ -0,0 +1,53 @@
//
// Created by jedi on 04.11.21.
//
#include "ntp.h"
#include "wifi.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Add extras/sntp component to makefile for this include to work */
#include <sntp.h>
#include <time.h>
#ifdef __cplusplus
}
#endif
#include <stdio.h>
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
SemaphoreHandle_t time_available_semaphore = nullptr;
void sntp_task(void *pvParameters) {
const char *servers[] = {SNTP_SERVERS};
(void) pvParameters;
while (!uxSemaphoreGetCount(wan_available_semaphore)) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
/* 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 */
vTaskDelete(nullptr);
}

23
firmware/ntp.h Normal file
View file

@ -0,0 +1,23 @@
//
// Created by jedi on 04.11.21.
//
#ifndef FIRMWARE_NTP_H
#define FIRMWARE_NTP_H
#include <FreeRTOS.h>
#include <semphr.h>
#ifdef __cplusplus
extern "C" {
#endif
extern SemaphoreHandle_t time_available_semaphore;
void sntp_task(void *pvParameters);
#ifdef __cplusplus
}
#endif
#endif //FIRMWARE_NTP_H

View file

@ -24,12 +24,13 @@
void system_clear_config() {
vPortEnterCritical();
uint32_t num_sectors = 0x2000 / sdk_flashchip.sector_size;
//uint32_t start = sdk_flashchip.chip_size - num_sectors * sdk_flashchip.sector_size;
uint32_t start = 0x00100000;
for (uint32_t i = 0; i < num_sectors; i++) {
spiflash_erase_sector(start + i * sdk_flashchip.sector_size);
}
if(sysparam_create_area(start, num_sectors, true) == SYSPARAM_OK) {
sysparam_init(start, 0);
sysparam_init(start, start + 0x2000);
}
sysparam_init(start, start + 0x2000);
sdk_system_restart();
@ -38,18 +39,19 @@ void system_clear_config() {
void system_init_config() {
uint32_t base_addr = 0x00100000;
uint32_t num_sectors;
sysparam_init(base_addr, 0);
sysparam_init(base_addr, base_addr + 0x2000);
if(sysparam_get_info(&base_addr, &num_sectors) != SYSPARAM_OK) {
syslog("Warning: WiFi config, sysparam not initialized\n");
num_sectors = 0x2000 / sdk_flashchip.sector_size;
//base_addr = sdk_flashchip.chip_size - (5 + num_sectors) * sdk_flashchip.sector_size;
if(sysparam_create_area(base_addr, num_sectors, true) == SYSPARAM_OK) {
sysparam_init(base_addr, 0);
sysparam_init(base_addr, base_addr + 0x2000);
}
sdk_system_restart();
}
}
#define MAX_IMAGE_SIZE 0x100000
#define MAX_IMAGE_SIZE 0x100000 /*1MB images max at the moment */
struct {
rboot_write_status status;
@ -67,10 +69,14 @@ void system_otaflash_init() {
otaflash_context.status = rboot_write_init(otaflash_context.base);
otaflash_context.head = otaflash_context.base;
otaflash_context.seq = 0;
//printf("slot: %u, base: %x, sector: %u\n", otaflash_context.slot, otaflash_context.base,
// otaflash_context.status.start_sector);
}
enum return_code system_otaflash_chunk(uint8_t *data, uint16_t len, uint16_t seq, uint32_t hash, uint16_t *ack) {
uint32_t local_hash = crc32(data, len);
//printf("@%x seq: %u, len: %u, hash: %x =? %x\n", otaflash_context.head, seq, len, hash, local_hash);
if(hash == local_hash && otaflash_context.seq == seq) {
if(otaflash_context.head % SECTOR_SIZE == 0) {
sdk_spi_flash_erase_sector(otaflash_context.head / SECTOR_SIZE);

View file

@ -204,7 +204,21 @@ void websocket_task(void *pvParameter) {
}
/*vTaskDelayMs(250);
{
uint8_t response[3];
uint16_t val;
val = sdk_system_adc_read();
response[2] = (uint8_t) val;
response[1] = val >> 8;
response[0] = 'V';
LOCK_TCPIP_CORE();
websocket_write(pcb, response, 3, WS_BIN_MODE);
UNLOCK_TCPIP_CORE();
}*/
vTaskDelayMs(500);
//printf("9: %d\n",gpio_read(9));
//printf("10: %d\n",gpio_read(10));
}
syslog_detach();
@ -245,9 +259,16 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
ret = ERROR;
val = 0;
int8_t en = 0;
bool togl = false;
switch (data[0]) {
case 'V': // ADC
/* This should be done on a separate thread in 'real' applications */
cmd = 'V';
ret = OK;
val = sdk_system_adc_read();
break;
case 'R': // Restart
cmd = 'R';
ret = OK;
@ -270,6 +291,18 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
ret = OK;
val = 0;
break;
case 'd': // Disable LED
signal_led(false);
cmd = 'G';
ret = OK;
val = 1;
break;
case 'e': // Enable LED
signal_led(true);
cmd = 'G';
ret = OK;
val = 0;
break;
case 'F':
togl = !togl;
signal_led(togl);
@ -292,6 +325,70 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
}
cmd = 'C';
break;
case 'T': {
auto A = *(int32_t *) &data[4];
auto B = *(int32_t *) &data[8];
auto C = *(int32_t *) &data[12];
auto D = *(int32_t *) &data[16];
/*sysparam_set_int32("noctis_A", A);
sysparam_set_int32("noctis_B", B);
sysparam_set_int32("noctis_C", C);
sysparam_set_int32("noctis_D", D);*/
}
cmd = 'T';
break;
case 'N': {
if(data[1] == 'W')
printf("WS2812\n");
else if(data[1] == 'A')
printf("APA103C\n");
else if(data[1] == 'Z')
printf("APA103C-Z\n");
auto num_leds = *(uint16_t *) &data[2];
/*sysparam_set_int32("noctis_leds", num_leds);*/
}
cmd = 'N';
break;
case 'S': {
if(data[1] == 'E')
en = 1;
char *ssid = &data[2];
size_t ssid_len = strlen(ssid);
char *password = &data[3 + ssid_len];
size_t password_len = strlen(password);
(void) password_len;
sysparam_set_int8("wifi_sta_enable", en);
sysparam_set_string("wifi_sta_ssid", ssid);
sysparam_set_string("wifi_sta_password", password);
}
cmd = 'S';
break;
case 'A': {
if(data[1] == 'E')
en = 1;
char *ssid = &data[2];
size_t ssid_len = strlen(ssid);
char *password = &data[3 + ssid_len];
size_t password_len = strlen(password);
(void) password_len;
sysparam_set_int8("wifi_ap_enable", en);
sysparam_set_string("wifi_ap_ssid", ssid);
sysparam_set_string("wifi_ap_password", password);
/*uint8_t ap_disable_if_sta = 0;
uint8_t ssid_hidden = 0;
uint8_t dns_enable = 0;
uint8_t mdns_enable = 0;*/
//sysparam_set_int8("wifi_ap_disable_if_sta", ap_disable_if_sta);
//sysparam_set_int8("wifi_ap_ssid_hidden", ssid_hidden);
//sysparam_set_int8("wifi_ap_dns", dns_enable);
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
}
cmd = 'A';
break;
default:
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]);
@ -327,11 +424,32 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
}
}
/*const char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
for (int i = 0; i < iNumParams; i++) {
if(strcmp(pcParam[i], "on") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, true);
} else if(strcmp(pcParam[i], "off") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, false);
} else if(strcmp(pcParam[i], "toggle") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_toggle(gpio_num);
}
}
return "/index.html";
}*/
extern "C" void httpd_task(void *pvParameters) {
(void) pvParameters;
while (!uxSemaphoreGetCount(wifi_available_semaphore))
while (!uxSemaphoreGetCount(wifi_available_semaphore)) {
vTaskDelay(500 / portTICK_PERIOD_MS);
}
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
httpd_init();

View file

@ -10,7 +10,7 @@ canvas {
width: 100%;
}
main section:target ~ section, main section#io, main section#wifi, main section#ota {
main section:target ~ section, main section#io, main section#wifi, main section#noctis, main section#ota {
display: none;
}

View file

@ -17,6 +17,9 @@
<label for="bmenub" class="burger pseudo button">&#9776;</label>
<div class="menu">
<a href="/#" class="button icon-picture">Dashboard</a>
<a href="/#io" class="button icon-puzzle">I/O</a>
<a href="/#noctis" class="button icon-puzzle">Time</a>
<a href="/#wifi" class="button icon-puzzle">Wifi Settings</a>
<a href="/#ota" class="button icon-picture">System</a>
</div>
</nav>
@ -75,6 +78,180 @@
</article>
</section>
<section id="io">
<h2>I/O</h2>
<article class="card">
<header>
<h3>Protocols</h3>
</header>
<div class="table">
<div class="row">
<label>MQTT</label>
<span><input type="checkbox" class="plain"/></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input type="submit" value="Save"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Station Mode <span class="label success">current connection</span></h3>
</header>
<div class="table">
<div class="row">
<label>WS2812 via I2S</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>APA102C via SPI</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>6 Channel SPI Dimmer</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>Binary Output on GPIO4</label>
<span><input type="checkbox" class="plain"/></span>
<span><button class="toggle button">Toggle</button></span>
</div>
<div class="row">
<label>Binary Output on GPIO5</label>
<span><input type="checkbox" class="plain"/></span>
<span><button class="toggle button">Toggle</button></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input type="submit" value="Save"></span>
</div>
</div>
</article>
</section>
<section id="noctis">
<h2>Time</h2>
<article class="card">
<header>
<h3>Times</h3>
</header>
<div class="table">
<div class="row">
<label>Sunrise Start</label>
<span><input type="time" class="plain" id="sunrise_start" value="07:30"/></span>
</div>
<div class="row">
<label>Sunrise End</label>
<span><input type="time" class="plain" id="sunrise_end" value="08:00"/></span>
</div>
<div class="row">
<label>Sunrise Limit</label>
<span><input type="time" class="plain" id="sunrise_shutdown" value="09:00"/></span>
</div>
<div class="row">
<label>Sunset</label>
<span><input type="time" class="plain" id="sunset_time" value="22:00"/></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input onclick="noctis_times();" type="submit" value="Save"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Output</h3>
</header>
<div class="table">
<div class="row">
<label>Leds</label>
<span><input type="number" min="0" step="1" class="plain" id="noctisnumber"/></span>
</div>
<div class="row">
<label>Mode</label>
<span>
<select id="noctismode">
<option value="W">WS2812</option>
<option value="A">APA102C</option>
<option value="Z">APA102C-Z</option>
</select>
</span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input onclick="noctis_output();" type="submit" value="Save"></span>
</div>
</div>
</article>
</section>
<section id="wifi">
<h2>Wifi Settings</h2>
<article class="card">
<header>
<h3>AP Mode</h3>
</header>
<div class="table">
<div class="row">
<label>Enable</label>
<span><input id="ap_toggle" type="checkbox" class="plain"/></span>
</div>
<div class="row">
<label>SSID</label>
<span><input id="ap_ssid" type="text" placeholder="SSID"/></span>
</div>
<div class="row">
<label>Password</label>
<span><input id="ap_pw" type="password" placeholder="Password"/></span>
</div>
<div class="row">
<span>AP IP</span>
<span><span class="postfill_apip">N/A</span></span>
</div>
<div class="row">
<span>AP MAC</span>
<span><span class="postfill_apmac">N/A</span></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input onclick="ap_update();" type="submit" value="Save"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Station Mode <span class="label success">current connection</span></h3>
</header>
<div class="table">
<div class="row">
<label>Eanable</label>
<span><input id="sta_toggle" type="checkbox" class="plain"/></span>
</div>
<div class="row">
<label>SSID</label>
<span><input id="sta_ssid" type="text" placeholder="SSID"/></span>
</div>
<div class="row">
<label>Password</label>
<span><input id="sta_pw" type="password" placeholder="Password"/></span>
</div>
<div class="row">
<span>Sation IP</span>
<span><span class="postfill_staip">N/A</span></span>
</div>
<div class="row">
<span>Station MAC</span>
<span><span class="postfill_stamac">N/A</span></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input onclick="sta_update();" type="submit" value="Save"></span>
</div>
</div>
</article>
</section>
<section id="dashboard">
<h2>Status</h2>
<div class="flex">
@ -180,6 +357,10 @@
<input type="checkbox" name="onoffswitch" id="led-switch" onclick="gpio()">
<span class="toggle button">toggle signal led</span>
</label>
<label>
<input type="checkbox" name="whiteswitch" id="white-switch" onclick="leds()">
<span class="toggle button">toggle white leds</span>
</label>
</footer>
</article>
</div>
@ -220,7 +401,7 @@
sbox = document.getElementById('status_box');
sbox.className = "label " + cls;
sbox.innerHTML = text;
console.info(text);
//console.info(text);
}
function startPolling() {
@ -325,11 +506,94 @@
wsWrite('D');
}
function leds() {
if (document.getElementById('white-switch').checked)
wsWrite('e');
else
wsWrite('d');
}
window.onload = function () {
wsOpen();
startPolling();
}
var sta_toggle = document.getElementById("sta_toggle");
var sta_ssid = document.getElementById("sta_ssid");
var sta_pw = document.getElementById("sta_pw");
function sta_update() {
var en = sta_toggle.checked;
const ssid = sta_ssid.value;
const password = sta_pw.value;
const buffer = new ArrayBuffer(ssid.length + password.length + 4);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len++, 'S');
view1.setChar(tx_len++, (en ? "E" : "D"));
tx_len = view1.setString(tx_len, ssid);
tx_len = view1.setString(tx_len, password);
wsWrite(buffer);
}
var ap_toggle = document.getElementById("ap_toggle");
var ap_ssid = document.getElementById("ap_ssid");
var ap_pw = document.getElementById("ap_pw");
function ap_update() {
var en = ap_toggle.checked;
const ssid = ap_ssid.value;
const password = ap_pw.value;
const buffer = new ArrayBuffer(ssid.length + password.length + 4);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len++, 'A');
view1.setChar(tx_len++, (en ? "E" : "D"));
tx_len = view1.setString(tx_len, ssid);
tx_len = view1.setString(tx_len, password);
wsWrite(buffer);
}
var sunrise_start = document.getElementById("sunrise_start");
var sunrise_end = document.getElementById("sunrise_end");
var sunrise_shutdown = document.getElementById("sunrise_shutdown");
var sunset_time = document.getElementById("sunset_time");
var noctisnumber = document.getElementById("noctisnumber");
var noctismode = document.getElementById("noctismode");
function noctis_times() {
const buffer = new ArrayBuffer(20);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len, 'T');
tx_len += 4;
view1.setUint32(tx_len, sunrise_start.valueAsNumber);
tx_len += 4;
view1.setUint32(tx_len, sunrise_end.valueAsNumber);
tx_len += 4;
view1.setUint32(tx_len, sunrise_shutdown.valueAsNumber);
tx_len += 4;
view1.setUint32(tx_len, sunset_time.valueAsNumber);
tx_len += 4;
wsWrite(buffer);
}
function noctis_output() {
const buffer = new ArrayBuffer(4);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len++, 'N');
view1.setChar(tx_len++, noctismode.value);
view1.setUint16(tx_len, noctisnumber.valueAsNumber);
tx_len += 2;
wsWrite(buffer);
}
var makeCRCTable = function () {
var c;
var crcTable = [];
@ -358,6 +622,8 @@
var firmware_file;
function load_firmware(evt) {
//console.log("load_firmware", evt);
var file = evt.target.files[0];
if (!file) {
return;
@ -395,11 +661,14 @@
reject({frame_error: i});
}, 2000);
wsWrite(frame.buffer);
//build packet: type, seq, len, hash, data
console.log(i, (end - begin), crc32(slice), (100 * end / buf.byteLength) + "%");
});
}
function transmit_firmware_final(buf, hash) {
return new Promise((resolve, reject) => {
console.log("final: ", buf.byteLength, hash.toString(16));
var frame = new ArrayBuffer(12);
var headerview = new DataView(frame);
headerview.setChar(0, 'C');
@ -416,8 +685,11 @@
}
function transmit_firmware(evt) {
console.log("transmit_firmware begin");
console.log("transmit_firmware", evt);
if (firmware_file) {
console.log("len", firmware_file.byteLength);
//console.log(crc32(firmware_file));
(async () => {
const ash = crc32(firmware_file);
for (var i = 0; i * chunk_size < firmware_file.byteLength; i++) {

View file

@ -22,9 +22,9 @@ extern "C" {
#include <espressif/esp_softap.h>
#include <dhcpserver.h>
const char *wificfg_default_ssid = "fiatlux_%02X%02X%02X";
const char *wificfg_default_ssid = "fiatlux-noctis_%02X%02X%02X";
const char *wificfg_default_password = "fiatlux02";
const char *wificfg_default_hostname = "fiatlux-%02x%02x%02x";
const char *wificfg_default_hostname = "fiatlux-noctis-%02x%02x%02x";
char *wifi_sta_ssid = nullptr;
char *wifi_sta_password = nullptr;
@ -32,8 +32,11 @@ char *wifi_ap_ssid = nullptr;
char *wifi_ap_password = nullptr;
SemaphoreHandle_t wifi_available_semaphore = nullptr;
SemaphoreHandle_t wan_available_semaphore = nullptr;
[[noreturn]] static void dns_task(void *pvParameters) {
printf("run dns task\n");
char *wifi_ap_ip_addr = nullptr;
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
if(!wifi_ap_ip_addr) {
@ -271,6 +274,7 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_ap_channel < 1 || wifi_ap_channel > 14) {
wifi_ap_channel = 6;
}
wifi_ap_channel = 3;
int8_t wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
sysparam_get_int8("wifi_ap_authmode", &wifi_ap_authmode);
@ -284,6 +288,7 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_ap_max_conn < 1 || wifi_ap_max_conn > 8) {
wifi_ap_max_conn = 3;
}
wifi_ap_max_conn = 8;
int32_t wifi_ap_beacon_interval = 100;
sysparam_get_int32("wifi_ap_beacon_interval", &wifi_ap_beacon_interval);
@ -354,8 +359,22 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_ap_ssid) free(wifi_ap_ssid);
if(wifi_ap_password) free(wifi_ap_password);
//gpio_enable(LED_PIN, GPIO_OUTPUT);
//gpio_write(LED_PIN, true);
printf("wifi wait for ip\n");
xSemaphoreGive(wifi_available_semaphore);
/* Wait until we have joined AP and are assigned an IP */
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelay(250 / portTICK_PERIOD_MS);
}
xSemaphoreGive(wan_available_semaphore);
//monitor loop connection here
printf("wifi task done\n");
vTaskDelete(nullptr);
}

View file

@ -13,6 +13,7 @@ extern "C" {
#endif
extern SemaphoreHandle_t wifi_available_semaphore;
extern SemaphoreHandle_t wan_available_semaphore;
void wifi_task(void *pvParameters);