#

初始化 Brain 类#

要在您的项目中使用 EXP Brain,请首先按如下方式初始化它:

# Create a new object "brain" with the Brain class.
brain = Brain()

This brain object will be used in all subsequent examples throughout this API documentation when referring to Brain class methods and attributes.

Brain 类具有六个预定义属性:

  • battery - 提供有关 EXP Brain 电池的信息,包括容量、温度、电压和电流。

  • button - 允许您检测按钮按下并为 EXP Brain 上的按钮事件分配功能。

  • timer - 提供测量经过时间的功能并为 EXP Brain 上基于时间的事件分配函数。

  • screen - 使您能够在 EXP Brain 的屏幕上显示文本和绘制图像。

  • sdcard - 提供对存储在 EXP Brain 的 SD 卡上的文件的访问。

  • three_wire_port - Provides access to 3-Wire Sensors connected to the EXP Brain’s 3-Wire Ports. Each port is identified by a letter (a through h) and can be accessed using the format brain.three_wire_port.x, where x is the port letter. For example, brain.three_wire_port.a refers to the Sensor connected to port A.

类方法#

除了属性之外,Brain 类还提供了几种可以直接使用的方法。

播放声音()#

The brain.play_sound(sound, volume) command plays a sound on the Brain.

参数

描述

声音

有效的 SoundType

体积

**可选。**音量,最大为 100。

**返回:**无。

# Play a sound of type TADA.
brain.play_sound(SoundType.TADA)

播放音符()#

The brain.play_note(octave, note, duration, volume) command plays a note on the Brain.

参数

描述

八度

要使用的八度。

笔记

要播放的音符。

期间

**可选。**播放音符的持续时间(以毫秒为单位)。

体积

**可选。**音量。最大值为 100。

**返回:**无。

# Play a note on the Brain.
brain.play_note(1, 4, 200, 50)

播放文件()#

The brain.play_file(filename, volume) command plays a sound file in .wav format on the Brain.

参数

描述

文件名

要播放的声音文件。

体积

**可选。**音量,最大为 100。

**返回:**无。

# Play the sound file titled "myfile.wav" on the Brain.
brain.play_file('myfile.wav', 50)

声音关闭()#

The brain.sound_off() command stops any sound that is playing.

**返回:**无。

# Turn off the sound on the Brain.
brain.sound_off()

声音处于活动状态()#

The brain.sound_is_active() command checks whether a sound is playing.

Returns: True if a sound is playing. False if a sound is not playing.

声音持续时间()#

The brain.sound_duration() command gets the total duration of the currently playing sound.

**返回:**以毫秒为单位的持续时间。

剩余声音()#

The brain.sound_remaining() command gets the remaining duration of the currently playing sound.

**返回:**以毫秒为单位的持续时间。