creating an object
Dgswaner
Posts: 795
I'm trying to make a simple object to control an LED, I want it to blink, be solid, stay on, etc. to be used as a status light. I can make the LED do what I want but it stops the program, I could use newcog but I'd prefer to not have to stop and start the cog to change the state of the LED. is there any documentation on creating an object? I've looked but I can't find anything.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
Comments
1) There are objects. This is a technique for encapsulating part of a program.
An object has its own data area and a bunch of subroutines.
Some of these subroutines are usable by other objects and some are not.
The ones that are usable are what define the behavior of the object to the rest of the "world".
2) There are cogs (processors). This is a mechanism for running parts of a program in parallel
You can have multiple objects that only use one processor.
You can have a single object that uses a whole bunch of processors to do its work
It turns out that it's handy to use an object to manage the use of a separate processor
so the rest of the program doesn't have to worry about the messiness of doing so.
For your LED blinking, don't worry about objects. Use cognew to start a Spin method to handle
the blinking using a separate cog. Get that working the way you expect it to work. If you want
to bundle that whole action up in a package for others to use, then learn how to take what you've
done and package it as an object.
This variable should tell the blinky cog what to do. Zero might mean "don't do anything other than wait for a while, then check again". One might mean "turn the LED on". Two might mean "turn the LED off". Values from three to ten might mean to "blink that many times a second".
Your blinky method just has a big CASE statement that chooses what to do based on the value of this variable (which is initialized to zero before doing the COGNEW). For blinking, the blinky method has to check the "magic variable" while it's blinking the LED to make sure that the main program hasn't changed the value, even to just a different blink rate.
Your main program just sets this variable to whatever it wants to happen.
Post Edited (Mike Green) : 7/10/2008 12:49:32 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
as a direct answer to your question: Yes it is the only way.
I'm not sure about what you want to do and what you have already understand and what not.
I would like to understand what you want to do and therefore it would be best if you post
your code regardless it is working or not. Programming is best learned by doing and learning from variying the code
and seeing what is happening.
best regards
Stefan
If all you want to do is turn the led on and off when required then you don't need to start another cog, just create functions in the object that can be called by your main program.
If you want the led to blink you have to ask yourself "do I want my main cog to take care of the blinking or do I want to leave it free". If you want the main cog to do the blinking then you will have to incorporate that carefully into the program, sending it into a loop will freeze the program. Of course the counters are a great way of creating a blinking led without the program having to do anything.
If you want to leave the main cog completely free then you will have to run the object in a seperate cog, creating a start function that will do the cognew.
Perhaps you might post a simple example program showing what you are trying to do.
Graham
It's best to think of a cog running a "process" rather than running an "object". The object controls which of its processes (or functions) run on separate cogs.
There are two main reasons to launch a cog: 1. To run assembly code (because the SPIN interpreter takes up a whole cog) or 2. To more easily implement concurrent or asynchronous processes. Otherwise, for the sake of simplicity you're better off trying to run your code in one cog (like the example here where you would use timers to blink your LEDs).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Graham
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
Post Edited (Dgswaner) : 7/18/2008 6:06:50 AM GMT