Shop OBEX P1 Docs P2 Docs Learn Events
object oriented programming: pointer passing — Parallax Forums

object oriented programming: pointer passing

Chris MicroChris Micro Posts: 160
edited 2009-05-15 07:47 in Propeller 1
Hello Together,

how can I pass one Object as an argument to another object?

Here is the problem:
Lets assume someone wants to write a windows manager. To initialize the windows manager it is necessary to pass the pointer of the vga driver so the vga functions can be used.

Has anyone an idea how to do it in spin?

Thanks,
chris

Comments

  • rjo_rjo_ Posts: 1,825
    edited 2009-05-14 05:23
    Chris,

    You could have 3 or 4 objects... your top object would talk to your i/o manager, your window manager and the vga driver.

    If you are trying to figure things out after reading the manual... don't. The manual is intended mostly as a reference guide. If you are really experienced, you can get buy with just the manual.

    Take a look at the education labs. If you go through them in sequence, everything will be a lot easier.

    Rich
  • RossHRossH Posts: 5,511
    edited 2009-05-14 05:38
    Hi Chris,

    SPIN is not an object oriented language in the sense you are probably used to. It uses some object-oriented terminology (e.g. calling functions 'methods), but what SPIN calls 'objects' are primarily a packaging convenience. They are more like what other languages call 'units' (e.g. Ada) or 'modules' (e.g. Modula-2).

    Ross.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-14 06:13
    Chris Micro said...
    how can I pass one Object as an argument to another object?
    To answer your question more directly: you can't.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2009-05-14 06:25
    To do what you want to do in Spin, the vga has to become a global resource.
    If you are using VGA_Text.spin and VGA.spin for example, all the per instance VAR
    variables in VGA_Text.spin need to be converted to global DAT variables. This will
    let you call vga.str(...) from any object that includes OBJ vga : "VGA_Text".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • Chris MicroChris Micro Posts: 160
    edited 2009-05-14 07:11
    The main reason why I asked this question was that I wanted to have well encapsulated objects.
    Isn't this the main cause for the OBEX?

    The idea was to be able to write a main programm with

    obj vga_text
    obj windows
    ...

    They are glued together in the main program. But to use windows, it is necessary to pass the object.
    jazzed said...
    If you are using VGA_Text.spin and VGA.spin for example, all the per instance VAR
    variables in VGA_Text.spin need to be converted to global DAT variables.

    That sounds very awkward to handle. What if someone wants to exchange the vga_text by tv_text? The procedure has to be done again.

    Is there a good way how this problem can be solved?
  • SRLMSRLM Posts: 5,045
    edited 2009-05-14 07:24
    Not quite OT, but here is a thread that helped me get my feet wet with the Propeller. You may find something useful floating around in there...
    http://forums.parallax.com/showthread.php?p=765086
  • rjo_rjo_ Posts: 1,825
    edited 2009-05-14 13:09
    Chris,

    Everything you want to do... you actually can do with the Prop.

    One of the problems I had was in my own preconceptions about how I thought things
    would work. It is amazing how confusing it can be, if you start out with the wrong constructs.

    You can have two objects calling the same driver, you just have to include the driver in two places and initialize it. Two different uses can have two different sets of initialization data, and Spin is clever enough to keep things separate in a very compact and efficient manner.

    You will hear that there are no pointers in Spin... there are pointers all over the place, but each one has a specific purpose and is intended to be used in a specific way, and they aren't called pointers[noparse]:)[/noparse]

    If you want to do something really complicated... you are going to have to be able to access lots of information. Right now, the best memory objects are for SD cards. There is a way to directly invoke a spin program and use data stored to a work file directly from an SD card... So, the sky is the limit. I'm not a professional programmer... but I think there is a general concept called "context switching." In the case of the Prop,
    the easiest way to switch contexts is to load a Spin program for each context switch and then populate your variables from a file you keep on your SD card. I've experimented with the general idea... it works and it is fast enough to be transparent to the user.


    Take a look at Oldbit's PropDos... lots of good stuff and your window manager would fit right in[noparse]:)[/noparse]

    Rich
  • jazzedjazzed Posts: 11,803
    edited 2009-05-14 13:36
    Chris Micro said...
    That sounds very awkward to handle. What if someone wants to exchange the vga_text by tv_text? The procedure has to be done again. Is there a good way how this problem can be solved?
    That is the way it can be solved in Spin. The TV_Text.spin "object" has the same public API as VGA_Text, so that is not a problem. The FullDuplexSerial "object" has a very similar API. It is easy to swap one for another for text output. You can add or delete whatever you want to any "object" for your use or sharing with others. Overloads and polymorphism would be better of course but Spin is not really an OOP language, it's a dot-do-this language.

    I've been down the road you are on. Your question is the first one I asked when I joined the forum last year ... the only reason I joined. I've been angry because the product did not fit my ideal. In the end one throws up one's hands and lives with the limitations or moves on to a better fit. The lack of memory is the biggest challenge though it has been fun trying to overcome that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-14 13:46
    You might also look at BST (BradC's Spin Tool) which is a 3rd party Spin IDE. It, like another 3rd party Spin compiler from mpark, has provisions for conditional compilation which makes it easier to substitute one object for another (like TV / VGA drivers). When you include an object, you create an instance of its variables. The compiler is smart enough to only include one copy of the code and of the constants / variables in the DAT sections. If you put all of the variables in the DAT sections(s), you can include the object multiple times with only one instance created ... very handy for the sort of use you intend.
  • mparkmpark Posts: 1,305
    edited 2009-05-14 14:42
    Homespun also supports object pointer passing.

    http://forums.parallax.com/showthread.php?p=777920
  • Chris MicroChris Micro Posts: 160
    edited 2009-05-14 15:22
    Thank you all for your help. A lot of stuff to read and links to follow ...

    Meanwhile I wrote a mini minimalistic window manager ( don't expect to much ) to test the concepts I can use in Spin.
    I tried to have a very clear structure of the window object.
    I would be glad if someone likes to discuss the structure and if it is suitable.

    There are heaps of problems which I discovered:

    1. somehow it is difficult to use strings and pointers with the bytemove command
    2. is there a dynamic memory allocation mechanism?
    Because the windows can have different sizes and in the example program I have to reserve always memory for the maximum size
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-14 16:39
    1) It's not difficult to use strings and pointers with the BYTEMOVE statement. What problems are you having?

    2) There's no built-in dynamic memory allocation mechanism. With the limited memory available, a generalized dynamic memory allocator is not that useful although one was written and is in the Object Exchange. I wrote a Propeller OS a long time ago that's referenced in one of the permanent thread indices (http://forums.parallax.com/showthread.php?p=609066). This was designed to use either a TV or a VGA display driver and allocated work areas for the various I/O drivers dynamically from the high end of memory downwards towards the stack.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-05-15 07:44
    Chris Micro said...
    how can I pass one Object as an argument to another object?
    If you really want to do this it is possible but to be useful it requires some extensions to the SPIN language that then need changes to the compiler or a preprocessor. Have a look at this thread if you really want to try and you'll get some idea of the problems you get trying to do it without the compiler/preprocessor. http://forums.parallax.com/showthread.php?p=701497
    If you want to write a preprocessor (that will run on a mac) than I'll finish off DOL.
    said...
    Here is the problem:
    Lets assume someone wants to write a windows manager. To initialize the windows manager it is necessary to pass the pointer of the vga driver so the vga functions can be used.
    I tried writing a window manager as well using the graphics driver. Wasn't very useful though because I didn't have enough memory. If you keep everything text based though it shouldn't be a problem. One comment on the code you attached is that just returning a string isn't going to be very helpful if you want your windows arbitrary sizes and places. You'll need to also include the width, height and position to the drawing routine. There has been at least one other window manager. Can't remember where I got it from so I've attached it.
  • Chris MicroChris Micro Posts: 160
    edited 2009-05-15 07:47
    Mike Greeny said...

    permanent thread indices (forums.parallax.com/forums/default.aspx?f=25&m=148376).

    Hi Mike, this is a very usefull link list I didn't know. It takes somehow time until I can find all information for the propeller burger.gif

    >1) It's not difficult to use strings and pointers with the BYTEMOVE statement. What problems are you having?
    The problem in my first example was that for the title there were only 4 chars written on the screen. I discoverd now that the strlength method didn't work:

    repeat n from 0 to strsize(stringptr)
        title[noparse][[/noparse]n]:=byte[noparse][[/noparse]stringptr++]
      'bytemove(@title,@byte[noparse][[/noparse]stringptr],XMAX)
    



    I replace it by this, which works
    PUB setTitle(stringptr)|ptr
      ptr:=@title 
      repeat
        byte[noparse][[/noparse]ptr++] :=byte[noparse][[/noparse]stringptr++]
      while byte[noparse][[/noparse]stringptr]<>0
      byte[noparse][[/noparse]ptr]:=0
    


    Mike Green said...
    2) There's no built-in dynamic memory allocation mechanism. With the limited memory available, a generalized dynamic memory allocator is not that useful although one was written and is in the Object Exchange. I wrote a Propeller OS a long time ago that's referenced in one of the permanent thread indices (forums.parallax.com/forums/default.aspx?f=25&m=148376).

    Congratulations to your work smile.gif
    My litle example is not meant to be a Operating system. I just want to see how far some object oriented principles are usefull on the propeller.
    And probably even the example programm might be usefull: it should be a simplicistic window manager. With the limited screen size it is often very helpfull to divide the screen in several tiles ( windows ) and to direct the ouput to this windows.

    I tried to implement some obect oriented principles I know from java: the getters and setters

      window.setPos(10,6)
      window.setTitle(string("second window"))
      window.setText(string("      counter"))
    



    Mike Green said...
    2) There's no built-in dynamic memory allocation mechanism.
    In my example I create several window objects. But I need some internal buffers in the object.
    For windows with smaller size less buffer memory would be needed. But I don't know how to create windows with different buffersize.

    As attachement the improved window version.

    chris
Sign In or Register to comment.