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"
- // commands (see datasheet)
- #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) {
- // fill entire buffer with the same byte
- for (int i = 0; i < OLED_BUF_LEN; i++) {
- buf[i] = fill;
- }
- };
- void fill_page(uint8_t *buf, uint8_t fill, uint8_t page) {
- // fill entire page with the same byte
- memset(buf + (page * OLED_WIDTH), fill, OLED_WIDTH);
- };
- // convenience methods for printing out a buffer to be rendered
- // mostly useful for debugging images, patterns, etc
- void print_buf_page(uint8_t buf[], uint8_t page) {
- // prints one page of a full length (128x4) buffer
- 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[]) {
- // prints all pages of a full length buffer
- 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) {
- // print a render area of generic size
- int area_width = area->end_col - area->start_col + 1;
- int area_height = area->end_page - area->start_page + 1; // in pages, not pixels
- 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) {
- // calculate how long the flattened buffer will be for a render 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) {
- // I2C write process expects a control byte followed by data
- // this "data" can be a command or data to follow up a command
- // Co = 1, D/C = 0 => the driver expects a command
- 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) {
- // in horizontal addressing mode, the column address pointer auto-increments
- // and then wraps around to the next page, so we can send the entire frame
- // buffer in one gooooooo!
- // copy our frame buffer into a new buffer because we need to add the control byte
- // to the beginning
- // TODO find a more memory-efficient way to do this..
- // maybe break the data transfer into pages?
- uint8_t *temp_buf = (uint8_t *)malloc(buflen + 1);
- for (int i = 1; i < buflen + 1; i++) {
- temp_buf[i] = buf[i - 1];
- }
- // Co = 0, D/C = 1 => the driver expects data to be written to RAM
- 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) {
- // some of these commands are not strictly necessary as the reset
- // process defaults to some of these but they are shown here
- // to demonstrate what the initialization sequence looks like
- // some configuration values are recommended by the board manufacturer
- oled_send_cmd(OLED_SET_DISP | 0x00); // set display off
- /* memory mapping */
- oled_send_cmd(OLED_SET_MEM_ADDR); // set memory address mode
- oled_send_cmd(0x00); // horizontal addressing mode
- /* resolution and layout */
- oled_send_cmd(OLED_SET_DISP_START_LINE); // set display start line to 0
- oled_send_cmd(OLED_SET_SEG_REMAP | 0x01); // set segment re-map
- // column address 127 is mapped to SEG0
- oled_send_cmd(OLED_SET_MUX_RATIO); // set multiplex ratio
- oled_send_cmd(OLED_HEIGHT - 1); // our display is only 32 pixels high
- oled_send_cmd(OLED_SET_COM_OUT_DIR | 0x08); // set COM (common) output scan direction
- // scan from bottom up, COM[N-1] to COM0
- oled_send_cmd(OLED_SET_DISP_OFFSET); // set display offset
- oled_send_cmd(0x00); // no offset
- oled_send_cmd(OLED_SET_COM_PIN_CFG); // set COM (common) pins hardware configuration
- oled_send_cmd(0x02); // manufacturer magic number
- /* timing and driving scheme */
- oled_send_cmd(OLED_SET_DISP_CLK_DIV); // set display clock divide ratio
- oled_send_cmd(0x80); // div ratio of 1, standard freq
- oled_send_cmd(OLED_SET_PRECHARGE); // set pre-charge period
- oled_send_cmd(0xF1); // Vcc internally generated on our board
- oled_send_cmd(OLED_SET_VCOM_DESEL); // set VCOMH deselect level
- oled_send_cmd(0x30); // 0.83xVcc
- /* display */
- oled_send_cmd(OLED_SET_CONTRAST); // set contrast control
- oled_send_cmd(0xFF);
- oled_send_cmd(OLED_SET_ENTIRE_ON); // set entire display on to follow RAM content
- oled_send_cmd(OLED_SET_NORM_INV); // set normal (not inverted) display
- oled_send_cmd(OLED_SET_CHARGE_PUMP); // set charge pump
- oled_send_cmd(0x14); // Vcc internally generated on our board
- oled_send_cmd(OLED_SET_SCROLL | 0x00); // deactivate horizontal scrolling if set
- // this is necessary as memory writes will corrupt if scrolling was enabled
- oled_send_cmd(OLED_SET_DISP | 0x01); // turn display on
- }
- void render(uint8_t *buf, struct render_area *area) {
- // update a portion of the display with a render 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
|