屏幕#

介绍#

screen 类派生自 brain 基类。它控制 V5 Brain 如何在其触摸屏上显示文本、数字和图形,以及如何响应触摸输入。

默认情况下,打印到大脑的字体是等宽小号,它有 12 行 48 列。

大脑的绘图分辨率为 480 x 240 像素。

V5 大脑屏幕,红色网格线显示了总共 12 行 48 列的布局。像素尺寸为 479 宽 x 239 高。左上角起始于 0,0 像素,位于第 1 行第 1 列;右下角结束于 479, 239 像素,位于第 12 行第 列。

使用权#

可以通过以下方式访问屏幕 screen 类:

Brain.Screen

笔记#

  • The screen object is provided by the Brain. It is not constructed directly.

  • 项目使用单个全局 Brain 对象,因此屏幕函数使用 Brain.Screen调用。

例子#

/* This constructor is required when using VS Code.
A Brain is generated automatically at the start of
VEXcode projects. */

// Create the V5 Brain
brain Brain = brain();

// Print text to the Brain screen
Brain.Screen.print("Hello!");

成员功能#

screen 类包含以下成员函数:

  • print — 将格式化的文本、数字或布尔值打印到 Brain 屏幕。

  • setCursor — 将文本光标移动到指定的行和列。

  • newLine — 清除当前行的剩余内容并将光标移至下一行。

  • clearLine — 清除当前行的剩余部分或清除指定的行。

  • row — 返回要打印文本的行。

  • column — 返回要打印文本的列。

  • getStringHeight — 使用当前字体返回字符串的像素高度。

  • getStringWidth — 使用当前字体返回字符串的像素宽度。

  • printAt — 在 x, y 像素位置打印格式化文本。

  • clearScreen — 清除大脑屏幕上的所有图形和文本。

  • setFont — 设置用于屏幕打印的字体。

  • setPenWidth — 设置绘制的线条和形状轮廓的粗细。

  • setPenColor — 设置文本、像素、线条和形状轮廓的颜色。

  • setFillColor — 设置绘制形状和打印文本背景的填充颜色。

  • setOrigin — 设置屏幕坐标的原点。

  • setClipRegion — 将屏幕输出限制在矩形区域内。

  • drawPixel — 在坐标处绘制一个像素。

  • drawLine — 在两点之间画一条线。

  • drawRectangle — 在 x, y 位置绘制一个指定大小的矩形。

  • drawCircle — 在 x, y 位置绘制一个具有指定半径的圆。

  • drawImageFromFile — 将 SD 卡文件中的图像绘制到屏幕上。

  • render — 启用双缓冲或将后缓冲区渲染到屏幕。

  • pressing — 返回屏幕当前是否被按下。

  • xPosition — 返回最近一次屏幕按下的 x 坐标。

  • yPosition — 返回最近一次屏幕按下的 y 坐标。

  • pressed — 注册一个在屏幕被按下时运行的回调函数。

  • released — 注册一个回调函数,当屏幕被按下后释放时运行。

在调用任何 screen 成员函数之前,必须创建一个 brain 实例,如下所示:

// Create the V5 Brain
brain Brain = brain();

print#

在 V5 Brain 屏幕上显示文本、数字或布尔值。

Available Functions

1 — 使用字符串字面量或只读 C 风格字符串打印格式化文本。

void print(
    const char* format,
    ... );

2 — 使用可修改的字符数组打印格式化文本。

void print(
    char* format,
    ... );

Parameters

范围

类型

描述

format

const char*

以字符串字面量或只读 C 风格字符串形式提供的格式字符串(例如, “Score: %d”)。

format

char*

以可修改字符数组形式提供的格式字符串。

…

插入到格式字符串中的一个或多个附加值,以逗号分隔。

Return Values

此函数不返回值。

Notes Examples
// Display a message at the starting cursor
Brain.Screen.print("Hello, Robot!");

V5 大脑的屏幕截图,显示屏幕左上角以白色文字打印着“Hello, Robot!”。

setCursor#

将光标移动到 V5 Brain 屏幕上的特定行和列。下一次 print 调用将从该位置开始打印。

Available Functions
void setCursor(
  int32_t row, 
  int32_t col );

Parameters

范围

类型

描述

row

int32_t

要将光标移动到的行。

col

int32_t

要将光标移动到的列。

Return Values

此函数不返回值。

Notes
  • 影响使用 print 时后续文本输出的起始位置。

  • 行和列基于当前设置的屏幕字体。

  • 默认字体(mono20)使用 12 行和 48 列。

  • 调用 setCursor 只会影响后续文本输出的位置。

Examples
// Display text starting at Row 3 Column 12
Brain.Screen.setCursor(3, 12);
Brain.Screen.print("Row 3, Column 12");

V5 Brain 的屏幕截图,显示屏幕上方靠近中心位置打印的第 3 行第 12 列。

newLine#

将光标移动到大脑屏幕上下一行的开头。

Available Functions
void newLine();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 如果光标位于句子的中间,则 newLine 将清除光标之后的行的其余部分,然后再移动到下一行。

  • 调用 newLine后,光标位于下一行的第一列。

  • 使用 setCursor 设置光标位置和行号。

Examples
// Display two lines of text
Brain.Screen.print("Line 1");
Brain.Screen.newLine();
Brain.Screen.print("Line 2");

V5 Brain 屏幕的截图,显示左上角有两行白色文字。上面一行是“第 1 行”,紧挨着它的第二行是“第 2 行”。

clearLine#

清除 V5 Brain 屏幕上某一行的文本。

Available Functions

1 — 清除光标位置的当前行剩余内容。

void clearLine();

2 — 使用当前配置的 填充颜色 清除指定的行。

void clearLine(
    int number );

3 — 使用预定义或 自定义颜色 对象清除指定的行。

void clearLine(
    int          number,
    const color& color );

4 — 使用十六进制颜色值清除指定的行。

void clearLine(
    int         number,
    const char* color );

5 — 使用色调值清除指定的行。

void clearLine(
    int number,
    int hue );

Parameters

范围

类型

描述

number

int

要清除的行号。行号从 1开始。

color

const color&

使用 color 对象清除行。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
这也可以使用自定义颜色对象。

color

const char*

使用十六进制颜色值清除行(例如, “#000000”)。

hue

int

使用 0–359范围内的整数色调值清除行,表示色轮上的度数。

Return Values

此函数不返回值。

Notes
  • 清除一行不会改变当前光标位置。

  • 使用 setCursor 设置光标位置和行号。

  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Display text on two rows
Brain.Screen.print("This text stays");
Brain.Screen.newLine();
Brain.Screen.print("This disappears");

// Wait 3 seconds before clearing row 2
wait(3, seconds);
Brain.Screen.clearLine(2);

row#

返回将在 V5 Brain 屏幕上打印文本的行。

Available Functions
int32_t row();

Parameters

此函数不接受任何参数。

Return Values

返回 int32_t ,表示要打印文本的行。

行号从1开始。

Notes
  • 光标位置基于 屏幕字体。

  • 返回值指示下一个打印输出将显示在哪里。

  • 当打印文本或调用 setCursor 时,行值会发生变化。

Examples
// Display the cursor's current row
Brain.Screen.setCursor(3, 12);
Brain.Screen.print(Brain.Screen.row());

V5 Brain 屏幕的屏幕截图,显示屏幕左上角第 3 行第 12 列位置的白色文字“3”。

column#

返回将在 V5 Brain 屏幕上打印文本的列。

Available Functions
int32_t column();

Parameters

此函数不接受任何参数。

Return Values

返回 int32_t ,表示要打印文本的列。

列编号从 1 开始。

Notes
  • 光标位置基于 屏幕字体。

  • 返回值指示下一个打印输出将显示在哪里。

  • 当打印文本或调用 setCursor 时,列值会发生变化。

Examples
// Display the cursor's current column
Brain.Screen.setCursor(5, 15);
Brain.Screen.print(Brain.Screen.column());

V5 Brain 屏幕的屏幕截图,显示屏幕左上角第 3 行第 15 列位置的白色文字“15”。

getStringHeight#

返回字符串在 V5 Brain 屏幕上渲染时的高度(以像素为单位)。

Available Functions
int32_t getStringHeight( 
    const char* cstr );

Parameters

范围

类型

描述

cstr

const char*

要测量的文本,以 C 风格字符串 的形式提供(例如, “Hello”)。

Return Values

返回 int32_t ,表示字符串在 Brain 屏幕上渲染时的像素高度。

Notes
  • 返回的高度取决于当前选定的屏幕 。

  • 该函数测量字符串的长度,但不将其绘制到屏幕上。

  • 返回的高度反映了字符串打印时的外观。

getStringWidth#

返回字符串在 V5 Brain 屏幕上渲染时的宽度(以像素为单位)。

Available Functions
int32_t getStringWidth( 
    const char* cstr );

Parameters

范围

类型

描述

cstr

const char*

要测量的文本,以 C 风格字符串 的形式提供(例如, “Hello”)。

Return Values

返回 int32_t ,表示字符串在 Brain 屏幕上渲染时的像素宽度。

Notes
  • 返回的 取决于当前选定的屏幕字体。

  • 该函数测量字符串的长度,但不将其绘制到屏幕上。

  • 返回的宽度反映了字符串打印时的显示方式。

printAt#

在 V5 脑屏幕上的特定坐标处打印文本、数字或布尔值。

Available Functions

1 — 在指定的像素位置打印格式化的输出。

void printAt(
    int32_t    x,
    int32_t    y,
    const char* format,
    ... );

2 — 在指定的像素位置打印格式化的输出,并设置文本背景是不透明还是透明。

void printAt(
    int32_t    x,
    int32_t    y,
    bool       bOpaque,
    const char* format,
    ... );

Parameters

范围

类型

描述

x

int32_t

打印位置的 x 坐标,以屏幕原点为参考。

y

int32_t

打印位置的 y 坐标,以屏幕原点为参考。

bOpaque

bool

Controls whether the printed text is drawn opaquely or transparently:

  • true — The background of the printed text is filled using the current fill color.
  • false — The text is drawn transparently and the background is not filled.

format

const char*

控制屏幕上打印内容的格式字符串(例如, “Score: %d”)。

…

插入到格式字符串中的一个或多个附加值,以逗号分隔。

Return Values

此函数不返回值。

Notes
  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行更改。

  • 打印文本使用 屏幕字体。

  • 打印文本使用 笔颜色。

  • 使用格式字符串时,传递的值的数量和类型必须与字符串中的 格式说明符 匹配。

Examples
// Print "Center" at the center of the screen
Brain.Screen.printAt(220, 120, "Center");

V5 Brain 屏幕的截图,屏幕中央有白色文字“Center”。

clearScreen#

清除大脑屏幕上的所有图形和文本,并将光标位置重置为第 1 行第 1 列。

Available Functions

1 — 将屏幕清空至黑色。

void clearScreen();

2 — 使用预定义或 自定义颜色 对象清除屏幕。

void clearScreen(
    const color& color );

3 — 使用十六进制颜色值清除屏幕。

void clearScreen(
    const char* color );

4 — 使用色调值清除屏幕。

void clearScreen(
    int hue );

Parameters

范围

类型

描述

color

const color&

使用 color 对象清除屏幕。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用以字符串形式表示的十六进制颜色值清除屏幕(例如, “#000000”)。

hue

int

使用 0–359范围内的整数色调值清除屏幕,表示色轮上的度数。

Return Values

此函数不返回值。

Notes
  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Clear screen after 2 seconds
Brain.Screen.print("VEXcode");
wait(2, seconds);
Brain.Screen.clearScreen();


// Clear screen to blue after 2 seconds
Brain.Screen.print("VEXcode");
wait(2, seconds);
Brain.Screen.clearScreen(blue);

V5 Brain 屏幕的截图,其中整个可打印区域呈亮蓝色。

setFont#

设置 V5 Brain 屏幕上显示的文本所使用的字体。

这控制在使用屏幕文本功能(例如 print)时应用于后续文本输出的字体。

Available Functions
void setFont(
  fontType font );

Parameters

范围

类型

描述

font

fontType

Sets the font to one of the following:

  • mono12
  • mono15
  • mono20
  • mono30
  • mono40
  • mono60
  • prop20
  • prop30
  • prop40
  • prop60
  • fontType::cjk16 — Can print Chinese, Japanese, and Korean characters.
    • Simplified Chinese — Can be seen up to column 28.
    • Traditional Chinese — Can be seen up to column 28.
    • Japanese (Kanji) — Can be seen up to column 28.
    • Korean (Hangul) — Can be seen up to column 30.
These options are shown below.

TA 截图,V5 Brain 屏幕左上角以 MONO 12 字体打印白色数字和字母。其中一行显示 AZ,约占屏幕宽度的四分之一。左下角显示 80 列和 20 行。
mono12

与上一张图相同,但字体为 Mono 15。图中一行显示“AZ”,几乎占据了屏幕一半的宽度。左下角显示“68 列 16 行”。
mono15

与上一张图相同,但字体为 Mono 20。图中一行显示“AZ”,占据了屏幕宽度的近三分之二。左下角显示“48 列 12 行”。
mono20

与上一张图相同,但字体为 Mono 30。图中一行显示“AZ”,几乎占据了整个屏幕宽度。左下角显示“32 列 8 行”。
mono30

与上一张图相同,但字体为 Mono 40。图中一行显示“AX”,横跨整个屏幕宽度。左下角显示“24 列 6 行”。
mono40

与上一张图片相同,但字体为 Mono 60。图片中一行显示 AP 字样,横跨屏幕宽度。底部标明 16 列和 4 行。
mono60

与上一张图片相同,但字体为 Prop 20。图中一行显示“AZ”,占据了屏幕宽度的近三分之二。左下角显示“48 列 12 行”。
prop20

与上一张图片相同,但字体为 Prop 30。图中一行显示 AZ,几乎占据了整个屏幕宽度。左下角显示 32 列和 8 行。
prop30

与上一张图片相同,但字体为 Prop 40。图中一行显示 AU,横跨屏幕宽度。左下角显示 24 列 6 行。
prop40

与上一张图片相同,但字体为 Prop 60。图片中一行显示“AN”,横跨屏幕宽度。底部显示“15 列 4 行”。
prop60

与上一张图片相同,但字体为CJK 16。其中一行显示AZ,占据屏幕约三分之二的面积。接下来的几行分别以简体中文、繁体中文、韩语和日语显示VEX代码。底部显示43列12行。
fontType::cjk16

Return Values

此函数不返回值。

Examples
// Print larger text
Brain.Screen.setFont(mono40);
Brain.Screen.print("VEX");

V5 Brain 屏幕的截图,显示左上角有白色文字,字体比默认字体大,内容为 VEX。


// Print in Chinese
Brain.Screen.setFont(fontType::cjk16);
Brain.Screen.print("VEX机器人");

V5 大脑屏幕的截图,左上角显示中文“VEX 机器人”字样。

setPenWidth#

设置绘制线条和形状轮廓的粗细。

Available Functions
void setPenWidth(
  uint32_t width );

Parameters

范围

类型

描述

width

uint32_t

笔的宽度,以像素为单位,范围从 0 到 32。

Return Values

此函数不返回值。

Notes Examples
// Draw a rectangle with a pen width of 10
Brain.Screen.setPenWidth(10);
Brain.Screen.drawRectangle(50, 50, 130, 60);

V5 Brain 屏幕的屏幕截图,显示屏幕左上角打印着一个带有粗边框的矩形。

setPenColor#

设置文本、像素、线条和形状轮廓的颜色。

Available Functions

1 — 使用预定义或 自定义颜色 对象设置画笔颜色。

void setPenColor(
    const color& color );

2 — 使用十六进制颜色值设置画笔颜色。

void setPenColor(
    const char* color );

3 — 使用色调值设置画笔颜色。

void setPenColor(
    int hue );

Parameters

范围

类型

描述

color

const color&

使用 color 对象设置画笔颜色。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent

color

const char*

使用以字符串形式表示的十六进制颜色值设置画笔颜色(例如, “#FF0000”)。

hue

int

使用 0–359范围内的整数色调值设置画笔颜色,表示色轮上的度数(例如, 0 为红色, 120 为绿色, 240 为蓝色)。

Return Values

此函数不返回值。

Notes
  • 项目开始时的默认画笔颜色为 white。

  • 笔的颜色用于绘制形状的轮廓(例如使用 drawCircle 或 drawRectangle),文本和数字输出(使用 print 和 printAt),以及使用 drawPixel 绘制的单个像素。

  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Draw a rectangle with orange borders
Brain.Screen.setPenColor(orange);
Brain.Screen.drawRectangle(50, 50, 130, 60);

V5 Brain 屏幕的截图,显示屏幕左上角有一个橙色矩形。

setFillColor#

设置绘制形状时使用的填充颜色。

Available Functions

1 — 使用预定义或 自定义颜色 对象设置填充颜色。

void setFillColor(
    const color& color );

2 — 使用十六进制颜色值设置填充颜色。

void setFillColor(
    const char* color );

3 — 使用色调值设置填充颜色。

void setFillColor(
    int hue );

Parameters

范围

类型

描述

color

const color&

使用 color 对象设置填充颜色。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent

color

const char*

使用以字符串形式表示的十六进制颜色值设置填充颜色(例如, “#00FF00”)。

hue

int

使用 0–359范围内的整数色调值设置填充颜色,表示色轮上的度数(例如, 0 为红色, 120 为绿色, 240 为蓝色)。

Return Values

此函数不返回值。

Notes
  • 项目开始时的默认填充颜色为 black。

  • 填充颜​​色用于形状的内部(例如使用 drawCircle 或 drawRectangle)。

  • 填充颜​​色用作打印文本的背景颜色(例如使用 print 和 printAt)。

    • 如果 transparent 与印刷文本一起使用,则背景将为 black。

  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Draw a rectangle filled with purple
  Brain.Screen.setFillColor(purple);
  Brain.Screen.drawRectangle(50, 100, 130, 60);

V5 Brain 屏幕的截图,显示屏幕左下角有一个填充紫色的矩形。

setOrigin#

设置大脑屏幕上用于绘图和基于坐标的打印的原点。

Available Functions
void setOrigin(
  int32_t x,
  int32_t y );

Parameters

范围

类型

描述

x

int32_t

原点相对于大脑屏幕左上角的 x 坐标,取值范围为 0 到 479。

y

int32_t

原点相对于大脑屏幕左上角的 y 坐标,范围从 0 到 239。

Return Values

此函数不返回值。

Notes
  • 默认原点位于屏幕左上角坐标 (0, 0)。

  • 原点适用于坐标函数,例如 drawLine、drawRectangle、drawCircle 和 printAt。

  • 该原点设置将一直有效,直到再次更改或项目重置为止。

setClipRegion#

在屏幕上定义一个矩形区域,所有图形和文本都将限制在该区域内。该区域之外的任何内容都不会显示。

Available Functions
void setClipRegion(
  int x,
  int y,
  int width,
  int height );

Parameters

范围

类型

描述

x

int

剪辑区域左上角的 x 坐标,取值范围为 0 到 479 的整数。

y

int

剪辑区域左上角的 y 坐标,取值范围为 0 到 239 的整数。

width

int

裁剪区域的宽度(以像素为单位),取值范围为 0 到 479 的整数。

height

int

裁剪区域的高度(以像素为单位),取值范围为 0 到 239 的整数。

Return Values

此函数不返回值。

Notes
  • 即使绘图函数超出裁剪区域,它们仍然会执行,但只有区域内的部分是可见的。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行更改。

  • 剪辑区域仅适用于当前线程。

  • 设置新的剪辑区域会替换当前线程之前设置的任何剪辑区域。

Examples
// Restrict text and drawings to a specific region
Brain.Screen.setClipRegion(0, 0, 120, 120);
Brain.Screen.drawRectangle(60, 60, 100, 100, red);
Brain.Screen.printAt(60, 60, "Cut off!");

V5 大脑屏幕的截图,白色文字显示“Cut of”,左上角象限下方有一个实心红色方块,反映了剪辑区域内打印的内容。

drawPixel#

在 V5 Brain 屏幕上选定的 x 和 y 像素位置绘制一个像素。

Available Functions
void drawPixel(
  int x,
  int y );

Parameters

范围

类型

描述

x

int

像素绘制位置的 x 坐标,取值范围为 0 到 479 之间的整数。

y

int

像素将被绘制的 y 坐标,取值范围为 0 到 239 的整数。

Return Values

此函数不返回值。

Notes
  • 使用 setPenColor 设置当前画笔颜色来绘制像素。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行设置。

  • 如果指定的像素位置在屏幕边界之外,则不会绘制该像素。

Examples
// Draw the pixels marking the corners of a square
Brain.Screen.drawPixel(250, 100);
Brain.Screen.drawPixel(275, 100);
Brain.Screen.drawPixel(250, 125);
Brain.Screen.drawPixel(275, 125);

V5 Brain 屏幕的截图,显示屏幕中心附近一个正方形的四个角由各种像素组成。

drawLine#

在 V5 Brain 屏幕上绘制连接两点的直线。

Available Functions
void drawLine(
  int x1,
  int y1,
  int x2,
  int y2 );

Parameters

范围

类型

描述

x1

int

直线的起始 x 坐标,取值范围为 0 到 479 的整数。

y1

int

直线的起始 y 坐标,取值范围为 0 到 239 的整数。

x2

int

直线的终点 x 坐标,取值范围为 0 到 479 的整数。

y2

int

直线的终点 y 坐标,取值范围为 0 到 239 的整数。

Return Values

此函数不返回值。

Notes
  • 使用 setPenColor 设置当前画笔颜色来绘制线条。

  • 线条的宽度由当前笔宽决定,该笔宽通过 setPenWidth 设置。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行设置。

  • 如果线条的任何部分超出屏幕边界,则只绘制可见部分。

Examples
// Draw a line from the top left to bottom right of the screen
Brain.Screen.drawLine(0, 0, 479, 239);

V5 Brain 屏幕的截图显示,屏幕中心有一条细斜线,从左上角延伸到右下角。

drawRectangle#

在 V5 Brain 屏幕上指定位置和大小绘制矩形。

Available Functions

1 — 使用当前配置的 填充颜色 绘制并填充矩形。

void drawRectangle(
    int x,
    int y,
    int width,
    int height );

2 — 使用预定义或自定义颜色对象绘制并填充矩形。

void drawRectangle(
    int           x,
    int           y,
    int           width,
    int           height,
    const color&  color );

3 — 使用十六进制颜色值绘制并填充矩形。

void drawRectangle(
    int           x,
    int           y,
    int           width,
    int           height,
    const char*   color );

4 — 使用色调值绘制并填充矩形。

void drawRectangle(
    int x,
    int y,
    int width,
    int height,
    int hue );

Parameters

范围

类型

描述

x

int

矩形左上角的 x 坐标,取值范围为 0 到 479 的整数。

y

int

矩形左上角的 y 坐标,取值范围为 0 到 239 的整数。

width

int

矩形的宽度,取值范围为 0 到 479 之间的整数。

height

int

矩形的高度,取值范围为 0 到 239 的整数。

color

const color&

使用 color 对象填充矩形。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用十六进制颜色值填充矩形(例如, “#FF0000”)。

hue

int

使用 0–359范围内的色调值填充矩形。

Return Values

此函数不返回值。

Notes
  • 使用 setPenColor 设置当前画笔颜色来绘制矩形轮廓。

  • 轮廓线粗细由当前笔宽决定,该笔宽通过 setPenWidth 设置。

  • 当未提供 color 参数时,矩形内部将使用 setFillColor 设置的当前填充颜色进行填充。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行更改。

  • 如果矩形的任何部分位于屏幕边界之外,则只绘制可见部分。

  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Draw a rectangle on the screen
Brain.Screen.drawRectangle(50, 50, 130, 60);

V5 大脑屏幕的截图显示,左上象限打印着一个带有细白边框的矩形。

drawCircle#

在 V5 Brain 屏幕上,于指定位置和半径处绘制一个圆。

Available Functions

1 — 使用当前配置的 填充颜色 绘制并填充一个圆。

void drawCircle(
    int x,
    int y,
    int radius );

2 — 使用预定义或自定义颜色对象绘制并填充一个圆。

void drawCircle(
    int           x,
    int           y,
    int           radius,
    const color&  color );

3 — 使用十六进制颜色值绘制并填充一个圆。

void drawCircle(
    int           x,
    int           y,
    int           radius,
    const char*   color );

4 — 使用色相值绘制并填充圆形。

void drawCircle(
    int x,
    int y,
    int radius,
    int hue );

Parameters

范围

类型

描述

x

int

圆心的 x 坐标,取值范围为 0 到 479 之间的整数。

y

int

圆心的 y 坐标,取值范围为 0 到 239 的整数。

radius

int

圆的半径,取值范围为 0 到 239 像素的整数。

color

const color&

使用 color 对象填充圆圈。预定义颜色包括:

  • black
  • white
  • red
  • green
  • blue
  • yellow
  • orange
  • purple
  • cyan
  • transparent
也可以使用自定义颜色对象。

color

const char*

使用十六进制颜色值填充圆圈(例如, “#FF0000”)。

hue

int

使用 0–359范围内的色调值填充圆。

Return Values

此函数不返回值。

Notes
  • 使用 setPenColor 设置当前画笔颜色来绘制圆的轮廓。

  • 轮廓线粗细由当前笔宽决定,该笔宽通过 setPenWidth 设置。

  • 当未提供 color 参数时,圆的内部将使用 setFillColor 设置的当前填充颜色进行填充。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行更改。

  • hue 参数值基于圆形色轮,如下图所示,其中值表示色轮上的度数(例如,红色接近 0°,绿色接近 120°,蓝色接近 240°)。

一个色轮,显示从 0 到 359 的颜色范围,其中红色为 0,绿色为 120,蓝色为 240。

Examples
// Draw a circle on the screen
Brain.Screen.drawCircle(240, 120, 40);

V5 Brain 屏幕的截图显示,中心画有一个带有细白边框的圆圈。

drawImageFromFile#

使用存储在 SD 卡 中的图像文件在 V5 Brain 屏幕上绘制图像。

Available Functions
bool drawImageFromFile(
  const char* name,
  int x,
  int y );

Parameters

范围

类型

描述

name

const char*

SD卡上图像的文件名。文件必须具有 .bmp 或 .png 扩展名。

x

int

图像左边缘的绘制位置的 x 坐标,以像素为单位,范围从 0 到 479。

y

int

图像顶部边缘的绘制位置的 y 坐标,以像素 0 到 239 为单位。

Return Values

返回一个布尔值指示图像是否成功绘制:

  • true — 图像已成功加载并绘制到屏幕上。4
  • false — 图像无法绘制(例如,文件不存在或无效)

Notes
  • 支持的图像格式有: .bmp 和 .png。

  • 图像文件大小不得超过 512 KB。

    • 建议使用 8 位 RLE 编码以最小化文件大小。

  • 支持的最大图像尺寸为 Brain 屏幕尺寸(约 479 x 239 像素)。

  • x 和 y 坐标是相对于当前屏幕原点的,可以使用 setOrigin 进行更改。

Examples
// Draw a bmp file on the Brain's screen at coordinate 0, 0
Brain.Screen.drawImageFromFile("test_image.bmp", 0, 0);

render#

启用大脑屏幕的双缓冲功能。调用后,任何绘图功能(例如文本、形状或图像)在项目剩余时间内都不会立即显示。相反,更新内容仅在再次使用渲染 render 才会显示。这可以实现更流畅、更可控的屏幕更新,但意味着在 render 之前,屏幕内容将不可见。

Available Functions

1 — 启用双缓冲或将后缓冲区渲染到屏幕。

bool render();

2 — 将后缓冲区渲染到屏幕上,并可控制垂直同步行为和可选的调度程序执行。

bool render(
    bool bVsyncWait,
    bool bRunScheduler = true );

Parameters

范围

类型

描述

bVsyncWait

bool

  • true — Wait for the screen’s vertical refresh before rendering to reduce visual tearing.
  • false — Do not wait for the screen’s vertical refresh before rendering.

bRunScheduler

bool

可选

  • true (默认)— 允许在等待渲染时运行后台任务。4
  • false — 不允许在等待渲染时运行后台任务。

Return Values

返回一个布尔值指示缓冲区中保存的图形是否已成功渲染到屏幕上:

  • true — 图形已成功渲染到屏幕上。4
  • false — 图形无法渲染到屏幕上
  • Notes
    • 调用 render 将需要 render 用于项目的其余部分。

    Examples
    Brain.Screen.render();
    
    // Draw text to the back buffer
    Brain.Screen.print("Render later...");
    
    // Wait for a screen press to render drawings
    while (!Brain.Screen.pressing()) {
      wait(20, msec);
    }
    Brain.Screen.render();
    
    

pressing#

报告 V5 Brain 屏幕当前是否被按下。

Available Functions
bool pressing();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示屏幕当前的按下状态:true — 屏幕当前被按下。或 false — 屏幕未被按下。

Examples
// Change the screen's color after it's pressed
while (Brain.Screen.pressing() == false) {
  wait(5, msec);
}

Brain.Screen.setFillColor(green);
Brain.Screen.drawRectangle(0, 0, 479, 239);


// Display different messages after the screen is pressed
while (!Brain.Screen.pressing()) {
  wait(5, msec);
}
Brain.Screen.print("First message!");
Brain.Screen.newLine();

// Lift finger to press the screen again
while (Brain.Screen.pressing()) {
  wait(5, msec);
}
while (Brain.Screen.pressing() == false) {
  wait(5, msec);
}
Brain.Screen.print("Second message!");
Brain.Screen.newLine();

xPosition#

返回 V5 Brain 屏幕上最后一次按下按钮的 x 坐标。

Available Functions
int32_t xPosition();

Parameters

此函数不接受任何参数。

Return Values

返回一个 int32_t 表示上次屏幕按下的 x 坐标,以像素为单位,范围从 0 到 479。

Examples
// Display a circle where the screen is pressed
while (Brain.Screen.pressing() == false) {
  wait(5, msec);
}

Brain.Screen.drawCircle(Brain.Screen.xPosition(), Brain.Screen.yPosition(), 20, white);

yPosition#

返回 V5 Brain 屏幕上最后一次按下按钮的 y 坐标。

Available Functions
int32_t yPosition();

Parameters

此函数不接受任何参数。

Return Values

返回一个 int32_t 表示上次屏幕按下的 y 坐标,以像素 0 到 239 为单位。

Examples
// Display a circle where the screen is pressed
while (Brain.Screen.pressing() == false) {
  wait(5, msec);
}

Brain.Screen.drawCircle(Brain.Screen.xPosition(), Brain.Screen.yPosition(), 20, white);

pressed#

注册一个在按下 V5 Brain 屏幕时要调用的函数。

Available Functions

1 — 按下屏幕时调用函数。

void pressed(
    void (*callback)(void)
);

2 — 当屏幕被按下时调用一个函数,并将用户定义的参数传递给回调函数。

void pressed(
    void (*callback)(void*),
    void* arg );

Parameters

范围

类型

描述

callback

void (*)(void)

屏幕按下时调用的函数。

callback

void ()(void)

屏幕按下时调用的函数,接收用户定义的参数。

arg

void*

用户定义的值,在屏幕被按下时传递给回调函数。

Return Values

此函数不返回值。

Notes
  • 每次按下大脑屏幕时,都会执行回调函数。

  • 一次只能注册一个回调函数;调用 pressed 会替换之前的回调函数。

  • 回调函数必须返回 void。

Examples

定义回调函数(在 int main()之外)

// Tracks how many times the screen is pressed.
void onScreenPressed(void* arg) {
  int* count = (int*)arg;
  (*count)++;

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Pressed %d times", *count);
}

在 int main()中注册回调函数

int main() {
  int pressCount = 0;

  // Register the callback and pass a value to update.
  Brain.Screen.pressed(onScreenPressed, &pressCount);

  while (true) {
    wait(20, msec);
  }
}

released#

注册一个函数,当按下 V5 Brain 屏幕后松开时,该函数将被调用。

Available Functions

1 — 按下屏幕后松开时调用函数。

void released(
    void (*callback)(void)
);

2 — 按下屏幕后松开时调用函数,并将用户定义的参数传递给回调函数。

void released(
    void (*callback)(void*),
    void* arg );

Parameters

范围

类型

描述

callback

void (*)(void)

按下屏幕后松开时调用的函数。

callback

void ()(void)

屏幕按下后松开时调用的函数,接收用户定义的参数。

arg

void*

用户定义的值,在按下屏幕后释放屏幕时传递给回调函数。

Return Values

此函数不返回值。

Notes
  • 每次按下 Brain 屏幕后松开时,都会执行回调函数。

  • 一次只能注册一个回调函数;再次调用 released 会替换之前的回调函数。

  • 回调函数必须返回 void。

Examples

定义回调函数(在 int main()之外)

// Tracks how many times the screen is released.
void onScreenReleased(void* arg) {
  int* count = (int*)arg;
  (*count)++;

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Released %d times", *count);
}

在 int main()中注册回调函数

int main() {
  int releaseCount = 0;

  // Register the callback and pass a value to update.
  Brain.Screen.released(onScreenReleased, &releaseCount);

  while (true) {
    wait(20, msec);
  }
}