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:microbitctrl_imgtouch_ontouch

imgTouch_OnTouch

The imgTouch_OnTouch function allows add custom LED array touch function.

The ev parameter is an event object which has 3 fields: X, Y in range 0..1 and action (“Up”, “Down”, “Move”).

Default touch handler works as below:

function ( ev )
{
  //app.Debug( "x=" + ev.X + " y=" + ev.Y );
 
  //Calculate which LED we are touching.
  var x = Math.floor(  ev.X * 5 );
  var y = Math.floor(  ev.Y * 5 );
 
  //Keep x and y between 0 and 4.
  x = Math.min( Math.max(x,0), 4 );
  y = Math.min( Math.max(y,0), 4 );
  //app.Debug( "LED = " + x + "," + y );
 
  if( ev.action=="Move" )
    _this.SetLED( x, y );
  else if( ev.action=="Up" )
    _this.SendLEDStates();
}

It could be changed to different behavior.

Example - Custom imgTouch_OnTouch handler

This script has custom touch handler toggling LEDs without swipe. Only one LED is toggled at “Down” action.

// 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 );
 
  // Adds custom imgTouch_OnTouch handler
  ctrl.imgTouch_OnTouch = imgTouch_OnTouch;
 
  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();
}
 
// Handle toggling LEDs without swipe. Only one LED is toggled at "Down" action.
function imgTouch_OnTouch( ev ) {
  //app.Debug( ev.X+':'+ev.Y+':'+ev.action );
 
  //Calculate which LED we are touching.
  var x = Math.floor(  ev.X * 5 );
  var y = Math.floor(  ev.Y * 5 );
 
  //Keep x and y between 0 and 4.
  x = Math.min( Math.max(x,0), 4 );
  y = Math.min( Math.max(y,0), 4 );
  //app.Debug( "LED = " + x + "," + y );
 
  if ( ev.action=="Down" ) {
    ctrl.ToggleLED( x, y );
  } 
}
plugins/microbitctrl_imgtouch_ontouch.txt · Last modified: 2016/10/24 05:02 (external edit)