1 /*
2  * Copyright (c) 2018 PHYTEC Messtechnik GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public Monochrome Character Framebuffer API
10  */
11 
12 #ifndef __CFB_H__
13 #define __CFB_H__
14 
15 #include <zephyr/device.h>
16 #include <zephyr/drivers/display.h>
17 #include <zephyr/sys/iterable_sections.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief Public Monochrome Character Framebuffer API
25  * @defgroup monochrome_character_framebuffer Monochrome Character Framebuffer
26  * @ingroup utilities
27  * @{
28  */
29 
30 enum cfb_display_param {
31 	CFB_DISPLAY_HEIGH		= 0,
32 	CFB_DISPLAY_WIDTH,
33 	CFB_DISPLAY_PPT,
34 	CFB_DISPLAY_ROWS,
35 	CFB_DISPLAY_COLS,
36 };
37 
38 enum cfb_font_caps {
39 	CFB_FONT_MONO_VPACKED		= BIT(0),
40 	CFB_FONT_MONO_HPACKED		= BIT(1),
41 	CFB_FONT_MSB_FIRST		= BIT(2),
42 };
43 
44 struct cfb_font {
45 	const void *data;
46 	enum cfb_font_caps caps;
47 	uint8_t width;
48 	uint8_t height;
49 	uint8_t first_char;
50 	uint8_t last_char;
51 };
52 
53 struct cfb_position {
54 	uint16_t x;
55 	uint16_t y;
56 };
57 
58 /**
59  * @brief Macro for creating a font entry.
60  *
61  * @param _name   Name of the font entry.
62  * @param _width  Width of the font in pixels
63  * @param _height Height of the font in pixels.
64  * @param _caps   Font capabilities.
65  * @param _data   Raw data of the font.
66  * @param _fc     Character mapped to first font element.
67  * @param _lc     Character mapped to last font element.
68  */
69 #define FONT_ENTRY_DEFINE(_name, _width, _height, _caps, _data, _fc, _lc)      \
70 	static const STRUCT_SECTION_ITERABLE(cfb_font, _name) = {	       \
71 		.data = _data,						       \
72 		.caps = _caps,						       \
73 		.width = _width,					       \
74 		.height = _height,					       \
75 		.first_char = _fc,					       \
76 		.last_char = _lc,					       \
77 	}
78 
79 /**
80  * @brief Print a string into the framebuffer.
81  *
82  * @param dev Pointer to device structure for driver instance
83  * @param str String to print
84  * @param x Position in X direction of the beginning of the string
85  * @param y Position in Y direction of the beginning of the string
86  *
87  * @return 0 on success, negative value otherwise
88  */
89 int cfb_print(const struct device *dev, const char *const str, uint16_t x, uint16_t y);
90 
91 /**
92  * @brief Print a string into the framebuffer.
93  * For compare to cfb_print, cfb_draw_text accept non tile-aligned coords
94  * and not line wrapping.
95  *
96  * @param dev Pointer to device structure for driver instance
97  * @param str String to print
98  * @param x Position in X direction of the beginning of the string
99  * @param y Position in Y direction of the beginning of the string
100  *
101  * @return 0 on success, negative value otherwise
102  */
103 int cfb_draw_text(const struct device *dev, const char *const str, int16_t x, int16_t y);
104 
105 /**
106  * @brief Draw a point.
107  *
108  * @param dev Pointer to device structure for driver instance
109  * @param pos position of the point
110  *
111  * @return 0 on success, negative value otherwise
112  */
113 int cfb_draw_point(const struct device *dev, const struct cfb_position *pos);
114 
115 /**
116  * @brief Draw a line.
117  *
118  * @param dev Pointer to device structure for driver instance
119  * @param start start position of the line
120  * @param end end position of the line
121  *
122  * @return 0 on success, negative value otherwise
123  */
124 int cfb_draw_line(const struct device *dev, const struct cfb_position *start,
125 		  const struct cfb_position *end);
126 
127 /**
128  * @brief Draw a rectangle.
129  *
130  * @param dev Pointer to device structure for driver instance
131  * @param start Top-Left position of the rectangle
132  * @param end Bottom-Right position of the rectangle
133  *
134  * @return 0 on success, negative value otherwise
135  */
136 int cfb_draw_rect(const struct device *dev, const struct cfb_position *start,
137 		  const struct cfb_position *end);
138 
139 /**
140  * @brief Clear framebuffer.
141  *
142  * @param dev Pointer to device structure for driver instance
143  * @param clear_display Clear the display as well
144  *
145  * @return 0 on success, negative value otherwise
146  */
147 int cfb_framebuffer_clear(const struct device *dev, bool clear_display);
148 
149 /**
150  * @brief Invert Pixels.
151  *
152  * @param dev Pointer to device structure for driver instance
153  *
154  * @return 0 on success, negative value otherwise
155  */
156 int cfb_framebuffer_invert(const struct device *dev);
157 
158 /**
159  * @brief Invert Pixels in selected area.
160  *
161  * @param dev Pointer to device structure for driver instance
162  * @param x Position in X direction of the beginning of area
163  * @param y Position in Y direction of the beginning of area
164  * @param width Width of area in pixels
165  * @param height Height of area in pixels
166  *
167  * @return 0 on success, negative value otherwise
168  */
169 int cfb_invert_area(const struct device *dev, uint16_t x, uint16_t y,
170 		    uint16_t width, uint16_t height);
171 
172 /**
173  * @brief Finalize framebuffer and write it to display RAM,
174  * invert or reorder pixels if necessary.
175  *
176  * @param dev Pointer to device structure for driver instance
177  *
178  * @return 0 on success, negative value otherwise
179  */
180 int cfb_framebuffer_finalize(const struct device *dev);
181 
182 /**
183  * @brief Get display parameter.
184  *
185  * @param dev Pointer to device structure for driver instance
186  * @param cfb_display_param One of the display parameters
187  *
188  * @return Display parameter value
189  */
190 int cfb_get_display_parameter(const struct device *dev,
191 			      enum cfb_display_param);
192 
193 /**
194  * @brief Set font.
195  *
196  * @param dev Pointer to device structure for driver instance
197  * @param idx Font index
198  *
199  * @return 0 on success, negative value otherwise
200  */
201 int cfb_framebuffer_set_font(const struct device *dev, uint8_t idx);
202 
203 /**
204  * @brief Set font kerning (spacing between individual letters).
205  *
206  * @param dev Pointer to device structure for driver instance
207  * @param kerning Font kerning
208  *
209  * @return 0 on success, negative value otherwise
210  */
211 int cfb_set_kerning(const struct device *dev, int8_t kerning);
212 
213 /**
214  * @brief Get font size.
215  *
216  * @param dev Pointer to device structure for driver instance
217  * @param idx Font index
218  * @param width Pointers to the variable where the font width will be stored.
219  * @param height Pointers to the variable where the font height will be stored.
220  *
221  * @return 0 on success, negative value otherwise
222  */
223 int cfb_get_font_size(const struct device *dev, uint8_t idx, uint8_t *width,
224 		      uint8_t *height);
225 
226 /**
227  * @brief Get number of fonts.
228  *
229  * @param dev Pointer to device structure for driver instance
230  *
231  * @return number of fonts
232  */
233 int cfb_get_numof_fonts(const struct device *dev);
234 
235 /**
236  * @brief Initialize Character Framebuffer.
237  *
238  * @param dev Pointer to device structure for driver instance
239  *
240  * @return 0 on success, negative value otherwise
241  */
242 int cfb_framebuffer_init(const struct device *dev);
243 
244 /**
245  * @brief Deinitialize Character Framebuffer.
246  *
247  * @param dev Pointer to device structure for driver instance
248  */
249 void cfb_framebuffer_deinit(const struct device *dev);
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 /**
256  * @}
257  */
258 
259 #endif /* __CFB_H__ */
260