Skip to content
pedalpete edited this page May 22, 2016 · 11 revisions

Why Favor

Favor separates business logic from hardware. Software is normally developed for specific hardware, but software developed with Favor can run on multiple different types of hardware. Favor accomplishes this by running a middleware layer that connects to a config file which is stored on your hardware.

The benefits of running favor is

  • separating business logic from hardware
  • testable code
  • run the same software on different hardware

To use favor on hardware, you'll need to create a config file with the specifications for your hardware.

Querying the config file

You can query the config file for the components connected to your device similar to querying the DOM in javascript.

$$('accelerator') will query your config file for all the accelerators listed in the type field

by name

$$('#component_name') will return the component with a name field defined

by count *

$$('x*3') will get the first 3 components

by attribute [attribute=match]

$$([position=left]) will get components where a position attribute exists with the value of left

by value .

$$('.stream') will get components where any attribute has the value of stream

Simple Interface

Favor has four simple methods, set, get, watch, removeWatch`.

To get a value from a component, call the get method with a callback

$$('temperature').get(function(val) {
       // do something with the val
});

To set a value call the set method with the value to set and a callback

$$('led').set(1, function(val) { 
   // do something after the val is set
});

For more about methods to interact with Favor

Config file

To use favor to interact with components connected to your device, you will need to create a configuration file which you will store on the device.

Example configuration files can be found in the favor example repo.

At the top level, you should provide a name for your config object.

  • name - string optional ** the name may be used in the future to identify your device
  • components array required ** the component array holds the list for each component attached to the device.

Components

Component Types

Favor currently supports 3 types of components. GPIO, I2C and SPI. Don't worry if you're not familiar with these different protocols. One of the goals of Favor is to make it easier to interact with components without digging deep into the details of how different components work.

Common Component Properties

Though each of the different protocols have slightly different object properties, the following properties are common across the different component types.

  • type - string required ** the type is the descriptor you give your component so you can query it using favor. An example would be led, temperature or accelerometer
  • interface - string - required ** tells favor what interface/protocol to use with this component. Valid options are 'gpio', 'i2c', 'spi'
  • address - number | string | hex required ** the address is the location on the device favor will use to communicate with your component. ** gpio: this would be the pin number ** i2c: this is the hex address of the device (you'll find it on the datasheet for the component your connecting to) ** spi: the string where the spi is located - '/dev/spidev0.0' | '/dev/spidev0.1' for linux
  • formatInput - function optional ** this lets you use a common input to send data to completely different devices. The function should take a single argument and return the value in a format which works with the component (see the component data sheet). This function must be synchronous
  • formatOutput - function optional ** this lets you take the value returned from the component and let you manipulate it so you have a common output for multiple devices.