diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 31f12dd..93433d9 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -23,7 +23,7 @@ void user_init(void) { wifi_available_semaphore = xSemaphoreCreateBinary(); - xTaskCreate(mqtt_task, "mqtt_task", 1024, NULL, 1, NULL); + //xTaskCreate(mqtt_task, "mqtt_task", 1024, NULL, 1, NULL); xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL); @@ -34,7 +34,7 @@ void user_init(void) { wifi_alive = xSemaphoreCreateBinary(); publish_queue = xQueueCreate(3, PUB_MSG_LEN); - xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL); + //xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL); } diff --git a/firmware/log.cpp b/firmware/log.cpp index 07e7f19..e3b9a00 100644 --- a/firmware/log.cpp +++ b/firmware/log.cpp @@ -17,14 +17,14 @@ char str_i32[] = "00000000"; extern "C" void syslog_i32(const uint32_t val) { - str_i32[1] = hex_lookup[val & 0xF]; - str_i32[0] = hex_lookup[(val >> 4) & 0xF]; - str_i32[3] = hex_lookup[(val >> 8) & 0xF]; - str_i32[2] = hex_lookup[(val >> 12) & 0xF]; - str_i32[5] = hex_lookup[(val >> 16) & 0xF]; - str_i32[4] = hex_lookup[(val >> 20) & 0xF]; - str_i32[7] = hex_lookup[(val >> 24) & 0xF]; - str_i32[6] = hex_lookup[(val >> 28) & 0xF]; + str_i32[7] = hex_lookup[val & 0xF]; + str_i32[6] = hex_lookup[(val >> 4) & 0xF]; + str_i32[5] = hex_lookup[(val >> 8) & 0xF]; + str_i32[4] = hex_lookup[(val >> 12) & 0xF]; + str_i32[3] = hex_lookup[(val >> 16) & 0xF]; + str_i32[2] = hex_lookup[(val >> 20) & 0xF]; + str_i32[1] = hex_lookup[(val >> 24) & 0xF]; + str_i32[0] = hex_lookup[(val >> 28) & 0xF]; // syslog(str_i32); } diff --git a/firmware/lux.cpp b/firmware/lux.cpp index 7f139ad..790ce87 100644 --- a/firmware/lux.cpp +++ b/firmware/lux.cpp @@ -36,32 +36,9 @@ struct apa10xx_pixel_t { uint8_t r = 0; }; -static ws2812_pixel_t next_colour(int i) { - ws2812_pixel_t colour = {{0, 0, 0, 0}}; - if(i == 8) { - colour.white = 32; - } else { - colour.red = i & 1 ? 32 : 0; - colour.green = i & 2 ? 32 : 0; - colour.blue = i & 4 ? 32 : 0; - } - - return colour; -} - -static apa10xx_pixel_t next_color(int i) { - apa10xx_pixel_t colour; - colour.global.mod = 8; - colour.r = i & 1 ? 32 : 0; - colour.g = i & 2 ? 32 : 0; - colour.b = i & 4 ? 32 : 0; - - return colour; -} - rgba_t top_color; rgba_t bottom_color; -uint16_t dimmer_value; +volatile uint16_t light_value; namespace fiatlux { @@ -246,6 +223,20 @@ extern "C" void signal_led(bool state) { fiatlux::signal::write_data(state); } +uint32_t spi_transfer_32_duplex(uint8_t bus, uint32_t val) { + uint32_t out = val; + uint32_t in; + spi_transfer(bus, &out, &in, 1, SPI_32BIT); + return in; +}; + +uint16_t spi_transfer_16_duplex(uint8_t bus, uint32_t val) { + uint16_t out = val; + uint16_t in; + spi_transfer(bus, &out, &in, 1, SPI_16BIT); + return in; +}; + /* This task uses the high level GPIO API (esp_gpio.h) to blink an LED. * @@ -266,7 +257,7 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { 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]; + //apa10xx_pixel_t leds[lux_apa10xx_number]; // TODO //lux_apa102c_number @@ -279,7 +270,7 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { fiatlux::relais::setup(); //fiatlux::apa10x::setup(); - spi_init(1, SPI_MODE0, SPI_FREQ_DIV_500K, true, SPI_BIG_ENDIAN, false); + spi_init(1, SPI_MODE0, SPI_FREQ_DIV_2M, true, SPI_BIG_ENDIAN, false); /* CC48 API: @@ -288,83 +279,70 @@ extern "C" [[noreturn]] void lux_task(void *pvParameters) { - [2*n+1] = 8bit PWM value */ - uint32_t dummy = 0; - union { struct { - unsigned dat: 16; - unsigned addr: 12; - unsigned op: 4; + unsigned dat: 12; + unsigned addr: 4; } __attribute__((packed)); - uint32_t _; + uint16_t _; } frame = {._=0}; - dimmer_value = 255; + light_value = 0xFFF; - uint16_t last_dimmer_value = 0; + uint16_t last_light_value = 0xFFFF; 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); - }*/ + if(last_light_value != light_value) { - if(last_dimmer_value != dimmer_value) { - for (int i = 0; i < 6; ++i) { + syslog("# "); + syslog_i32(light_value); + syslog("\n"); - frame.op = 0xA; - frame.addr = i * 2 + 1; - frame.dat = 255; - spi_transfer_32(1, frame._); + for (int i = 0; i < 6; ++i) { - vTaskDelay(13 / portTICK_PERIOD_MS); - //for (volatile int i = 0; i < 512 * 32; ++i); + frame.addr = i; + frame.dat = light_value; + uint16_t reply = spi_transfer_16_duplex(1, frame._); - frame.op = 0xA; - frame.addr = i * 2; - frame.dat = dimmer_value << 4; - spi_transfer_32(1, frame._); + /*if(!i) { + syslog(" > "); + syslog_i32(frame._); - vTaskDelay(13 / portTICK_PERIOD_MS); - //for (volatile int i = 0; i < 512 * 32; ++i); - } - //last_dimmer_value = dimmer_value; + if(reply) { + syslog(" < "); + syslog_i32(reply); + } + syslog("\n"); + }*/ + + for (volatile int k = 0; k < 512; ++k); } - for (int i = 0; i < 120; i++) - pixels[i] = {{top_color.r, top_color.g, top_color.b, top_color.a}}; - - for (int i = 120; i < 240; i++) - pixels[i] = {{bottom_color.r, bottom_color.g, bottom_color.b, bottom_color.a}}; - - 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); + last_light_value = light_value; } + + vTaskDelay(200 / portTICK_PERIOD_MS); + + for (int i = 0; i < 120; i++) + pixels[i] = {{top_color.r, top_color.g, top_color.b, top_color.a}}; + + for (int i = 120; i < 240; i++) + pixels[i] = {{bottom_color.r, bottom_color.g, bottom_color.b, bottom_color.a}}; + + 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); + vTaskDelay(200 / portTICK_PERIOD_MS);*/ } diff --git a/firmware/lux.h b/firmware/lux.h index 1c9d9e5..e524a7f 100644 --- a/firmware/lux.h +++ b/firmware/lux.h @@ -24,7 +24,7 @@ typedef struct { extern rgba_t top_color; extern rgba_t bottom_color; -extern uint16_t dimmer_value; +extern volatile uint16_t light_value; #ifdef __cplusplus } diff --git a/firmware/messages.h b/firmware/messages.h index d054569..737138a 100644 --- a/firmware/messages.h +++ b/firmware/messages.h @@ -55,9 +55,9 @@ namespace fiatlux { } __attribute__((packed)); struct system_info { - unsigned walltime; - unsigned uptime; - int heap; + uint32_t walltime; + uint32_t uptime; + int32_t heap; uint32_t chipid; uint32_t flashid; uint32_t flashsize; diff --git a/firmware/mkwebfs.py b/firmware/mkwebfs.py index e48548c..495e525 100755 --- a/firmware/mkwebfs.py +++ b/firmware/mkwebfs.py @@ -78,12 +78,14 @@ for file in httpFiles: else: response = b'HTTP/1.0 200 OK\r\n' response += b"lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n" + #fext = file.split('.')[-1] response += b'Content-type: ' + mimeType.encode() + b'\r\n' binFile = open(file, 'rb') binData = binFile.read() compEff = False if args.minify: + # shell: binData | minify --html-keep-document-tags --mime text/html | minData p = subprocess.Popen(["minify", "--html-keep-document-tags", "--mime", mimeType], stdin=subprocess.PIPE, stdout=subprocess.PIPE) minData = p.communicate(binData)[0] diff --git a/firmware/web.cpp b/firmware/web.cpp index 35f4eee..eda4dfe 100644 --- a/firmware/web.cpp +++ b/firmware/web.cpp @@ -30,7 +30,6 @@ using namespace fiatlux; #define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS) - uint16_t voltage_val; struct { @@ -44,7 +43,7 @@ void websocket_task(void *pvParameter) { auto *pcb = (struct tcp_pcb *) pvParameter; size_t connstarttime = xTaskGetTickCount(); - has_changed = {true, true, true}; + has_changed = {true, true, true, true}; syslog_attach(); unsigned local_log_tail = syslog_current_tail(); @@ -73,7 +72,7 @@ void websocket_task(void *pvParameter) { } //Global Info - if(false && has_changed.global) { + if(has_changed.global) { timeval tv{}; gettimeofday(&tv, nullptr); size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; @@ -95,6 +94,11 @@ void websocket_task(void *pvParameter) { frame.msg.flashsize = flash_size; //frame.msg. = ; // hostname + LOCK_TCPIP_CORE(); + websocket_write(pcb, (unsigned char *) &frame, sizeof(frame), WS_BIN_MODE); + has_changed.global = false; + UNLOCK_TCPIP_CORE(); + free(hostname); /*LOCK_TCPIP_CORE(); websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE); @@ -107,13 +111,13 @@ void websocket_task(void *pvParameter) { gettimeofday(&tv, nullptr); size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; - printf("conn %d: %d.%.%.%d <-> %d.%d.&d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip), + printf("conn %d: %d.%d.%d.%d <-> %d.%d.%d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip), IP2STR(&pcb->remote_ip)); char response[192]; size_t len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \"" - IPSTR - "\"" "}", connuptime, IP2STR(&pcb->remote_ip)); + IPSTR + "\"" "}", connuptime, IP2STR(&pcb->remote_ip)); if(len < sizeof(response)) { LOCK_TCPIP_CORE(); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); @@ -213,7 +217,7 @@ void websocket_task(void *pvParameter) { res.ret = OK; res.cmd = (messages::id) 'V'; LOCK_TCPIP_CORE(); - websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE); + websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE); UNLOCK_TCPIP_CORE(); taskYIELD(); } else { @@ -257,7 +261,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, res.ret = OK; } else if(data[0] == 'B') { // Disable LED - uint32_t val = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24); + uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24); bottom_color.r = data[7]; bottom_color.g = data[6]; bottom_color.b = data[5]; @@ -270,17 +274,28 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, res.val = 1; } else if(data[0] == 'T') { // Disable LED - uint32_t val = data[4] | (data[5] << 8) | (data[6] << 16) | (data[7] << 24); + uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24); top_color.r = data[7]; top_color.g = data[6]; top_color.b = data[5]; - syslog("B"); + syslog("T"); syslog_i32(val); syslog("\n"); //signal_led(false); res.cmd = (messages::id) 'T'; res.ret = OK; res.val = 1; + } else if(data[0] == 'J') { + // Disable LED + uint32_t val = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24); + light_value = data[7] | (data[6] << 8); + syslog("J"); + syslog_i32(val); + syslog("\n"); + //signal_led(false); + res.cmd = (messages::id) 'J'; + res.ret = OK; + res.val = 1; } else if(data[0] == 'D') { // Disable LED syslog("G\n"); @@ -358,7 +373,7 @@ void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, } LOCK_TCPIP_CORE(); - websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE); + websocket_write(pcb, (uint8_t *) &res, sizeof(res), WS_BIN_MODE); UNLOCK_TCPIP_CORE(); if(res.ret == OK) { @@ -418,9 +433,15 @@ extern "C" void httpd_task(void *pvParameters) { websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb); httpd_init(); + uint16_t last_voltage = 0; while (1) { - voltage_val = sdk_system_adc_read(); - has_changed.voltage = true; - vTaskDelayMs(1000); + uint16_t voltage = sdk_system_adc_read(); + if(false && voltage != last_voltage) { + last_voltage = voltage; + voltage_val = voltage; + has_changed.voltage = true; + } + //has_changed.global = true; + vTaskDelayMs(5000); } } \ No newline at end of file diff --git a/firmware/webdir/index.html b/firmware/webdir/index.html index 6b5540f..5304ebe 100644 --- a/firmware/webdir/index.html +++ b/firmware/webdir/index.html @@ -259,17 +259,23 @@ @@ -279,12 +285,18 @@ - + \ No newline at end of file