#

初始化 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 - Provides information about the EXP Brain’s battery, including capacity, temperature, voltage, and current.

  • button - Allows you to detect button presses and assign functions to button events on the EXP Brain.

  • timer - Provides functionality to measure elapsed time and assign functions to time-based events on the EXP Brain.

  • screen - Enables you to display text and draw images on the EXP Brain’s screen.

  • sdcard - Provides access to files stored on the EXP Brain’s SD card.

  • 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.

参数

描述

sound

A valid SoundType.

volume

**可选。**音量,最大为 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.

参数

描述

octave

要使用的八度。

note

要播放的音符。

duration

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

volume

**可选。**音量。最大值为 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.

参数

描述

filename

要播放的声音文件。

volume

**可选。**音量,最大为 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.

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