First step: FEZ Panda II with LuxPlug

FEZ Panda II with LuxPlugLast evening I created my first program for the FEZ Panda II; I attached a JeeLabs LuxPlug to it and just started trying.

I found a tutorial on how to do I2C with the .Net MF and used that one as a starting point. It didn’t take long before I found out that for me, in this case, it was easier to rely on the TSL2561 datasheet than to try to use someone else’s code to create my first C# class for this sensor.

Probably because this first try was an easy one; I guess in the future, when things become more complicated, I’ll have to take some peeks at how others do certain things. But for now, I’m pleased with how this first exercise worked out!

The code is not finished yet, but it’s in a stage where it works and produces the raw data coming from the data registers. Good enough for a post 🙂

Here’s a small piece (the constructor part) of the class code:

public TSL2561(byte I2CAddress, int ClockinKHz)
{
  I2CConfig = new I2CDevice.Configuration(I2CAddress, ClockinKHz);
  I2C = new I2CDevice(I2CConfig);

  // read the ID register.
  var Actions = new I2CDevice.I2CTransaction[2];
  byte[] rx = new byte[1];
  Actions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x0a });
  Actions[1] = I2CDevice.CreateReadTransaction(rx);
  if (I2C.Execute(Actions, 1000) == 0)
  {
    Debug.Print("Read ID Register failed");
    // exit or something
  }
  else
  {
    Debug.Print("ID value: " + rx[0].ToString());
  }
  // 4 msb must be 0001 for a TSL2561
  if ((rx[0] & 0xf0) != 0xa0)
  {
    // exit or something
  }

  setGain(0x10);
  Thread.Sleep(5);   // Mandatory after each Write transaction !!!
}

Not that bad if I may say so…

The sensor is being read every 5 seconds and this produces the following output, where it shows the decimal values of low and high bytes of both ADC channels:

--------------
Result: 156 1
Result: 69 5
--------------
Result: 160 1
Result: 80 5
--------------
Result: 174 1
Result: 114 5
--------------
Result: 177 1
Result: 119 5
--------------

Nice! Calculations with these numbers is something that’s on the to-do list for now, but that should not give any big problems anymore; no, but what I think will be the biggest obstacle to overcome is my knowledge of C#.

For example, I’m used to displaying numbers in hexadecimal format. So I added a x.ToString(“X2”) (where x is a byte) in a Debug.Print(), knowing I’ve used this before myself in VB.Net and have seen this being used in C# too – I got no build errors, but I did get an exception… apparently the .Net Micro Framework doesn’t support this? I have to press F1 for help too much for my taste.. 😉 A matter of learning on the job I guess..

Now let’s see if I can find that 20×4 LCD for my next ‘project’; and maybe a spare LCD Plug, to keep the number of wires acceptable.

Tagged , . Bookmark the permalink.

2 Responses to First step: FEZ Panda II with LuxPlug

  1. Eric VDB says:

    To convert a byte to hex you can use this function:

    public static class ConvertString
    {
    static readonly string hex = “0123456789ABCDEF”;

    ///
    /// Converts Byte to a Hexadecimal string, like .ToString(“X2”) would do;
    ///
    /// Byte to represent has a hexadecimal string.
    /// 2 character string of hexadecimal representation of the number
    public static string FromByteToHex(byte number)
    {
    return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
    }
    }

    Have fun

  2. Thanks, it’s as if I have to start all over again with collecting a new set of ‘tools’ like that 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *