|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <math.h> |
| 4 | + |
| 5 | +#include "pico/stdlib.h" |
| 6 | +#include "hardware/clocks.h" |
| 7 | +#include "hardware/i2c.h" |
| 8 | + |
| 9 | +#include "u8g2.h" |
| 10 | + |
| 11 | +#define OLED_I2C_SDA_PIN (18) |
| 12 | +#define OLED_I2C_SCL_PIN (19) |
| 13 | +#define OLED_I2C_SPEED (100UL) |
| 14 | +#define OLED_I2C_INST (i2c1) |
| 15 | + |
| 16 | +static u8g2_t u8g2; |
| 17 | + |
| 18 | +static uint8_t u8x8_gpio_and_delay_pico(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) |
| 19 | +{ |
| 20 | + switch (msg) |
| 21 | + { |
| 22 | + case U8X8_MSG_GPIO_AND_DELAY_INIT: |
| 23 | + break; |
| 24 | + |
| 25 | + case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second |
| 26 | + break; |
| 27 | + case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds |
| 28 | + break; |
| 29 | + case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds |
| 30 | + break; |
| 31 | + case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second |
| 32 | + sleep_ms(arg_int); |
| 33 | + break; |
| 34 | + case U8X8_MSG_DELAY_I2C: |
| 35 | + /* arg_int is 1 or 4: 100KHz (5us) or 400KHz (1.25us) */ |
| 36 | + sleep_us(arg_int <= 2 ? 5 : 1); |
| 37 | + break; |
| 38 | + |
| 39 | + default: |
| 40 | + u8x8_SetGPIOResult(u8x8, 1); // default return value |
| 41 | + break; |
| 42 | + } |
| 43 | + return 1; |
| 44 | +} |
| 45 | + |
| 46 | +static uint8_t u8x8_byte_pico_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) |
| 47 | +{ |
| 48 | + uint8_t *data; |
| 49 | + static uint8_t buffer[132]; |
| 50 | + static uint8_t buf_idx; |
| 51 | + |
| 52 | + switch (msg) |
| 53 | + { |
| 54 | + case U8X8_MSG_BYTE_SEND: |
| 55 | + data = (uint8_t *)arg_ptr; |
| 56 | + while (arg_int > 0) |
| 57 | + { |
| 58 | + assert(buf_idx < 132); |
| 59 | + buffer[buf_idx++] = *data; |
| 60 | + data++; |
| 61 | + arg_int--; |
| 62 | + } |
| 63 | + break; |
| 64 | + |
| 65 | + case U8X8_MSG_BYTE_INIT: |
| 66 | + i2c_init(OLED_I2C_INST, OLED_I2C_SPEED * 1000); |
| 67 | + gpio_set_function(OLED_I2C_SDA_PIN, GPIO_FUNC_I2C); |
| 68 | + gpio_set_function(OLED_I2C_SCL_PIN, GPIO_FUNC_I2C); |
| 69 | + gpio_pull_up(OLED_I2C_SDA_PIN); |
| 70 | + gpio_pull_up(OLED_I2C_SCL_PIN); |
| 71 | + break; |
| 72 | + |
| 73 | + case U8X8_MSG_BYTE_SET_DC: |
| 74 | + break; |
| 75 | + |
| 76 | + case U8X8_MSG_BYTE_START_TRANSFER: |
| 77 | + buf_idx = 0; |
| 78 | + break; |
| 79 | + |
| 80 | + case U8X8_MSG_BYTE_END_TRANSFER: |
| 81 | + { |
| 82 | + uint8_t addr = u8x8_GetI2CAddress(u8x8) >> 1; |
| 83 | + int ret = i2c_write_blocking(OLED_I2C_INST, addr, buffer, buf_idx, false); |
| 84 | + printf("%d\n", ret); |
| 85 | + if ((ret == PICO_ERROR_GENERIC) || (ret == PICO_ERROR_TIMEOUT)) |
| 86 | + { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + } |
| 90 | + break; |
| 91 | + |
| 92 | + default: |
| 93 | + return 0; |
| 94 | + } |
| 95 | + return 1; |
| 96 | +} |
| 97 | + |
| 98 | +int main() |
| 99 | +{ |
| 100 | + stdio_init_all(); |
| 101 | + // set_sys_clock_khz(280000, true); |
| 102 | + stdio_uart_init_full(uart0, 921600, 0, 1); |
| 103 | + |
| 104 | + printf("Starting\n"); |
| 105 | + |
| 106 | + const uint LED_PIN = PICO_DEFAULT_LED_PIN; |
| 107 | + gpio_init(LED_PIN); |
| 108 | + gpio_set_dir(LED_PIN, GPIO_OUT); |
| 109 | + |
| 110 | + u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, |
| 111 | + u8x8_byte_pico_hw_i2c, |
| 112 | + u8x8_gpio_and_delay_pico); |
| 113 | + |
| 114 | + u8g2_SetI2CAddress(&u8g2, 0x78); |
| 115 | + u8g2_InitDisplay(&u8g2); |
| 116 | + u8g2_SetPowerSave(&u8g2, 0); |
| 117 | + u8g2_SetContrast(&u8g2, 255); |
| 118 | + u8g2_ClearDisplay(&u8g2); |
| 119 | + |
| 120 | + u8g2_DrawCircle(&u8g2, 64, 32, 10, U8G2_DRAW_ALL); |
| 121 | + u8g2_SendBuffer(&u8g2); |
| 122 | + |
| 123 | + while (true) |
| 124 | + { |
| 125 | + gpio_put(LED_PIN, 0); |
| 126 | + sleep_ms(100); |
| 127 | + gpio_put(LED_PIN, 1); |
| 128 | + sleep_ms(100); |
| 129 | + } |
| 130 | +} |
0 commit comments