PDA

View Full Version : i2c bus serial plugin



woniol
December 21st, 2005, 07:19 AM
I'm going to write a simple i2c bus serial plugin for G 4.0

I had one for G 3.2.9 which was not a real plugin. I managed to write it in lua script using dlportio.dll and ist lua functions. The small device was connected to the lpt port.

Now I would like to rewrite the script to work on a serial port (usb2serial converter)
So far I managed to write a simple my_i2c_device.lua which opens com port on Girder start and closes it on Gireder close. By setting a local variable with self.Serial I can use :
res], [err] = [Port]:SetDTR([on])
[on], [err] = [Port]:GetDTR()
[res], [err] = [Port]:SetRTS([on])
[on], [err] = [Port]:GetRTS()
[on], [err] = [Port]:GetDCD()
[on], [err] = [Port]:GetDSR()
[on], [err] = [Port]:GetRI()
commands from lua.
My point is to write functions like I2C.SendAddress, I2CSendData, I2CReceiveData
which could be used in any lua script.

Need some help where to put those functions, and how to call them from lua Scripting Command. i know i can put them in my script but I woul like them to be more accesible.

PM me if You are interested in such a plugin or schematics.
Maciej Wachalski

Promixis
December 21st, 2005, 09:24 AM
Hi,

This is going to be easier using one of the predefined serial classes.

What are the port settings?

Is there a protocol document?

woniol
December 21st, 2005, 11:06 AM
Hallo, thanks for the interest and help.

Here are the links:
Schematics for serial2i2c converter
http://maciej.mewy.net/i2c/SerialI2C.pdf

basic 8bit IO port on i2c with i2c timing
http://maciej.mewy.net/i2c/PCF8574_4.pdf

and i2c specification
http://maciej.mewy.net/i2c/i2c_spec.pdf

In fact this conwerter doesn't use seria data lines.
I uses controll linenes: 2 for input and 2 for output

I2c uses only 2 lines SDA adn SCL, and all i need is to write a function to triger putput lines and listen to input lines according to i2c_spec.

Example code from my old script looks like this:
function setsda(war)
if (war == 0) then
nsdaport = bnot(sdaport)
port = PORTIO_ReadBYTE(portnumber)
tempport = band(nsdaport,port)
PORTIO_WriteBYTE(portnumber,tempport)
else
port = PORTIO_ReadBYTE(portnumber)
tempport = bor(sdaport,port)
PORTIO_WriteBYTE(portnumber,tempport)
end
end

function sendbit(war3)
if (war3==0) then
setsda(1)
delay()
setscl(0)
delay()
setscl(1)
delay()
else
setsda(0)
delay()
setscl(0)
delay()
setscl(1)
delay()
end
end

function sendbyte(byte)
maska = 128
for i=0,7 do
bajttemp = band(byte,maska)
if (bytetemp>0) then sendbit(1) else sendbit(0)
end
maska = shiftr(maska,1)
end
end

BTW: I use 5 of those PCF8574's to controll oll my house lights :)

Promixis
December 21st, 2005, 01:26 PM
Interesting stuff, I had never read about the IC bus in detail before.

I am unsure if the serial plugin will do what you want in terms of signaling using the control lines. I have not tried that before.

The DLPortIO plugin is available in G4 if the above doesn't work.

Lua 4 is similar to Lua 5. Lua 5 is covered in the manual and the Lua 5 book is online. Let me know how else I can help.

woniol
December 21st, 2005, 02:28 PM
Ok,
now i'm just interested where to put functins, to have acces to them form any Scripting Action in girder. Is there a way to put them in my_i2c_device.lua which is located in \plugins\serial directory? So they could be loaded if my_i2c_device is asigned to serial port.

In a simple way i want to write some functins for i2c control and write a simple my_i2c_device.lua script and locate it in \plugins\serial. Then in girder Options->Plug-ins->Serial(tab) asign my_i2c_device to one of the serial ports.
When girder stars i woul like to have access to my functions so i don't need to them in every Scripting Action.
The proplem is where to put those functions?

Promixis
December 21st, 2005, 02:37 PM
yes, you would create you own device file. see one of the .lua files in the serial directory. In your case, inherit from the serial simple class or base class.

you set the GlobalName property for access from lua.

assign your port from the serial plugin setup page.

add scripting actions as needed from the Action tree.

woniol
December 22nd, 2005, 04:00 AM
It realy helped....GlobalName property is what i was looking for.
Now i can write all functions in my own device file and access them from lua.
When i write my functions i'll post them on the forum together with schematics.

One more question for You. Is there a way (function) that would cause Girder triger event when one or more serial port control lines changes.
It should check selected lines with f.e.: GetDTR() or GetRTS() command and when it changes girder should triger en event.

It has something to do with CallBack function......

Promixis
December 22nd, 2005, 07:34 AM
you can add this to the ReceiveResponse method

woniol
December 27th, 2005, 01:40 PM
Can You some a short example of this.
Generating Girder event on serial.DSR (DSR line change).

Promixis
December 27th, 2005, 01:44 PM
start a new device file, using the SimpleSerial class.

In the ReceiveResponse method use this test

if math.band (code,serial.DSR) > 0 then
....

end


to test for a change in the serial line.

see the manual for using the gir.TriggerEvent command to trigger an event

woniol
December 27th, 2005, 02:33 PM
My test device file looks as simple as this:

local device = serial.Classes.Base:New({

Name = "I2C",
Description = "I2C driver",

BaudRate = 9600,
Parity = 0,
StopBits = 0,
DataBits = 8,
FlowControl = 'N',
IntraCharacterDelay = 500,
Status = "Not Running",
GlobalName = 'I2C',


Initialize = function (self)

self.Serial:SetDTR(false)
self.Serial:SetRTS(false)
win.Sleep(500)
self.Serial:SetDTR(true)
self.Serial:SetRTS(true)
win.Sleep(500)
self.Serial:RxClear()
self.Serial:Write('IR')
win.Sleep(500)

gir.LogMessage('I2C', 'I2C driver Enabled',3)
self:UpdateStatus ("Enabled")
return true
end,

ReceiveResponse = function ( self, data, code )
if math.band (code,serial.DSR) > 0 then
gir.TriggerEvent( "I2cinterrupt", 100 )
end
serial.Classes.Simple.ReceiveResponse (self,data,code)
end,

}
)
serial.AddDevice (device)

but ReceiveResponse method doesn't seem to work......

Rob H
December 27th, 2005, 02:37 PM
First of all you should probably base the device on serial.Classes.Simple rather than serial.Classes.Base

Try that first and if it still doesn't work let us know.

woniol
December 27th, 2005, 02:43 PM
Changed
local device = serial.Classes.Base:New({
to
local device = serial.Classes.Simple:New({

and still doesn't work,
any ideas?

Rob H
December 27th, 2005, 02:57 PM
Okay, when you say it doesn't seem to work just what do you mean?

You might want to put a few print statements in there to see if your code is being called.

woniol
December 27th, 2005, 03:09 PM
I tried it already:

ReceiveResponse = function ( self, data, code )
print('ReceivedResponse function called')
if math.band (code,serial.DSR) > 0 then
gir.TriggerEvent( "Myevent", 100 )
print('Event shoul be trigged')
end
serial.Classes.Simple.ReceiveResponse (self,data,code)
end,

i tried the same with serial.RING which is used in CallerID
and nothing is being printed in lua scripting console

like ReceiveResponse was not called at all.....

Rob H
December 27th, 2005, 03:13 PM
If you open up the variable inspector, can you confirm that you have a variable called I2C

woniol
December 27th, 2005, 03:15 PM
Yes, it's there.
And all stuff like
I2C.Serial:SetRTS(true)
ring=I2C.Serial:GetRI()
print(ring)
works....

Rob H
December 27th, 2005, 03:22 PM
I've just noticed that you aren't calling serial.Classes.Simple.Initialize(self) from your Initialize() function.

I think that might be the code that sets up ReceiveResponse as a callback.

Call that before you make any calls to self.Serial methods.

woniol
December 27th, 2005, 03:39 PM
:lol:
That was it.
Thank You very much.
I want to use to call a function when any of the I2C devices calls an interrupt.
This shoud be usefull for keypads. When a key is pressed 'interrupt line' changes its state, 'girder event is called, checks which button and then trigs another event.....
wow
Thank You again
I'll let You know now the progress of I2C plugin + schematics goes.

Rob H
December 27th, 2005, 03:47 PM
Good news.

Interesting project.