r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Red] Red language group

Kaj
7-Feb-2012
[4820x4]
Ok, though I can't think of anything I need to write in R/S at present. 
 I need GUI based apps though
A cross-platform GTK+ binding is available, with a REBOL like dialect
if one wants to test Red/System: port some simple C stuff?
Yes, that's a good approach
Pekr
11-Feb-2012
[4824x7]
I am trying to wrap our LED screen control dll. I am not sure how 
well it is defined, as LED Studio and surrounding SW is rather weak 
and sometimes crashes, but I tried in R2, thinking I again reached 
some R2 DLL interfacing limit/bug, and am trying now in Red/System. 
Well, my first attempt to wrap some DLL functions here. So - I can 
turn-on/off led screen, even if I don't set COM port, open-sending-card, 
etc. But when I try to call functions to get e.g. brightness, contrast, 
it crashes. Those funcs are defined as e.g.:

typedef int	 (WINAPI *LSN_GETBRIGHT)();       // 0..100
typedef bool (WINAPI *LSN_SETBRIGHT)(int);
typedef int (WINAPI *LSN_GETCOLORTEMP)(int);//ScreenNumb

typedef bool (WINAPI *LSN_SETCOLORTEMP)(int,int);//ScreenNumb,nColorId 
0,1,2,3


None of above functions work for me, although above code is from 
sources to LEDSet application, where those funcitons work, those 
are just being set via dialog boxes (which I can invoke even from 
Red/System, so those are part of DLL ...

My definitions are:

      led-get-brightness: "LSN_GetBright" [
         return: [integer!]
      ]           

      led-set-brightness: "LSN_SetBright" [
         brightness [integer!]
         return: [integer!]
      ]

      led-get-color-temperature: "LSN_GetColorTemp" [
          screen-number [integer!]
          return: [integer!]
       ]                 


etc. So what coul be causing run time error? I am running on a PC, 
where I don't have internal LED screen communication card. I thought, 
that DLL functionality might check for the screen, can't find it, 
and so the app returns error, which does not fit return value - e.g. 
some error code/string, or a dialog box. But moving the exe to the 
PC where the card is, it i just the same - some functions work, I 
can see LED screen being turned on/off, but those brightness etc. 
don't ....
The error I get is something like this:

*** Runtime Error 99: unknown error
*** at: 7572FC56h
Reading the docs, i have one question - could we say, that aliased 
sctructures are the same what we know as a literals in REBOL? I can 
imagine the declaration being :

book: literal struct![]


so instead of calling it a virtual datatype and suggesting to use 
exclamation mark to distinguish it in a code, it could as well be:

'book: literal struct! []

Anyway - it does not matter, just curious ....
Maybe one another question - I know Red/System is supposed to be 
closer to C, but it will be also used by those, who would like to 
proceed using Red itself (that is why I did not like the print-line, 
?? vs print/prin) - so the question - why is there so many constructors, 
instead of one make! and e.g. a block format? I mean:

declare struct!
alias struct!
#import for a library

Intead of:

make struct!
make lit-struct!
make routine!


? I will understand the answer of a type, that it is better to be 
understood by those knowing C language. So far, I can't e.g. understand, 
why a library is wrapped in a preprocessor kind of way, using #import, 
which does not fit those preprocessor things like #define, #ifdef, 
etc.?
Btw - once you add TYPE? function,I wonder how you distinguish declared 
struct! from an aliase one :-) I know that function will return integer, 
but you will surely print some name in the end, so will you come 
with an artificial type-name for an aliased struct to distinguish?
If it would be upon me, I would better try to stay consistent with 
the Red level - so if Red will have make!, Red/System should have 
make too, no matter how C users will aproach it. Those who will use 
Red/System, are going to use Red most probably too, so I can not 
see much reasons to differ, just because we can state - Red and Red/System 
are two different things :-) I am curious how you will keep up with 
such a differences in future, once Red layer becomes available :-)
c: 2

p/c: 1234                              ;-- writes 123 (4 bytes) at 
40000004h


Isn't there a typo? Shouldn't it be - "writes 1234 (4 bytes) at 40000004h"?
Kaj
11-Feb-2012
[4831]
Looks like it was written with a sleep deficit :-)
Pekr
11-Feb-2012
[4832]
just a small typo, so other than that, docs are good :-)
Kaj
11-Feb-2012
[4833x2]
Literals are a quite generic concept from REBOL. An ALIAS is a much 
more specific thing: it's a name for a struct!. If you want to compare 
it to REBOL, you could say that it's bound to the struct, so not 
just a literal
I wonder if TYPE would be a better name for it
Pekr
11-Feb-2012
[4835]
so that is has already a type, and the type is struct, whereas literal 
is just a literal?
Kaj
11-Feb-2012
[4836]
There are no literals in Red/System, at least not as symbols
Pekr
11-Feb-2012
[4837x3]
because "alias", although I am not a native english speaking person, 
in my understanding, is something already existing, which we reference. 
But a struct alias is just like a class, it is not structure yet. 
But, it does not probably matter. Simply put coming from REBOL I 
would like to see those two similar. Reading further in docs I can 
see, that REBOL can't be easily mapped to C, so some specialisties 
might be needed ...
It is more of a philosophy, what is more confusing to whom. E.g. 
Carl proposes dialecting as the same word, being simply used in different 
context. In real life, it might work, but not sure about programming. 
E.g. 'copy in 'parse having slightly different aspects than 'copy 
in REBOL itself. That is why I thought that make! could be used even 
in Red/System, but not sure ...
The reason why I am also opting for such an aproach is, that there 
will be possibility to inline Red/System in Red itself, so it should 
look similar, where possible. I e.g. liked how Cyphre did JIT to 
R2 - the code still looks like REBOL, it is just a limited subset 
...
Kaj
11-Feb-2012
[4840x3]
I share some of your uneasy feelings about the overuse of preprocessing 
constructs. I would like them to become first-class, but it's hard 
to come up with the best solutions
I think Doc will say that we'll reconsider them in the Red/System 
rewrite in Red
Your binding looks quite good to me, but how is your #import defined, 
and how is WINAPI defined in the C code?
Pekr
11-Feb-2012
[4843]
Yes, that might actually happen, after all Red/System is the target, 
so new needs might show-up during the Red design, who knows ....
Kaj
11-Feb-2012
[4844]
They will, and they have
Pekr
11-Feb-2012
[4845]
I tried stdcall and iirc even cdecl. How do I find out?
Kaj
11-Feb-2012
[4846x2]
It should probably be cdecl, and the WINAPI definition probably contains 
the answer. Did it make a difference?
The issue looks to be that the C functions are defined as WINAPI 
pointers, instead of just WINAPI functions
Pekr
11-Feb-2012
[4848]
No, still error ...
Kaj
11-Feb-2012
[4849]
Is LSN_GETBRIGHT really defined as LSN_GetBright, etc, or are the 
function names different?
Pekr
11-Feb-2012
[4850x2]
maybe not, I found this: 

extern LSN_GETBRIGHT		fpGetBright	;
Does it mean, that the real func name is fpGetBright?
Kaj
11-Feb-2012
[4852]
I think so, but the C code is also doing an indirection through a 
function pointer, hence the fp
Pekr
11-Feb-2012
[4853x2]
But some functions  work although defined the same way. There might 
be some more functionality happenening under the hood, and who knows, 
that the code tries to return. OTOH the specs are clear enough - 
if it is supposed to return integer, then integer is simply an integer. 
And dialog windows work - I e.g. got dialoc saying "LED screen not 
found", so the GUI is inside the dlls ...
Never mind, I just wanted to try it in Red. Good way to become familiar 
with the system trying to use it for practical purpose. I might try 
to continue investigatin COM communication, but R2 serial communication 
is also not much to be desired :-)
Kaj
11-Feb-2012
[4855]
Are all C functions defined as pointers (with * and fp markers)? 
Can you give an example of a binding that works?
Andreas
11-Feb-2012
[4856]
typedef int (WINAPI *LSN_GETBRIGHT)()


^^ That defines LSN_GETBRIGHT to be another name for a pointer to 
a stdcall function that takes no arguments and returns an int. (WINAPI 
== __stdcall)

extern LSN_GETBRIGHT		fpGetBright	;


^^ That now is the actual "function", although held indirectly as 
a function pointer. So somewhere in a library is a fpGetBright value 
which holds a LSN_GETBRIGHT function pointer.


In C you can directly call that as e.g. `int brightness = fpGetBright();`. 
However, I fear in Red/System it's at the moment not possible to 
perform that call.
Pekr
11-Feb-2012
[4857x2]
sorry, sitting in the bar, running via team viewer on my htc sensation, 
so limited pc usability right now :-)
Andreas - interesting. How is that some functions being defined the 
same way work?
Andreas
11-Feb-2012
[4859]
No idea, which one works :) ?
Pekr
11-Feb-2012
[4860x2]
typedef bool (WINAPI *LSN_ISPOWER)(void);		//??????,???0?1?
typedef bool (WINAPI *LSN_POWER)(bool);			//??????,??0??,1??
typedef bool (WINAPI *LSN_ISLOCK)(void);			//????,???0?1?
typedef bool (WINAPI *LSN_LOCK)(bool);			//????,??0?1?
uff, team viewer had someproblem switching focus, or it was me :-) 
so, turning on and off the screen works ... not just a - led screen 
reacts ...
Andreas
11-Feb-2012
[4862x2]
Well, maybe there actually is a LSN_Power function in the DLL you 
are linking to :)
I'd directly look at the DLL's exports first, to see what's actually 
going on there. After that you can go looking for the corresponding 
definitions in C headers or docs.
Pekr
11-Feb-2012
[4864]
actually i have a whole app source fes, at least i think. but there 
is not much to look into, that is just an utility to parametrose 
led screen. I dont want to bother anyone with it, I thought you might 
see some obvious error i am doing, but apparently the issue is more 
complex ...
Kaj
12-Feb-2012
[4865x3]
The obvious error is that the C function names you're using are incorrect. 
As Andreas said, you probably happened to hit some functions by that 
name for the ones that do something
The complex issue is that the functions are imported through pointers. 
Red/System can use function pointers if you write wrapper functions 
and pass them the function pointers. But Red/System can't import 
simple values yet, only functions, so you can't import the function 
pointers the way the C code does
However, the functions may also be exported the normal way, in which 
case you wouldn't have to use the pointers, but you would have to 
find the names of the real functions. For fpGetBright I would look 
for GetBright for example
Pekr
12-Feb-2012
[4868x2]
Here's how they are defined:


fpIsPower		= (LSN_ISPOWER)GetProcAddress(g_hLedCtrlInst, "LSN_IsPower");

 fpPower			= (LSN_POWER)GetProcAddress(g_hLedCtrlInst, "LSN_Power");

 fpIsLock		= (LSN_ISLOCK)GetProcAddress(g_hLedCtrlInst, "LSN_IsLock");
	fpLock			= (LSN_LOCK)GetProcAddress(g_hLedCtrlInst, "LSN_Lock");

 fpGetBright		= (LSN_GETBRIGHT)GetProcAddress(g_hLedCtrlInst, "LSN_GetBright");


First three work, fpGetBright, which is wrapped as LSN_GetBright 
(as this is actual name in exports of DLL), does not work. They all 
look to me being defined the same way, no? How is that first three 
work?
btw - does that mean, that Red/System is still not generally good 
enough, to wrrap "any" C interfacing?