1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330 |
- #include <ctype.h>
- #include <math.h>
- #include "esp_dsp.h"
- #include "squeezelite.h"
- #include "slimproto.h"
- #include "display.h"
- #include "gds.h"
- #include "gds_text.h"
- #include "gds_draw.h"
- #include "gds_image.h"
- #pragma pack(push, 1)
- struct grfb_packet {
- char opcode[4];
- s16_t brightness;
- };
- struct grfe_packet {
- char opcode[4];
- u16_t offset;
- u8_t transition;
- u8_t param;
- };
- struct grfs_packet {
- char opcode[4];
- u8_t screen;
- u8_t direction;
- u32_t pause;
- u32_t speed;
- u16_t by;
- u16_t mode;
- u16_t width;
- u16_t offset;
- };
- struct grfg_packet {
- char opcode[4];
- u16_t screen;
- u16_t width;
- };
- struct grfa_packet {
- char opcode[4];
- u32_t length;
- u16_t x;
- u16_t y;
- u32_t offset;
- };
- struct visu_packet {
- char opcode[4];
- u8_t which;
- u8_t count;
- union {
- struct {
- u32_t width;
- union {
- struct {
- u32_t bars;
- u32_t spectrum_scale;
- };
- u32_t style;
- };
- } 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 {
- u32_t mono;
- u32_t bandwidth;
- u32_t preemph;
- struct {
- u32_t pos;
- u32_t width;
- u32_t orient;
- u32_t bar_width;
- u32_t bar_space;
- u32_t clipping;
- u32_t bar_intens;
- u32_t bar_cap_intens;
- } channels[2];
- };
- struct {
- u32_t mono;
- u32_t style;
- struct {
- u32_t pos;
- u32_t width;
- } channels[2];
- } classical_vu;
- };
- };
- struct ANIC_header {
- char opcode[4];
- u32_t length;
- u8_t mode;
- };
- struct dmxt_packet {
- char opcode[4];
- u16_t x;
- u16_t length;
- };
- #pragma pack(pop)
- static struct {
- TaskHandle_t task;
- int wake;
- bool owned;
- struct {
- SemaphoreHandle_t mutex;
- int width, height;
- bool dirty;
- };
- } displayer = { .dirty = true, .owned = true };
- static uint32_t *grayMap;
- #define LONG_WAKE (10*1000)
- #define SB_HEIGHT 32
- #define FFT_LEN_BIT 7
- #define FFT_LEN (1 << FFT_LEN_BIT)
- #define RMS_LEN_BIT 6
- #define RMS_LEN (1 << RMS_LEN_BIT)
- #define VU_WIDTH 160
- #define VU_HEIGHT SB_HEIGHT
- #define VU_COUNT 48
- #define ARROW_WIDTH 11
- #define DISPLAY_BW 20000
- static struct scroller_s {
-
- u8_t screen;
- u32_t pause;
- u16_t mode;
- s16_t by;
-
- bool active, first, overflow;
- int scrolled;
- int speed, wake;
- 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;
- static struct {
- u8_t *data;
- u32_t size;
- u16_t x, y;
- bool enable, full;
- } artwork;
- #define MAX_BARS 32
- #define VISU_ESP32 0x10
- static EXT_RAM_ATTR struct {
- int bar_gap, bar_width, bar_border;
- bool rotate;
- struct bar_s {
- int current, max;
- int limit;
- } bars[MAX_BARS];
- float spectrum_scale;
- int n, col, row, height, width, border, style, max;
- enum { VISU_BLANK, VISU_VUMETER = 0x01, VISU_SPECTRUM = 0x02, VISU_WAVEFORM } mode;
- struct {
- u8_t *frame;
- int width;
- bool active;
- } back;
- } visu;
- static EXT_RAM_ATTR struct {
- float fft[FFT_LEN*2], samples[FFT_LEN*2], hanning[FFT_LEN];
- int levels[2];
- } meters;
- static EXT_RAM_ATTR struct {
- int mode;
- int max;
- u16_t config;
- struct bar_s bars[MAX_BARS] ;
- } led_visu;
- static EXT_RAM_ATTR uint8_t vu_bitmap[VU_WIDTH * VU_HEIGHT];
- extern const uint8_t vu_base[] asm("_binary_vu_s_data_start");
- extern const struct {
- uint8_t offset;
- uint8_t data[VU_HEIGHT * ARROW_WIDTH];
- } vu_arrow[VU_COUNT] asm("_binary_arrow_data_start");
- #define ANIM_NONE 0x00
- #define ANIM_TRANSITION 0x01
- #define ANIM_SCROLL_ONCE 0x02
- #define ANIM_SCREEN_1 0x04
- #define ANIM_SCREEN_2 0x08
- #define SCROLL_STACK_SIZE (3*1024)
- #define LINELEN 40
- static log_level loglevel = lINFO;
- static bool (*slimp_handler_chain)(u8_t *data, int len);
- static void (*notify_chain)(in_addr_t ip, u16_t hport, u16_t cport);
- 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 sendSETD(u16_t width, u16_t height, u16_t led_config);
- static void sendANIC(u8_t code);
- 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 grfa_handler(u8_t *data, int len);
- static void visu_handler(u8_t *data, int len);
- static void dmxt_handler(u8_t *data, int len);
- static void displayer_task(void* arg);
- void *led_display;
-
- bool sb_displayer_init(void) {
- static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
- static EXT_RAM_ATTR StackType_t xStack[SCROLL_STACK_SIZE] __attribute__ ((aligned (4)));
-
-
- if ((GDS_GetWidth(display) <= 0 || GDS_GetHeight(display) <= 0) && !led_display) {
- LOG_INFO("no display or led visualizer for LMS");
- return false;
- }
-
- if (display) {
-
- displayer.width = GDS_GetWidth(display);
- displayer.height = min(GDS_GetHeight(display), SB_HEIGHT);
-
-
- if (GDS_GetMode(display) > GDS_GRAYSCALE) {
- grayMap = malloc(256*sizeof(*grayMap));
- for (int i = 0; i < 256; i++) grayMap[i] = GDS_GrayMap(display, i);
- }
-
-
- visu.bar_gap = 1;
- visu.back.frame = calloc(1, (displayer.width * displayer.height) / 8);
-
-
- memcpy(vu_bitmap, vu_base, sizeof(vu_bitmap));
-
-
- scroller.scroll.max = (displayer.width * displayer.height / 8) * (15 + 1);
- scroller.scroll.frame = malloc(scroller.scroll.max);
- scroller.back.frame = malloc(displayer.width * displayer.height / 8);
- scroller.frame = malloc(displayer.width * displayer.height / 8);
-
-
- display_bus_chain = display_bus;
- display_bus = display_bus_handler;
- }
-
- if (led_display) {
-
- led_visu.mode = VISU_VUMETER;
- }
-
-
- sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
-
- dsps_fft2r_init_fc32(meters.fft, FFT_LEN);
- dsps_wind_hann_f32(meters.hanning, FFT_LEN);
-
-
- displayer.mutex = xSemaphoreCreateMutex();
- displayer.task = xTaskCreateStatic( (TaskFunction_t) displayer_task, "sb_displayer", SCROLL_STACK_SIZE, NULL, ESP_TASK_PRIO_MIN + 1, xStack, &xTaskBuffer);
-
-
- slimp_handler_chain = slimp_handler;
- slimp_handler = handler;
-
- notify_chain = server_notify;
- server_notify = server;
-
- return display != NULL;
- }
- static bool display_bus_handler(void *from, enum display_bus_cmd_e cmd) {
-
- 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;
- }
- static void sendANIC(u8_t code) {
- struct ANIC_header pkt_header;
- memset(&pkt_header, 0, sizeof(pkt_header));
- memcpy(&pkt_header.opcode, "ANIC", 4);
- pkt_header.length = htonl(sizeof(pkt_header) - 8);
- pkt_header.mode = code;
- LOCK_P;
- send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
- UNLOCK_P;
- }
-
- static void sendSETD(u16_t width, u16_t height, u16_t led_config) {
- struct SETD_header pkt_header;
-
- memset(&pkt_header, 0, sizeof(pkt_header));
- memcpy(&pkt_header.opcode, "SETD", 4);
- pkt_header.id = 0xfe;
- pkt_header.length = htonl(sizeof(pkt_header) + 6 - 8);
-
- LOG_INFO("sending dimension %ux%u", width, height);
- width = htons(width);
- height = htons(height);
-
- LOCK_P;
- send_packet((uint8_t *) &pkt_header, sizeof(pkt_header));
- send_packet((uint8_t *) &width, 2);
- send_packet((uint8_t *) &height, 2);
- send_packet((uint8_t *) &led_config, 2);
- UNLOCK_P;
- }
- static void server(in_addr_t ip, u16_t hport, u16_t cport) {
- char msg[32];
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
- sprintf(msg, "%s:%hu", inet_ntoa(ip), hport);
- if (display && displayer.owned) GDS_TextPos(display, GDS_FONT_LINE_1, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, msg);
- displayer.dirty = true;
-
- xSemaphoreGive(displayer.mutex);
-
-
- sendSETD(GDS_GetWidth(display), GDS_GetHeight(display), led_visu.config);
-
- if (notify_chain) (*notify_chain)(ip, hport, cport);
- }
- static bool handler(u8_t *data, int len){
- bool res = true;
- 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, "grfa", 4)) {
- grfa_handler(data, len);
- } else if (!strncmp((char*) data, "visu", 4)) {
- visu_handler(data, len);
- } else if (!strncmp((char*) data, "dmxt", 4)) {
- dmxt_handler(data, len);
- } else {
- res = false;
- }
-
- if (*slimp_handler_chain) res |= (*slimp_handler_chain)(data, len);
-
- return res;
- }
- static void makeprintable(unsigned char * line) {
- for (int n = 0; n < LINELEN; n++) {
- switch (line[n]) {
- case 11:
- line[n] = '#';
- break;;
- case 16:
- line[n] = '>';
- break;;
- case 22:
- line[n] = '@';
- break;;
- case 145:
- line[n] = ' ';
- break;;
- case 152:
- line[n] = 'o';
- break;
- default:
- break;
- }
- }
- }
- static bool charisok(unsigned char c) {
- switch (c) {
- case 11:
- case 16:
- case 22:
- case 145:
- case 152:
- return true;
- break;;
- default:
- return isprint(c);
- }
- }
- static void show_display_buffer(char *ddram) {
- char line1[LINELEN+1];
- char *line2;
- memset(line1, 0, LINELEN+1);
- strncpy(line1, ddram, LINELEN+1);
- line1[LINELEN] = '\0';
- line2 = &(ddram[LINELEN]);
- line2[LINELEN] = '\0';
-
- makeprintable((unsigned char *)line1);
- makeprintable((unsigned char *)line2);
- LOG_DEBUG("\n\t%.40s\n\t%.40s", line1, line2);
- GDS_TextLine(display, 1, GDS_TEXT_LEFT, GDS_TEXT_CLEAR, line1);
- GDS_TextLine(display, 2, GDS_TEXT_LEFT, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, line2);
- }
- static void vfdc_handler( u8_t *_data, int bytes_read) {
- unsigned short *data = (unsigned short*) _data, *display_data;
- char ddram[(LINELEN + 1) * 2];
- int n, addr = 0;
- bytes_read -= 4;
- if (bytes_read % 2) bytes_read--;
-
- display_data = &(data[5]);
- memset(ddram, ' ', LINELEN * 2);
- for (n = 0; n < (bytes_read/2); n++) {
- unsigned short d;
- unsigned char t, c;
- d = ntohs(display_data[n]);
- t = (d & 0x00ff00) >> 8;
- c = (d & 0x0000ff);
- switch (t) {
- case 0x03:
- if (!charisok(c)) c = ' ';
- if (addr <= LINELEN * 2) {
- ddram[addr++] = c;
- }
- break;
- case 0x02:
- switch (c) {
- case 0x06:
- memset(ddram, ' ', LINELEN * 2);
- break;
- case 0x02:
- addr = 0;
- break;
- case 0xc0:
- addr = LINELEN;
- break;
- }
- }
- }
- show_display_buffer(ddram);
- }
- void draw_VU(struct GDS_Device * display, int level, int x, int y, int width, bool rotate) {
-
- uint8_t *data = vu_bitmap;
- int offset = level > 0 ? vu_arrow[level].offset * VU_HEIGHT : 0;
-
- memcpy(data + offset, vu_arrow[level].data, sizeof(vu_arrow[level].data));
-
-
- if (width > VU_WIDTH) {
- if (rotate) y += (width - VU_WIDTH) / 2;
- else x += (width - VU_WIDTH) / 2;
- width = VU_WIDTH;
- } else {
- data += (VU_WIDTH - width) / 2 * VU_HEIGHT;
- }
- if (GDS_GetMode(display) <= GDS_GRAYSCALE) {
-
- int scale = 8 - GDS_GetDepth(display);
-
-
- if (rotate) {
- for (int r = 0; r < width; r++) {
- for (int c = VU_HEIGHT; --c >= 0;) {
- GDS_DrawPixelFast(display, c + x, r + y, *data++ >> scale);
- }
- }
- } else {
- for (int r = 0; r < width; r++) {
- for (int c = 0; c < VU_HEIGHT; c++) {
- GDS_DrawPixelFast(display, r + x, c + y, *data++ >> scale);
- }
- }
- }
- } else {
-
- if (rotate) {
- for (int r = 0; r < width; r++) {
- for (int c = VU_HEIGHT; --c >= 0;) {
- GDS_DrawPixelFast(display, c + x, r + y, grayMap[*data++]);
- }
- }
- } else {
- for (int r = 0; r < width; r++) {
- for (int c = 0; c < VU_HEIGHT; c++) {
- GDS_DrawPixelFast(display, r + x, c + y, grayMap[*data++]);
- }
- }
- }
- }
-
-
- memcpy(vu_bitmap + offset, vu_base + offset, sizeof(vu_arrow[level].data));
-
-
- GDS_SetDirty(display);
- }
- static void grfe_handler( u8_t *data, int len) {
- struct grfe_packet *pkt = (struct grfe_packet*) data;
-
-
- if (pkt->transition != 'c') {
- LOG_INFO("Transition %c requested with offset %hu, param %d", pkt->transition, pkt->offset, pkt->param);
- sendANIC(ANIM_TRANSITION);
- }
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
- scroller.active = false;
-
-
- if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
- xSemaphoreGive(displayer.mutex);
- return;
- }
-
-
- if (displayer.owned) {
-
- int width = ((len - sizeof(struct grfe_packet)) * 8) / displayer.height;
-
- if (displayer.dirty || (artwork.enable && width == displayer.width && artwork.y < displayer.height)) {
- GDS_Clear(display, GDS_COLOR_BLACK);
- displayer.dirty = false;
- }
-
-
- if (!(visu.mode & VISU_ESP32)) {
- visu.back.width = width;
- memset(visu.back.frame, 0, (displayer.width * displayer.height) / 8);
- memcpy(visu.back.frame, data + sizeof(struct grfe_packet), (width * displayer.height) / 8);
-
- visu.back.active = *visu.back.frame || memcmp(visu.back.frame, visu.back.frame + 1, width - 1);
- }
-
- GDS_DrawBitmapCBR(display, data + sizeof(struct grfe_packet), width, displayer.height, GDS_COLOR_WHITE);
- GDS_Update(display);
- }
-
- xSemaphoreGive(displayer.mutex);
-
- LOG_DEBUG("grfe frame %u", len);
- }
- static void grfb_handler(u8_t *data, int len) {
- struct grfb_packet *pkt = (struct grfb_packet*) data;
-
- pkt->brightness = htons(pkt->brightness);
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
- if (pkt->brightness <= 0) {
- GDS_DisplayOff(display);
- } else {
- GDS_DisplayOn(display);
- GDS_SetContrast(display, 255 * powf(pkt->brightness / 5.0f, 3));
- }
-
- xSemaphoreGive(displayer.mutex);
-
- LOG_INFO("brightness %hu", pkt->brightness);
- }
- static void grfs_handler(u8_t *data, int len) {
- struct grfs_packet *pkt = (struct grfs_packet*) data;
- int size = len - sizeof(struct grfs_packet);
- int offset = htons(pkt->offset);
-
- LOG_DEBUG("grfs s:%u d:%u p:%u sp:%u by:%hu m:%hu w:%hu o:%hu",
- (int) pkt->screen,
- (int) pkt->direction,
- htonl(pkt->pause),
- htonl(pkt->speed),
- htons(pkt->by),
- htons(pkt->mode),
- htons(pkt->width),
- htons(pkt->offset)
- );
-
-
- if (!offset) {
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
- scroller.screen = pkt->screen;
- scroller.pause = htonl(pkt->pause);
- scroller.speed = htonl(pkt->speed);
- scroller.mode = htons(pkt->mode);
- scroller.scroll.width = htons(pkt->width);
- scroller.first = true;
- scroller.overflow = false;
-
-
- if (pkt->direction == 1) {
- scroller.scrolled = 0;
- scroller.by = htons(pkt->by);
- } else {
- scroller.scrolled = scroller.scroll.width;
- scroller.by = -htons(pkt->by);
- }
- xSemaphoreGive(displayer.mutex);
- }
-
- if (scroller.scroll.size + size < scroller.scroll.max && !scroller.overflow) {
- memcpy(scroller.scroll.frame + offset, data + sizeof(struct grfs_packet), size);
- scroller.scroll.size = offset + size;
- LOG_INFO("scroller current size %u (w:%u)", scroller.scroll.size, scroller.scroll.width);
- } else {
- LOG_INFO("scroller too large %u/%u (w:%u)", scroller.scroll.size + size, scroller.scroll.max, scroller.scroll.width);
- scroller.scroll.width = scroller.scroll.size / (displayer.height / 8) - scroller.back.width;
- scroller.overflow = true;
- }
- }
- static void grfg_handler(u8_t *data, int len) {
- struct grfg_packet *pkt = (struct grfg_packet*) data;
-
- LOG_DEBUG("gfrg s:%hu w:%hu (len:%u)", htons(pkt->screen), htons(pkt->width), len);
-
- if (((visu.mode & VISU_ESP32) && !visu.col && visu.row < displayer.height) || artwork.full) {
- return;
- }
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
-
- scroller.width = htons(pkt->width);
- scroller.back.width = ((len - sizeof(struct grfg_packet)) * 8) / displayer.height;
- memcpy(scroller.back.frame, data + sizeof(struct grfg_packet), len - sizeof(struct grfg_packet));
-
-
- 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];
-
-
- if (displayer.owned) {
- GDS_DrawBitmapCBR(display, scroller.frame, scroller.back.width, displayer.height, GDS_COLOR_WHITE);
- GDS_Update(display);
- }
-
-
- if (!visu.mode || visu.col || visu.row >= displayer.height) scroller.active = true;
-
-
- LOG_DEBUG("resuming scrolling task");
-
- xSemaphoreGive(displayer.mutex);
-
-
- vTaskResume(displayer.task);
- }
- static void grfa_handler(u8_t *data, int len) {
- struct grfa_packet *pkt = (struct grfa_packet*) data;
- int size = len - sizeof(struct grfa_packet);
- int offset = htonl(pkt->offset);
- int length = htonl(pkt->length);
-
- artwork.enable = (length != 0);
-
-
- if (length < 32) {
- if (artwork.enable) {
-
- artwork.x = htons(pkt->x);
- artwork.y = htons(pkt->y);
- } else if (artwork.size) GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
-
- artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
- LOG_DEBUG("gfra en:%u x:%hu, y:%hu", artwork.enable, artwork.x, artwork.y);
-
-
- return;
- }
-
-
- if (!offset) {
-
- if (artwork.size) {
- GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
- artwork.size = 0;
- }
-
-
- artwork.x = htons(pkt->x);
- artwork.y = htons(pkt->y);
- artwork.full = artwork.enable && artwork.x == 0 && artwork.y == 0;
- if (artwork.data) free(artwork.data);
- artwork.data = malloc(length);
- }
-
-
- memcpy(artwork.data + offset, data + sizeof(struct grfa_packet), size);
- artwork.size += size;
- if (artwork.size == length) {
- GDS_ClearWindow(display, artwork.x, artwork.y, -1, -1, GDS_COLOR_BLACK);
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
- GDS_DrawJPEG(display, artwork.data, artwork.x, artwork.y, artwork.y < displayer.height ? (GDS_IMAGE_RIGHT | GDS_IMAGE_TOP) : GDS_IMAGE_CENTER);
- xSemaphoreGive(displayer.mutex);
- free(artwork.data);
- artwork.data = NULL;
- }
-
- LOG_DEBUG("gfra l:%u x:%hu, y:%hu, o:%u s:%u", length, artwork.x, artwork.y, offset, size);
- }
- void spectrum_scale(int n, struct bar_s *bars, int max, float *samples) {
- float rate = visu_export.rate;
-
- for (int i = 0, j = 1; i < n && j < (FFT_LEN / 2); i++) {
- float power, count;
-
- for (count = 0, power = 0; j * visu_export.rate < bars[i].limit * FFT_LEN && j < FFT_LEN / 2; j++, count += 1) {
- power += samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1];
- }
-
- if (j >= (FFT_LEN / 2)) {
-
- if (count) power /= count * 2.;
- } else if (count) {
-
- float ratio = j - (bars[i].limit * FFT_LEN) / rate;
- power += (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) * ratio;
-
-
- power /= (count + ratio) * 2;
- } else {
-
- power = (samples[2*j] * samples[2*j] + samples[2*j+1] * samples[2*j+1]) / 2.;
- }
-
-
- bars[i].current = max * (0.01667f*10*(log10f(0.0000001f + power) - log10f(FFT_LEN*(visu_export.gain == FIXED_ONE ? 256 : 2))) - 0.2543f);
- if (bars[i].current > max) bars[i].current = max;
- else if (bars[i].current < 0) bars[i].current = 0;
- }
- }
- void vu_scale(struct bar_s *bars, int max, int *levels) {
-
- for (int i = 2; --i >= 0;) {
- bars[i].current = max * (0.01667f*10*log10f(0.0000001f + (levels[i] >> (visu_export.gain == FIXED_ONE ? 8 : 1))) - 0.2543f);
- if (bars[i].current > max) bars[i].current = max;
- else if (bars[i].current < 0) bars[i].current = 0;
- }
- }
- void visu_draw(void) {
-
- int clear = 0;
- for (int i = visu.n; --i >= 0;) clear = max(clear, visu.bars[i].max);
- if (clear) GDS_ClearExt(display, false, false, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
-
-
- if (!(visu.mode & VISU_ESP32) && visu.back.active) {
- GDS_DrawBitmapCBR(display, visu.back.frame, visu.back.width, displayer.height, GDS_COLOR_WHITE);
- }
- if ((visu.mode & ~VISU_ESP32) != VISU_VUMETER || !visu.style) {
-
- for (int i = visu.n; --i >= 0;) {
-
- 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--;
- else if (!clear) continue;
- if (visu.rotate) {
- int x1 = visu.col;
- int y1 = visu.row + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
- for (int j = 0; j <= visu.bars[i].current; j += 2)
- GDS_DrawLine(display, x1 + j, y1, x1 + j, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
-
- if (visu.bars[i].max > 2) {
- GDS_DrawLine(display, x1 + visu.bars[i].max, y1, x1 + visu.bars[i].max, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
- if (visu.bars[i].max < visu.max - 1) GDS_DrawLine(display, x1 + visu.bars[i].max + 1, y1, x1 + visu.bars[i].max + 1, y1 + visu.bar_width - 1, GDS_COLOR_WHITE);
- }
- } else {
- int x1 = visu.col + visu.border + visu.bar_border + i*(visu.bar_width + visu.bar_gap);
- int y1 = visu.row + visu.height - 1;
- for (int j = 0; j <= visu.bars[i].current; j += 2)
- GDS_DrawLine(display, x1, y1 - j, x1 + visu.bar_width - 1, y1 - j, GDS_COLOR_WHITE);
-
- if (visu.bars[i].max > 2) {
- GDS_DrawLine(display, x1, y1 - visu.bars[i].max, x1 + visu.bar_width - 1, y1 - visu.bars[i].max, GDS_COLOR_WHITE);
- if (visu.bars[i].max < visu.max - 1) GDS_DrawLine(display, x1, y1 - visu.bars[i].max + 1, x1 + visu.bar_width - 1, y1 - visu.bars[i].max + 1, GDS_COLOR_WHITE);
- }
- }
- }
- } else if (displayer.width / 2 >= 3 * VU_WIDTH / 4) {
- if (visu.rotate) {
- draw_VU(display, visu.bars[0].current, 0, visu.row, visu.height / 2, visu.rotate);
- draw_VU(display, visu.bars[1].current, 0, visu.row + visu.height / 2, visu.height / 2, visu.rotate);
- } else {
- draw_VU(display, visu.bars[0].current, 0, visu.row, visu.width / 2, visu.rotate);
- draw_VU(display, visu.bars[1].current, visu.width / 2, visu.row, visu.width / 2, visu.rotate);
- }
- } else {
- int level = (visu.bars[0].current + visu.bars[1].current) / 2;
- draw_VU(display, level, 0, visu.row, visu.rotate ? visu.height : visu.width, visu.rotate);
- }
- }
- static void displayer_update(void) {
-
- if ((artwork.full && !led_visu.mode) || pthread_mutex_trylock(&visu_export.mutex)) {
- return;
- }
-
- int mode = (visu.mode & ~VISU_ESP32) | led_visu.mode;
-
-
- if (visu_export.level < (mode & VISU_SPECTRUM ? FFT_LEN : RMS_LEN) && visu_export.running) {
- pthread_mutex_unlock(&visu_export.mutex);
- return;
- }
-
-
- meters.levels[0] = meters.levels[1] = 0;
- memset(meters.samples, 0, sizeof(meters.samples));
-
- if (visu_export.running) {
-
-
- if (mode & VISU_VUMETER) {
- s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
- int *left = &meters.levels[0], *right = &meters.levels[1];
-
- for (int i = RMS_LEN; --i >= 0;) {
- *left += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
- iptr += BYTES_PER_FRAME / 4;
- *right += (*iptr * *iptr + (1 << (RMS_LEN_BIT - 2))) >> (RMS_LEN_BIT - 1);
- iptr += BYTES_PER_FRAME / 4;
- }
- }
-
-
- if (mode & VISU_SPECTRUM) {
- s16_t *iptr = (s16_t*) visu_export.buffer + (BYTES_PER_FRAME / 4) - 1;
-
- for (int i = 0 ; i < FFT_LEN ; i++) {
-
- meters.samples[i * 2 + 0] = (float) (*iptr + *(iptr+BYTES_PER_FRAME/4)) * meters.hanning[i];
- meters.samples[i * 2 + 1] = 0;
- iptr += 2 * BYTES_PER_FRAME / 4;
- }
-
- dsps_fft2r_fc32_ae32(meters.samples, FFT_LEN);
- dsps_bit_rev_fc32_ansi(meters.samples, FFT_LEN);
- }
-
- }
-
-
- visu_export.level = 0;
- pthread_mutex_unlock(&visu_export.mutex);
-
- if (visu.mode && !artwork.full) {
- if (visu.mode & VISU_SPECTRUM) spectrum_scale(visu.n, visu.bars, visu.max, meters.samples);
- else for (int i = 2; --i >= 0;) vu_scale(visu.bars, visu.max, meters.levels);
- visu_draw();
- }
-
-
- if (led_visu.mode) {
-
-
- }
- }
- static void spectrum_limits(int min, int n, int pos) {
- if (n / 2) {
- int step = ((DISPLAY_BW - min) * visu.spectrum_scale) / (n/2);
- visu.bars[pos].limit = min + step;
- for (int 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 - n/2, pos + n/2);
- } else {
- visu.bars[pos].limit = DISPLAY_BW;
- }
- }
- static void visu_fit(int bars, int width, int height) {
-
- if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
- visu.n = bars ? bars : MAX_BARS;
- visu.max = height - 1;
- 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;
- visu.max = (visu.style ? VU_COUNT : height) - 1;
- }
-
- do {
- visu.bar_width = (width - visu.border - visu.bar_gap * (visu.n - 1)) / visu.n;
- if (visu.bar_width > 0) break;
- } while (--visu.n);
-
- visu.bar_border = (width - visu.border - (visu.bar_width + visu.bar_gap) * visu.n + visu.bar_gap) / 2;
- }
- 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);
-
-
-
-
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
- visu.mode = pkt->which;
-
-
- if (visu.row >= displayer.height) GDS_ClearExt(display, false, true, visu.col, visu.row, visu.col + visu.width - 1, visu.row + visu.height - 1);
- if (visu.mode) {
-
- visu.col = visu.border = 0;
- visu.rotate = false;
-
-
- if (visu.mode & VISU_ESP32) {
- if (pkt->count >= 4) {
-
- pkt->height = htonl(pkt->height);
- pkt->row = htonl(pkt->row);
- pkt->col = htonl(pkt->col);
- visu.style = 0;
- visu.width = htonl(pkt->width);
- visu.height = pkt->height ? pkt->height : displayer.height;
- visu.col = pkt->col < 0 ? displayer.width + pkt->col : pkt->col;
- visu.row = pkt->row < 0 ? GDS_GetHeight(display) + pkt->row : pkt->row;
- visu.border = htonl(pkt->border);
- bars = htonl(pkt->bars);
- visu.spectrum_scale = htonl(pkt->spectrum_scale) / 100.;
- } else {
-
- visu.width = htonl(pkt->full.width);
- visu.height = GDS_GetHeight(display);
-
-
- if (GDS_GetHeight(display) > displayer.height) {
-
- visu.height -= displayer.height;
- visu.row = displayer.height;
- if (artwork.enable && artwork.y) {
-
- if (visu.width != artwork.x) {
- visu.height = artwork.y - displayer.height;
- if (visu.height <= 0) {
- visu.height = displayer.height;
- LOG_WARN("No room left for visualizer, disable it or increase artwork offset %d", artwork.y);
- }
- } else visu.rotate = true;
- }
- } else visu.row = 0;
-
-
- if ((visu.mode & ~VISU_ESP32) == VISU_SPECTRUM) {
- bars = htonl(pkt->full.bars);
- visu.spectrum_scale = htonl(pkt->full.spectrum_scale) / 100.;
- } else {
-
- visu.style = htonl(pkt->full.style);
- }
- }
- } else {
-
- visu.row = 0;
- visu.height = GDS_GetHeight(display);
- visu.width = displayer.width;
- visu.spectrum_scale = 0.25;
- if (visu.mode == VISU_SPECTRUM) {
- bars = visu.width / (htonl(pkt->channels[0].bar_width) + htonl(pkt->channels[0].bar_space));
- } else {
- visu.style = htonl(pkt->classical_vu.style);
- if (visu.style) visu.row = visu.height - VU_HEIGHT;
- }
- }
-
- if (bars > MAX_BARS) bars = MAX_BARS;
-
-
- if (visu.rotate) visu_fit(bars, visu.height, visu.width);
- else visu_fit(bars, visu.width, visu.height);
-
- if (visu.bar_width < 0) {
- visu.mode = VISU_BLANK;
- LOG_WARN("Not enough room for displaying visu");
- } else {
-
- if (visu.row < displayer.height) scroller.active = false;
- vTaskResume(displayer.task);
- }
- displayer.wake = 0;
-
-
- for (int i = visu.n; --i >= 0;) visu.bars[i].max = 0;
-
- GDS_ClearExt(display, 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");
- }
-
- xSemaphoreGive(displayer.mutex);
- }
- static void dmxt_handler( u8_t *data, int len) {
- struct dmxt_packet *pkt = (struct dmxt_packet*) data;
- uint16_t offset = htons(pkt->x);
- uint16_t length = htons(pkt->length);
- LOG_INFO("dmx packet len:%u offset:%u", length, offset);
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
-
- xSemaphoreGive(displayer.mutex);
- }
- static void displayer_task(void *args) {
- int sleep;
- while (1) {
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
-
-
- if (!scroller.active && !visu.mode && !led_visu.mode) {
- xSemaphoreGive(displayer.mutex);
- vTaskSuspend(NULL);
- xSemaphoreTake(displayer.mutex, portMAX_DELAY);
- scroller.wake = displayer.wake = 0;
- }
-
- if (!visu.mode && !led_visu.mode) displayer.wake = LONG_WAKE;
- if (!scroller.active) scroller.wake = LONG_WAKE;
-
-
- if (scroller.active && scroller.wake <= 0) {
-
- scroller.wake = LONG_WAKE;
-
-
- 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) GDS_DrawBitmapCBR(display, scroller.frame, scroller.width, displayer.height, GDS_COLOR_WHITE);
-
-
- scroller.wake = scroller.speed;
- } else if (scroller.first || !scroller.mode) {
-
- scroller.first = false;
-
-
- if (scroller.mode) {
- sendANIC(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);
- }
-
-
- scroller.scrolled = scroller.by < 0 ? scroller.scroll.width : 0;
- }
- }
-
- if ((visu.mode || led_visu.mode) && displayer.wake <= 0 && displayer.owned) {
- displayer_update();
- displayer.wake = 100;
- }
-
-
- if (display && displayer.owned) GDS_Update(display);
- else if (!led_display) displayer.wake = LONG_WAKE;
-
-
- xSemaphoreGive(displayer.mutex);
-
- sleep = min(displayer.wake, scroller.wake);
- vTaskDelay(sleep / portTICK_PERIOD_MS);
- scroller.wake -= sleep;
- displayer.wake -= sleep;
- }
- }
|