Python API for Codey
led
— Onboard RGB LED
led
— Onboard RGB LEDFunction
led.show(r, g, b)
Set the displayed color of the RGB LED, parameters:
r refers to the value of red component, parameter range is
0 ~ 255
,0 with no red component and 255 the highest red component.g refers to the value of green component, parameter range is
0 ~ 255
,0 with no green component and 255 the highest green component.b refers to the value of blue component, parameter range is
0 ~ 255
,0 with no blue component and 255 the highest blue component.
led.set_red(val)
Set the red color value of the RGB LED, parameters:
val refers to the value of red component, parameter range is
0 ~ 255
,0 with no red component and 255 the highest red component.
led.set_green(val)
Set the green color value of the RGB LED, parameters:
val refers to the value of green component, parameter range is
0 ~ 255
,0 with no green component and 255 the highest green component.
led.set_blue(val)
Set the blue color value of the RGB LED, parameters:
val refers to the value of blue component, parameter range is
0 ~ 255
,0 with no blue component and 255 the highest blue component.
led.off()
Turn off the RGB LED.
Sample Code:
display
— Face Panel
display
— Face PanelFace Panel Introduction
As shown in the figure above, the face panel has the upper left corner as the coordinate 0 point, and the direction of x and y is indicated by the arrow. About parameters displayed, take above figure as an example. The upper three of the first column data are lit and the data is converted to 11100000
, that is, hexadecimal 0xe0.The second column verse is converted to 00001101
, that is, In hexadecimal 0x0d. All the lattices in the above figure are converted to e00d0000000000000000000000000000
.
Function
display.show_image(image, pos_x = 0, pos_y = 0, time_s = None)
Display custom dot matrix graphics as image parameters, parameters:
image string data, each column of the dot matrix has 8 display points, which is 1 byte of data, converted to a hexadecimal string. Therefore, 16 columns of lattices need to be represented by 32 string data.
pos_x displays the offset of the x-axis of the graph on the panel. The parameter range is
-15 ~ 15
, It starts from the 0 position as default if this parameter is not set.pos_y displays the offset of the graph on the y-axis of the panel. The parameter range is
-7 ~ 7
, It starts from the 0 position as default if this parameter is not set.time_s displays the time in seconds (in seconds). If this parameter is not set, the display remains unchanged until there is a clear screen or resetting the panel operation.
display.show(var, pos_x = 0, pos_y = 0, wait = True)
Display data in full-type data parameters, parameters:
var full type, where the display of numeric and time types is treated specially, and the time format display needs to satisfy:
[x]x:[x]x
format (regular expressiond?d:dd?
)pos_x displays the offset of the data on the x-axis of the panel. The parameter range is
-15 ~ 15
. It starts from the 0 position as default if this parameter is not set.pos_y displays the offset of the data on the y-axis of the panel. The parameter range is
-7 ~ 7
. It starts from the 0 position as default if this parameter is not set.wait sets whether to block the display, where True: means blocking until the display is complete, False: means display but not blocking.
display.set_pixel(pos_x, pos_y, status)
Set the brightness and deactivation status of a single pixel of the panel, Parameters:
pos_x The coordinates of the x-axis for the pixel on the panel. The parameter range is
0 ~ 15
.pos_y The coordinates of the y-axis for the pixel on the panel. The parameter range is
0 ~ 7
.status Boolean value,where
True
indicates that the pixel is lit, andFalse
: indicates that the pixel is off.
display.get_pixel(pos_x, pos_y)
Get the current on and off states of a single pixel on the panel. The return value is a Boolean value, where True
: indicates that the pixel is lit, and False
: indicates that the pixel is off, parameter:
pos_x The coordinates of the x-axis for the pixel on the panel. The parameter range is
0 ~ 15
.pos_y The coordinates of the y-axis for the pixel on the panel. The parameter range is
0 ~ 7
.
display.toggle_pixel(pos_x, pos_y)
Toggle the current on and off states of a single pixel on the panel, parameter:
pos_x The coordinates of the x-axis for the pixel on the panel. The parameter range is
0 ~ 15
.pos_y The coordinates of the y-axis for the pixel on the panel. The parameter range is
0 ~ 7
.
display.clear()
Turn off all the LED lights on the display.
Sample Code:
speaker
— Onboard Speaker
speaker
— Onboard SpeakerFunction
speaker.stop_sounds()
Stop all sounds.
speaker.play_melody(file_name)
Playing an audio file, the function will not block when playing, but if it is called continuously, the next playback will stop the previous playback, Parameters:
file_name String type, the audio file name of the wav format burned in Codey Rocky flash. When inputting, the format suffix
.wav
can also be omitted.The optional sound file has:
speaker.play_melody_until_done(file_name)
The audio file is played until it stops, and the function blocks playback, that is, the next instruction cannot be executed until the sound is played, parameter:
file_name String type, the audio file name of the wav format burned in Codey Rocky flash. When inputting, the format name
.wav
can also be omitted. For specific optional parameters, seeplay_melody
.
speaker.play_note(note_num, beat = None)
Play note, digital note definitions please refer to: scratch digital note descriptionfor()Beats(block)), prameters:
note_num numeric value, range of values
48 - 72
, or string type, such asC4
.beat value data, indicates the number of beats, the default value is always playing. notes and frequency is as follows:
speaker.play_tone(frequency, time = None)
Play the setting frequency sound, parameters:
frequency Numerical data, the frequency of sound which is played, and its value range is
0 ~ 5000
.time Numerical data, indicating the playback time (in
milliseconds - ms
) and its value range is0 ~ the value range limit
.
speaker.rest(number)
Stop the beat, parameters:
number Numerical data, the number of paused beats, its value range is
0 ~ the value range limit
.
Constant
speaker.volume
Numerical data, the property value of the volume, you can modify or read this value. Modify this value to control the volume. Its value range is 0 ~ 100
.
speaker.tempo
Numerical data, indicating the nature of the playback speed, in bmp
(beat per minute), which is the length of each beat.Its value range is 6 ~ 600
. The default value is 60, which means that the duration of one beat is 1 second. The beats of the rest
and play_note
functions are affected by this constant.
Sample Code:
sound_sensor
— Onboard Sound Sensor
sound_sensor
— Onboard Sound SensorFunction
sound_sensor.get_loudness()
Get the sound intensity detected by the sound sensor, and the return value is the volume. The value range is 0 ~ 100
.
Sample Code:
light_sensor
— Onboard Light Sensor
light_sensor
— Onboard Light SensorFunction
light_sensor.get_value()
Get the light intensity detected by the light sensor, and the return value is the intensity value of visible light. The value range is 0 ~ 100
.
Sample Code:
potentiometer
— Onboard Potentiometer
potentiometer
— Onboard PotentiometerFunction
potentiometer.get_value()
Get the current value of the potentiometer knob. The value range is 0 ~ 100
.
Sample Code:
button_a
— Onboard Button A
button_a
— Onboard Button AFunction
button_a.is_pressed()
Get the current state of button A. The result returned is True
: the button is pressed, or the False
: button is not pressed.
Sample Code:
button_b
— Onboard Button B
button_b
— Onboard Button BFunction
button_b.is_pressed()
Get the current state of button B. The result returned is True
: the button is pressed, or the False
: button is not pressed.
Sample Code:
button_c
— Onboard Button C
button_c
— Onboard Button CFunction
button_c.is_pressed()
Get the current state of button C. The result returned is True
: the button is pressed, or the False
: button is not pressed.
Sample Code:
motion_sensor
— Onboard Motion Sensor
motion_sensor
— Onboard Motion SensorMotion Sensor Introduction
As shown in the picture above, the direction of the roll and pitch are based on the right-handed screw rule.
Both roll and pitch are 0°
when the Codey is horizontally placed.
Roll range:
-90° ~ 90°
Pitch range:
-180 ° ~ 180 °
Function
motion_sensor.get_roll()
Get the roll of the Euler angle, the returned data range is -90 ~ 90
.
motion_sensor.get_pitch()
Get the pitch of the Euler angle, the returned data range is -180 ~ 180
.
motion_sensor.get_yaw()
Get the yaw of the Euler angle, The returned data range is 0 ~ 360
. Since the Codey onboard sensor is a six-axis sensor, there is no electronic compass. So in fact the yaw angle is just the integral of the Z-axis angular velocity. It has accumulated errors. If you want to get a true yaw angle, this API is not suitable for use.
motion_sensor.get_rotation(axis)
Get the angle at which the Codey rotates on the three axes, and the counterclockwise direction is the positive direction, parameter:
axis String type, with
x
,y
,z
representing the axis defined by Codey.
motion_sensor.reset_rotation(axis = "all")
The current angle of initial rotation around the three axes is 0, and the get_rotation()
will start at 0, parameter:
axis string type, with
x
,y
,z
representing the axis defined by Codey, andall
representing all three axes. This is also the default value for this function.
motion_sensor.is_shaked()
Check if the Codey is shaken, the return value is a Boolean value, where True
means that Codey is shaken, and False
means that Codey is not shaken.
motion_sensor.get_shake_strength()
If the Codey is shaken, this function can obtain the intensity of the shaking. The value of the return value range is 0 ~ 100
. The larger the value, the greater the intensity of the shaking.
motion_sensor.is_tilted_left()
Check if Codey is tilted to the left, and the return value is a Boolean value, where True
means that Codey is tilted to the left, and False
means that Codey is not tilted to the left.
motion_sensor.is_tilted_right()
Check if Codey is tilted to the right, and the return value is a Boolean value, where True
means that Codey is tilted to the right, and False
means that Codey is not tilted to the right.
motion_sensor.is_ears_up()
Check if the ear of Codey is up, the return value is a Boolean value, where True
means that the ear of Codey is facing up, and False
means that the ear of Codey is not facing up.
motion_sensor.is_ears_down()
Check if the ear of Codey is down, the return value is a Boolean value, where True
means that the ear of Codey is facing down, and False
means that the ear of Codey is not facing down.
motion_sensor.is_display_up()
Check if the face panel of Codey is up, the return value is a Boolean value, where True
means that the panel of Codey is facing up, and False
means that the panel of Codey is not facing up.
motion_sensor.is_display_down()
Check if the face panel of Codey is down, the return value is a Boolean value, where True
means that the panel of Codey is facing down, and False
means that the panel of Codey is not facing down.
motion_sensor.is_upright()
Check if Codey is upright, the return value is a Boolean value, where True
means that Codey is upright, and False
means that Codey is not upright.
motion_sensor.get_acceleration(axis)
Get the acceleration values of the three axes in m/s^2
, Parameters:
axis String type, with
x
,y
,z
representing the axis defined by Codey.
motion_sensor.get_gyroscope(axis)
Get the angular velocity values of the three axes in °/sec
, Parameters:
axis String type, with
x
,y
,z
representing the axis defined by Codey。
Sample Code 1:
Sample Code 2:
Sample Code 3:
ir
— Onboard Infrared Transceiver
ir
— Onboard Infrared TransceiverFunction
ir.receive()
Returns the string information received by the infrared receiver, so the data sent by the infrared sender must end with \n
. If it is a remote control command that receives the NEC encoding protocol, use another function receive_remote_code()
.
ir.receive_remote_code()
Get the date from infrared remote controller. The data contains two parts: address and content, so it returns a list data which length 2. The first parameter is the address code, and the latter parameter is the data code.
ir.send(str)
Send infrared string, parameters:
str The string data to be emitted, the function send will add the
\n
terminator at the end of the string automatically.
ir.start_learning()
Start infrared learning and only support remote control commands that learn the standard NEC protocol.
ir.stop_learning()
Stop infrared learning.
ir.save_learned_result(index)
Save the learned infrared coding result to the corresponding area, parameters:
index the value range is 0 ~ 15, a total of 16 storage areas.
ir.send_learned_result(index = 1)
Send infrared learning saved infrared code, the learning result of the area with index = 1 is set as default, parameters:
index The index value range is
0 ~ 15
, a total of 16 storage areas.
ir.learn(time = 3)
Infrared learning time
seconds, after calling this API will save the infrared information learned in time
seconds. Default saved to the area with index = 1, parameter:
time learning time, in
seconds
.
Sample Code 1:
Sample Code 2:
wifi
— Onboard Wi-Fi
wifi
— Onboard Wi-FiFunction
wifi.start(ssid = "wifi_ssid", password = "password", mode = codey.wifi.STA)
Start wifi connection, the API will not block process, API exit does not mean that wifi is connected, you need to call wifi.is_connected()
to judge, Parameter:
ssid string type, Wi-Fi account.
password string type, Wi-Fi password.
mode starts the Wi-Fi mode.
wifi.is_connected()
Check if Wi-Fi is connected, the return value is Boolean, where True
means that wifi has established a connection, False
means that wifi has not yet established a connection.
Constant
wifi.STA
The Wi-Fi station mode, that is, the wireless adapter mode. In this mode, wifi can be connected to the router.
wifi.AP
Wireless access point mode, the general wireless routing / bridge works in this mode. In this mode, it allows other wireless devices to access.
wifi.APSTA
The Wi-Fi AP and STA modes coexist.
Sample Code:
battery
— Built-in Lithium Battery
battery
— Built-in Lithium BatteryFunction
battery.get_voltage()
Get the current battery voltage, the return value is a floating point data. The unit is V
.
battery.get_percentage()
Get the percentage of remaining battery power. The return value is an integer. The data range is 0 ~ 100
, where 100 means there is still 100% of the remaining battery.
Sample Code:
codey_timer
— Counter
codey_timer
— CounterFunction
codey.get_timer()
Gets the current value of the timer (the timer runs from the time the user script starts), the return value is a floating point data. The unit is seconds
.
codey.reset_timer()
Initialize the value of the timer.
Sample Code:
codey_broadcast
— Broadcast Module
codey_broadcast
— Broadcast ModuleFunction
codey.broadcast(str)
A broadcast can be sent to the serial port, Bluetooth, and its own event monitoring unit, Parameter:
str The content of the broadcast.
Sample Code:
codey_external_module_detect
— External Module Access Detection
codey_external_module_detect
— External Module Access DetectionFunction
codey.has_neuron_connected()
Check whether there is any neuron module connected to Codey, and the return value is a Boolean value, where True
indicates that the neuron module is connected to Codey (including the connecting of the Rocky), and False
indicates that there is no neuron module connected to the Codey.
codey.is_rocky_connected()
Check if the Rocky is connected to the Codey, the return value is a Boolean value, where True
means that there is a Rocky connected to the Codey, and False
means no Rocky connected to Codey.
Sample Code:
codey_script_control
— Script/Thread Control
codey_script_control
— Script/Thread ControlFunction
codey.stop_this_script()
Stop the current script, consistent with the scratch stop script feature.
codey.stop_other_scripts()
Stop other scripts, consistent with the scratch stop other scripts feature.
codey.stop_this_script()
Stop all scripts, consistent with the scratch stop all scripts.
Sample Code:
event
— Event Processing Unit
event
— Event Processing UnitEvent Processing Unit Introduction
The way user events are used currently supports two ways of writing. One is registration:
The other is to use a decorator, such as:
Function
event.start(callback)
Startup event.
event.shaked(callback)
Codey was shaken event.
event.received(callback, msgstr)
Broadcast reception detection event. In addition to the callback parameter, the parameter:
msgstr string type, the string to be matched. The event will be triggered when the received string matches the matching string.
event.button_a_pressed(callback)
Button A pressed event.
event.button_b_pressed(callback)
Button B pressed event.
event.button_c_pressed(callback)
Button C pressed event.
event.tilted_left(callback)
Codey left-tilted event.
event.tilted_right(callback)
Codey right-tilted event.
event.ears_up(callback)
Codey ears-up event.
event.ears_down(callback)
Codey ears-down event.
event.ir_received(callback, ir_str)
Infrared string reception detection event. In addition to the callback parameter, the parameter:
ir_str string type, the string to be matched. The event will be triggered when the received string matches the matching string.
event.greater_than(callback, threshold, type_str)
The threshold comparison event, which will be triggered when the threshold is exceeded. In addition to the callback parameter, the parameter:
threshold value data, set the threshold for triggering.
type_str string data, currently only supports
sound_sensor
: volume sensor,timer
: timer.
event.less_than(callback, threshold, type_str)
Threshold comparison event, triggered below the threshold, in addition to the callback parameter, the parameter:
threshold value data, set the threshold for triggering
type_str string data, currently only supports
light_sensor
: light sensor.
Sample Code:
Last updated