This shows you the differences between two versions of the page.
plugins:bbc_microbit [2016/10/23 16:38] madlyr Added methods table list |
plugins:bbc_microbit [2019/03/04 16:22] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 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:// | ||
- | |||
- | //:!: 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: | ||
- | < | ||
- | Then you can create the microbit object like this: | ||
- | < | ||
- | You will need to connect to your micro:bit using Bluetooth and you can scan for it like this: | ||
- | < | ||
- | |||
- | ===== Methods ===== | ||
- | ^ Method ^ Description ^ | ||
- | | [[microbit_analogread|AnalogRead( pin )]] | | | ||
- | | [[microbit_connect|Connect( address )]] | | | ||
- | | [[microbit_createcontroller|CreateController( size )]] | | | ||
- | | [[microbit_disconnect|Disconnect()]] | | | ||
- | | [[microbit_getimage|GetImage()]] | | | ||
- | | [[microbit_getversion|GetVersion( num, txt )]] | | | ||
- | | [[microbit_oncancel|OnCancel()]] | | | ||
- | | [[microbit_onconnect|OnConnect()]] | | | ||
- | | [[microbit_ondevice|OnDevice( name, address, bonded, rssi )]] | | | ||
- | | [[microbit_ondisconnect|OnDisconnect()]] | | | ||
- | | [[microbit_onreceive|OnReceive( data )]] | | | ||
- | | [[microbit_onselect|OnSelect( title, body )]] | | | ||
- | | [[microbit_reset|Reset()]] | | | ||
- | | [[microbit_save|Save()]] | | | ||
- | | [[microbit_scan|Scan( address )]] | | | ||
- | | [[microbit_scrolltext|ScrollText( text, interval )]] | | | ||
- | | [[microbit_send|Send( msg )]] | | | ||
- | | [[microbit_sendcode|SendCode( code )]] | | | ||
- | | [[microbit_setleds|SetLEDs( bits )]] | | | ||
- | | [[microbit_setonbutton|SetOnButton( callback )]] | | | ||
- | | [[microbit_setonconnect|SetOnConnect( callback )]] | | | ||
- | | [[microbit_setondevice|SetOnDevice( callback )]] | | | ||
- | | [[microbit_setondisconnect|SetOnDisconnect( callback )]] | | | ||
- | | [[microbit_setonevent|SetOnEvent( callback )]] | | | ||
- | | [[microbit_setonmotion|SetOnMotion( callback )]] | | | ||
- | | [[microbit_setonreceive|SetOnReceive( callback )]] | | | ||
- | | [[microbit_setontemp|SetOnTemp( callback )]] | | | ||
- | | [[microbit_setsplitmode|SetSplitMode( mode, p1, p2 )]] | | | ||
- | | [[microbit_startscan|StartScan()]] | | | ||
- | | [[microbit_stopscan|StopScan()]] | | | ||
- | | [[microbit_watchaccelerometer|WatchAccelerometer( precision, interval, cancel )]] | | | ||
- | | [[microbit_watchbutton|WatchButton( name, cancel )]] | | | ||
- | | [[microbit_watchinput|WatchInput( pin, cancel )]] | | | ||
- | =====Examples===== | ||
- | |||
- | ====Send smiley face click to collapse contents ==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | btn = app.CreateButton( " | ||
- | btn.SetOnTouch( btn_OnTouch ); | ||
- | lay.AddChild( btn ); | ||
- | app.AddLayout( lay ); | ||
- | |||
- | microbit = app.CreateMicroBit(); | ||
- | microbit.Scan(); | ||
- | } | ||
- | |||
- | function btn_OnTouch() | ||
- | { | ||
- | microbit.SetLEDs( " | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ====Send scrolling texts==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | btn = app.CreateButton( " | ||
- | btn.SetOnTouch( btn_OnTouch ); | ||
- | lay.AddChild( btn ); | ||
- | app.AddLayout( lay ); | ||
- | |||
- | microbit = app.CreateMicroBit(); | ||
- | microbit.Scan(); | ||
- | } | ||
- | |||
- | function btn_OnTouch() | ||
- | { | ||
- | microbit.ScrollText( " | ||
- | } | ||
- | </ | ||
- | |||
- | 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: | ||
- | <code javascript> | ||
- | | ||
- | </ | ||
- | |||
- | |||
- | ====Detect button presses click to collapse contents==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | txt = app.CreateText( "Press a button on your micro: | ||
- | 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==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | txt = app.CreateText( "Move your micro: | ||
- | 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 + "," | ||
- | } | ||
- | </ | ||
- | 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). | ||
- | <code javascript> | ||
- | | ||
- | | ||
- | </ | ||
- | See http:// | ||
- | bit | ||
- | |||
- | ====Save code on the micro:bit click to collapse contents==== | ||
- | <code javascript> | ||
- | app.LoadPlugin( " | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | var code = "var text = 'Fred Blogs'; | ||
- | "g = Graphics.createArrayBuffer(5, | ||
- | " | ||
- | "var x = 0;\n" + | ||
- | " | ||
- | " | ||
- | " | ||
- | "if( x++ > text.length*5 ) x=0; | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | |||
- | txt = app.CreateTextEdit( code, 0.9, 0.7, " | ||
- | lay.AddChild( txt ); | ||
- | app.AddLayout( lay ); | ||
- | |||
- | btnSave = app.CreateButton( " | ||
- | btnSave.SetOnTouch( btnSave_OnTouch ); | ||
- | lay.AddChild( btnSave ); | ||
- | |||
- | btnReset = app.CreateButton( " | ||
- | 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(); | ||
- | } | ||
- | </ |