Python API for Neuron Extension Modules
dc_motor_driver
– Dual DC Motor Driver
dc_motor_driver
– Dual DC Motor DriverFunction
dc_motor_driver.set_power(speed, ch = 0)
Set power for the motor driver in each channel, parameters:
speed Refers to power value of the motor controlled, the parameter range is
-100 ~ 100
.ch Refers to channel number of the motor controlled, the parameter range is
0 ~ 2
, and0
: stands for both slots,1
: for slot 1 channel,2: for slot 2 channel.
Sample Code:
servo_driver
– Dual Servo Driver
servo_driver
– Dual Servo DriverFunction
servo_driver.set_angle(position, ch = 0)
set power for the servo driver in each channel, parameters:
position Refers to turning angle value of the servo controlled, the parameter range is
0 ~ 180
.ch Refers to channel number servo controlled, the parameter range is
0 ~ 2
, and0
: stands for both slots,1
: for slot 1 channel,2: for slot 2 channel.
Sample Code:
led_strip
– LED Strip Driver
led_strip
– LED Strip DriverFunction
led_strip.set_single(index, red_value, green_value, blue_value)
Set color of each light on the LED Strip, parameters:
index Set the light order No, the parameter range is
1 ~ the value of total lights on the LED Strip
。red_value Set LED red value, the parameter range is
0 ~ 255
, 0 means no red color, 255 means the brightest red color.green_value Set LED green value, the parameter range is
0 ~ 255
, 0 means no green color, 255 means the brightest green color.blue_value Set LED blue value, the parameter range is
0 ~ 255
, 0 means no blue color, 255 means the brightest blue color.
led_strip.set_all(red_value, green_value, blue_value)
Set color for all lights, parameters:
red_value Set LED red value, the parameter range is
0 ~ 255
, 0 means no red color, 255 means the brightest red color.green_value Set LED green value, the parameter range is
0 ~ 255
, 0 means no green color, 255 means the brightest green color.blue_value Set LED blue value, the parameter range is
0 ~ 255
, 0 means no blue color, 255 means the brightest blue color.
led_strip.set_effect(mode, speed, list)
Set effect of the LED Strip, parameters:
mode effect mode, the parameter range is
0 ~ 5
0: means static mode: lights status keep the last setting。
4: means breathing mode: the lights change at the speed of human breath, that is they turn on/off each three seconds.
5: means gradient mode: all lights on the strip change their color gradually to the new setting color in a specific setting time.
speed dynamic change speed, the parameter range is
0 ~ 8
, 0 means the slowest speed and 8 is the fastest(It only works when there is dynamic change setting of lights status).list changeable parameter list, the parameter range is
0 ~ 8
,the first parameter means the first light color, the second parameter means the second light color, and so on; And color parameters are as below:black(0x00)
,red(0x01)
,orange(0x02)
,yellow(0x03)
,green(0x04)
,cray(0x05)
,blue(0x06)
,purple(0x07)
andwhile(0x08)
.
Sample Code:
led_panel
– RGB LED Panel
led_panel
– RGB LED PanelFunction
led_panel.set_all(red_value, green_value, blue_value)
Set and display color of all lights on the panel, parameters:
red_value Set LED red value, the parameter range is
0 ~ 255
, 0 means no red color, 255 means the brightest red color.green_value Set LED green value, the parameter range is
0 ~ 255
, 0 means no green color, 255 means the brightest green color.blue_value Set LED blue value, the parameter range is
0 ~ 255
, 0 means no blue color, 255 means the brightest blue color.
led_panel.set_pixel(x, y, red_value, green_value, blue_value)
Set color for each pixel on the panel, parameters:
x pixel’s X position on the panel, the parameter range is
0 ~ 7
.y pixel’s Y position on the panel, the parameter range is
0 ~ 7
.red_value Set LED red value, the parameter range is
0 ~ 255
, 0 means no red color, 255 means the brightest red color.green_value Set LED green value, the parameter range is
0 ~ 255
, 0 means no green color, 255 means the brightest green color.blue_value Set LED blue value, the parameter range is
0 ~ 255
, 0 means no blue color, 255 means the brightest blue color.
led_panel.show_image(list, mode = 0)
Set the display content as image parameter mode, parameters:
list changeable parameter list, the parameter range is
0 ~ 8
,the first parameter means the first light color, the second parameter means the second light color, and so on; And color parameters are as below:black(0x00)
,red(0x01)
,orange(0x02)
,yellow(0x03)
,green(0x04)
,cray(0x05)
,blue(0x06)
,purple(0x07)
andwhile(0x08)
.mode contents displaying mode, the parameter range is
0 ~ 3
0:means emerging mode, setting image will display directly.
1:means erase mode, original image disappear gradually and new setting image will display gradually and vertically.
2:means left moving mode, original image moves to the left and disappear gradually and new setting image will move to the left until display the whole image.
3:means right moving mode, original image moves to the right and disappear gradually and new setting image will move to the right until display the whole image.
led_panel.set_animation(frame_index, list)
Set the animation content on the panel, parameters:
frame_index index of the animation frame, the parameter range is
0 ~ 3
; 0 mean the first frame, 1 means the second, and so on.list changeable parameter list, the parameter range is
0 ~ 8
,the first parameter means the first light color, the second parameter means the second light color, and so on; And color parameters are as below:black(0x00)
,red(0x01)
,orange(0x02)
,yellow(0x03)
,green(0x04)
,cray(0x05)
,blue(0x06)
,purple(0x07)
andwhile(0x08)
.
led_panel.show_animation(frame_speed, mode)
Show the animation frame setting by set_animation
, parameters:
frame_speed switch speed of the animation frame content, the parameter range is
0 ~ 2
0:means slow speed that the animation frame rolls every one second
1:means normal speed that the animation frame rolls every half second
2:means fast speed that the animation frame rolls every 0.2 second.
mode frame change mode, the parameter range is
0 ~ 3
0:means emerging mode, setting image will display directly.
1:means erase mode, original image disappear gradually and new setting image will display gradually and vertically.
2:means left moving mode, original image moves to the left and disappear gradually and new setting image will move to the left until display the whole image.
3:means right moving mode, original image moves to the right and disappear gradually and new setting image will move to the right until display the whole image.
led_panel.show_string(red_value, green_value, blue_value, list)
Display the string as the setting color, parameters:
red_value Set LED red value, the parameter range is
0 ~ 255
, 0 means no red color, 255 means the brightest red color.green_value Set LED green value, the parameter range is
0 ~ 255
, 0 means no green color, 255 means the brightest green color.blue_value Set LED blue value, the parameter range is
0 ~ 255
, 0 means no blue color, 255 means the brightest blue color.list changeable parameter list, the first character, the second character, the third character…
led_panel.clear()
Clear the display of the panel.
Sample Code:
buzzer
– Buzzer
buzzer
– BuzzerFunction
buzzer.play_note(note_num, beat = None)
Play note, digital note definitions please refer to: scratch digital note description, 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:
buzzer.play_tone(frequency, time = None)
Play the tone of setting frequency, prameters:
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
.
buzzer.rest(number)
Stop the beat, parameters:
number Numerical data, the number of paused beats, its value range is
0 ~ the value range limit
.
Constant
buzzer.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:
button
– Button
button
– ButtonFunction
button.is_pressed()
Get current status of button; the result will be True
: button pressed or False
: button is not pressed.
Sample Code:
funny_touch
– Funny Touch
funny_touch
– Funny TouchFunny Touch User Guide
Funny touch can be connected to any conductive object (such as bananas and water) and turn it into a touch switch. A simple and interesting interactive effect can be achieved by detecting the conducting state between funny switches and GND wire.
How to use?
Plug the funny switch to slot 1 and the GND wire to slot 2.
Clip a funny switch to a conductive object.
Hold the metal clip of the GND wire and touch the conductive object with the other hand, the relevant indicator will light up and the block will send out an on signal.
Note: Alligator clip is sharp, please do not clip yourself with the funny switch or the clip of GND wire, it may hurt you.
Function
funny_touch.is_red_touched()
Whether the red clip is touched or not, result will be True
: yes, it is touched, or False
: no, it isn’t touched.
funny_touch.is_green_touched()
Whether the green clip is touched or not, result will be True
: yes, it is touched, or False
: no, it isn’t touched.
funny_touch.is_yellow_touched()
Whether the yellow clip is touched or not, result will be True
: yes, it is touched, or False
: no, it isn’t touched.
funny_touch.is_blue_touched()
Whether the blue clip is touched or not, result will be True
: yes, it is touched, or False
: no, it isn’t touched.
Sample Code:
ultrasonic_sensor
– Ultrasonic Sensor
ultrasonic_sensor
– Ultrasonic SensorFunction
ultrasonic_sensor.get_distance()
Get the distance (cm
) between the obstacle ahead and ultrasonic sensor; the result is floating point, ranging 0 ~ 300
cm; but measure distance ranges 3 ~ 300
cm as detection is not exact enough within 3 cm.
Sample Code:
gyro_sensor
– Gyro Sensor User Guide
gyro_sensor
– Gyro Sensor User GuideRefer to the picture below for Gyro module coordinate system:
Function
gyro_sensor.get_roll()
Get the roll of the Euler angle, the returned data range is -90 ~ 90
.
gyro_sensor.get_pitch()
Get the pitch of the Euler angle, the returned data range is -180 ~ 180
.
gyro_sensor.get_yaw()
Get the yaw of the Euler angle, The returned data range is -32768 ~ 32767
,Since the gyro 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.
gyro_sensor.is_shaked()
Check if the gyro sensor is shaken, the return value is a Boolean value, where True
means that gyro sensor is shaken, and False
means that gyro sensor is not shaken.
gyro_sensor.get_acceleration(axis)
Get the acceleration values of the three axes in g
, Parameters:
axis String type, with
x
,y
,z
representing the axis defined by gyro sensor.
gyro_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 gyro sensor.
Sample Code 1:
Sample Code 2:
Sample Code 3:
pir_sensor
– PIR Sensor
pir_sensor
– PIR SensorFunction
pir_sensor.is_activated()
Get the detecting result from the sensor. Result will be True
: it detects human nearby or False
: it doesn’t detect human nearby.
Sample Code:
soil_moisture
– Soil Moisture
soil_moisture
– Soil MoistureFunction
soil_moisture.get_value()
Get humidity of soil detected, ranging 0 ~ 100
; the higher value is, the higher humidity is.
Sample Code:
Last updated