User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

plugins:bbc_microbit

BBC micro:bit

Taken from the official DroidScript documentation with some more texts and API documentation.

Getting Started

:!: Please make sure you have the Espruino firmware file installed on your micro:bit before trying to use this plugin. See http://www.microbit-js.org for more details.

:!: You have to install the plugin over the plugin section within DroidScript

Using the BBC micro:bit Plugin

In order to use the micro:bit, you must first load the plugin at the top of your script using the LoadPlugin method like this:

app.LoadPlugin( "MicroBit" );

Then you can create the microbit object like this:

microbit = app.CreateMicroBit();

You will need to connect to your micro:bit using Bluetooth and you can scan for it like this:

microbit = microbit.Scan();

MicroBit Methods

Using MicroBitCtrl Control

This control shows micro:bit image which shows interactive LED array and Bluetooth indicator.

In order to use the MicroBitCtrl control, you must first load the plugin and initialize MicroBit object:

//Load the MicroBit plugin.
app.LoadPlugin( "MicroBit" );
 
//Called when application is started.
function OnStart()
{
  //Lock screen orientation to Portrait.
  app.SetOrientation( "Portrait" );
 
  //Create our main layout.
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
  lay.SetBackColor( "#222222" );
 
  //Create the Microbit plugin.
  microbit = app.CreateMicroBit();
 
  //Create MicroBit controller.
  ctrl = microbit.CreateController( 0.9 );
  lay.AddChild( ctrl );
 
  //Create horizontal layout for buttons
  layButs = app.CreateLayout( "Linear", "Horizontal" );
  layButs.SetMargins( 0,0.05,0,0 );
  lay.AddChild( layButs );
 
  //Create a bluetooth connect button.
  btnConnect = app.CreateButton( "[fa-bluetooth-b]", 0.25, 0.15, "FontAwesome" );
  btnConnect.SetTextSize( 66 );
  btnConnect.SetTextColor( "#3366ff" );
  btnConnect.SetOnTouch( txtBT_OnTouchDown );
  layButs.AddChild( btnConnect );
 
  //Create clear button.
  btnClear= app.CreateButton( "[fa-times]", 0.25, 0.15, "FontAwesome" );
  btnClear.SetTextSize( 66 );
  btnClear.SetTextColor( "#ff4444" );
  btnClear.SetOnTouch( btnClear_OnClick );
  layButs.AddChild( btnClear );
 
  //Add main layout to the app.
  app.AddLayout( lay );
}
 
//Handle Bluetooth button press.
function txtBT_OnTouchDown()
{
  microbit.Scan();
}
 
//Handle 'clear' button press.
function btnClear_OnClick()
{
  ctrl.Clear();
}

MicroBitCtrl Methods

Clear() Clears LEDs to gray color.
SendLEDStates() Sends new LED states to micro:bit.
SetBluetoothState( connected ) If connected is true, then function sets Bluetooth indicator in blue color, if false, in gray color.
SetLED( x, y ) Sets LED in x and y position (range 0..4). It sets LED in red color.
ToggleLED( x, y ) Toggles LED in x and y position (range 0..4). It sets LED in red or gray color.
imgTouch_OnTouch( ev ) Custom LED array touch handler.

Examples

Send smiley face click to collapse contents

app.LoadPlugin( "MicroBit" );
 
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
  btn = app.CreateButton( "Smile" );
  btn.SetOnTouch( btn_OnTouch );
  lay.AddChild( btn );
  app.AddLayout( lay );
 
  microbit = app.CreateMicroBit();
  microbit.Scan();
}
 
function btn_OnTouch()
{
  microbit.SetLEDs( "0111010001001000010010001" );
}

Send scrolling texts

app.LoadPlugin( "MicroBit" );
 
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
  btn = app.CreateButton( "Send" );
  btn.SetOnTouch( btn_OnTouch );
  lay.AddChild( btn );
  app.AddLayout( lay );
 
  microbit = app.CreateMicroBit();
  microbit.Scan();
}
 
function btn_OnTouch()
{
  microbit.ScrollText( "Hello", 150 );
}

If you want to receive messages from your micro:bit, then you will need to create a function to be called when new messages are available (a callback function). You can set the name of your function like this:

 microbit.SetOnReceive( MyFunc );

Detect button presses click to collapse contents

app.LoadPlugin( "MicroBit" );
 
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
  txt = app.CreateText( "Press a button on your micro:bit" );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  microbit = app.CreateMicroBit();
  microbit.SetOnConnect( OnConnect );
  microbit.Scan();
}
 
function OnConnect()
{
  microbit.SetOnButton( OnButton );
}
 
function OnButton( name, state )
{
  txt.SetText( name + " : " + state );
}

Detect movement click to collapse contents

app.LoadPlugin( "MicroBit" );
 
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
  txt = app.CreateText( "Move your micro:bit" );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  microbit = app.CreateMicroBit();
  microbit.SetOnConnect( OnConnect );
  microbit.Scan();
}
 
function OnConnect()
{
  microbit.SetOnMotion( OnMotion );
}
 
function OnMotion( x, y, z )
{
  txt.SetText( x + "," + y + "," + z );
}

You can also send programs to the micro:bit and have them run every time the micro:bit is powered up using the following methods (Advanced).

 microbit.SendCode( code );
 microbit.Save();

See http://www.espruino.com/MicroBit for more code samples that can be run directly on the micro:bit bit

Save code on the micro:bit click to collapse contents

app.LoadPlugin( "MicroBit" );
 
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
 
  var code = "var text = 'Fred Blogs';\n" + 
  "g = Graphics.createArrayBuffer(5,5,1);\n" +
  "g.flip = function(){show(this.buffer);};\n" +
  "var x = 0;\n" +
  "\n" + 
  "function draw()\n" +
  "{\n" +
  "if( x++ > text.length*5 ) x=0;\n" +
  "g.clear();\n" +
  "g.drawString( text, 5-x);\n" +
  "g.flip();\n" +
  "}\n" +
  "\n" + 
  "setInterval(draw,150);\n";
 
  txt = app.CreateTextEdit( code, 0.9, 0.7, "MultiLine" );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  btnSave = app.CreateButton( "Save", 0.3, 0.1 );
  btnSave.SetOnTouch( btnSave_OnTouch );
  lay.AddChild( btnSave );
 
  btnReset = app.CreateButton( "Reset", 0.3, 0.1 );
  btnReset.SetOnTouch( btnReset_OnTouch );
  lay.AddChild( btnReset );
 
  microbit = app.CreateMicroBit();
  microbit.Scan();
}
 
function btnSave_OnTouch()
{
  microbit.SendCode( txt.GetText() );
  microbit.Save();
}
 
function btnReset_OnTouch()
{
  microbit.Reset();
}

Troubleshooting

There is a separate article on Troubleshooting this plugin.

plugins/bbc_microbit.txt · Last modified: 2019/03/04 16:22 (external edit)