User Tools

Site Tools


plugins:bbc_microbit

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

plugins:bbc_microbit [2016/10/23 14:44]
madlyr created
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://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: 
-<code>app.LoadPlugin( "MicroBit" );</code> 
-Then you can create the microbit object like this: 
-<code>microbit = app.CreateMicroBit();</code> 
-You will need to connect to your micro:bit using Bluetooth and you can scan for it like this: 
-<code>microbit = microbit.Scan();</code> 
- 
-=====Examples===== 
- 
-====Example - Send smiley face click to collapse contents ==== 
-<code javascript> 
-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" ); 
-} 
-</code> 
- 
-  
-====Example - Send scrolling texts==== 
-<code javascript> 
-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 ); 
-} 
-</code> 
- 
-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> 
- microbit.SetOnReceive( MyFunc ); 
-</code> 
- 
- 
-====Example - Detect button presses click to collapse contents==== 
-<code javascript> 
-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 ); 
-} 
-</code> 
- 
- 
-====Example - Detect movement click to collapse contents==== 
-<code javascript> 
-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 ); 
-} 
-</code> 
-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> 
- microbit.SendCode( code ); 
- microbit.Save(); 
-</code> 
-See http://www.espruino.com/MicroBit for more code samples that can be run directly on the micro:bit 
-bit 
- 
-====Example - Save code on the micro:bit click to collapse contents==== 
-<code javascript> 
-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(); 
-} 
-</code> 
plugins/bbc_microbit.txt ยท Last modified: 2019/03/04 16:22 (external edit)