Skip to content

Commit 9922d66

Browse files
committed
Added another test app
1 parent 5985b0e commit 9922d66

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ file(GLOB U8G2_SRCS
1515
"external/u8g2/csrc/*.c"
1616
)
1717

18-
add_compile_options(-Wall -Werror -fdata-sections -ffunction-sections)
18+
add_compile_options(-Wall -fdata-sections -ffunction-sections)
1919
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
2020
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)
2121

@@ -267,3 +267,20 @@ target_link_libraries(rpi_lcd_3d_skull_test PRIVATE
267267
tinygl_1b)
268268

269269
pico_add_extra_outputs(rpi_lcd_3d_skull_test)
270+
271+
add_executable(rpi_oled_test
272+
src/rpi_oled_test.c
273+
)
274+
275+
target_link_libraries(rpi_oled_test
276+
pico_stdlib
277+
hardware_irq
278+
hardware_pwm
279+
hardware_pio
280+
hardware_dma
281+
hardware_i2c
282+
u8g2)
283+
284+
pico_add_extra_outputs(rpi_oled_test)
285+
# pico_enable_stdio_usb(rpi_oled_test 1)
286+
# pico_enable_stdio_uart(rpi_oled_test 0)

src/rpi_oled_test.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)