2016-11-30 06:55:37 +01:00
# include "pxt.h"
2016-04-02 02:45:18 +02:00
enum class Button {
A = MICROBIT_ID_BUTTON_A ,
B = MICROBIT_ID_BUTTON_B ,
//% block="A+B"
AB = MICROBIT_ID_BUTTON_AB ,
} ;
2022-03-22 17:36:19 +01:00
enum class ButtonEvent {
//% blockIdentity="input.buttonEventValueId"
//% block="pressed down"
Down = MICROBIT_BUTTON_EVT_DOWN ,
//% blockIdentity="input.buttonEventValueId"
//% block="released up"
Up = MICROBIT_BUTTON_EVT_UP ,
//% blockIdentity="input.buttonEventValueId"
//% block="clicked"
Click = MICROBIT_BUTTON_EVT_CLICK ,
//% blockIdentity="input.buttonEventValueId"
//% block="long clicked"
LongClick = MICROBIT_BUTTON_EVT_LONG_CLICK ,
} ;
2016-04-02 02:45:18 +02:00
enum class Dimension {
//% block=x
X = 0 ,
//% block=y
Y = 1 ,
//% block=z
Z = 2 ,
//% block=strength
Strength = 3 ,
} ;
enum class Rotation {
//% block=pitch
Pitch = 0 ,
//% block=roll
Roll = 1 ,
} ;
enum class TouchPin {
2017-01-31 22:36:32 +01:00
P0 = MICROBIT_ID_IO_P12 ,
P1 = MICROBIT_ID_IO_P0 ,
P2 = MICROBIT_ID_IO_P1 ,
P3 = MICROBIT_ID_IO_P16
2016-04-02 02:45:18 +02:00
} ;
enum class AcceleratorRange {
/**
* The accelerator measures forces up to 1 gravity
*/
//% block="1g"
OneG = 1 ,
/**
* The accelerator measures forces up to 2 gravity
*/
//% block="2g"
TwoG = 2 ,
/**
* The accelerator measures forces up to 4 gravity
*/
//% block="4g"
FourG = 4 ,
/**
* The accelerator measures forces up to 8 gravity
*/
//% block="8g"
EightG = 8
} ;
enum class Gesture {
/**
* Raised when shaken
*/
//% block=shake
2019-12-02 05:58:26 +01:00
//% jres=gestures.shake
2016-05-12 21:35:40 +02:00
Shake = MICROBIT_ACCELEROMETER_EVT_SHAKE ,
2016-04-02 02:45:18 +02:00
/**
* Raised when the logo is upward and the screen is vertical
*/
//% block="logo up"
2019-12-02 05:58:26 +01:00
//% jres=gestures.tiltforward
2016-05-12 21:35:40 +02:00
LogoUp = MICROBIT_ACCELEROMETER_EVT_TILT_UP ,
2016-04-02 02:45:18 +02:00
/**
* Raised when the logo is downward and the screen is vertical
*/
//% block="logo down"
2019-12-02 05:58:26 +01:00
//% jres=gestures.tiltbackwards
2016-05-12 21:35:40 +02:00
LogoDown = MICROBIT_ACCELEROMETER_EVT_TILT_DOWN ,
2016-04-02 02:45:18 +02:00
/**
2020-05-08 18:35:38 +02:00
* Raised when the screen is pointing up and the board is horizontal
2016-04-02 02:45:18 +02:00
*/
//% block="screen up"
2019-12-02 05:58:26 +01:00
//% jres=gestures.frontsideup
2016-05-12 21:35:40 +02:00
ScreenUp = MICROBIT_ACCELEROMETER_EVT_FACE_UP ,
2016-04-02 02:45:18 +02:00
/**
2020-05-08 18:35:38 +02:00
* Raised when the screen is pointing down and the board is horizontal
2016-04-02 02:45:18 +02:00
*/
//% block="screen down"
2019-12-02 05:58:26 +01:00
//% jres=gestures.backsideup
2016-05-12 21:35:40 +02:00
ScreenDown = MICROBIT_ACCELEROMETER_EVT_FACE_DOWN ,
2016-04-02 02:45:18 +02:00
/**
* Raised when the screen is pointing left
*/
//% block="tilt left"
2019-12-02 05:58:26 +01:00
//% jres=gestures.tiltleft
2016-05-12 21:35:40 +02:00
TiltLeft = MICROBIT_ACCELEROMETER_EVT_TILT_LEFT ,
2016-04-02 02:45:18 +02:00
/**
* Raised when the screen is pointing right
*/
//% block="tilt right"
2019-12-02 05:58:26 +01:00
//% jres=gestures.tiltright
2016-10-11 22:48:25 +02:00
TiltRight = MICROBIT_ACCELEROMETER_EVT_TILT_RIGHT ,
2016-04-02 02:45:18 +02:00
/**
* Raised when the board is falling !
*/
//% block="free fall"
2019-12-02 05:58:26 +01:00
//% jres=gestures.freefall
2016-05-20 14:39:44 +02:00
FreeFall = MICROBIT_ACCELEROMETER_EVT_FREEFALL ,
/**
* Raised when a 3 G shock is detected
*/
//% block="3g"
2019-12-02 05:58:26 +01:00
//% jres=gestures.impact3g
2016-05-20 14:39:44 +02:00
ThreeG = MICROBIT_ACCELEROMETER_EVT_3G ,
/**
* Raised when a 6 G shock is detected
*/
//% block="6g"
2019-12-02 05:58:26 +01:00
//% jres=gestures.impact6g
2017-12-14 20:00:47 +01:00
SixG = MICROBIT_ACCELEROMETER_EVT_6G ,
/**
* Raised when a 8 G shock is detected
*/
//% block="8g"
2019-12-02 05:58:26 +01:00
//% jres=gestures.impact8g
2017-12-14 20:00:47 +01:00
EightG = MICROBIT_ACCELEROMETER_EVT_8G
2016-04-02 02:45:18 +02:00
} ;
2016-04-02 06:26:06 +02:00
2017-12-14 20:00:47 +01:00
enum class MesDpadButtonInfo {
//% block="A down"
ADown = MES_DPAD_BUTTON_A_DOWN ,
//% block="A up"
AUp = MES_DPAD_BUTTON_A_UP ,
//% block="B down"
BDown = MES_DPAD_BUTTON_B_DOWN ,
//% block="B up"
BUp = MES_DPAD_BUTTON_B_UP ,
//% block="C down"
CDown = MES_DPAD_BUTTON_C_DOWN ,
//% block="C up"
CUp = MES_DPAD_BUTTON_C_UP ,
//% block="D down"
DDown = MES_DPAD_BUTTON_D_DOWN ,
//% block="D up"
DUp = MES_DPAD_BUTTON_D_UP ,
//% block="1 down"
_1Down = MES_DPAD_BUTTON_1_DOWN ,
//% block="1 up"
_1Up = MES_DPAD_BUTTON_1_UP ,
//% block="2 down"
_2Down = MES_DPAD_BUTTON_2_DOWN ,
//% block="2 up"
_2Up = MES_DPAD_BUTTON_2_UP ,
//% block="3 down"
_3Down = MES_DPAD_BUTTON_3_DOWN ,
//% block="3 up"
_3Up = MES_DPAD_BUTTON_3_UP ,
//% block="4 down"
_4Down = MES_DPAD_BUTTON_4_DOWN ,
//% block="4 up"
_4Up = MES_DPAD_BUTTON_4_UP ,
} ;
//% color=#B4009E weight=99 icon="\uf192"
2016-04-02 06:26:06 +02:00
namespace input {
2022-03-22 17:36:19 +01:00
/**
* Do something when a button ( A , B or both A + B ) receives an event .
* @ param button the button
* @ param body code to run when event is raised
* @ param eventType event Type
*/
//% help=input/on-button-event weight=100 blockGap=16
//% blockId=device_button_selected_event block="on button %NAME| %eventType=control_button_event_value_id"
//% parts="buttonpair"
//% group="Events"
void onButtonEvent ( Button button , int eventType , Action body ) {
registerWithDal ( ( int ) button , eventType , body ) ;
}
2016-04-02 06:26:06 +02:00
/**
2017-12-14 20:00:47 +01:00
* Do something when a button ( A , B or both A + B ) is pushed down and released again .
* @ param button the button that needs to be pressed
* @ param body code to run when event is raised
2016-04-02 06:26:06 +02:00
*/
2019-12-02 05:58:26 +01:00
//% help=input/on-button-pressed weight=85 blockGap=16
2017-01-20 05:55:31 +01:00
//% blockId=device_button_event block="on button|%NAME|pressed"
2016-08-22 17:48:48 +02:00
//% parts="buttonpair"
2022-03-22 17:36:19 +01:00
//% deprecated=true
//% group="Events"
2016-04-02 06:26:06 +02:00
void onButtonPressed ( Button button , Action body ) {
registerWithDal ( ( int ) button , MICROBIT_BUTTON_EVT_CLICK , body ) ;
}
/**
2016-09-26 18:59:52 +02:00
* Do something when when a gesture is done ( like shaking the micro : bit ) .
2017-12-14 20:00:47 +01:00
* @ param gesture the type of gesture to track , eg : Gesture . Shake
* @ param body code to run when gesture is raised
2016-04-02 06:26:06 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/on-gesture weight=98 blockGap=16
2017-01-20 05:55:31 +01:00
//% blockId=device_gesture_event block="on |%NAME"
2016-08-22 17:48:48 +02:00
//% parts="accelerometer"
2019-12-02 05:58:26 +01:00
//% NAME.fieldEditor="gestures" NAME.fieldOptions.columns=4
2022-03-22 17:36:19 +01:00
//% group="Events"
2016-04-02 06:26:06 +02:00
void onGesture ( Gesture gesture , Action body ) {
2017-12-14 20:00:47 +01:00
int gi = ( int ) gesture ;
if ( gi = = MICROBIT_ACCELEROMETER_EVT_3G & & uBit . accelerometer . getRange ( ) < 3 )
uBit . accelerometer . setRange ( 4 ) ;
else if ( ( gi = = MICROBIT_ACCELEROMETER_EVT_6G | | gi = = MICROBIT_ACCELEROMETER_EVT_8G ) & & uBit . accelerometer . getRange ( ) < 6 )
2016-10-11 22:48:25 +02:00
uBit . accelerometer . setRange ( 8 ) ;
2017-12-14 20:00:47 +01:00
registerWithDal ( MICROBIT_ID_GESTURE , gi , body ) ;
2016-04-02 06:26:06 +02:00
}
2019-12-02 05:58:26 +01:00
/**
* Tests if a gesture is currently detected .
* @ param gesture the type of gesture to detect , eg : Gesture . Shake
*/
2022-03-22 17:36:19 +01:00
//% help=input/is-gesture weight=86 blockGap=8
2019-12-02 05:58:26 +01:00
//% blockId=deviceisgesture block="is %gesture gesture"
//% parts="accelerometer"
//% gesture.fieldEditor="gestures" gesture.fieldOptions.columns=4
2022-03-22 17:36:19 +01:00
//% group="States"
2019-12-02 05:58:26 +01:00
bool isGesture ( Gesture gesture ) {
// turn on acceleration
uBit . accelerometer . getX ( ) ;
int gi = ( int ) gesture ;
return uBit . accelerometer . getGesture ( ) = = gi ;
}
2022-03-22 17:36:19 +01:00
/**
* Do something when a pin receives an touch event ( while also touching the GND pin ) .
* @ param name the pin , eg : TouchPin . P0
* @ param body the code to run when event is fired on pin
*/
//% help=input/on-pin-touch weight=99 blockGap=16
//% blockId=device_pin_custom_event block="on pin %name| %eventType=control_button_event_value_id"
//% group="Events"
void onPinTouchEvent ( TouchPin name , int eventType , Action body ) {
auto pin = getPin ( ( int ) name ) ;
if ( ! pin ) return ;
// Forces the PIN to switch to makey-makey style detection.
pin - > isTouched ( ) ;
registerWithDal ( ( int ) name , eventType , body ) ;
}
2016-08-22 17:48:48 +02:00
/**
2017-12-14 20:00:47 +01:00
* Do something when a pin is touched and released again ( while also touching the GND pin ) .
* @ param name the pin that needs to be pressed , eg : TouchPin . P0
2016-08-09 00:23:18 +02:00
* @ param body the code to run when the pin is pressed
2016-04-02 06:26:06 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/on-pin-pressed weight=83 blockGap=16
2017-12-14 20:00:47 +01:00
//% blockId=device_pin_event block="on pin %name|pressed"
2022-03-22 17:36:19 +01:00
//% group="Events"
//% deprecated=true
2016-04-02 06:26:06 +02:00
void onPinPressed ( TouchPin name , Action body ) {
auto pin = getPin ( ( int ) name ) ;
if ( ! pin ) return ;
// Forces the PIN to switch to makey-makey style detection.
pin - > isTouched ( ) ;
registerWithDal ( ( int ) name , MICROBIT_BUTTON_EVT_CLICK , body ) ;
}
2016-08-09 00:23:18 +02:00
/**
* Do something when a pin is released .
2017-12-14 20:00:47 +01:00
* @ param name the pin that needs to be released , eg : TouchPin . P0
2016-08-09 00:23:18 +02:00
* @ param body the code to run when the pin is released
*/
2019-12-02 05:58:26 +01:00
//% help=input/on-pin-released weight=6 blockGap=16
2017-01-20 05:55:31 +01:00
//% blockId=device_pin_released block="on pin %NAME|released"
2016-10-11 22:48:25 +02:00
//% advanced=true
2022-03-22 17:36:19 +01:00
//% group="Events"
//% deprecated=true
2016-08-09 00:23:18 +02:00
void onPinReleased ( TouchPin name , Action body ) {
auto pin = getPin ( ( int ) name ) ;
if ( ! pin ) return ;
// Forces the PIN to switch to makey-makey style detection.
pin - > isTouched ( ) ;
registerWithDal ( ( int ) name , MICROBIT_BUTTON_EVT_UP , body ) ;
}
2016-04-02 06:26:06 +02:00
/**
* Get the button state ( pressed or not ) for ` ` A ` ` and ` ` B ` ` .
2017-12-14 20:00:47 +01:00
* @ param button the button to query the request , eg : Button . A
2016-04-02 06:26:06 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/button-is-pressed weight=89
2016-04-02 06:26:06 +02:00
//% block="button|%NAME|is pressed"
//% blockId=device_get_button2
2019-12-02 05:58:26 +01:00
//% icon="\uf192" blockGap=8
2016-08-22 17:48:48 +02:00
//% parts="buttonpair"
2022-03-22 17:36:19 +01:00
//% group="States"
2016-04-02 06:26:06 +02:00
bool buttonIsPressed ( Button button ) {
if ( button = = Button : : A )
return uBit . buttonA . isPressed ( ) ;
else if ( button = = Button : : B )
return uBit . buttonB . isPressed ( ) ;
else if ( button = = Button : : AB )
return uBit . buttonAB . isPressed ( ) ;
return false ;
}
2016-07-26 22:51:41 +02:00
/**
* Get the pin state ( pressed or not ) . Requires to hold the ground to close the circuit .
2017-12-14 20:00:47 +01:00
* @ param name pin used to detect the touch , eg : TouchPin . P0
2016-07-26 22:51:41 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/pin-is-pressed weight=87
2017-01-20 05:55:31 +01:00
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed"
2016-07-26 22:51:41 +02:00
//% blockGap=8
2022-03-22 17:36:19 +01:00
//% group="States"
2016-07-26 22:51:41 +02:00
bool pinIsPressed ( TouchPin name ) {
auto pin = getPin ( ( int ) name ) ;
return pin & & pin - > isTouched ( ) ;
}
2016-04-02 06:26:06 +02:00
int getAccelerationStrength ( ) {
double x = uBit . accelerometer . getX ( ) ;
double y = uBit . accelerometer . getY ( ) ;
double z = uBit . accelerometer . getZ ( ) ;
return ( int ) sqrt ( x * x + y * y + z * z ) ;
2017-12-14 20:00:47 +01:00
}
2016-04-02 06:26:06 +02:00
/**
* Get the acceleration value in milli - gravitys ( when the board is laying flat with the screen up , x = 0 , y = 0 and z = - 1024 )
2019-12-02 05:58:26 +01:00
* @ param dimension x , y , or z dimension , eg : Dimension . X
2016-04-02 06:26:06 +02:00
*/
2017-01-20 05:55:31 +01:00
//% help=input/acceleration weight=58
2016-04-02 06:26:06 +02:00
//% blockId=device_acceleration block="acceleration (mg)|%NAME" blockGap=8
2016-08-22 17:48:48 +02:00
//% parts="accelerometer"
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2016-04-02 06:26:06 +02:00
int acceleration ( Dimension dimension ) {
switch ( dimension ) {
case Dimension : : X : return uBit . accelerometer . getX ( ) ;
case Dimension : : Y : return uBit . accelerometer . getY ( ) ;
case Dimension : : Z : return uBit . accelerometer . getZ ( ) ;
case Dimension : : Strength : return getAccelerationStrength ( ) ;
}
return 0 ;
}
/**
2016-04-13 17:55:20 +02:00
* Reads the light level applied to the LED screen in a range from ` ` 0 ` ` ( dark ) to ` ` 255 ` ` bright .
2016-04-02 06:26:06 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/light-level weight=59
2017-01-20 05:55:31 +01:00
//% blockId=device_get_light_level block="light level" blockGap=8
2016-08-22 17:48:48 +02:00
//% parts="ledmatrix"
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2016-04-02 06:26:06 +02:00
int lightLevel ( ) {
return uBit . display . readLightLevel ( ) ;
}
2022-03-22 17:36:19 +01:00
/**
* gets the level of loudness from 0 ( silent ) to 255 ( loud )
*/
//% blockId="soundLevel" weight=58
//% block="soundLevel" blockGap=8
//% group="Sensors"
int soundLevel ( ) {
int level = uBit . io . P21 . getAnalogValue ( ) ;
int min = level ;
int max = level ;
for ( int i = 0 ; i < 32 ; i + + ) {
level = uBit . io . P21 . getAnalogValue ( ) ;
if ( level > max ) {
max = level ;
} else if ( level < min ) {
min = level ;
}
}
level = floor ( ( max - min + 0.5 ) / 4 ) ; //max can be up to 1023; + 0,5 to prevent division by 0, floor to get rid of decimals, divide by 4 to get a value between 0 and 255
return level ;
}
2016-10-12 04:36:00 +02:00
/**
* Get the current compass heading in degrees .
*/
//% help=input/compass-heading
2017-01-20 05:55:31 +01:00
//% weight=56
2016-10-12 04:36:00 +02:00
//% blockId=device_heading block="compass heading (°)" blockGap=8
//% parts="compass"
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2016-10-12 04:36:00 +02:00
int compassHeading ( ) {
return uBit . compass . heading ( ) ;
}
/**
* Gets the temperature in Celsius degrees ( ° C ) .
*/
2022-03-22 17:36:19 +01:00
//% weight=57
2016-10-12 04:36:00 +02:00
//% help=input/temperature
//% blockId=device_temperature block="temperature (°C)" blockGap=8
//% parts="thermometer"
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2016-10-12 04:36:00 +02:00
int temperature ( ) {
return uBit . thermometer . getTemperature ( ) ;
}
2016-04-02 06:26:06 +02:00
/**
2016-09-26 18:59:52 +02:00
* The pitch or roll of the device , rotation along the ` ` x - axis ` ` or ` ` y - axis ` ` , in degrees .
2019-12-02 05:58:26 +01:00
* @ param kind pitch or roll
2016-04-02 06:26:06 +02:00
*/
2016-08-10 22:10:40 +02:00
//% help=input/rotation weight=52
2017-01-20 05:55:31 +01:00
//% blockId=device_get_rotation block="rotation (°)|%NAME" blockGap=8
2016-10-12 04:36:00 +02:00
//% parts="accelerometer" advanced=true
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2016-04-02 06:26:06 +02:00
int rotation ( Rotation kind ) {
switch ( kind ) {
case Rotation : : Pitch : return uBit . accelerometer . getPitch ( ) ;
case Rotation : : Roll : return uBit . accelerometer . getRoll ( ) ;
}
2016-10-11 22:48:25 +02:00
return 0 ;
2016-04-02 06:26:06 +02:00
}
/**
* Get the magnetic force value in ` ` micro - Teslas ` ` ( ` ` µ T ` ` ) . This function is not supported in the simulator .
2019-12-02 05:58:26 +01:00
* @ param dimension the x , y , or z dimension , eg : Dimension . X
2016-04-02 06:26:06 +02:00
*/
2022-03-22 17:36:19 +01:00
//% help=input/magnetic-force weight=49
2017-01-20 05:55:31 +01:00
//% blockId=device_get_magnetic_force block="magnetic force (µT)|%NAME" blockGap=8
2016-08-22 17:48:48 +02:00
//% parts="compass"
2016-10-11 22:48:25 +02:00
//% advanced=true
2022-03-22 17:36:19 +01:00
//% group="Sensors"
2020-09-08 11:04:25 +02:00
TNumber magneticForce ( Dimension dimension ) {
if ( ! uBit . compass . isCalibrated ( ) )
uBit . compass . calibrate ( ) ;
double d = 0 ;
switch ( dimension ) {
case Dimension : : X : d = uBit . compass . getX ( ) ; break ;
case Dimension : : Y : d = uBit . compass . getY ( ) ; break ;
case Dimension : : Z : d = uBit . compass . getZ ( ) ; break ;
case Dimension : : Strength : d = uBit . compass . getFieldStrength ( ) ; break ;
}
return fromDouble ( d / 1000.0 ) ;
2016-04-02 06:26:06 +02:00
}
/**
* Obsolete , compass calibration is automatic .
*/
2017-12-14 20:00:47 +01:00
//% help=input/calibrate-compass advanced=true
//% blockId="input_compass_calibrate" block="calibrate compass"
2022-03-22 17:36:19 +01:00
//% weight=20 gap=8
//% group="Configuration"
2019-12-02 05:58:26 +01:00
void calibrateCompass ( ) {
uBit . compass . calibrate ( ) ;
2017-12-14 20:00:47 +01:00
}
2016-04-02 06:26:06 +02:00
2022-03-22 17:36:19 +01:00
/**
* Returns ' true ' when the compass is calibrated . Otherwise returns ' false ' .
*/
//% help=input/calibrate-compass advanced=true
//% blockId="input_compass_is_calibrated" block="is compass calibrated"
//% weight=19
//% group="System"
bool isCalibratedCompass ( ) {
return ( uBit . compass . isCalibrated ( ) = = 1 ) ;
}
/**
* Obsolete , compass calibration is automatic .
*/
//% help=input/calibrate-compass advanced=true
//% blockId="input_compass_clear_calibration" block="clear calibration compass"
//% weight=17
//% group="Configuration"
//% blockHidden=true
void clearCalibrationCompass ( ) {
uBit . compass . clearCalibration ( ) ;
}
/**
* Obsolete , compass calibration is automatic .
*/
//% help=input/calibrate-compass advanced=true
//% blockId="input_compass_assume_calibration" block="assume calibration compass"
//% weight=16
//% group="Configuration"
//% blockHidden=true
void assumeCalibrationCompass ( ) {
uBit . compass . assumeCalibration ( ) ;
}
2016-04-02 06:26:06 +02:00
/**
* Sets the accelerometer sample range in gravities .
* @ param range a value describe the maximum strengh of acceleration measured
*/
2016-07-20 11:44:32 +02:00
//% help=input/set-accelerometer-range
2017-01-20 05:55:31 +01:00
//% blockId=device_set_accelerometer_range block="set accelerometer|range %range"
2022-03-22 17:36:19 +01:00
//% weight=22 gap=8
2016-08-22 17:48:48 +02:00
//% parts="accelerometer"
2016-10-11 22:48:25 +02:00
//% advanced=true
2022-03-22 17:36:19 +01:00
//% group="Configuration"
2016-04-02 06:26:06 +02:00
void setAccelerometerRange ( AcceleratorRange range ) {
uBit . accelerometer . setRange ( ( int ) range ) ;
}
}