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.  

2012-11-21

Open Source Embedded Distributions

Hi,

This is a short list of most know Linux embedded distributions:


  • LTIB (http://www.ltib.org) Linux Target Image Builder
  • Buildroot (http://buildroot.uclibc.org/)
  • T2 SDE (http://www.t2-project.org)
  • PTXdist (http://www.ptxdist.org)
  • DENX Emebedded Linux Development Kit (http://www.denx.de)
  • OpenEmbedded (http://wiki.openembedded.net/)

Installing/Configuring TFTP Server on Fedora

Hi,

In this post we will explain how to install and configure a TFTP Server on Fedora.

The purpose of a TFTP Server in an embedded Linux installation is to allow the  bootloader (Uboot) to download the new compiled kernel from the development host computer to the Flash Memory of the board.



  • Installing the TFTP Server:
First you should be logged as root.
From the shell:
yum install tftp-server

  •  Configuring the TFTP Server:

The configuration file for the TFTP service handled by xinetd resides at /etc/xinetd.d/tftp
From the shell (always logged in as root) edit this file using vi for example. Two lines will be changed.


server_args = -s /tftpboot
disable = no
Save and exit

  •  Restarting network services:

From the shell (always logged in as root):
chkconfig tftp on

chkconfig xinetd on

service xinetd restart

Note: To be able to test the new TFTP Server installation from your localhost you should first install TFTP Client on your machine using:
yum install tftp 

2012-04-04

DOTNET: Implementing a Custom Indexer

So you need to be able to access data in your custom type like an array?


You can enable array-style indexing for you class by implementing a custom indexer. A custom indexer
is like a property, but the name of the property is the keyword this. You can choose any type to be used
as the index and any type to return as the result.


public class TelephoneItem
{
    ArrayList _number = 
new ArrayList();

   
public TelephoneItem()
   {
      
//SomeConstructor  }
   
public void Add(string PhoneNumber)
   {
       _number.Add(PhoneNumber);
   }
   
public object this [int idx]
   {
      
get     {
         
if(_number.Count < idx)<BR>          {
            
return _number[idx];
         }
         
else        {
            
throw new IndexOutOfRangeException("[TelephoneItem.get_Item]" +
                "Index Out of Range");
         }  
      }
      
set     {
         
if(_number.Count < idx)<BR>          {
             _number[idx] = 
value;
         }
         
else        {
            
throw new IndexOutOfRangeException("[TelephoneItem.set_Item]" +
                "Index Out of Range");
         }
           }        
   }
}

2012-03-23

DOTNET: Implementing implicit and explicit conversion operator methods


You can specify how your type is converted to other types and, equally, how other types are converted to
your type, by declaring conversion operators in your class. A conversion operator is a static method that
is named for the type that you wish to convert to and that has the type you wish to convert from. For
example, the following method fragment is a conversion operator from the myClass type that converts
an instance of string to an instance of myClass (String TO myClass):

public static explicit operator myClass(string str)
{
return new myClass() 
this.Text = str 
};
}

Defining this member in the myClass class allows us to perform conversions such as the following:

myClass C1 = (myClass)"Hello";

Note that we have had to explicitly cast the string to myClass—this is because our conversion operator
included the explicit keyword. You can enable implicit conversion by using the implicit keyword, such
as this:

public static implicit operator myClass(string str)
{
return new myClass() 
this.Text = str 
};
}

With the implicit keyword, now both of the following statements would compile:
myClass C1 = (myClass)"Hello";
myClass C2 = "Hello";

Conversion operators must always be static, and you must choose between an explicit and an
implicit conversion—you cannot define different conversion operators for the same pair of types but
with different keywords.

DOTNET: Overloading operators


To implement operators in your classes, you simply define static methods to overload the operator you
want to use—for example, the following fragment shows the declaration of a method that implements
the addition operator (+) to be used when adding together two instances of the type myClass:

public static string operator +(myClass C1, myClass w2)

Notice that the result of our addition is a string—you can return any type you choose. You can also
define the behavior for when operators are applied on different types, such as the following, which
declares a method that overrides the operator for when an instance of myClass and an int are added
together:

public static myClass operator +(myClass C1, int i)

The following fragment allows us to use the operator like this:

myClass newMyClass = C1 + 7;

Note that the order of the arguments is important—the previous fragment defines the behavior for a
myClass + int operation, but not int + myClass (i.e., the same types, but with their order reversed). We would need to define another method to support both orderings.

You can override the following operators:
+, -, *, /, %, &, |, ^, <<, >>