Pages

2013-01-30

Node.js: Inheriting from Node Event Emitter


If you are interested in using Node’s event emitter pattern throughout your application, you can create a pseudo-class and make it inherit from EventEmitter like this:


util = require('util');var EventEmitter = require('events').EventEmitter;

// Here is the MyClass constructor:

var MyClass = function() {}util.inherits(MyClass, EventEmitter);
By creating a class that inherits from EventEmitter, instances of MyClass can emit events:


MyClass.prototype.someMethod = function() {this.emit("custom event", "argument 1", "argument 2");};
Here, when the someMethod method is called on an instance of MyClass, the example emits an event
named custom event. The event also emits some data, in this case two strings: "argument 1" and
"argument 2". This data will be passed along as arguments to the event listeners.


Clients of MyClass instances can listen to the event named custom event like this:
var myInstance = new MyClass();myInstance.on('custom event', function(str1, str2) {console.log('got a custom event with the str1 %s and str2 %s!', str1, str2);});
For example, you could build a pseudo-class named Ticker that emits a “tick” event every second: 

var util = require('util'),EventEmitter = require('events').EventEmitter;var Ticker = function() {var self = this;setInterval(function() {self.emit('tick');}, 1000);};util.inherits(Ticker, EventEmitter);

Clients of this class could instantiate this Ticker class and listen for the “tick” events like so:
var ticker = new Ticker();ticker.on("tick", function() {console.log("tick");});





2013-01-21

TI MSP430: Understanding BCS (Basic Clock System)

Each family of MSP430 has a slightly different clock system, though many of the principles are the same for each.  The x2xx family uses the BCS+ system, which provides the MSP430 with three separate clocks that can run as quickly as 16 MHz in particular conditions.  

The reason we have three clocks instead of just one or even two is to compromise between systems that need speed and the ability to minimize power consumption, one of the real hallmarks of the MSP430.  Faster clocks consume more power, so to really reduce the power used we need slower clocks.  But some functions need to respond and conclude quickly, so we also need fast clocks.  

You can design around the use of a single clock, but having the flexibility of three is powerful.  Let's look at the three clocks available in BCS+:


  • MCLK:  This is the Master Clock, the one that drives the processor and times the commands in your program.  This is typically a high frequency clock, but can be configured for low frequencies if needed.
  • SMCLK: The Sub-Main Clock is a secondary clock that is often used by other peripherals.  It can be the same frequency as MCLK or different, depending on the application.
  • ACLK: The Auxilliary Clock is usually timed outside the MSP430 and is typically used for peripherals.  Often, this is a low frequency clock, but can also be used at high frequencies when desired.
There are also up to four sources available in BCS+ to drive each of the three clocks:
  • LFXT1CLK:  The name of this source implies its use with a low-frequency crystal, and this is often the case.  A 32,768 Hz crystal can be connected to your MSP430 for low-power, high-accuracy timing.  In some x2xx devices, this source also has a high frequency mode that can use crystals from 400 kHz to 16 MHz, assuming the chip is powered with a large enough voltage to handle the frequency.  In addition, other resonators and external signals can be used for this source.
  • XT2CLK:  Again, this source is named for its implied use with a second crystal, but in this case only a high frequency crystal.  It can also use other resonators and external signals.  This source is not available on every x2xx device.  (See page 5-2 of the x2xx Family User's Guide to see which devices use XT2.)
  • DCOCLK:  The internal source is the digitally controlled oscillator.  Though not as accurate and stable as crystal sources, the DCO is still quite good and can be configured to operate at a wide range of frequencies.
  • VLOCLK:  The MSP430 includes a second internal oscillator for very-low power, very-low frequency applications.  The VLO can run slower than the LFXT1 watch crystal, and typically is used at about 12 kHz.  This source is not available in all x2xx devices.  (Refer to 5-2 in the User's Guide.)
Each clock can also divide the frequency by which it's sourced by a factor of 2, 4, or 8.  In addition, many peripherals can further divide their clock by the same factors, giving a large number of possible frequencies available from just one source.  

While MCLK and SMCLK can use any of the four sources, ACLK uses only LFXT1 or VLO.  The default configuration for MCLK and SMCLK is to use the DCO at about 1.1 MHz, and for ACLK is to use LFXT1.