0.0.1 • Published 7 years ago

mneurons v0.0.1

Weekly downloads
4
License
-
Repository
-
Last release
7 years ago

#neurons-engine

Introduction

Neurons Engine is the JavaScript implementation of neurons central processing block, which runs on both Node and browser environment. The features of the engine includes:

  • Neurons binary protocol implementation.
  • Neurons blocks auto discovery and management.
  • Communication drivers used for running on different platforms.

How To Use

PC/RPI/IntelEdison

  1. install NodeJS environment.
  2. run node cli.js.
  3. connect the blocks with serial port.

Browser

  1. copy browser/neurons_engine.js to your project.
  2. add <script src="/path/to/neurons_engine.js"></script> in your html <head> tag.

Programming API

Create LogicEngine

In Node

    var engine = require('./lib/engine/logic').create(
      {"driver": "serial", "loglevel": "DEBUG"}
    );

In Browser

    var engine = createNeuronsLogicEngine(
      {"driver": "makeblockhd", "loglevel": "DEBUG"}
    );

Create FlowEngine

In Node

    var engine = require('./lib/engine/flow').create(
      {"driver": "serial", "loglevel": "DEBUG"}
    );

In Browser

    var engine = createNeuronsFlowEngine(
      {"driver": "makeblockhd", "loglevel": "DEBUG"}
    );


#### configuration

* `driver`: the driver to use ,currently support 
    * **serial**(Node Only), 
    * **makeblock**(Makeblock APP Only) 

* `loglevel`: the loglevel to set ,when set, will not print the log which priority lower than set.
  * currently support loglevel
    * **TRACE**,
    * **DEBUG**,
    * **INFO**,
    * **WARN**,
    * **ERROR**,
    * **FATAL**

### Engine.registerBlockType(option)

Adds a new type of block to the engine. (See block definition bellow.)

### Engine.unregisterBlockType(name)

Remove block type by name.

### Engine.getActiveBlocks()

Get all active blocks and their values. Example result:

``` javascript
    {
        "1_KEY_BUTTON": [
            [0],
            [1]
        ],
        "DC_MOTOR": [
            [100, 200]
        ],
        "4_NUMERIC_DISPLAY": [
            [1.23],
            [2.34]
        ]
    }

Engine.getBlockStatus(name, idx = 0)

Queries a block's status by name. if idx is provided, will return the status of the {idx}th block of the name. If the required block does not exist, will return null.

Return the values array of the block if block exists. eg: [1, 34].

If there is substatus in the block, will return a bunch of sub status, wrapping to an object, eg: {sub1: [1, 2], sub2: [3, 4]}.

Engine.setBlockStatus(name, values, idx = 0)

Sets a block's status to values. For example:

engine.setBlockStatus('DC_MOTOR', [122, 233])

This API only works well for blocks with no sub status.

Engine.sendBlockCommand(name, command, params, idx = 0)

Sends a command to block of name given. Only works for blocks with sub command.

  • name: the name of the block type. eg: "SMART_SERVO"
  • command: the name of the block command, eg: "setspeed"
  • params: the params of the block command, eg: 1, 2, 3

Engine Events

use Engine.on(event, callback) to register an event handler.

support events:

'handshake'

callback(name, idx)

  • name: the name of block type
  • idx: the {idx}th block

'error'

callback(error)

'blockListChanges'

callback(list)

  • list: the active blocks list. see Engine.getActiveBlocks

'blockStatusChanges'

callback(name, idx, values)

values: see Engine.GetBlockStatus

Electronic Block Definition

All support electronic blocks are defined in lib/blocks/electronic folder by JSON objects. You can define your own block for extension:

var example = {
  name: 'example', // the block name, MUST be unique
  type: 0x00, // the block type id 
  subtype: 0x01, // the block subtype id , optional.
  status: ['byte', 'BYTE', 'short', 'float'] // the block status values type definition.
};

For blocks will sub status:

var example2 = {
  // the name of the block, must not be unique.
  name: 'example2',
  // the typeid of block.
  type: 0x00,
  // subtypeid of block, optional.
  // subtype: 0x01,
  // available sub status' datatype.
  status: {
    'sub1' : {
      'subid': 0x01,
      'datatype': ['float']
    },
    'sub2' : {
      'subid': 0x02, 
      'datatype' : ['byte', 'BYTE', 'short', 'float']
    }
  }
};

For blocks with sub command:

var example3 = {
  // the name of the block, must not be unique.
  name: 'example3',
  // the typeid of block.
  type: 0x01,
  // subtypeid of block, optional.
  // subtype: 0x01,
  // available sub status' datatype.
  status: {
    'sub1' : {
      'subid': 0x01,
      'datatype': ['float']
    },
    'sub2' : {
      'subid': 0x02, 
      'datatype' : ['byte', 'BYTE', 'short', 'float']
    }
  },
  // block's commands
  commands: {
    'command1': {
      'commandid': 0x01,
      'datatype': ['float']
    }, 
    'command2' : {
      'commandid': 0x02, 
      'datatype' : ['byte', 'BYTE', 'short', 'float']
    }
  }
};

supported value type:

  • "byte": 7bit/byte byte
  • "BYTE": 8bit/byte byte (only use when the byte length < 128)
  • "short": 7bit/byte short
  • "SHORT": 7bit/byte short, but ignore most significant byte (only use when the MSB is 0x00)
  • "long": 0x11111111 // 7bit/byte long
  • "float": 7bit/byte float
  • "double": 7bit/byte double
  • "string": 7bit/byte byte array, in TLV format

development

run tests

gulp test

run syntax check

gulp jshint

export to browser

gulp browserify

compress after browserify

gulp compress