vex_sdk/
display.rs

1//! Brain Display
2
3use core::ffi::{c_char, VaList};
4
5use crate::map_jump_table;
6
7/// A decoded image written to by VEXos.
8#[repr(C, packed)]
9#[derive(Debug, Copy, Clone, Eq, PartialEq)]
10pub struct v5_image {
11    /// Definitive width of the output image.
12    pub width: u16,
13
14    /// Definitive height of the output image.
15    pub height: u16,
16
17    /// Buffer of RGB8 pixels that containing the image's data.
18    ///
19    /// This field must be set before the read operation as a pointer to the pre-allocated pixel buffer.
20    /// After an image read operation, said image’s pixels are written to the location specified by this field.
21    pub data: *mut u32,
22
23    /// Points to the first pixel of the second row in the pixel buffer.
24    ///
25    /// Only set by the SDK after a [`vexImageBmpRead`] call.
26    pub p: *mut u32,
27}
28
29map_jump_table! {
30    0x640 =>
31        /// Sets the color (encoded as RGB8) used for all future non-erasing display draws.
32        pub fn vexDisplayForegroundColor(col: u32),
33    0x644 =>
34        /// Sets the color (encoded as RGB8) used for all future erasing display draws.
35        pub fn vexDisplayBackgroundColor(col: u32),
36    0x648 =>
37        /// Fills the entire framebuffer with the current background color.
38        pub fn vexDisplayErase(),
39    0x64c =>
40        /// Moves a region of the screen defined by all the pixels whose y-axis coordinate are
41        /// within the range [nStartLine, 272) `nLines` pixels upwards, without affecting portions
42        /// of the screen outside the specified scroll region.
43        ///
44        /// Since `nLines` is a signed integer, a negative value will move the pixels in the
45        /// region downwards instead. Pixels that move outside the region being scrolled are
46        /// discarded, and any portions of the region that no longer have a value after the
47        /// operation are set to the background color.
48        pub fn vexDisplayScroll(nStartLine: i32, nLines: i32),
49    0x650 =>
50        /// Moves a rectangular region of the screen `nLines` pixels upwards, without affecting
51        /// portions of the screen outside the specified scroll region.
52        ///
53        /// Since `nLine` is a signed integer, a negative value will move the pixels in the
54        /// region downwards instead. Pixels that move outside the region being scrolled are
55        /// discarded, and any portions of the region that no longer have a value after the
56        /// operation are set to the background color.
57        ///
58        /// # Bugs
59        ///
60        /// It appears that this function is somewhat bugged at the time of writing (on VEXos 1.1.4),
61        /// as it will overwrite one too many lines, setting the bottommost row of scroll data to the
62        /// background color.
63        pub fn vexDisplayScrollRect(x1: i32, y1: i32, x2: i32, y2: i32, nLines: i32),
64    0x654 =>
65        /// Draw a buffer of pixels to a rectangular region of the screen.
66        ///
67        /// Each u32 element in the buffer is considered a pixel and is parsed in the same format used by
68        /// vexDisplayForegroundColor (RGB8). The function allows you to specify the region to write to, the
69        /// pointer to your image buffer, and the stride (or the number of u32 pixels in your buffer per 1 row).
70        pub fn vexDisplayCopyRect(x1: i32, y1: i32, x2: i32, y2: i32, pSrc: *mut u32, srcStride: i32),
71    0x658 =>
72        /// Fills a given pixel of the screen with the current foreground color.
73        pub fn vexDisplayPixelSet(x: u32, y: u32),
74    0x65c =>
75        /// Fills a given pixel of the screen with the current background color.
76        pub fn vexDisplayPixelClear(x: u32, y: u32),
77    0x660 =>
78        /// Draws a one-pixel wide stroke line between two points with the current foreground color.
79        pub fn vexDisplayLineDraw(x1: i32, y1: i32, x2: i32, y2: i32),
80    0x664 =>
81        /// Draws a one-pixel wide stroke line between two points with the current background color.
82        pub fn vexDisplayLineClear(x1: i32, y1: i32, x2: i32, y2: i32),
83    0x668 =>
84        /// Strokes a one-pixel wide rectangular region of the screen with the current foreground color.
85        pub fn vexDisplayRectDraw(x1: i32, y1: i32, x2: i32, y2: i32),
86    0x66c =>
87        /// Fills a rectangular region of the screen with the current background color.
88        pub fn vexDisplayRectClear(x1: i32, y1: i32, x2: i32, y2: i32),
89    0x670 =>
90        /// Fills rectangular region of the screen with the current foreground color.
91        pub fn vexDisplayRectFill(x1: i32, y1: i32, x2: i32, y2: i32),
92    0x674 =>
93        /// Strokes a one-pixel wide circle defined by a center-point and a radius with the current foreground color.
94        pub fn vexDisplayCircleDraw(xc: i32, yc: i32, radius: i32),
95    0x678 =>
96        /// Fills a circular region of the screen with the current background color.
97        pub fn vexDisplayCircleClear(xc: i32, yc: i32, radius: i32),
98    0x67c =>
99        /// Fills a circular region of the screen with the current foreground color.
100        pub fn vexDisplayCircleFill(xc: i32, yc: i32, radius: i32),
101    0x6a8 => pub fn vexDisplayTextSize(n: u32, d: u32),
102    0x6b4 => pub fn vexDisplayFontNamedSet(pFontName: *const c_char),
103    0x6b8 =>
104        /// Gets the currently set foreground color as an RGB8 color.
105        pub fn vexDisplayForegroundColorGet() -> u32,
106    0x6bc =>
107        /// Gets the currently set background color as an RGB8 color.
108        pub fn vexDisplayBackgroundColorGet() -> u32,
109    0x6c0 =>
110        /// Returns the calculated width (in pixels) of a string if it were to be drawn to the display.
111        ///
112        /// This function uses the text size of the last text drawing operation for calculating width.
113        pub fn vexDisplayStringWidthGet(pString: *const c_char) -> i32,
114    0x6c4 =>
115        /// Returns the calculated height (in pixels) of a string if it were to be drawn to the display.
116        ///
117        /// This function uses the text size of the last text drawing operation for calculating height.
118        pub fn vexDisplayStringHeightGet(pString: *const c_char) -> i32,
119    0x794 =>
120        /// Sets a rectangular region of the display's framebuffer that the current task is allowed to modify.
121        ///
122        /// When set, any draws to the display made by the calling task outside of its defined clip region will not be drawn.
123        pub fn vexDisplayClipRegionSet(x1: i32, y1: i32, x2: i32, y2: i32),
124    0x7a0 =>
125        /// Enables double-buffered mode on the display, flushing the intermediate framebuffer.
126        ///
127        /// The first time this function is called, double-buffered mode will be enabled. In order for future draws to be
128        /// seen, this function will need to be called each frame to draw the secondary buffer to the display.
129        ///
130        /// To re-enable immediate-mode rendering (single-buffer), see [`vexDisplayDoubleBufferDisable`].
131        ///
132        /// # Arguments
133        ///
134        /// - `bVsyncWait`: Sleep the current task until the screen is ready to refresh.
135        /// - `bRunScheduler`: Call [`vexTasksRun`](crate::task::vexTasksRun) while waiting for a refresh.
136        pub fn vexDisplayRender(bVsyncWait: bool, bRunScheduler: bool),
137    0x7a4 =>
138        /// Disables double-buffered mode, switching back to immediate mode rendering.
139        pub fn vexDisplayDoubleBufferDisable(),
140    0x7a8 =>
141        /// Sets a rectangular region of the display's framebuffer that the a given task index is allowed to modify.
142        ///
143        /// When set, any draws to the display made by the target task outside of its defined clip region will not be drawn.
144        ///
145        /// Derived from <https://github.com/jpearman/V5_CompetitionTest/blob/efb7214b983d30d5583e39b343161c26d7187766/include/comp_debug.h#L40>
146        pub fn vexDisplayClipRegionSetWithIndex(index: i32, x1: i32, y1: i32, x2: i32, y2: i32),
147    0x990 =>
148        /// Decodes a bitmap-encoded image passed to `ibuf` into a buffer of pixels that can be drawn to the display.
149        ///
150        /// # Arguments
151        ///
152        /// - `ibuf`: The PNG file as a buffer of bytes.
153        /// - `obuf`: A decoded image encoded as RGB8 pixels that will be written to if the operation succeeds.
154        /// - `maxw`: Width capacity of the image buffer.
155        /// - `maxh`: Height capacity of the image buffer.
156        ///
157        /// # Return
158        ///
159        /// `1` if the operation is successful, `0` if it failed.
160        ///
161        /// # Safety
162        ///
163        /// - `oBuf` must point to an initialized [`v5_image`] struct or null.
164        /// - `(*oBuf).data` must point to a mutable allocated image buffer that is at least `maxw * maxh * 4` bytes long or be null.
165        pub fn vexImageBmpRead(ibuf: *const u8, oBuf: *mut v5_image, maxw: u32, maxh: u32) -> u32,
166    0x994 =>
167        /// Decodes a PNG file passed to `ibuf` into a buffer of pixels that can be drawn to the display. This function uses
168        /// `libpng` internally to decode the file's contents.
169        ///
170        /// # Arguments
171        ///
172        /// - `ibuf`: The PNG file as a buffer of bytes.
173        /// - `obuf`: A decoded image encoded as RGB8 pixels that will be written to if the operation succeeds.
174        /// - `maxw`: Width capacity of the image buffer.
175        /// - `maxh`: Height capacity of the image buffer.
176        /// - `ibuflen`: Length of the input buffer.
177        ///
178        /// # Return
179        ///
180        /// `1` if the operation is successful, `0` if it failed.
181        ///
182        /// # Safety
183        ///
184        /// - `ibuf` must be null, OR point to a buffer of at least length ibuflen.
185        /// - `oBuf` must point to an initialized [`v5_image`] struct or null.
186        /// - `(*oBuf).data` must point to a mutable allocated image buffer that is at least `maxw * maxh * 4` bytes long or be null.
187        pub fn vexImagePngRead(ibuf: *const u8, oBuf: *mut v5_image, maxw: u32, maxh: u32, ibuflen: u32) -> u32,
188
189    0x680 =>
190        /// Draws a string of text to the display at a given top-left coordinate.
191        ///
192        /// Uses the current foreground color for the text itself, and the current background color if `bOpaque` is `true`.
193        pub fn vexDisplayVPrintf(xpos: i32, ypos: i32, bOpaque: i32, format: *const c_char, args: VaList),
194    0x684 =>
195        /// Draws a string of text to the display at a given line.
196        ///
197        /// Uses the current foreground color for the text itself, and the current background color if `bOpaque` is `true`.
198        pub fn vexDisplayVString(nLineNumber: i32, format: *const c_char, args: VaList),
199    0x688 =>
200        /// Draws a string of text to the display at a given top-left coordinate.
201        ///
202        /// Uses the current foreground color as the text color.
203        pub fn vexDisplayVStringAt(xpos: i32, ypos: i32, format: *const c_char, args: VaList),
204    0x68c =>
205        /// Draws a string of large-sized text to the display at a given line.
206        ///
207        /// Uses the current foreground color as the text color.
208        pub fn vexDisplayVBigString(nLineNumber: i32, format: *const c_char, args: VaList),
209    0x690 =>
210        /// Draws a string of large-sized text to the display at a top-left coordinate.
211        ///
212        /// Uses the current foreground color as the text color.
213        pub fn vexDisplayVBigStringAt(xpos: i32, ypos: i32, format: *const c_char, args: VaList),
214    0x6b0 =>
215        /// Draws a string of small-sized text to the display at a given line.
216        ///
217        /// Uses the current foreground color as the text color.
218        pub fn vexDisplayVSmallStringAt(xpos: i32, ypos: i32, format: *const c_char, args: VaList),
219    0x694 =>
220        /// Draws a string of center-justified text to the display at a given line.
221        ///
222        /// Uses the current foreground color as the text color.
223        pub fn vexDisplayVCenteredString(nLineNumber: i32, format: *const c_char, args: VaList),
224    0x698 =>
225        /// Draws a string of large-sized, center-justified text to the display at a given line.
226        ///
227        /// Uses the current foreground color as the text color.
228        pub fn vexDisplayVBigCenteredString(nLineNumber: i32, format: *const c_char, args: VaList),
229}
230
231pub unsafe extern "C" fn vexDisplayPrintf(
232    xpos: i32,
233    ypos: i32,
234    bOpaque: i32,
235    format: *const c_char,
236    mut args: ...
237) {
238    unsafe { vexDisplayVPrintf(xpos, ypos, bOpaque, format, args.as_va_list()) }
239}
240
241pub unsafe extern "C" fn vexDisplayString(nLineNumber: i32, format: *const c_char, mut args: ...) {
242    unsafe { vexDisplayVString(nLineNumber, format, args.as_va_list()) }
243}
244
245pub unsafe extern "C" fn vexDisplayStringAt(
246    xpos: i32,
247    ypos: i32,
248    format: *const c_char,
249    mut args: ...
250) {
251    unsafe { vexDisplayVStringAt(xpos, ypos, format, args.as_va_list()) }
252}
253
254pub unsafe extern "C" fn vexDisplayBigString(
255    nLineNumber: i32,
256    format: *const c_char,
257    mut args: ...
258) {
259    unsafe { vexDisplayVBigString(nLineNumber, format, args.as_va_list()) }
260}
261
262pub unsafe extern "C" fn vexDisplayBigStringAt(
263    xpos: i32,
264    ypos: i32,
265    format: *const c_char,
266    mut args: ...
267) {
268    unsafe { vexDisplayVBigStringAt(xpos, ypos, format, args.as_va_list()) }
269}
270
271pub unsafe extern "C" fn vexDisplaySmallStringAt(
272    xpos: i32,
273    ypos: i32,
274    format: *const c_char,
275    mut args: ...
276) {
277    unsafe { vexDisplayVSmallStringAt(xpos, ypos, format, args.as_va_list()) }
278}
279
280pub unsafe extern "C" fn vexDisplayCenteredString(
281    nLineNumber: i32,
282    format: *const c_char,
283    mut args: ...
284) {
285    unsafe { vexDisplayVCenteredString(nLineNumber, format, args.as_va_list()) }
286}
287
288pub unsafe extern "C" fn vexDisplayBigCenteredString(
289    nLineNumber: i32,
290    format: *const c_char,
291    mut args: ...
292) {
293    unsafe { vexDisplayVBigCenteredString(nLineNumber, format, args.as_va_list()) }
294}