123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "pico/stdlib.h"
- #include "pico/binary_info.h"
- #include "hardware/i2c.h"
- #include "sd1306.h"
- #define OLED_SET_CONTRAST _u(0x81)
- #define OLED_SET_ENTIRE_ON _u(0xA4)
- #define OLED_SET_NORM_INV _u(0xA6)
- #define OLED_SET_DISP _u(0xAE)
- #define OLED_SET_MEM_ADDR _u(0x20)
- #define OLED_SET_COL_ADDR _u(0x21)
- #define OLED_SET_PAGE_ADDR _u(0x22)
- #define OLED_SET_DISP_START_LINE _u(0x40)
- #define OLED_SET_SEG_REMAP _u(0xA0)
- #define OLED_SET_MUX_RATIO _u(0xA8)
- #define OLED_SET_COM_OUT_DIR _u(0xC0)
- #define OLED_SET_DISP_OFFSET _u(0xD3)
- #define OLED_SET_COM_PIN_CFG _u(0xDA)
- #define OLED_SET_DISP_CLK_DIV _u(0xD5)
- #define OLED_SET_PRECHARGE _u(0xD9)
- #define OLED_SET_VCOM_DESEL _u(0xDB)
- #define OLED_SET_CHARGE_PUMP _u(0x8D)
- #define OLED_SET_HORIZ_SCROLL _u(0x26)
- #define OLED_SET_SCROLL _u(0x2E)
- #define OLED_ADDR _u(0x3C)
- #define OLED_WRITE_MODE _u(0xFE)
- #define OLED_READ_MODE _u(0xFF)
- void fill(uint8_t buf[], uint8_t fill) {
-
- for (int i = 0; i < OLED_BUF_LEN; i++) {
- buf[i] = fill;
- }
- };
- void fill_page(uint8_t *buf, uint8_t fill, uint8_t page) {
-
- memset(buf + (page * OLED_WIDTH), fill, OLED_WIDTH);
- };
- void print_buf_page(uint8_t buf[], uint8_t page) {
-
- for (int j = 0; j < OLED_PAGE_HEIGHT; j++) {
- for (int k = 0; k < OLED_WIDTH; k++) {
- printf("%u", (buf[page * OLED_WIDTH + k] >> j) & 0x01);
- }
- printf("\n");
- }
- }
- void print_buf_pages(uint8_t buf[]) {
-
- for (int i = 0; i < OLED_NUM_PAGES; i++) {
- printf("--page %d--\n", i);
- print_buf_page(buf, i);
- }
- }
- void print_buf_area(uint8_t *buf, struct render_area *area) {
-
- int area_width = area->end_col - area->start_col + 1;
- int area_height = area->end_page - area->start_page + 1;
- for (int i = 0; i < area_height; i++) {
- for (int j = 0; j < OLED_PAGE_HEIGHT; j++) {
- for (int k = 0; k < area_width; k++) {
- printf("%u", (buf[i * area_width + k] >> j) & 0x01);
- }
- printf("\n");
- }
- }
- }
- void calc_render_area_buflen(struct render_area *area) {
-
- area->buflen = (area->end_col - area->start_col + 1) * (area->end_page - area->start_page + 1);
- }
- #ifdef i2c_default
- void oled_send_cmd(uint8_t cmd) {
-
-
-
- uint8_t buf[2] = {0x80, cmd};
- i2c_write_blocking(i2c_default, (OLED_ADDR & OLED_WRITE_MODE), buf, 2, false);
- }
- void oled_send_buf(uint8_t buf[], int buflen) {
-
-
-
-
-
-
-
- uint8_t *temp_buf = (uint8_t *)malloc(buflen + 1);
- for (int i = 1; i < buflen + 1; i++) {
- temp_buf[i] = buf[i - 1];
- }
-
- temp_buf[0] = 0x40;
- i2c_write_blocking(i2c_default, (OLED_ADDR & OLED_WRITE_MODE), temp_buf, buflen + 1, false);
- free(temp_buf);
- }
- void oled_init(void) {
-
-
-
-
- oled_send_cmd(OLED_SET_DISP | 0x00);
-
- oled_send_cmd(OLED_SET_MEM_ADDR);
- oled_send_cmd(0x00);
-
- oled_send_cmd(OLED_SET_DISP_START_LINE);
- oled_send_cmd(OLED_SET_SEG_REMAP | 0x01);
-
- oled_send_cmd(OLED_SET_MUX_RATIO);
- oled_send_cmd(OLED_HEIGHT - 1);
- oled_send_cmd(OLED_SET_COM_OUT_DIR | 0x08);
-
- oled_send_cmd(OLED_SET_DISP_OFFSET);
- oled_send_cmd(0x00);
- oled_send_cmd(OLED_SET_COM_PIN_CFG);
- oled_send_cmd(0x02);
-
- oled_send_cmd(OLED_SET_DISP_CLK_DIV);
- oled_send_cmd(0x80);
- oled_send_cmd(OLED_SET_PRECHARGE);
- oled_send_cmd(0xF1);
- oled_send_cmd(OLED_SET_VCOM_DESEL);
- oled_send_cmd(0x30);
-
- oled_send_cmd(OLED_SET_CONTRAST);
- oled_send_cmd(0xFF);
- oled_send_cmd(OLED_SET_ENTIRE_ON);
- oled_send_cmd(OLED_SET_NORM_INV);
- oled_send_cmd(OLED_SET_CHARGE_PUMP);
- oled_send_cmd(0x14);
- oled_send_cmd(OLED_SET_SCROLL | 0x00);
-
- oled_send_cmd(OLED_SET_DISP | 0x01);
- }
- void render(uint8_t *buf, struct render_area *area) {
-
- oled_send_cmd(OLED_SET_COL_ADDR);
- oled_send_cmd(area->start_col);
- oled_send_cmd(area->end_col);
- oled_send_cmd(OLED_SET_PAGE_ADDR);
- oled_send_cmd(area->start_page);
- oled_send_cmd(area->end_page);
- oled_send_buf(buf, area->buflen);
- }
- #endif
|