Plant Monitor SAM3X8E
Loading...
Searching...
No Matches
st7789.c File Reference

Interface for ST7789 driver. More...

#include "drivers/st7789.h"

Go to the source code of this file.

Functions

void st7789_init (void)
 ST7789 initialization.
void st7789_clear (const uint16_t color)
 Clears display.
void st7789_draw_glyph (const uint16_t xs, const uint16_t ys, const uint16_t glyph_width, const uint16_t glyph_height, const uint16_t fg_color, const uint16_t bg_color, const uint8_t *bitmap_data)
 Draws a character glyph to the display.

Detailed Description

Interface for ST7789 driver.

Contains implementations of functions for initializing the display, clearing, and rendering glyphs.

Author
Vladimir Zhelev

Definition in file st7789.c.

Function Documentation

◆ st7789_clear()

void st7789_clear ( const uint16_t color)

Clears display.

Fills display with a single color, which can be thought of as "clearing" the scren.

Parameters
colorColor to fill display with upon clear.

Definition at line 190 of file st7789.c.

191{
192 st7789_set_window(0, DISPLAY_WIDTH - 1, 0, DISPLAY_HEIGHT - 1);
193
194 uint8_t pixel_buffer[DISPLAY_WIDTH * 2u];
195
196 for (size_t i = 0; i < sizeof(pixel_buffer); i += 2 )
197 {
198 pixel_buffer[i] = color >> 8u;
199 pixel_buffer[i+1] = color & 0xFFu;
200 }
201
202 for (size_t j = 0; j < DISPLAY_HEIGHT; j++)
203 st7789_write_data(pixel_buffer, sizeof(pixel_buffer));
204}
#define DISPLAY_HEIGHT
Definition st7789.h:26
#define DISPLAY_WIDTH
Definition st7789.h:23

◆ st7789_draw_glyph()

void st7789_draw_glyph ( const uint16_t xs,
const uint16_t ys,
const uint16_t glyph_width,
const uint16_t glyph_height,
const uint16_t fg_color,
const uint16_t bg_color,
const uint8_t * bitmap_data )

Draws a character glyph to the display.

Takes coordinates, glyph dimensions, and bitmap data to display a character glyph on the screen.

Parameters
xsColumn to start drawing glyph.
ysRow to start drawing glyph.
glyph_widthWidth of passed glyph.
glyph_heightHeight of passed glyph.
fg_colorColor
bg_color
bitmap_dataByte array containing glyph data. Each row is a single byte.

Definition at line 206 of file st7789.c.

207{
208 st7789_set_window(xs, (xs + glyph_width) - 1, ys, (ys + glyph_height) - 1);
209
210 uint8_t glyph_buffer[(glyph_width * glyph_height) * 2u];
211
212 for (size_t i = 0; i < glyph_height; i++)
213 {
214 for (size_t j = 0; j < glyph_width; j++)
215 {
216 uint16_t color = (bitmap_data[i] & (1u << (7u - j))) ? fg_color : bg_color;
217 size_t pixel_index = ((i * glyph_width) + j) * 2u;
218
219 glyph_buffer[pixel_index] = color >> 8u;
220 glyph_buffer[pixel_index + 1] = color & 0xFF;
221 }
222 }
223
224 st7789_write_data(glyph_buffer, sizeof(glyph_buffer));
225}

◆ st7789_init()

void st7789_init ( void )

ST7789 initialization.

Configures and initializes ST7789 for drawing.

Definition at line 108 of file st7789.c.

109{
110 /* Initialize timer counter */
111 tc_init();
112
113 /* Disable watchdog timer */
114 wdt_disable();
115
116 /* Initialize SPI */
117 spi_init(&st7789_cfg);
118
119 /* Initialize GPIO */
123
124 /* Perform HW reset */
125 st7789_reset();
126
127 /* Perform SW reset */
128 st7789_write_cmd(SWRESET);
129 tc_delay_ms(5);
130
131 /* Configure row/column addressing order */
132 st7789_write_cmd(MADCTL);
133 st7789_write_data(madctl_init, sizeof(madctl_init));
134
135 st7789_write_cmd(COLMOD);
136 st7789_write_data(colmod_init, sizeof(colmod_init));
137
138 /* Turn on inversion */
139 st7789_write_cmd(INVON);
140
141 /* Column & row address windows */
142 st7789_write_cmd(CASET);
143 st7789_write_data(caset_init, sizeof(caset_init));
144 st7789_write_cmd(RASET);
145 st7789_write_data(raset_init, sizeof(raset_init));
146
147 /* Porch settings */
148 st7789_write_cmd(PORCTRL);
149 st7789_write_data(porctrl_init, sizeof(porctrl_init));
150
151 /* Gate control */
152 st7789_write_cmd(GCTRL);
153 st7789_write_data(gctrl_init, sizeof(gctrl_init));
154
155 /* Common electrode voltage across LCD panel */
156 st7789_write_cmd(VCOMS);
157 st7789_write_data(vcoms_init, sizeof(vcoms_init));
158
159 st7789_write_cmd(LCMCTRL);
160 st7789_write_data(lcmctrl_init, sizeof(lcmctrl_init));
161
162 st7789_write_cmd(VDVVRHEN);
163 st7789_write_data(vdvvrhen_init, sizeof(vdvvrhen_init));
164
165 st7789_write_cmd(VRHS);
166 st7789_write_data(vrhs_init, sizeof(vrhs_init));
167
168 st7789_write_cmd(VDVSET);
169 st7789_write_data(vdvset_init, sizeof(vdvset_init));
170
171 st7789_write_cmd(FRCTRL2);
172 st7789_write_data(frctrl2_init, sizeof(frctrl2_init));
173
174 st7789_write_cmd(PWCTRL1);
175 st7789_write_data(pwctrl1_init, sizeof(pwctrl1_init));
176
177 st7789_write_cmd(PVGAMCTRL);
178 st7789_write_data(pvgamctrl_init, sizeof(pvgamctrl_init));
179
180 st7789_write_cmd(NVGAMCTRL);
181 st7789_write_data(nvgamctrl_init, sizeof(nvgamctrl_init));
182
183 st7789_write_cmd(SLPOUT);
184 tc_delay_ms(120);
185
186 st7789_write_cmd(DISPON);
187 tc_delay_ms(20);
188}
void pio_init(const pio_id_t pio_id, const uint32_t pin)
Initializes GPIO pin.
Definition pio.c:12
@ PIO_B
Definition pio.h:24
void spi_init(const spi_cfg_t *cfg)
Initializes SPI0 peripheral of SAM3X8E.
Definition spi.c:13
#define VDVSET
Definition st7789.h:133
#define RST_PIN
Definition st7789.h:31
#define PORCTRL
Definition st7789.h:124
#define COLMOD
Definition st7789.h:105
#define DC_PIN
Definition st7789.h:30
#define PVGAMCTRL
Definition st7789.h:143
#define FRCTRL2
Definition st7789.h:135
#define PWCTRL1
Definition st7789.h:140
#define DISPON
Definition st7789.h:92
#define SLPOUT
Definition st7789.h:85
#define VCOMS
Definition st7789.h:128
#define MADCTL
Definition st7789.h:101
#define LCMCTRL
Definition st7789.h:129
#define VRHS
Definition st7789.h:132
#define CASET
Definition st7789.h:93
#define NVGAMCTRL
Definition st7789.h:144
#define INVON
Definition st7789.h:89
#define CS_PIN
Definition st7789.h:29
#define RASET
Definition st7789.h:94
#define VDVVRHEN
Definition st7789.h:131
#define GCTRL
Definition st7789.h:126
#define SWRESET
Definition st7789.h:75
void tc_delay_ms(uint32_t ms)
Creates a hardware delay in milliseconds.
Definition tc.c:32
void tc_init(void)
Initializes TC0->Channel0.
Definition tc.c:12
void wdt_disable(void)
Disables SAM3X8E watchdog timer.
Definition tc.c:41