TI Sensortag CC2650 Demo period

Discuss mobile apps for Texas Instruments products.
theres
Posts: 2
Joined: 12:26, 23 Jul 2015

TI Sensortag CC2650 Demo period

Postby theres » 12:37, 23 Jul 2015

Hello everybody!

I'm trying to understand the code of the Evothings example "TI Sensortag CC2650 Demo".
I really don't understand the part of setting the acceleration period:

Code: Select all

   // Set accelerometer period to 100 ms.
   device.writeCharacteristic(
      app.sensortag.MOVEMENT_PERIOD,
      new Uint8Array([10]),
      function()
      {
         console.log('Status: writeCharacteristic ok.');
      },
      function(errorCode)
      {
         console.log('Error: writeCharacteristic: ' + errorCode + '.');
      });


Where is it concretly set? By defining this new array?

Can anyone help me, please? That would be very nice. :)

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: TI Sensortag CC2650 Demo period

Postby Fredrik » 13:17, 23 Jul 2015

This code writes the value "10" to the characteristic defined by "MOVEMENT_PERIOD". The SensorTag takes periods in units of 10 ms. So writing "10" gives you a period of (10*10=100) ms. If you had written "5", you'd have gotten a period of 50 ms.

theres
Posts: 2
Joined: 12:26, 23 Jul 2015

Re: TI Sensortag CC2650 Demo period

Postby theres » 14:12, 23 Jul 2015

Thank you very much for answering so fast.

I'm new in this web-app topic and was confused of using an array to pass the parameter. By adding a timestamp, the period varied very much.

I'm also thinking of better using the tisensortag.js library for my application. In examples I mostly see the main code in index.html. Is it possible to reference libraries in other .js files? I cannot really see what is good programming pracise to get organized code and files. Maybe someone has a good link for a tutorial or something like that?

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: TI Sensortag CC2650 Demo period

Postby micke » 08:39, 24 Jul 2015

Hi Theres,

Here are examples of JavaScript tutorials:

https://developer.mozilla.org/en-US/Lea ... ipt_basics
http://htmldog.com/guides/javascript/
http://www.w3schools.com/js/default.asp

It works fine to move the JavaScript code in for instance the TI SensorTag from index.html to a separate .js file.

Let's say we put the JavaScript code inside the script tag in index.html (this file: https://github.com/evothings/evothings- ... index.html) in a file called app.js. The content of app.js would look like this:

Code: Select all

// Globals.
var sprite
var sensortag

function initialise()
{
   initialiseSprite()
   initialiseSensorTag()
}

function initialiseSprite()
{
   sprite = SpriteManager.makeSprite()
   sprite.setDOMElement(document.getElementById('sprite'))
}

function displaySprite()
{
   sprite.whenLoaded(function()
   {
      sprite.show()
      sprite.setCenterX(SpriteManager.getPlayfieldWidth() / 2)
      sprite.setCenterY(SpriteManager.getPlayfieldHeight() / 2)
   })
}

function moveSprite(dx, dy)
{
   var x = sprite.getCenterX() + dx
   var y = sprite.getCenterY() - dy

   x = Math.min(x, SpriteManager.getPlayfieldWidth())
   x = Math.max(x, 0)

   y = Math.min(y, SpriteManager.getPlayfieldHeight())
   y = Math.max(y, 0)

   sprite.setCenterX(x)
   sprite.setCenterY(y)
}

function initialiseSensorTag()
{
   // Create SensorTag CC2650 instance.
   sensortag = evothings.tisensortag.createInstance(
      evothings.tisensortag.CC2650_BLUETOOTH_SMART)

   // Uncomment to use SensorTag CC2541.
   //sensortag = evothings.tisensortag.createInstance(
   //   evothings.tisensortag.CC2541_BLUETOOTH_SMART)

   // Set up callbacks and sensors.
   sensortag
      .statusCallback(statusHandler)
      .errorCallback(errorHandler)
      .accelerometerCallback(accelerometerHandler, 100)
}

function connect()
{
   sensortag.connectToNearestDevice()
}

function disconnect()
{
   sensortag.disconnectDevice()
   displayStatus('Disconnected')
   sprite.hide()
}

function statusHandler(status)
{
   displayStatus(status)

   if (evothings.tisensortag.ble.status.SENSORTAG_ONLINE == status)
   {
      displaySprite()
   }
}

function errorHandler(error)
{
   if (evothings.easyble.error.DISCONNECTED == error)
   {
      displayStatus('Disconnected')
      sprite.hide()
   }
   else
   {
      displayStatus('Error: ' + error)
   }
}

function accelerometerHandler(data)
{
   var values = sensortag.getAccelerometerValues(data)
   var dx = values.x * 50
   var dy = values.y * 50 * -1
   moveSprite(dx, dy)
}

function displayStatus(status)
{
   document.getElementById('status').innerHTML = status
}

document.addEventListener(
   'deviceready',
   function() { evothings.scriptsLoaded(initialise) },
   false)


Note that script tags are not used in .js files, only in .html files.

In index.html add this script tag:

Code: Select all

<script src="app.js"></script>


The script tags in index.html now look like this:

Code: Select all

<script src="cordova.js"></script>
<script src="libs/evothings/evothings.js"></script>
<script src="libs/evothings/tisensortag/tisensortag.js"></script>
<script src="SpriteManager.js"></script>
<script src="app.js"></script>


One important part of the code to understand is the following snippet:

Code: Select all

document.addEventListener(
   'deviceready',
   function() { evothings.scriptsLoaded(initialise) },
   false)


This delays the call to the function initialise until all parts of the SensorTag library have loaded.

If calling initialise directly, like this, the program may not work (do NOT do this):

Code: Select all

initialise()


Note the difference between putting parens after the function name and excluding them. Adding the parens calls the function immediately, stating the function name without parens passes a reference to the function so that it can be called later. This is what happens in the document.addEventListener code snippet.

Lets look at it again, this time with some comments to clarify:

Code: Select all

// We call the function "addEventListener" which is part of the object "document".
document.addEventListener(
   // The event we want to listen for is "deviceready", which is specified as a string.
   // In JavaScript you can use singe or double quote marks for specifying strings.
   'deviceready',
   // Here we pass in an anonymous function as a parameter (a function without a name).
   // This function will be called later when the "deviceready" event happens. Some code
   // in the JavaScript framework will call this, we don't have to bother about where this
   // happens, JavasScript takes care of it.
   function() { evothings.scriptsLoaded(initialise) },
   // false indicates that the event should not "bubble", it not that important for this example.
   false)


Let's have look at the function with no name (breaking it up on multiple lines for readability):

Code: Select all

// The function have no name, and this is called an "anonymous function" or "closure".
function()
{
   // When the anonymous function is called the function "scriptsLoaded" which is
   // defined on the object "evothings" is executed. The function "initialize" is passed
   // as a parameter. The "scriptsLoaded" function will call the "initialize" function
   // when all Evothings library have been loaded.
   evothings.scriptsLoaded(initialise)
}


Hope this helps!

Best, Mikael

User avatar
micke
Posts: 256
Joined: 20:49, 18 Nov 2013

Re: TI Sensortag CC2650 Demo period

Postby micke » 18:38, 25 Jul 2015

Hi again Theres, wrote a tutorial based on your question:

http://evothings.com/javascript-tutoria ... your-code/

Best, Mikael


Return to “Texas Instruments”

Who is online

Users browsing this forum: No registered users and 5 guests