board/main/embedded_display.c

145 lines
4.3 KiB
C

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "codetab.h"
#include "config_key.h"
#include "driver/i2c.h"
#include "esp_log.h"
#include "i2c.c"
#define Brightness 0xCF
#define X_WIDTH 128
#define Y_WIDTH 64
void i2cWriteByte(uint8_t reg, uint8_t data) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, SSD1306_ADDRESS << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, reg, true);
i2c_master_write_byte(cmd, data, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
void i2cWriteCommand(uint8_t data) { i2cWriteByte(0x00, data); }
void i2cWriteData(uint8_t data) { i2cWriteByte(0x40, data); }
void display_fill(uint8_t bmpData) {
for (int y = 0; y < 8; y++) {
i2cWriteCommand(0xB0 + y);
i2cWriteCommand(0x01);
i2cWriteCommand(0x10);
for (int x = 0; x < X_WIDTH; x++) {
i2cWriteData(bmpData);
}
}
}
void display_fill_rect(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
uint8_t bmpData) {
for (int y = y0; y < y1; y++) {
i2cWriteCommand(0xB0 + y);
i2cWriteCommand(0x01);
i2cWriteCommand(0x10);
for (int x = x0; x < x1; x++) {
i2cWriteData(bmpData);
}
}
}
void display_set_pos(uint8_t x, uint8_t y) {
i2cWriteCommand(0xB0 + y);
i2cWriteCommand(((x & 0xF0) >> 4) | 0x10);
i2cWriteCommand((x & 0x0F) | 0x01);
}
void display_print8_str(uint8_t x, uint8_t y, char str[]) {
if (!is_embedded_display_online) {
return;
}
uint8_t c;
for (uint8_t ch, ci = 0; ch = str[ci], ch != '\0'; ci++, x += 8) {
c = ch - 0x20;
display_set_pos(x, y);
for (uint8_t cx = 0; cx < 8; cx++) {
i2cWriteData(F8X16[c * 16 + cx]);
}
display_set_pos(x, y + 1);
for (uint8_t cx = 0; cx < 8; cx++) {
i2cWriteData(F8X16[c * 16 + cx + 8]);
};
}
}
void init_display() {
if (is_embedded_display_online == 0) {
ESP_LOGE("display", "display is offline");
return;
}
i2cWriteCommand(0xAE); // display off
i2cWriteCommand(0x00); // set lower column address
i2cWriteCommand(0x10); // set higher column address
i2cWriteCommand(0x40); // set start line address
i2cWriteCommand(0x81); // set contrast control register
i2cWriteCommand(0xCF); // set contrast
i2cWriteCommand(0xA1); // set segment re-map
i2cWriteCommand(0xC8); // set COM output scan direction
i2cWriteCommand(0xA6); // set normal display
i2cWriteCommand(0xA8); // set multiplex ratio(1 to 64)
i2cWriteCommand(0x3F); // 1/64 duty
i2cWriteCommand(0xD3); // set display offset
i2cWriteCommand(0x00); // not offset
i2cWriteCommand(0xD5); // set display clock divide ratio/oscillator frequency
i2cWriteCommand(0x80); // set divide ratio
i2cWriteCommand(0xD9); // set pre-charge period
i2cWriteCommand(0xF1); // set pre-charge voltage
i2cWriteCommand(0xDA); // set com pins hardware configuration
i2cWriteCommand(0x12);
i2cWriteCommand(0xDB); // set vcomh
i2cWriteCommand(0x40); // 0.77*vcc
i2cWriteCommand(0x20); // set memory addressing mode
i2cWriteCommand(0x02); // set page addressing mode
i2cWriteCommand(0x8D); // set charge pump enable/disable
i2cWriteCommand(0x14); // set(0x10) disable
i2cWriteCommand(0xAF); // display on
display_fill(0x00);
display_set_pos(0, 0);
}
void gui_update_config_uint8(uint8_t key, uint8_t value) {
char changing_str[12] = "NC";
switch (key) {
case ui_input_key_display_0_brightness:
sprintf(changing_str, "Dis0: % 3d", value);
break;
case ui_input_key_display_1_brightness:
sprintf(changing_str, "Dis1: % 3d", value);
break;
case ui_input_key_computer_volume:
sprintf(changing_str, "CVol: % 3d", value);
break;
case ui_input_key_display_ambient_lighting_level:
sprintf(changing_str, "ALLv: % 3d", value);
break;
case ui_input_key_display_ambient_lighting_mode:
sprintf(changing_str, "ALMd: % 3d", value);
break;
case ui_input_key_display_0_mode:
sprintf(changing_str, "Dis0M: % 2d", value);
break;
case ui_input_key_display_1_mode:
sprintf(changing_str, "Dis1M: % 2d", value);
break;
default:
strcpy(changing_str, "NC");
break;
}
display_fill_rect(0, 6, 128, 8, 0);
display_print8_str(8, 6, changing_str);
}