|
@@ -18,6 +18,8 @@
|
|
|
*/
|
|
|
|
|
|
#include <ctype.h>
|
|
|
+#include <math.h>
|
|
|
+#include "esp_dsp.h"
|
|
|
#include "squeezelite.h"
|
|
|
#include "slimproto.h"
|
|
|
#include "display.h"
|
|
@@ -54,6 +56,27 @@ struct grfg_packet {
|
|
|
u16_t width; // # of pixels of scrollable
|
|
|
};
|
|
|
|
|
|
+struct visu_packet {
|
|
|
+ char opcode[4];
|
|
|
+ u8_t which;
|
|
|
+ u8_t count;
|
|
|
+ union {
|
|
|
+ struct {
|
|
|
+ u32_t bars;
|
|
|
+ u32_t spectrum_scale;
|
|
|
+ } full;
|
|
|
+ struct {
|
|
|
+ u32_t width;
|
|
|
+ u32_t height;
|
|
|
+ s32_t col;
|
|
|
+ s32_t row;
|
|
|
+ u32_t border;
|
|
|
+ u32_t bars;
|
|
|
+ u32_t spectrum_scale;
|
|
|
+ };
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
struct ANIC_header {
|
|
|
char opcode[4];
|
|
|
u32_t length;
|
|
@@ -64,20 +87,62 @@ struct ANIC_header {
|
|
|
|
|
|
extern struct outputstate output;
|
|
|
|
|
|
+static struct {
|
|
|
+ TaskHandle_t task;
|
|
|
+ SemaphoreHandle_t mutex;
|
|
|
+ int width, height;
|
|
|
+ bool dirty;
|
|
|
+ bool owned;
|
|
|
+} displayer = { .dirty = true, .owned = true };
|
|
|
+
|
|
|
+#define LONG_WAKE (10*1000)
|
|
|
+#define SB_HEIGHT 32
|
|
|
+
|
|
|
+// lenght are number of frames, i.e. 2 channels of 16 bits
|
|
|
+#define FFT_LEN_BIT 6
|
|
|
+#define FFT_LEN (1 << FFT_LEN_BIT)
|
|
|
+#define RMS_LEN_BIT 6
|
|
|
+#define RMS_LEN (1 << RMS_LEN_BIT)
|
|
|
+
|
|
|
+#define DISPLAY_BW 20000
|
|
|
+
|
|
|
static struct scroller_s {
|
|
|
// copy of grfs content
|
|
|
- u8_t screen, direction;
|
|
|
- u32_t pause, speed;
|
|
|
- u16_t by, mode, full_width, window_width;
|
|
|
- u16_t max, size;
|
|
|
+ u8_t screen;
|
|
|
+ u32_t pause, speed;
|
|
|
+ int wake;
|
|
|
+ u16_t mode;
|
|
|
+ s16_t by;
|
|
|
// scroller management & sharing between grfg and scrolling task
|
|
|
- TaskHandle_t task;
|
|
|
- u8_t *scroll_frame, *back_frame;
|
|
|
- bool active, updated;
|
|
|
- u8_t *scroll_ptr;
|
|
|
- int scroll_len, scroll_step;
|
|
|
+ bool active, first;
|
|
|
+ int scrolled;
|
|
|
+ struct {
|
|
|
+ u8_t *frame;
|
|
|
+ u32_t width;
|
|
|
+ u32_t max, size;
|
|
|
+ } scroll;
|
|
|
+ struct {
|
|
|
+ u8_t *frame;
|
|
|
+ u32_t width;
|
|
|
+ } back;
|
|
|
+ u8_t *frame;
|
|
|
+ u32_t width;
|
|
|
} scroller;
|
|
|
|
|
|
+#define MAX_BARS 32
|
|
|
+static EXT_RAM_ATTR struct {
|
|
|
+ int bar_gap, bar_width, bar_border;
|
|
|
+ struct {
|
|
|
+ int current, max;
|
|
|
+ int limit;
|
|
|
+ } bars[MAX_BARS];
|
|
|
+ float spectrum_scale;
|
|
|
+ int n, col, row, height, width, border;
|
|
|
+ enum { VISU_BLANK, VISU_VUMETER, VISU_SPECTRUM, VISU_WAVEFORM } mode;
|
|
|
+ int speed, wake;
|
|
|
+ float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
|
|
|
+} visu;
|
|
|
+
|
|
|
#define ANIM_NONE 0x00
|
|
|
#define ANIM_TRANSITION 0x01 // A transition animation has finished
|
|
|
#define ANIM_SCROLL_ONCE 0x02
|
|
@@ -91,23 +156,27 @@ static u8_t SETD_width;
|
|
|
#define LINELEN 40
|
|
|
|
|
|
static log_level loglevel = lINFO;
|
|
|
-static SemaphoreHandle_t display_mutex;
|
|
|
+
|
|
|
static bool (*slimp_handler_chain)(u8_t *data, int len);
|
|
|
static void (*slimp_loop_chain)(void);
|
|
|
static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
|
|
|
-static int display_width, display_height;
|
|
|
+static bool (*display_bus_chain)(void *from, enum display_bus_cmd_e cmd);
|
|
|
|
|
|
#define max(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
|
static void server(in_addr_t ip, u16_t hport, u16_t cport);
|
|
|
static void send_server(void);
|
|
|
static bool handler(u8_t *data, int len);
|
|
|
+static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd);
|
|
|
static void vfdc_handler( u8_t *_data, int bytes_read);
|
|
|
static void grfe_handler( u8_t *data, int len);
|
|
|
static void grfb_handler(u8_t *data, int len);
|
|
|
static void grfs_handler(u8_t *data, int len);
|
|
|
static void grfg_handler(u8_t *data, int len);
|
|
|
-static void scroll_task(void* arg);
|
|
|
+static void visu_handler(u8_t *data, int len);
|
|
|
+
|
|
|
+static void displayer_task(void* arg);
|
|
|
+
|
|
|
|
|
|
/* scrolling undocumented information
|
|
|
grfs
|
|
@@ -150,18 +219,25 @@ bool sb_display_init(void) {
|
|
|
}
|
|
|
|
|
|
// need to force height to 32 maximum
|
|
|
- display_width = display->width;
|
|
|
- display_height = min(display->height, 32);
|
|
|
+ displayer.width = display->width;
|
|
|
+ displayer.height = min(display->height, SB_HEIGHT);
|
|
|
SETD_width = display->width;
|
|
|
-
|
|
|
+
|
|
|
+ // create visu configuration
|
|
|
+ visu.bar_gap = 1;
|
|
|
+ visu.speed = 100;
|
|
|
+ dsps_fft2r_init_fc32(visu.fft, FFT_LEN);
|
|
|
+ dsps_wind_hann_f32(visu.hanning, FFT_LEN);
|
|
|
+
|
|
|
// create scroll management task
|
|
|
- display_mutex = xSemaphoreCreateMutex();
|
|
|
- scroller.task = xTaskCreateStatic( (TaskFunction_t) scroll_task, "scroll_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
|
|
|
+ displayer.mutex = xSemaphoreCreateMutex();
|
|
|
+ displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "displayer_thread", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
|
|
|
|
|
|
// size scroller
|
|
|
- scroller.max = (display_width * display_height / 8) * 10;
|
|
|
- scroller.scroll_frame = malloc(scroller.max);
|
|
|
- scroller.back_frame = malloc(display_width * display_height / 8);
|
|
|
+ scroller.scroll.max = (displayer.width * displayer.height / 8) * 10;
|
|
|
+ scroller.scroll.frame = malloc(scroller.scroll.max);
|
|
|
+ scroller.back.frame = malloc(displayer.width * displayer.height / 8);
|
|
|
+ scroller.frame = malloc(displayer.width * displayer.height / 8);
|
|
|
|
|
|
// chain handlers
|
|
|
slimp_handler_chain = slimp_handler;
|
|
@@ -173,9 +249,39 @@ bool sb_display_init(void) {
|
|
|
notify_chain = server_notify;
|
|
|
server_notify = server;
|
|
|
|
|
|
+ display_bus_chain = display_bus;
|
|
|
+ display_bus = display_bus_handler;
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+/****************************************************************************************
|
|
|
+ * Receive display bus commands
|
|
|
+ */
|
|
|
+static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
|
|
|
+ // don't answer to own requests
|
|
|
+ if (from == &displayer) return false ;
|
|
|
+
|
|
|
+ LOG_INFO("Display bus command %d", cmd);
|
|
|
+
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
+
|
|
|
+ switch (cmd) {
|
|
|
+ case DISPLAY_BUS_TAKE:
|
|
|
+ displayer.owned = false;
|
|
|
+ break;
|
|
|
+ case DISPLAY_BUS_GIVE:
|
|
|
+ displayer.owned = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
+
|
|
|
+ if (display_bus_chain) return (*display_bus_chain)(from, cmd);
|
|
|
+ else return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/****************************************************************************************
|
|
|
* Send message to server (ANIC at that time)
|
|
|
*/
|
|
@@ -224,7 +330,9 @@ static void send_server(void) {
|
|
|
static void server(in_addr_t ip, u16_t hport, u16_t cport) {
|
|
|
char msg[32];
|
|
|
sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
|
|
|
- display->text(DISPLAY_FONT_DEFAULT, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
|
|
|
+ if (displayer.owned) display->text(DISPLAY_FONT_DEFAULT, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, msg);
|
|
|
+ SETD_width = display->width;
|
|
|
+ displayer.dirty = true;
|
|
|
if (notify_chain) (*notify_chain)(ip, hport, cport);
|
|
|
}
|
|
|
|
|
@@ -234,22 +342,21 @@ static void server(in_addr_t ip, u16_t hport, u16_t cport) {
|
|
|
static bool handler(u8_t *data, int len){
|
|
|
bool res = true;
|
|
|
|
|
|
- // don't do anything if we dont own the display (no lock needed)
|
|
|
- if (!output.external || output.state < OUTPUT_STOPPED) {
|
|
|
- if (!strncmp((char*) data, "vfdc", 4)) {
|
|
|
- vfdc_handler(data, len);
|
|
|
- } else if (!strncmp((char*) data, "grfe", 4)) {
|
|
|
- grfe_handler(data, len);
|
|
|
- } else if (!strncmp((char*) data, "grfb", 4)) {
|
|
|
- grfb_handler(data, len);
|
|
|
- } else if (!strncmp((char*) data, "grfs", 4)) {
|
|
|
- grfs_handler(data, len);
|
|
|
- } else if (!strncmp((char*) data, "grfg", 4)) {
|
|
|
- grfg_handler(data, len);
|
|
|
- } else {
|
|
|
- res = false;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (!strncmp((char*) data, "vfdc", 4)) {
|
|
|
+ vfdc_handler(data, len);
|
|
|
+ } else if (!strncmp((char*) data, "grfe", 4)) {
|
|
|
+ grfe_handler(data, len);
|
|
|
+ } else if (!strncmp((char*) data, "grfb", 4)) {
|
|
|
+ grfb_handler(data, len);
|
|
|
+ } else if (!strncmp((char*) data, "grfs", 4)) {
|
|
|
+ grfs_handler(data, len);
|
|
|
+ } else if (!strncmp((char*) data, "grfg", 4)) {
|
|
|
+ grfg_handler(data, len);
|
|
|
+ } else if (!strncmp((char*) data, "visu", 4)) {
|
|
|
+ visu_handler(data, len);
|
|
|
+ } else {
|
|
|
+ res = false;
|
|
|
+ }
|
|
|
|
|
|
// chain protocol handlers (bitwise or is fine)
|
|
|
if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
|
|
@@ -374,12 +481,30 @@ static void vfdc_handler( u8_t *_data, int bytes_read) {
|
|
|
* Process graphic display data
|
|
|
*/
|
|
|
static void grfe_handler( u8_t *data, int len) {
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
|
|
|
scroller.active = false;
|
|
|
- display->draw_cbr(data + sizeof(struct grfe_packet), display_height);
|
|
|
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
+ // we are not in control or we are displaying visu on a small screen, do not do screen update
|
|
|
+ if (visu.mode && !visu.col && visu.row < SB_HEIGHT) {
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (displayer.owned) {
|
|
|
+ // did we have something that might have write on the bottom of a SB_HEIGHT+ display
|
|
|
+ if (displayer.dirty) {
|
|
|
+ display->clear(true);
|
|
|
+ displayer.dirty = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // draw new frame
|
|
|
+ display->draw_cbr(data + sizeof(struct grfe_packet), displayer.width, displayer.height);
|
|
|
+ display->update();
|
|
|
+ }
|
|
|
+
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
|
|
|
LOG_DEBUG("grfe frame %u", len);
|
|
|
}
|
|
@@ -415,36 +540,46 @@ static void grfs_handler(u8_t *data, int len) {
|
|
|
htonl(pkt->pause), // in ms
|
|
|
htonl(pkt->speed), // in ms
|
|
|
htons(pkt->by), // # of pixel of scroll step
|
|
|
- htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
|
|
|
- htons(pkt->width), // total width of animation
|
|
|
+ htons(pkt->mode), // 0=continuous, 1=once and stop, 2=once and end
|
|
|
+ htons(pkt->width), // last column of animation that contains a "full" screen
|
|
|
htons(pkt->offset) // offset if multiple packets are sent
|
|
|
);
|
|
|
|
|
|
// new grfs frame, build scroller info
|
|
|
if (!offset) {
|
|
|
// use the display as a general lock
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
|
|
|
// copy & set scroll parameters
|
|
|
scroller.screen = pkt->screen;
|
|
|
- scroller.direction = pkt->direction;
|
|
|
scroller.pause = htonl(pkt->pause);
|
|
|
scroller.speed = htonl(pkt->speed);
|
|
|
- scroller.by = htons(pkt->by);
|
|
|
scroller.mode = htons(pkt->mode);
|
|
|
- scroller.full_width = htons(pkt->width);
|
|
|
- scroller.updated = scroller.active = true;
|
|
|
+ scroller.scroll.width = htons(pkt->width);
|
|
|
+ scroller.first = true;
|
|
|
+
|
|
|
+ // background excludes space taken by visu (if any)
|
|
|
+ scroller.back.width = displayer.width - ((visu.mode && visu.row < SB_HEIGHT) ? visu.width : 0);
|
|
|
+
|
|
|
+ // set scroller steps & beginning
|
|
|
+ if (pkt->direction == 1) {
|
|
|
+ scroller.scrolled = 0;
|
|
|
+ scroller.by = htons(pkt->by);
|
|
|
+ } else {
|
|
|
+ scroller.scrolled = scroller.scroll.width;
|
|
|
+ scroller.by = -htons(pkt->by);
|
|
|
+ }
|
|
|
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
}
|
|
|
|
|
|
// copy scroll frame data (no semaphore needed)
|
|
|
- if (scroller.size + size < scroller.max) {
|
|
|
- memcpy(scroller.scroll_frame + offset, data + sizeof(struct grfs_packet), size);
|
|
|
- scroller.size = offset + size;
|
|
|
- LOG_INFO("scroller current size %u", scroller.size);
|
|
|
+ if (scroller.scroll.size + size < scroller.scroll.max) {
|
|
|
+ memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
|
|
|
+ scroller.scroll.size = offset + size;
|
|
|
+ LOG_INFO("scroller current size %u", scroller.scroll.size);
|
|
|
} else {
|
|
|
- LOG_INFO("scroller too larger %u/%u", scroller.size + size, scroller.max);
|
|
|
+ LOG_INFO("scroller too larger %u/%u", scroller.scroll.size + size, scroller.scroll.max);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -456,109 +591,309 @@ static void grfg_handler(u8_t *data, int len) {
|
|
|
|
|
|
LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
|
|
|
|
|
|
- memcpy(scroller.back_frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
|
|
|
- scroller.window_width = htons(pkt->width);
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
+
|
|
|
+ // size of scrollable area (less than background)
|
|
|
+ scroller.width = htons(pkt->width);
|
|
|
+ memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
|
|
|
+
|
|
|
+ // update display asynchronously (frames are oganized by columns)
|
|
|
+ memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
|
|
|
+ for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
|
|
|
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+ // can only write if we really own display
|
|
|
+ if (displayer.owned) {
|
|
|
+ display->draw_cbr(scroller.frame, scroller.back.width, displayer.height);
|
|
|
+ display->update();
|
|
|
+ }
|
|
|
+
|
|
|
+ // now we can active scrolling, but only if we are not on a small screen
|
|
|
+ if (!visu.mode || visu.col || visu.row >= SB_HEIGHT) scroller.active = true;
|
|
|
+
|
|
|
+ // if we just got a content update, let the scroller manage the screen
|
|
|
+ LOG_DEBUG("resuming scrolling task");
|
|
|
+
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
+
|
|
|
+ // resume task once we have background, not in grfs
|
|
|
+ vTaskResume(displayer.task);
|
|
|
+}
|
|
|
+
|
|
|
+/****************************************************************************************
|
|
|
+ * Update visualization bars
|
|
|
+ */
|
|
|
+static void visu_update(void) {
|
|
|
+ // no need to protect against no woning the display as we are playing
|
|
|
+ if (pthread_mutex_trylock(&visu_export.mutex)) return;
|
|
|
+
|
|
|
+ // not enough samples
|
|
|
+ if (visu_export.level < (visu.mode == VISU_VUMETER ? RMS_LEN : FFT_LEN) * 2 && visu_export.running) {
|
|
|
+ pthread_mutex_unlock(&visu_export.mutex);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // reset bars for all cases first
|
|
|
+ for (int i = visu.n; --i >= 0;) visu.bars[i].current = 0;
|
|
|
+
|
|
|
+ if (visu_export.running && visu_export.running) {
|
|
|
+
|
|
|
+ if (visu.mode == VISU_VUMETER) {
|
|
|
+ s16_t *iptr = visu_export.buffer;
|
|
|
+
|
|
|
+ // calculate sum(L²+R²), try to not overflow at the expense of some precision
|
|
|
+ for (int i = RMS_LEN; --i >= 0;) {
|
|
|
+ visu.bars[0].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
|
|
|
+ iptr++;
|
|
|
+ visu.bars[1].current += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
|
|
|
+ iptr++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // convert to dB (1 bit remaining for getting X²/N, 60dB dynamic starting from 0dBFS = 3 bits back-off)
|
|
|
+ for (int i = visu.n; --i >= 0;) {
|
|
|
+ visu.bars[i].current = 32 * (0.01667f*10*log10f(0.0000001f + (visu.bars[i].current >> 1)) - 0.2543f);
|
|
|
+ if (visu.bars[i].current > 31) visu.bars[i].current = 31;
|
|
|
+ else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // on xtensa/esp32 the floating point FFT takes 1/2 cycles of the fixed point
|
|
|
+ for (int i = 0 ; i < FFT_LEN ; i++) {
|
|
|
+ // don't normalize here, but we are due INT16_MAX and FFT_LEN / 2 / 2
|
|
|
+ visu.samples[i * 2 + 0] = (float) (visu_export.buffer[2*i] + visu_export.buffer[2*i + 1]) * visu.hanning[i];
|
|
|
+ visu.samples[i * 2 + 1] = 0;
|
|
|
+ }
|
|
|
|
|
|
- // can't be in grfs as we need full size & scroll_width
|
|
|
- if (scroller.updated) {
|
|
|
- scroller.scroll_len = display_width * display_height / 8 - (display_width - scroller.window_width) * display_height / 8;
|
|
|
- if (scroller.direction == 1) {
|
|
|
- scroller.scroll_ptr = scroller.scroll_frame;
|
|
|
- scroller.scroll_step = scroller.by * display_height / 8;
|
|
|
- } else {
|
|
|
- scroller.scroll_ptr = scroller.scroll_frame + scroller.size - scroller.scroll_len;
|
|
|
- scroller.scroll_step = -scroller.by * display_height / 8;
|
|
|
+ // actual FFT that might be less cycle than all the crap below
|
|
|
+ dsps_fft2r_fc32_ae32(visu.samples, FFT_LEN);
|
|
|
+ dsps_bit_rev_fc32_ansi(visu.samples, FFT_LEN);
|
|
|
+ float rate = visu_export.rate;
|
|
|
+
|
|
|
+ // now arrange the result with the number of bar and sampling rate (don't want DC)
|
|
|
+ for (int i = 0, j = 1; i < visu.n && j < (FFT_LEN / 2); i++) {
|
|
|
+ float power, count;
|
|
|
+
|
|
|
+ // find the next point in FFT (this is real signal, so only half matters)
|
|
|
+ for (count = 0, power = 0; j * visu_export.rate < visu.bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
|
|
|
+ power += visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1];
|
|
|
+ }
|
|
|
+ // due to sample rate, we have reached the end of the available spectrum
|
|
|
+ if (j >= (FFT_LEN / 2)) {
|
|
|
+ // normalize accumulated data
|
|
|
+ if (count) power /= count * 2.;
|
|
|
+ } else if (count) {
|
|
|
+ // how much of what remains do we need to add
|
|
|
+ float ratio = j - (visu.bars[i].limit * FFT_LEN) / rate;
|
|
|
+ power += (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) * ratio;
|
|
|
+
|
|
|
+ // normalize accumulated data
|
|
|
+ power /= (count + ratio) * 2;
|
|
|
+ } else {
|
|
|
+ // no data for that band (sampling rate too high), just assume same as previous one
|
|
|
+ power = (visu.samples[2*j] * visu.samples[2*j] + visu.samples[2*j+1] * visu.samples[2*j+1]) / 2.;
|
|
|
+ }
|
|
|
+
|
|
|
+ // convert to dB and bars, same back-off
|
|
|
+ if (power) visu.bars[i].current = 32 * (0.01667f*10*(log10f(power) - log10f(FFT_LEN/2*2)) - 0.2543f);
|
|
|
+ if (visu.bars[i].current > 31) visu.bars[i].current = 31;
|
|
|
+ else if (visu.bars[i].current < 0) visu.bars[i].current = 0;
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- scroller.updated = false;
|
|
|
+ // we took what we want, we can release the buffer
|
|
|
+ visu_export.level = 0;
|
|
|
+ pthread_mutex_unlock(&visu_export.mutex);
|
|
|
+
|
|
|
+ display->clear(false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
|
|
|
+
|
|
|
+ for (int i = visu.n; --i >= 0;) {
|
|
|
+ int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
|
|
|
+ int y1 = visu.row + visu.height - 1;
|
|
|
+
|
|
|
+ if (visu.bars[i].current > visu.bars[i].max) visu.bars[i].max = visu.bars[i].current;
|
|
|
+ else if (visu.bars[i].max) visu.bars[i].max--;
|
|
|
+
|
|
|
+ for (int j = 0; j <= visu.bars[i].current; j += 2)
|
|
|
+ display->draw_line( x1, y1 - j, x1 + visu.bar_width - 1, y1 - j);
|
|
|
+
|
|
|
+ if (visu.bars[i].max > 2) {
|
|
|
+ display->draw_line( x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max);
|
|
|
+ display->draw_line( x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/****************************************************************************************
|
|
|
+ * Visu packet handler
|
|
|
+ */
|
|
|
+void spectrum_limits(int min, int n, int pos) {
|
|
|
+ if (n / 2) {
|
|
|
+ int i;
|
|
|
+ float step = (DISPLAY_BW - min) * visu.spectrum_scale / (n/2);
|
|
|
+ visu.bars[pos].limit = min + step;
|
|
|
+ for (i = 1; i < n/2; i++) visu.bars[pos+i].limit = visu.bars[pos+i-1].limit + step;
|
|
|
+ spectrum_limits(visu.bars[pos + n/2 - 1].limit, n/2, pos + n/2);
|
|
|
+ } else {
|
|
|
+ visu.bars[pos].limit = DISPLAY_BW;
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+/****************************************************************************************
|
|
|
+ * Visu packet handler
|
|
|
+ */
|
|
|
+static void visu_handler( u8_t *data, int len) {
|
|
|
+ struct visu_packet *pkt = (struct visu_packet*) data;
|
|
|
+ int bars = 0;
|
|
|
+
|
|
|
+ LOG_DEBUG("visu %u with %u parameters", pkt->which, pkt->count);
|
|
|
+
|
|
|
+ /*
|
|
|
+ If width is specified, then respect all coordinates, otherwise we try to
|
|
|
+ use the bottom part of the display and if it is a small display, we overwrite
|
|
|
+ text
|
|
|
+ */
|
|
|
+
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
+ visu.mode = pkt->which;
|
|
|
+
|
|
|
+ // little trick to clean the taller screens when switching visu
|
|
|
+ if (visu.row >= SB_HEIGHT) display->clear(false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row - visu.height - 1);
|
|
|
|
|
|
- if (!scroller.active) {
|
|
|
- // this is a background update and scroller has been finished, so need to update here
|
|
|
- u8_t *frame = malloc(display_width * display_height / 8);
|
|
|
- memcpy(frame, scroller.back_frame, display_width * display_height / 8);
|
|
|
- for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
|
|
|
- display->draw_cbr(frame, display_height);
|
|
|
- free(frame);
|
|
|
- LOG_DEBUG("direct drawing");
|
|
|
+ if (visu.mode) {
|
|
|
+ if (pkt->count >= 4) {
|
|
|
+ // small visu, then go were we are told to
|
|
|
+ pkt->height = htonl(pkt->height);
|
|
|
+ pkt->row = htonl(pkt->row);
|
|
|
+ pkt->col = htonl(pkt->col);
|
|
|
+
|
|
|
+ visu.width = htonl(pkt->width);
|
|
|
+ visu.height = pkt->height ? pkt->height : SB_HEIGHT;
|
|
|
+ visu.col = pkt->col < 0 ? display->width + pkt->col : pkt->col;
|
|
|
+ visu.row = pkt->row < 0 ? display->height + pkt->row : pkt->row;
|
|
|
+ visu.border = htonl(pkt->border);
|
|
|
+ bars = htonl(pkt->bars);
|
|
|
+ visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
|
|
|
+ } else {
|
|
|
+ // full screen visu, try to use bottom screen if available
|
|
|
+ visu.width = display->width;
|
|
|
+ visu.height = display->height > SB_HEIGHT ? display->height - SB_HEIGHT : display->height;
|
|
|
+ visu.col = visu.border = 0;
|
|
|
+ visu.row = display->height - visu.height;
|
|
|
+ bars = htonl(pkt->full.bars);
|
|
|
+ visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
|
|
|
+ }
|
|
|
+
|
|
|
+ // try to adapt to what we have
|
|
|
+ if (visu.mode == VISU_SPECTRUM) {
|
|
|
+ visu.n = bars ? bars : MAX_BARS;
|
|
|
+ if (visu.spectrum_scale <= 0 || visu.spectrum_scale > 0.5) visu.spectrum_scale = 0.5;
|
|
|
+ spectrum_limits(0, visu.n, 0);
|
|
|
+ } else {
|
|
|
+ visu.n = 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ do {
|
|
|
+ visu.bar_width = (visu.width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
|
|
|
+ if (visu.bar_width > 0) break;
|
|
|
+ } while (--visu.n);
|
|
|
+ visu.bar_border = (visu.width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
|
|
|
+
|
|
|
+ // give up if not enough space
|
|
|
+ if (visu.bar_width < 0) {
|
|
|
+ visu.mode = VISU_BLANK;
|
|
|
+ LOG_WARN("Not enough room for displaying visu");
|
|
|
+ } else {
|
|
|
+ // de-activate scroller if we are taking main screen
|
|
|
+ if (visu.row < SB_HEIGHT) scroller.active = false;
|
|
|
+ vTaskResume(displayer.task);
|
|
|
+ }
|
|
|
+ visu.wake = 0;
|
|
|
+
|
|
|
+ // reset bars maximum
|
|
|
+ for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
|
|
|
+
|
|
|
+ display->clear(false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row - visu.height - 1);
|
|
|
+
|
|
|
+ LOG_INFO("Visualizer with %u bars of width %d:%d:%d:%d (%w:%u,h:%u,c:%u,r:%u,s:%.02f)", visu.n, visu.bar_border, visu.bar_width, visu.bar_gap, visu.border, visu.width, visu.height, visu.col, visu.row, visu.spectrum_scale);
|
|
|
+ } else {
|
|
|
+ LOG_INFO("Stopping visualizer");
|
|
|
}
|
|
|
- else {
|
|
|
- // if we just got a content update, let the scroller manage the screen
|
|
|
- LOG_DEBUG("resuming scrolling task");
|
|
|
- vTaskResume(scroller.task);
|
|
|
- }
|
|
|
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
-}
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
+}
|
|
|
|
|
|
/****************************************************************************************
|
|
|
* Scroll task
|
|
|
- */
|
|
|
-static void scroll_task(void *args) {
|
|
|
- u8_t *frame = NULL;
|
|
|
- int len = display_width * display_height / 8;
|
|
|
+ * - with the addition of the visualizer, it's a bit a 2-headed beast not easy to
|
|
|
+ * maintain, so som better separation between the visu and scroll is probably needed
|
|
|
+ */
|
|
|
+static void displayer_task(void *args) {
|
|
|
+ int sleep;
|
|
|
|
|
|
while (1) {
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
|
|
|
- // suspend ourselves if nothing to do, grfg will wake us up
|
|
|
- if (!scroller.active) {
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
+ // suspend ourselves if nothing to do, grfg or visu will wake us up
|
|
|
+ if (!scroller.active && !visu.mode) {
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
vTaskSuspend(NULL);
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+ xSemaphoreTake(displayer.mutex, portMAX_DELAY);
|
|
|
+ scroller.wake = visu.wake = 0;
|
|
|
}
|
|
|
|
|
|
- // lock screen & active status
|
|
|
- frame = malloc(display_width * display_height / 8);
|
|
|
-
|
|
|
+ // go for long sleep when either item is disabled
|
|
|
+ if (!visu.mode) visu.wake = LONG_WAKE;
|
|
|
+ if (!scroller.active) scroller.wake = LONG_WAKE;
|
|
|
+
|
|
|
// scroll required amount of columns (within the window)
|
|
|
- while (scroller.direction == 1 ? (scroller.scroll_ptr <= scroller.scroll_frame + scroller.size - scroller.scroll_step - len) :
|
|
|
- (scroller.scroll_ptr + scroller.scroll_step >= scroller.scroll_frame) ) {
|
|
|
-
|
|
|
- // don't do anything if we have aborted
|
|
|
- if (!scroller.active) break;
|
|
|
-
|
|
|
- // scroll required amount of columns (within the window)
|
|
|
- memcpy(frame, scroller.back_frame, display_width * display_height / 8);
|
|
|
- for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
|
|
|
- scroller.scroll_ptr += scroller.scroll_step;
|
|
|
- display->draw_cbr(frame, display_height);
|
|
|
+ if (scroller.active && scroller.wake <= 0) {
|
|
|
+ // by default go for the long sleep, will change below if required
|
|
|
+ scroller.wake = LONG_WAKE;
|
|
|
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
- vTaskDelay(scroller.speed / portTICK_PERIOD_MS);
|
|
|
-
|
|
|
- xSemaphoreTake(display_mutex, portMAX_DELAY);
|
|
|
+ // do we have more to scroll (scroll.width is the last column from which we havea full zone)
|
|
|
+ if (scroller.by > 0 ? (scroller.scrolled <= scroller.scroll.width) : (scroller.scrolled >= 0)) {
|
|
|
+ memcpy(scroller.frame, scroller.back.frame, scroller.back.width * displayer.height / 8);
|
|
|
+ for (int i = 0; i < scroller.width * displayer.height / 8; i++) scroller.frame[i] |= scroller.scroll.frame[scroller.scrolled * displayer.height / 8 + i];
|
|
|
+ scroller.scrolled += scroller.by;
|
|
|
+ if (displayer.owned) display->draw_cbr(scroller.frame, scroller.width, displayer.height);
|
|
|
+
|
|
|
+ // short sleep & don't need background update
|
|
|
+ scroller.wake = scroller.speed;
|
|
|
+ } else if (scroller.first || !scroller.mode) {
|
|
|
+ // at least one round done
|
|
|
+ scroller.first = false;
|
|
|
+
|
|
|
+ // see if we need to pause or if we are done
|
|
|
+ if (scroller.mode) {
|
|
|
+ // can't call directly send_packet from slimproto as it's not re-entrant
|
|
|
+ ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
|
|
|
+ LOG_INFO("scroll-once terminated");
|
|
|
+ } else {
|
|
|
+ scroller.wake = scroller.pause;
|
|
|
+ LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
|
|
|
+ }
|
|
|
+
|
|
|
+ // need to reset pointers for next scroll
|
|
|
+ scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // update visu if active
|
|
|
+ if (visu.mode && visu.wake <= 0) {
|
|
|
+ visu_update();
|
|
|
+ visu.wake = 100;
|
|
|
}
|
|
|
|
|
|
- // done with scrolling cycle reset scroller ptr
|
|
|
- scroller.scroll_ptr = scroller.scroll_frame + (scroller.direction == 2 ? scroller.size - scroller.scroll_len : 0);
|
|
|
-
|
|
|
- // scrolling done, update screen and see if we need to continue
|
|
|
- if (scroller.active) {
|
|
|
- memcpy(frame, scroller.back_frame, len);
|
|
|
- for (int i = 0; i < scroller.scroll_len; i++) frame[i] |= scroller.scroll_ptr[i];
|
|
|
- display->draw_cbr(frame, display_height);
|
|
|
- free(frame);
|
|
|
-
|
|
|
- // see if we need to pause or if we are done
|
|
|
- if (scroller.mode) {
|
|
|
- scroller.active = false;
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
- // can't call directly send_packet from slimproto as it's not re-entrant
|
|
|
- ANIC_resp = ANIM_SCROLL_ONCE | ANIM_SCREEN_1;
|
|
|
- LOG_INFO("scroll-once terminated");
|
|
|
- } else {
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
- vTaskDelay(scroller.pause / portTICK_PERIOD_MS);
|
|
|
- LOG_DEBUG("scroll cycle done, pausing for %u (ms)", scroller.pause);
|
|
|
- }
|
|
|
- } else {
|
|
|
- free(frame);
|
|
|
- xSemaphoreGive(display_mutex);
|
|
|
- LOG_INFO("scroll aborted");
|
|
|
- }
|
|
|
+ // need to make sure we own display
|
|
|
+ if (displayer.owned) display->update();
|
|
|
+
|
|
|
+ // release semaphore and sleep what's needed
|
|
|
+ xSemaphoreGive(displayer.mutex);
|
|
|
+
|
|
|
+ sleep = min(visu.wake, scroller.wake);
|
|
|
+ vTaskDelay(sleep / portTICK_PERIOD_MS);
|
|
|
+ scroller.wake -= sleep;
|
|
|
+ visu.wake -= sleep;
|
|
|
}
|
|
|
}
|
|
|
|