Re: euphoria to arduino
- Posted by mattlewis (admin) Jan 09, 2012
- 2309 views
Thanks again Arthur, it worked, also changed lpWBbuf to object
----------------------- sio_write ----------------------- global function sio_write(integer port, object lpWBuf, integer len) --return c_func(sio_write_,{port, lpWBuf, len}) return c_func(sio_write_,{port,allocate_string(lpWBuf),8}) end function
I can now send a string of 8 bits to turn on or off 8 LED's, which will be transistors eventually.
Note that the way you have written this, you will end up leaking memory. I don't know how long your program runs, or how often this function is called, but eventually, it will eat up all of the memory on your system.
The function, allocate_string() allocates memory from the operating system. If you are using Euphoria 4.0, you can add an extra parameter (which Arthur did) to specify if it should be freed when all of your references go away. This uses a new feature for 4.0. However, if you're using 3.1, then you'll need to do something like this:
global function sio_write(integer port, object lpWBuf, integer len) atom ptr, ret ptr = allocate_string( lpWBuff ) ret = c_func(sio_write_,{port,allocate_string(lpWBuf),8}) free( ptr ) return ret end function
If you know that you'll be using a particular size string (say, 8 bytes), then you could do something like this:
constant ptr = allocate( 8 ) global function sio_write(integer port, object lpWBuf, integer len) poke( ptr, lpWBuff ) return c_func(sio_write_,{port,ptr,8}) end function
That way, you save yourself the time of allocating and freeing the memory constantly.
Matt