Shop OBEX P1 Docs P2 Docs Learn Events
C++ style classes in SPIN — Parallax Forums

C++ style classes in SPIN

Jon87Jon87 Posts: 14
edited 2010-05-20 23:31 in Propeller 1
My current project shows a menu on a tv and uses a remote control to control X10 devices on or off etc.

The next step is to allow the user to add X10 devices through the menu. The main problem i see is that each X10 device will have a name, a room, a number (1 to 16) and a type (appliance or lamp). All these values need to be linked somehow

for example, if the user wants to control a microwave with X10, they would enter "microwave", select kitchen, select number 3 and select appliance.
I would then add "microwave" to the menu and if i select it, i'll have the option to turn it on or off(using the number 3 which is linked to "microwave")

I'm not 100% sure but i think in C++, a class would be used to achieve this

Any help would be greatly appreciated

Cheers
Jon

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-20 14:02
    Spin doesn't provide C++ style classes. You could certainly create an object that implements X10 devices, but most I/O drivers are written so that, when you declare a reference to them, you always get a separate instance with its own instance variables. This is not what you want for something like a TV driver that would be referenced from your main program and an X10 device object that implements the X10 menus. It's possible to modify a TV driver so that the variables are global to the instances (like class variables), but you may not want to do that.

    What I'm saying is that you can implement your menu items by using an object, but it may not be as easy as you think and may not save you much in the way of effort. You may be better off just using an array for each type of datum (like name, room, etc.) in parallel and integrating this into your main program.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-05-20 15:35
    I agree with Mike's suggestion about using arrays in Spin.· It would look something like this:

    CON
    · MAX_NAME_SIZE = 20
    · MAX_ROOMS···· = 15
    · MAX_DEVICES·· = 50

    DAT
    · RoomNames···· byte 0[noparse][[/noparse]MAX_NAME_SIZE*MAX_ROOMS]
    · DeviceNames·· byte 0[noparse][[/noparse]MAX_NAME_SIZE*MAX_DEVICES]
    · DeviceRooms·· byte 0]MAX_DEVICES]
    · DeviceTypes·· byte 0[noparse][[/noparse]MAX_DEVICES]
    · DeviceNumbers byte 0[noparse][[/noparse]MAX_DEVICES]
    · DeviceStates· byte 0[noparse][[/noparse]MAX_DEVICES]

    The names should be null-terminated, and you would access entries as follows:

    · Room := DeviceRooms[noparse][[/noparse]Index]
    · Type := DeviceTypes[noparse][[/noparse]Index]
    · Number := DeviceNumbers[noparse][[/noparse]Index]
    · State := DeviceStates[noparse][[/noparse]Index]
    · DeviceName := @DeviceNames + (Index * MAX_NAME_SIZE)
    · RoomName := @RoomNames + (Room * MAX_NAME_SIZE)

    Dave
  • Jon87Jon87 Posts: 14
    edited 2010-05-20 16:40
    Thanks, this is massively helpful. Should keep me going for a while
  • RossHRossH Posts: 5,519
    edited 2010-05-20 23:31
    Hi Jon,

    SPIN objects are not objects in the sense this term is used in C++. In other languages SPIN objects would be called "program units", "modules" or "packages" - they provide encapsulation of code and data and define an interface but are not themeselves a data type.

    If you are coming from C++ you may get a better grip on SPIN if you think of it as a modular language rather than an object-oriented one.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
Sign In or Register to comment.