View Full Version : C++ style classes in SPIN
05-20-2010, 07:41 PM
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
05-20-2010, 09:02 PM
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.
05-20-2010, 10:35 PM
I agree with Mike's suggestion about using arrays in Spin.· It would look something like this:
· MAX_NAME_SIZE = 20
· MAX_ROOMS···· = 15
· MAX_DEVICES·· = 50
· RoomNames···· byte 0[MAX_NAME_SIZE*MAX_ROOMS]
· DeviceNames·· byte 0[MAX_NAME_SIZE*MAX_DEVICES]
· DeviceRooms·· byte 0]MAX_DEVICES]
· DeviceTypes·· byte 0[MAX_DEVICES]
· DeviceNumbers byte 0[MAX_DEVICES]
· DeviceStates· byte 0[MAX_DEVICES]
The names should be null-terminated, and you would access entries as follows:
· Room := DeviceRooms[Index]
· Type := DeviceTypes[Index]
· Number := DeviceNumbers[Index]
· State := DeviceStates[Index]
· DeviceName := @DeviceNames + (Index * MAX_NAME_SIZE)
· RoomName := @RoomNames + (Room * MAX_NAME_SIZE)
05-20-2010, 11:40 PM
Thanks, this is massively helpful. Should keep me going for a while
05-21-2010, 06:31 AM
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.
Catalina - a FREE C compiler for the Propeller - see Catalina (http://forums.parallax.com/showthread.php?p=844004)