123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include <string.h>
- #include <ctype.h>
- #include <arpa/inet.h>
- #include "esp_log.h"
- #include "config.h"
- #include "ssd1306.h"
- #include "ssd1306_draw.h"
- #include "ssd1306_font.h"
- #include "ssd1306_default_if.h"
- #define LINELEN 40
- #define I2C_PORT 1
- #define I2C_ADDRESS 0x3C
- #define TAG "display"
- static struct SSD1306_Device I2CDisplay;
- static bool init, display;
- void display_init(void) {
- char *item = config_alloc_get(NVS_TYPE_STR, "display_config");
- if (!item || !*item) {
- ESP_LOGI(TAG, "no display");
- return;
- }
- if (strstr(item, "I2C")) {
- int scl = -1, sda = -1;
- int width = -1, height = -1;
- char *p;
-
- init = true;
-
- if ((p = strcasestr(item, "scl")) != NULL) scl = atoi(strchr(p, '=') + 1);
- if ((p = strcasestr(item, "sda")) != NULL) sda = atoi(strchr(p, '=') + 1);
- if ((p = strcasestr(item, "width")) != NULL) width = atoi(strchr(p, '=') + 1);
- if ((p = strcasestr(item, "height")) != NULL) height = atoi(strchr(p, '=') + 1);
- if (sda != -1 && scl != -1 && width != -1 && height != -1) {
- SSD1306_I2CMasterInitDefault( I2C_PORT, sda, scl );
- SSD1306_I2CMasterAttachDisplayDefault( &I2CDisplay, width, height, I2C_ADDRESS, -1);
- SSD1306_SetFont( &I2CDisplay, &Font_droid_sans_fallback_15x17 );
- ESP_LOGI(TAG, "initialized I2C display %dx%d (sda:%d, scl:%d)", width, height, sda, scl);
- display = true;
- } else {
- ESP_LOGI(TAG, "cannot initialized I2C display %s [%dx%d sda:%d, scl:%d]", item, width, height, sda, scl);
- }
- } else {
-
- }
- }
- unsigned char printable(unsigned char c) {
- switch (c) {
- case 11:
- return '#';
- break;;
- case 16:
- return '>';
- break;;
- case 22:
- return '@';
- break;;
- case 145:
- return ' ';
- break;;
- case 152:
- return 'o';
- break;
- default:
- return c;
- }
- }
- void makeprintable(unsigned char * line) {
- for (int n = 0; n < LINELEN; n++) line[n] = printable(line[n]);
- }
- void show_display_buffer(char *ddram) {
- char line1[LINELEN+1];
- char *line2;
- memset(line1, 0, LINELEN+1);
- strncpy(line1, ddram, LINELEN);
- line2 = &(ddram[LINELEN]);
- line2[LINELEN] = '\0';
-
- makeprintable((unsigned char *)line1);
- makeprintable((unsigned char *)line2);
- ESP_LOGI(TAG, "\n\t%.40s\n\t%.40s", line1, line2);
- if (display) {
- SSD1306_Clear( &I2CDisplay, SSD_COLOR_BLACK );
- SSD1306_FontDrawAnchoredString( &I2CDisplay, TextAnchor_NorthWest, line1, SSD_COLOR_WHITE );
- SSD1306_FontDrawAnchoredString( &I2CDisplay, TextAnchor_SouthWest, line2, SSD_COLOR_WHITE );
- SSD1306_Update( &I2CDisplay );
- }
- }
- bool charisok(unsigned char c) {
- switch (c) {
- case 11:
- case 16:
- case 22:
- case 145:
- case 152:
- return true;
- break;;
- default:
- return isprint(c);
- }
- }
- void vfd_data( unsigned short *data, int bytes_read) {
- unsigned short *display_data;
- char ddram[LINELEN * 2];
- int n;
- int addr = 0;
- if (!init) display_init();
- 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);
- }
|