Wednesday 13 October 2010

Basic Netduino HL1606 driver port.


Straight port of Arduino library to interface with RGB LED strips built with the HL1606 driver chip.

This hasnt even been tested yet, I just converted the code without even analysing it.

[EDIT] OK I've had a chance to look at the code and what its suppose to do. The HL1606 is basically 2* shift register and 2* LED driver. The code below is Bit Bashing it's way through, which is a pretty basic way of controlling, especially since there should be some hardware support which can do the job for us (e.g. SPI).

I'm going to have a go a writing a driver which is a bit more efficient than the below code. I'll keep you posted!



http://code.google.com/p/ledstrip/



using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

using System.IO;
using System.Text;

namespace NetduinoLEDSriptDriver
{
class HL1606Driver
{
enum BitOrder
{
LSBFirst,
MSBFirst
}

enum LEDState
{
OFF, // 0b00
ON, // 0b01
UP, // 0b10
DOWN, // 0b11

};

//#define _BV(bit) (1 << (bit)) //#include
//Converts a bit number into a byte value.
private const uint NONCMD = 0xFF;
private const uint LATCH = 0x07;
private const uint SPEED2X = 0x06;
private const bool LOW = false;
private const bool HIGH = true;

static OutputPort _dPin;
private OutputPort _sPin;
private OutputPort _latchPin;
private OutputPort _clkPin;

private bool _faderEnabled;
private uint _faderPulseNextEdge;

//System Function
protected void digitalWrite(OutputPort port, bool state)
{
port.Write(state);
}

protected bool digitalRead(OutputPort port)
{
return port.Read();
}

//public void pinMode(uint port, uint state)
//{

//}

public HL1606Driver(Cpu.Pin dPin, Cpu.Pin sPin, Cpu.Pin latchPin, Cpu.Pin clkPin)
{

//Setup Ports
try
{
_dPin = new OutputPort(dPin, false);
_sPin = new OutputPort(sPin, false); ;
_latchPin = new OutputPort(latchPin, false);
_clkPin = new OutputPort(clkPin, false);

}
catch (Exception)
{

throw;
}

_faderEnabled = false;

//Setup Port Inital States
wakeup();

}

void wakeup()
{
digitalWrite(_dPin, LOW);
digitalWrite(_sPin, LOW);
digitalWrite(_latchPin, LOW);
digitalWrite(_clkPin, LOW);
}


void faderCrank()
{
short mymillis;

if (!_faderEnabled) return;
SystemTime t = new SystemTime();

mymillis = t.Milliseconds;

// Give us 250ms slop in case we don't exactly catch our edge.
if (mymillis >= _faderPulseNextEdge && mymillis < _faderpulsehalfwidth =" _faderPulseNewHalfWidth;" _faderpulsenextedge =" (uint)mymillis" myvar =" value;" myvar ="=" _faderenabled =" false;" _faderpulsehalfwidth =" 0;" _faderpulsenewhalfwidth =" 0;" _faderpulsenewhalfwidth =" myVar;" _faderenabled =" true;" _faderpulsehalfwidth =" myVar;" t =" new" _faderpulsenextedge =" (uint)t.Milliseconds" value ="=" _faderenabled =" false;" _faderpulsehalfwidth =" 0;" _faderpulsenewhalfwidth =" 0;" _faderpulsenewhalfwidth =" value;" _faderenabled =" true;" _faderpulsehalfwidth =" value;" t =" new" _faderpulsenextedge =" (uint)t.Milliseconds" cmd =" 0;" flags =" LATCH;">= NONCMD || bluecmd >= NONCMD || greencmd >= NONCMD) return;

cmd |= (greencmd << cmd =" 0;" flags =" LATCH">= NONCMD || bluecmd >= NONCMD || greencmd >= NONCMD) return;

cmd |= (greencmd << i =" 0;" _bitorder ="=" mask =" (byte)(1" mask =" (byte)(1" ms =" (int)delay">

No comments: