How to set nub modes... and read them IN FPC!


ZXDunny

Deep avatar
Joined
Oct 12, 2010
Messages
2,585
Hi -


As some of you may be aware, I'm developing a BASIC interpreter for the Pandora, PandBAS. What I want to do is add the ability to change nub modes in BASIC, and I understand that this involves writing a string to a device in the linux system. I've seen examples of how to do this in C/C++, but how does one go about it in FPC (Pascal)? Specifically, to read/write the nubs' modes, and query their position values.


Any ideas? Linux devices are not something I'm familiar with. I know you can do it with the echo command in a terminal, but I'd like to do it in code.


Cheers,


D.
 
from my basic understanding you can just open the device as a file and write to it ....


can't remember what the syntax is in pascal ... thats almost 20 years ago ;)


*edit*


here's the source to the c api used for reading the nubs and stuff.


http://git.openpando...pnd_io_evdev.c;


h=e88870a22304d89752d9cb5efd695ba56faf30bc;hb=2bb1805c65b796796a701e2934ceddcbd568544a


function of interest is



Code:
unsigned char pnd_evdev_catchup ( unsigned char blockp ) {


*edit2*


you'll need to translate input_event definition here


http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/linux/input.h#L26


and the defines here


http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/linux/input.h#L144


then i think it should be "easy enough" to read the nubs?
 
Last edited by a moderator:
I think you can open the device and manipulate it directly with ioctl. This is not trivial, and you risk breaking things.


Alternatively and a much better solution, you could open "/proc/pandora/nub0/mode" as a file and simply write the string "absolute" for joystick, or "mouse" for mouse, and then close the file. Same with nub1. Mode for mouse buttons is "mbuttons".


I'm assuming you know how to open a file for writing in Pascal. ;)
 
I'm assuming you know how to open a file for writing in Pascal. ;)

Cheers, that's exactly what I was looking for :)


My problem is that all file access in PandaBAS is sandboxed, so the system isn't available to me through my file i/o routines and I'll have to do it direct :)


Ta!


D.
 
Oh, and one other thing - do I read a file to get the nubs' x/y values? Does the nub mode affect that?


Edit: Also, as I'm new to this whole "writing to /proc/" thing, what data sizes do I need to use? I get that writing a string to it would be fine, but say I want to set CPU speeds, or nub sensitivity, or whatever, do I write values as 4-byte ints? Longwords? 8-byte floats (doubles)?


Edit of the edit: Say I open a TFileStream to "/proc/pandora/nub0/mode" - how many bytes do (or can) I read? Just read a buffer of say, 10 bytes and that will be enough? If I open a real file and read ten bytes from a 5 byte file, FPC will return a partially filled buffer (or rather, it will partially fill the buffer and return the number of bytes read). Will these "pseudo-files" behave the same way?


D.
 
Last edited by a moderator:
/proc and /sys files are treated exactly the same as any other file on the system. Open them and read the entire file exactly as you would any file, and write back the same way. The only difference is what the system does after you close the file.


As for reading the actual X/Y coordinates of the mouse, I'm not sure if there's such an easy way of doing it. Does Pascal not have mouse bindings?


edit: and for the rest of your question about writing ints, don't. Everything is strings. To change the CPU speed to 600mhz, write the string "600" to /proc/pandora/cpu_mhz_max. Open the file again and read in the string to find out what the current CPU max speed is.
 
Last edited by a moderator:
As for reading the actual X/Y coordinates of the mouse, I'm not sure if there's such an easy way of doing it. Does Pascal not have mouse bindings?

Oh, I can already read the mouse - MOUSEX and MOUSEY and MOUSEBTN functions return those, but I grab them from SDL events. The stuff I'm after isn't part of SDL as far as I'm aware :)


But the nubs and the touchscreen both return mouse events, and it would be nice if I could set nubs to stick mode within the BASIC program (with say a NUBMODE id,mode command) and give the user access to all the input devices. NUBX and NUBY functions would be nice too, so I need to be able to read the nubs' positions too.

edit: and for the rest of your question about writing ints, don't. Everything is strings. To change the CPU speed to 600mhz, write the string "600" to /proc/pandora/cpu_mhz_max. Open the file again and read in the string to find out what the current CPU max speed is.

Ahh, that's much clearer. Thanks!


Is there a list of /proc/ and /sys/ "files" that can be accessed, and what they do?


D.
 
Last edited by a moderator:
Is there a list of /proc/ and /sys/ "files" that can be accessed, and what they do?
/sys/ is readonly as far as I know and most file in /proc are either read-only or only writable by root (definatly a good thing).


And for what they do depend, trial & guessing is a good way to know, else I might find that manual for you ;)
 
I don't think there's an official list. Just do a directory listing of "/proc/pandora/" and "/sys/class/". They should be pretty self explanatory. :)


As for reading the joystick in absolute mode, I'm positive there was a way to open a file for streaming and read packets off of it (like, 2 bytes for x, 2 bytes for y, etc...) but I can't remember how I did that, or even if I did that and didn't just imagine such an easy solution.
 
Look in libpnd; theres example source for all this, and I've tried to keep it deliberately simple and commented.


(see pnd_io_evdev for a simple C api to reading inputs including nubs X/.Y)


jeff


For that matter, you could probably use libpnd :)


(I should add some notes for the very oldest code, some of the stuff needs root to work, such as setting cpu speed; for that, you'd use the op_cpu type sh-scritps which are sudo already, but the io stuff is all ready uto use in any app.)
 
Last edited by a moderator:
If you can do what you want with SDL, use that, I'd even say we should remove libpnd functions which duplicate that functionality and see if something breaks. This stuff is painful to maintain, I have to do pretty nasty kernels hacks for things like libpnd (input handling part of it) to keep working.
 
In LOAD81, I'm writing to the /proc file and reading from it like any other file (to switch modes), but using SDL_Joystick events to receive data. It works pretty well ..
 
In LOAD81, I'm writing to the /proc file and reading from it like any other file (to switch modes), but using SDL_Joystick events to receive data. It works pretty well ..

That was what I was expecting to have to do. I assume that SDL_Joystick events do not apply when the nubs are in mouse modes?


D.
 
Ok, ANOTHER problem!


I initialise SDL (with joystick initialisation), I enable the joysticks, I assign SDL_Joysticks to each of the nubs. I can then read the two axes of each nub without issue. However, this only works if I set the nubs to joystick mode before I start my program.


If the nubs are in mouse mode before I start, then when my program switches them to joystick mode (and I've verified that the switch actually works!) then SDL still cannot read the axes, despite them being in the right mode.


I've even tried SDL_QuitSubSystem() and SDL_InitSubSystem() and re-assigned then nubs to new SDL_Joystick types, but still no go. I just cannot read the joystick axes even though I've programmatically changed their modes to "absolute" mode. The only time I can read them is when I set them up before my program runs, but that's unacceptable - the user needs to be able to change them to whatever mode they like at will, and if that mode is absolute, they should be able to read the axes :(


Any idea what else I can try?


D.
 
Back
Top