Timer in Morph

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Timer in Morph

Garret Raziel
hi, can anyone help me with some code? I want to have a Morph object that change his color by values that squeak gets from SerialPort. I have wroted code of getting values and changing color but I dunno how to do that the morph change its color forever. If i call method "AMorph changeColor" in some to:do: it stucks and shows only last color. I want to use some kind of timer, that will call this method every second and updates this Morph. what must I use?

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Edgar J. De Cleene
Re: [Newbies] Timer in Morph


On 2/4/10 7:18 PM, "Garret Raziel" <[hidden email]> wrote:

hi, can anyone help me with some code? I want to have a Morph object that change his color by values that squeak gets from SerialPort. I have wroted code of getting values and changing color but I dunno how to do that the morph change its color forever. If i call method "AMorph changeColor" in some to:do: it stucks and shows only last color. I want to use some kind of timer, that will call this method every second and updates this Morph. what must I use?

You should use step and stepTime methods.

In step you put whatever you like your morph do.
In stepTime  you put the time in milliseconds for each change

I add a example, you could drag and drop on any Squeak you have and see a red thing in the upper corner of Squeak telling the name of the current project and position of mouse.

Select load as morph.

Edgar


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

InfoMorph.morph (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Andreas.Raab
In reply to this post by Garret Raziel
Garret Raziel wrote:
> hi, can anyone help me with some code? I want to have a Morph object
> that change his color by values that squeak gets from SerialPort. I have
> wroted code of getting values and changing color but I dunno how to do
> that the morph change its color forever. If i call method "AMorph
> changeColor" in some to:do: it stucks and shows only last color. I want
> to use some kind of timer, that will call this method every second and
> updates this Morph. what must I use?

Use stepping, i.e.,

MyMorph>>wantsSteps
        "Yes, please"
        ^true

MyMorph>>stepTime
        "1/sec please"
        ^1000 "msecs"

MyMorph>>step
        self changeColor.

Cheers,
   - Andreas
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Garret Raziel
In reply to this post by Edgar J. De Cleene
Thanks so much, it just works! And I have another problem, but now I will be propably more difficult. I have morph that reads values from SerialPort and on the other end of Serial cabel I have an arduino (AVR microchip). I am reading values from 0 to 255 on arduino's pin (I have infrared receiver here) and I am sending them immediately over serial to squeak. Here I am setting Morph's color to value I have received every 500 steps. I have this code:

readColor
    | s n |
    n := SerialPort new.
    n baudRate: 9600.
    n openPort: 7.
    s := n readString asInteger.
    Transcript show: s; cr.
    s := s - 46 * (1/60).
    self color: (Color r:s g:s b:s).
    n close.

and it works almost fine. But there is a bug. Time to time I receive only a part of value, for example I am receiving: 56 55 56 55 5 6 55 ... 102 103 101
103 100 102 1 1 0 102 102. Those one-digit values are truly false and it looks like the mistake in receiving. How can I synchonize sending and receiving? Or do I have to use other method of SerialPort? Thanks.

On Thu, Feb 4, 2010 at 9:46 PM, Edgar J. De Cleene <[hidden email]> wrote:



On 2/4/10 7:18 PM, "Garret Raziel" <[hidden email]> wrote:

hi, can anyone help me with some code? I want to have a Morph object that change his color by values that squeak gets from SerialPort. I have wroted code of getting values and changing color but I dunno how to do that the morph change its color forever. If i call method "AMorph changeColor" in some to:do: it stucks and shows only last color. I want to use some kind of timer, that will call this method every second and updates this Morph. what must I use?

You should use step and stepTime methods.

In step you put whatever you like your morph do.
In stepTime  you put the time in milliseconds for each change

I add a example, you could drag and drop on any Squeak you have and see a red thing in the upper corner of Squeak telling the name of the current project and position of mouse.

Select load as morph.

Edgar


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re[2]: [Newbies] Timer in Morph

Herbert König
Hi Garret,

i can only guess here but i would use one Serial port and keep it open
all the time.

GR> readColor
GR>     | s n |
GR>     n := SerialPort new.
GR>     n baudRate: 9600.
GR>     n openPort: 7.

What happens if the port is opened in the middle of a transmitted
Byte? I will take the first rising edge of the signal as a start bit.


GR>     s := n readString asInteger.
GR>     Transcript show: s; cr.
GR>     s := s - 46 * (1/60).
GR>     self color: (Color r:s g:s b:s).
GR>     n close.

GR> and it works almost fine. But there is a bug. Time to time I
GR> receive only a part of value, for example I am receiving: 56 55 56
GR> 55 5 6 55 ... 102 103 101
GR> 103 100 102 1 1 0 102 102. Those one-digit values are truly
GR> false and it looks like the mistake in receiving.

The example I constructed might account for this error

GR> How can I
GR> synchonize sending and receiving? Or do I have to use other method
GR> of SerialPort? Thanks.

Depends strongly on what you can do on your microcontroller. And I
hope your infrared is not involved in the serial communication. If you
use a Serial to USB converter we have another candidate for errors.

Assuming the serial ports buffer is big enough to collect all data
while you're not listening, I would:

- Open the port maybe when the Morph is created
- in the step method read a string (or ByteArray)
- only use the last received Character or Byte.
- close the port on deleting the morph.

If that doesn't help come back with a more detailed description of
your setup and the problem. Can you afford to loose data? Can you
influence what is sent from the microcontroller?


--
Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Yoshiki Ohshima-2
In reply to this post by Garret Raziel
At Fri, 5 Feb 2010 18:56:43 +0100,
Garret Raziel wrote:
>
> and it works almost fine. But there is a bug. Time to time I receive only a part of value, for example I am receiving: 56 55
> 56 55 5 6 55 ... 102 103 101
> 103 100 102 1 1 0 102 102. Those one-digit values are truly false and it looks like the mistake in receiving. How can I
> synchonize sending and receiving? Or do I have to use other method of SerialPort? Thanks.

  It appears that data is sent in variable length format; i.e., a
value less than 10 is one byte, less than 100 is two bytes, and less
than 256 is three bytes.  If so, the receiving side cannot know truely
the beginning and end of data, unless both sides truly synchronous,
but Squeak cannot do it, especially through Morphic's #step method.

  The best way is to change the protocol so that either you send each
value in one byte, without rendering it to string, and Squeak side
read them in bulk but process them one by one.  Alternatively, you can
add an "end marker" (any character other than '0' to '9') at the end
of each data and have Squeak look for it.

  Let us say you take the first approach, things are quite simple;
In your #step (or #readColor) method, write something like:

     | buffer value aSerialPort |
     aSerialPort := ...
     buffer := aSerialPort readByteArray.
     buffer size = 0 ifTrue: [^ self].
     s := buffer last.
     Transcript show: s; cr.
     s := s - 46 * (1/60).
     self color: (Color r:s g:s b:s).

(For changing color, doing so many times in one #step doesn't make
sense, as only the last value is visible so here you can discard other
values.)

  I didn't think you need to open the port every time you try to read
it.  But certainly it is easier to code that way.

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Garret Raziel
Thanks, but I have done it with opening port in initialization, but it didn't help. And I have tried to change it to reading a byteArray and it didn't help too. Here is the monticello package of my class. I'll try to do something with my arduino too.

On Sat, Feb 6, 2010 at 7:30 PM, Yoshiki Ohshima <[hidden email]> wrote:
At Fri, 5 Feb 2010 18:56:43 +0100,
Garret Raziel wrote:
>
> and it works almost fine. But there is a bug. Time to time I receive only a part of value, for example I am receiving: 56 55
> 56 55 5 6 55 ... 102 103 101
> 103 100 102 1 1 0 102 102. Those one-digit values are truly false and it looks like the mistake in receiving. How can I
> synchonize sending and receiving? Or do I have to use other method of SerialPort? Thanks.

 It appears that data is sent in variable length format; i.e., a
value less than 10 is one byte, less than 100 is two bytes, and less
than 256 is three bytes.  If so, the receiving side cannot know truely
the beginning and end of data, unless both sides truly synchronous,
but Squeak cannot do it, especially through Morphic's #step method.

 The best way is to change the protocol so that either you send each
value in one byte, without rendering it to string, and Squeak side
read them in bulk but process them one by one.  Alternatively, you can
add an "end marker" (any character other than '0' to '9') at the end
of each data and have Squeak look for it.

 Let us say you take the first approach, things are quite simple;
In your #step (or #readColor) method, write something like:

    | buffer value aSerialPort |
    aSerialPort := ...
    buffer := aSerialPort readByteArray.
    buffer size = 0 ifTrue: [^ self].
    s := buffer last.
    Transcript show: s; cr.
    s := s - 46 * (1/60).
    self color: (Color r:s g:s b:s).

(For changing color, doing so many times in one #step doesn't make
sense, as only the last value is visible so here you can discard other
values.)

 I didn't think you need to open the port every time you try to read
it.  But certainly it is easier to code that way.

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

ArdIrda-GR.1.mcz (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Timer in Morph

Garret Raziel
Wow, many thanks to all of you! Now it works! Problem was in sending partly in my arduino (I have sending it by println (print and then print newline) and not print and I have sending as a decimal, not byte) and partly in Squeak (where I am receiving it into string and using it by readString first asInteger). Now it works perfectly. Thanks again. *SOLVED*

On Sat, Feb 6, 2010 at 9:24 PM, Garret Raziel <[hidden email]> wrote:
Thanks, but I have done it with opening port in initialization, but it didn't help. And I have tried to change it to reading a byteArray and it didn't help too. Here is the monticello package of my class. I'll try to do something with my arduino too.


On Sat, Feb 6, 2010 at 7:30 PM, Yoshiki Ohshima <[hidden email]> wrote:
At Fri, 5 Feb 2010 18:56:43 +0100,
Garret Raziel wrote:
>
> and it works almost fine. But there is a bug. Time to time I receive only a part of value, for example I am receiving: 56 55
> 56 55 5 6 55 ... 102 103 101
> 103 100 102 1 1 0 102 102. Those one-digit values are truly false and it looks like the mistake in receiving. How can I
> synchonize sending and receiving? Or do I have to use other method of SerialPort? Thanks.

 It appears that data is sent in variable length format; i.e., a
value less than 10 is one byte, less than 100 is two bytes, and less
than 256 is three bytes.  If so, the receiving side cannot know truely
the beginning and end of data, unless both sides truly synchronous,
but Squeak cannot do it, especially through Morphic's #step method.

 The best way is to change the protocol so that either you send each
value in one byte, without rendering it to string, and Squeak side
read them in bulk but process them one by one.  Alternatively, you can
add an "end marker" (any character other than '0' to '9') at the end
of each data and have Squeak look for it.

 Let us say you take the first approach, things are quite simple;
In your #step (or #readColor) method, write something like:

    | buffer value aSerialPort |
    aSerialPort := ...
    buffer := aSerialPort readByteArray.
    buffer size = 0 ifTrue: [^ self].
    s := buffer last.
    Transcript show: s; cr.
    s := s - 46 * (1/60).
    self color: (Color r:s g:s b:s).

(For changing color, doing so many times in one #step doesn't make
sense, as only the last value is visible so here you can discard other
values.)

 I didn't think you need to open the port every time you try to read
it.  But certainly it is easier to code that way.

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners