diff --git a/firmware/lux.cpp b/firmware/lux.cpp index 790ce87..db17968 100644 --- a/firmware/lux.cpp +++ b/firmware/lux.cpp @@ -36,10 +36,12 @@ struct apa10xx_pixel_t { uint8_t r = 0; }; -rgba_t top_color; -rgba_t bottom_color; +volatile rgba_t top_color; +volatile rgba_t bottom_color; volatile uint16_t light_value; +volatile uint16_t peripheral_version; + namespace fiatlux { struct hal_error_t { @@ -237,12 +239,28 @@ uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val) { return in; }; +void reset_bus() { + gpio_write(4, false); + for (volatile int k = 0; k < 128; ++k); + gpio_write(4, true); +} + /* This task uses the high level GPIO API (esp_gpio.h) to blink an LED. * */ extern "C" [[noreturn]] void lux_task(void *pvParameters) { + fiatlux::signal::setup(); + fiatlux::relais::setup(); + + gpio_write(4, false); + for (volatile int k = 0; k < 512 * 2; ++k); + gpio_write(4, true); + gpio_enable(4, GPIO_OUT_OPEN_DRAIN); + gpio_write(4, true); + for (volatile int k = 0; k < 512 * 4; ++k); + int32_t lux_ws2812_number = 240; auto ret = sysparam_get_int32("lux_ws2812_number", &lux_ws2812_number); if(ret != SYSPARAM_OK) @@ -266,9 +284,6 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { //fiatlux::spi_dimmer::setup(); - fiatlux::signal::setup(); - fiatlux::relais::setup(); - //fiatlux::apa10x::setup(); spi_init(1, SPI_MODE0, SPI_FREQ_DIV_2M, true, SPI_BIG_ENDIAN, false); @@ -287,8 +302,20 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { uint16_t _; } frame = {._=0}; - light_value = 0xFFF; + { + frame.addr = 15; + frame.dat = 0; + spi_transfer_16_duplex(1, frame._); + for (volatile int k = 0; k < 512 * 2; ++k); + uint16_t reply = spi_transfer_16_duplex(1, 0x0000); + peripheral_version = reply; + for (volatile int k = 0; k < 512 * 2; ++k); + syslog("peripheral_version: "); + syslog_i32(peripheral_version); + syslog("\n"); + } + light_value = 0xFFF; uint16_t last_light_value = 0xFFFF; while (true) { @@ -301,30 +328,57 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { syslog_i32(light_value); syslog("\n"); + uint16_t last_frame = 0x0000; + uint16_t reply = 0x0000; + uint8_t error_count = 0; + for (int i = 0; i < 6; ++i) { - frame.addr = i; + frame.addr = i + 1; frame.dat = light_value; - uint16_t reply = spi_transfer_16_duplex(1, frame._); + reply = spi_transfer_16_duplex(1, frame._); - /*if(!i) { + if(reply != last_frame) + error_count++; + + { syslog(" > "); syslog_i32(frame._); - - if(reply) { - syslog(" < "); - syslog_i32(reply); - } + syslog(" < "); + syslog_i32(reply); + syslog(" -- "); + syslog_i32(last_frame); syslog("\n"); - }*/ + } - for (volatile int k = 0; k < 512; ++k); + last_frame = frame._; + + for (volatile int k = 0; k < 512 * 2; ++k); } - last_light_value = light_value; - } + reply = spi_transfer_16_duplex(1, 0x0000); + if(reply != last_frame) + error_count++; - vTaskDelay(200 / portTICK_PERIOD_MS); + { + syslog(" > "); + syslog_i32(frame._); + syslog(" < "); + syslog_i32(reply); + syslog(" -- "); + syslog_i32(last_frame); + syslog("\n"); + } + + if(!error_count) { + last_light_value = light_value; + } else { + syslog("reset_bus\n"); + reset_bus(); + } + + vTaskDelay(5000 / portTICK_PERIOD_MS); + } for (int i = 0; i < 120; i++) pixels[i] = {{top_color.r, top_color.g, top_color.b, top_color.a}}; @@ -334,6 +388,8 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { ws2812_i2s_update(pixels, PIXEL_RGBW); + vTaskDelay(200 / portTICK_PERIOD_MS); + /*//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); diff --git a/firmware/lux.h b/firmware/lux.h index e524a7f..bd0d535 100644 --- a/firmware/lux.h +++ b/firmware/lux.h @@ -22,10 +22,12 @@ typedef struct { uint8_t a; } rgba_t; -extern rgba_t top_color; -extern rgba_t bottom_color; +extern volatile rgba_t top_color; +extern volatile rgba_t bottom_color; extern volatile uint16_t light_value; +extern volatile uint16_t peripheral_version; + #ifdef __cplusplus } #endif diff --git a/firmware/messages.h b/firmware/messages.h index 737138a..eaf9bab 100644 --- a/firmware/messages.h +++ b/firmware/messages.h @@ -12,19 +12,21 @@ namespace fiatlux { FIRMWARE_CHECK, VOLTAGE_INFO, SYSTEM_INFO, + LIGHTS, count }; - const char* id_to_string(id val){ - static const char* lookup[] = { - "NONE", - "RESTART", - "CLEAR_CONFIG", - "FIRMWARE_FRAME", - "FIRMWARE_CHECK", - "VOLTAGE_INFO", - "SYSTEM_INFO", - "out of range" + const char *id_to_string(id val) { + static const char *lookup[] = { + "NONE", + "RESTART", + "CLEAR_CONFIG", + "FIRMWARE_FRAME", + "FIRMWARE_CHECK", + "VOLTAGE_INFO", + "SYSTEM_INFO", + "LIGHTS", + "out of range" }; if(val < id::count) return lookup[val]; @@ -32,7 +34,7 @@ namespace fiatlux { return lookup[id::count]; } - template + template struct frame { id cmd; T msg; @@ -61,6 +63,9 @@ namespace fiatlux { uint32_t chipid; uint32_t flashid; uint32_t flashsize; + uint16_t version; + uint16_t peripheral_version; + /* " \"hostname\" : \"%s\"" "}" @@ -72,5 +77,11 @@ namespace fiatlux { uint8_t ret; uint16_t val; } __attribute__((packed)); + + struct lights { + uint32_t top_color; + uint32_t bottom_color; + uint16_t white_brightness; + } __attribute__((packed)); } } \ No newline at end of file diff --git a/firmware/web.cpp b/firmware/web.cpp index eda4dfe..72b13f0 100644 --- a/firmware/web.cpp +++ b/firmware/web.cpp @@ -28,6 +28,9 @@ extern "C" { using namespace fiatlux; +int version = 23; + + #define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS) uint16_t voltage_val; @@ -37,13 +40,14 @@ struct { bool connection; bool wifi; bool voltage; + bool lights; } has_changed; void websocket_task(void *pvParameter) { auto *pcb = (struct tcp_pcb *) pvParameter; size_t connstarttime = xTaskGetTickCount(); - has_changed = {true, true, true, true}; + has_changed = {true, true, true, true, true}; syslog_attach(); unsigned local_log_tail = syslog_current_tail(); @@ -92,6 +96,8 @@ void websocket_task(void *pvParameter) { frame.msg.chipid = chip_id; frame.msg.flashid = flash_id; frame.msg.flashsize = flash_size; + frame.msg.version = version; + frame.msg.peripheral_version = peripheral_version; //frame.msg. = ; // hostname LOCK_TCPIP_CORE(); @@ -105,6 +111,18 @@ void websocket_task(void *pvParameter) { has_changed.global = false; UNLOCK_TCPIP_CORE();*/ + taskYIELD(); + } else if(has_changed.lights) { + messages::frame frame; + frame.cmd = messages::LIGHTS; + frame.msg.top_color = *(uint32_t*)&top_color; + frame.msg.bottom_color = *(uint32_t*)&bottom_color; + frame.msg.white_brightness = light_value; + LOCK_TCPIP_CORE(); + websocket_write(pcb, (unsigned char *) &frame, sizeof(frame), WS_BIN_MODE); + has_changed.lights = false; + UNLOCK_TCPIP_CORE(); + taskYIELD(); } else if(has_changed.connection) { timeval tv{}; @@ -265,6 +283,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, bottom_color.r = data[7]; bottom_color.g = data[6]; bottom_color.b = data[5]; + has_changed.lights = true; syslog("B"); syslog_i32(val); syslog("\n"); @@ -278,6 +297,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, top_color.r = data[7]; top_color.g = data[6]; top_color.b = data[5]; + has_changed.lights = true; syslog("T"); syslog_i32(val); syslog("\n"); @@ -289,6 +309,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, // Disable LED uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24); light_value = data[7] | (data[6] << 8); + has_changed.lights = true; syslog("J"); syslog_i32(val); syslog("\n"); diff --git a/firmware/webdir/index.html b/firmware/webdir/index.html index 5304ebe..22e3c4d 100644 --- a/firmware/webdir/index.html +++ b/firmware/webdir/index.html @@ -176,7 +176,11 @@
Firmware Version - N/A + N/A +
+
+ Peripheral Version + N/A
Flash ID @@ -261,19 +265,19 @@ +
+ change top color + +
+ change bottom color + +
@@ -358,12 +362,12 @@ function onMessage(evt) { retries = 0; if (typeof evt.data == 'string') { + var cmd = "JSON"; var data = JSON.parse(evt.data); - console.log("deprecated json:", data); } else { var data = {}; var dv = new DataView(evt.data); - let cmds = ["NONE", "RESTART", "CLEAR_CONFIG", "FIRMWARE_FRAME", "FIRMWARE_CHECK", "VOLTAGE_INFO", "SYSTEM_INFO"]; + let cmds = ["NONE", "RESTART", "CLEAR_CONFIG", "FIRMWARE_FRAME", "FIRMWARE_CHECK", "VOLTAGE_INFO", "SYSTEM_INFO", "LIGHTS"]; //console.log("[0]", dv.getUint8(0)); let fst = dv.getUint8(0); var cmd; @@ -381,6 +385,12 @@ data.chipid = Number(htonl(dv.getUint32(13))).toString(16); data.flashid = Number(htonl(dv.getUint32(17))).toString(16); data.flashsize = htonl(dv.getUint32(21)); + data.firmware_version = htons(dv.getUint16(25)); + data.peripheral_version = htons(dv.getUint16(27)); + } else if (cmd === "LIGHTS") { + data.top_color = htonl(dv.getUint32(1)); + data.bottom_color = htonl(dv.getUint32(5)); + data.white_brightness = htons(dv.getInt16(9)); } else if (cmd === "VOLTAGE_INFO") { console.log(cmd, buf2hex(evt.data)); } else if (cmd === "G") { @@ -404,16 +414,23 @@ } else { console.log("[0]", dv.getUint8(0)); console.log('unknown command', cmd, val); - console.log(cmd); + console.log(fst, cmd); } } + console.log(cmd, data); for (const [key, value] of Object.entries(data)) { const elements = document.querySelectorAll(".postfill_" + key); if (!elements.length) unused_values[key] = value; else for (i = 0; i < elements.length; ++i) { - elements[i].innerHTML = value; + if (elements[i].type === "color") { + elements[i].value = "#" + (value).toString(16).padStart(6, '0'); + } else if (elements[i].type === "range") { + elements[i].value = value; + } else { + elements[i].innerHTML = value; + } } } document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values); diff --git a/firmware/wifi.cpp b/firmware/wifi.cpp index df919c5..b2d9d79 100644 --- a/firmware/wifi.cpp +++ b/firmware/wifi.cpp @@ -144,6 +144,7 @@ extern "C" void wifi_task(void *pvParameters) { int8_t wifi_ap_enable = 1; sysparam_get_int8("wifi_sta_enable", &wifi_sta_enable); sysparam_get_int8("wifi_ap_enable", &wifi_ap_enable); + wifi_ap_enable = 0; if(!wifi_sta_enable) wifi_ap_enable = 1; @@ -352,9 +353,11 @@ extern "C" void wifi_task(void *pvParameters) { uint8_t retries = 30; while (1) { + + retries = 30; while ((status != STATION_GOT_IP) && (retries)) { status = sdk_wifi_station_get_connect_status(); - printf("%s: status = %d\n\r", __func__, status); + printf("%s: status = %d [%d retries left]\n\r", __func__, status, retries); if(status == STATION_WRONG_PASSWORD) { printf("WiFi: wrong password\n\r"); break; @@ -380,6 +383,6 @@ extern "C" void wifi_task(void *pvParameters) { } printf("WiFi: disconnected\n\r"); sdk_wifi_station_disconnect(); - vTaskDelay(1000 / portTICK_PERIOD_MS); + vTaskDelay(10000 / portTICK_PERIOD_MS); } } \ No newline at end of file