Hi,
Here is a basic MAX7456 class I wrote. Any comments for better coding will be appreciated!
I had a problem with the String.IndexOf method (c# didn’t like any way that I tried to invoke it… :(), so currently I have written this search myself.
How do I take this class and make it a true .net micro driver?
using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using Microsoft.SPOT;
public class MAX7456
{
public const uint NTSC = 0;
public const uint PAL = 1;
static SPI.Configuration MaxSpiConfig =
new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di0,
false, 0, 0, false, true, 1000, SPI.SPI_module.SPI1);
SPI MaxSpi = new SPI(MaxSpiConfig);
public void WriteString(uint PosX, uint PosY, string Str)
{
byte[] TxData = new byte[Str.Length];
UInt16 PosXY = (UInt16)(PosY*30 + PosX); //calc absolute position from x,y coordinates
byte[] Character = new byte[37] //character array NOT COMPLETE!!!
{
32, //'space'
49, //'1'
50, //'2'
51, //'3'
52, //'4'
53, //'5'
54, //'6'
55, //'7'
56, //'8'
57, //'9'
48, //'0'
65, //'A'
66,
67,
68,
69,
70,
71,
72,
73,
74, //'J'
75,
76,
77,
78,
79,
80,
81,
82,
83,
84, //'T'
85,
86,
87,
88,
89,
90 //'Z'
};
byte[] TempStr = System.Text.Encoding.UTF8.GetBytes(Str); //convert string to byte array
for (uint i = 0; i < TempStr.Length; i++) //character write loop
{
UInt16 PosXYTemp = PosXY; //buffer curser origin
PosXYTemp = (UInt16)(PosXYTemp + i); //move curser to next position
uint PosHigh = (uint)(PosXYTemp >> 8);
uint PosLow = (uint)(PosXYTemp - (PosHigh * 256));
WriteReg(0x05, PosHigh);
WriteReg(0x06, PosLow);
byte u = TempStr[i];
for (uint CharIndex = 0; CharIndex < Character.Length; CharIndex++) //search for character in the array
{
if (Character[CharIndex] == u)
{
//character array to character matrix conversion
uint HighNibble = CharIndex / 16;
uint LowNibble = CharIndex % 16; //matrix y is low nibble, matrix x is low nibble
uint HighLow = HighNibble * 16 + LowNibble;
WriteReg(0x07, HighLow);
break;
}
}
}
}
public void WriteReg(uint Reg, uint Data)
{
byte[] TxData = new byte[2];
byte[] RxData = new byte[2];
TxData[0] = (byte)Reg;
TxData[1] = (byte)Data;
MaxSpi.WriteRead(TxData, RxData);
}
public uint ReadReg(uint Reg)
{
byte[] TxData = new byte[2];
byte[] RxData = new byte[2];
TxData[0] = (byte)Reg;
TxData[1] = 0;
MaxSpi.WriteRead(TxData, RxData);
return (RxData[1]); //read data is stored in byte #1 in RX array
}
public void Init(uint OffsetX, uint OffsetY, uint System)
{
uint PalNtsc = 0;
WriteReg(0x00, 1); //do a software reset
Thread.Sleep(5); //give enough time for reset to take place
uint ReadData = ReadReg(0xec); //read factory black level, set bit 4 and re-write
WriteReg(0x6c, ReadData + 16);
WriteReg(0x04, 0);
WriteReg(0x02, OffsetX); //x offset register
WriteReg(0x03, OffsetY); //y offset register
if (System == PAL) PalNtsc = 0x48; //set PAL or NTSC and enable OSD
else PalNtsc = 0x08;
WriteReg(0x00, PalNtsc);
}
public void ClearDisplay()
{
uint Temp = ReadReg(0x04);
WriteReg(0x04,Temp + 4);
Thread.Sleep(5);
}
}
Thanks,
S.B.D