Serial killer

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
hamlynart
Posts: 101
Joined: Wed Mar 19, 2008 7:10 pm
Contact:

Serial killer

Post by hamlynart » Tue Dec 16, 2008 1:54 am

I've been really enjoying working on a new project over the last few days and I seem to have hit a problem which might make all the effort in vain.

The idea was to build an interface for an Arduino microcontroller. I have multiple readings and outputs and the idea was that they could all be running at the same time.

However I didn't try two outputs simultaneously until just now and it doesn't seem to work.

Code: Select all

on mouseUp
      put the label of btn "Port" into thePort
  if the hilite of me is true then
    if fld "sec13" >= 1 and fld "pause13" >= 1  then
      repeat while the hilite of me is true with messages
        write "E" to driver thePort
        set the backgroundColor of graphic "Rectangle13" to red
        wait for fld "sec13" seconds with messages
                write "R" to driver thePort
                        set the backgroundColor of graphic "Rectangle13" to black
                wait for fld "pause13" seconds  with messages
      end repeat
      end if
    end if
end mouseUp
If I run the above on one button and try to run another button at the same time the second button stops the first. If I stop the second the first resumes.

I know that only one program can communicate via serial at a time but does this also limit what I'm trying to do, and if so, is there some way to stack up or buffer the outputs?

Any Help or advice much appreciated.

Jim H

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Tue Dec 16, 2008 6:45 am

Revolution is single-threaded, and when you use "wait with messages" the next button will effectively block the first until it is done, and then the first button resumes.

You may be better off with the 'send in time' construct. This allows you to tell the engine to send a message to the same or another control in x seconds or milliseconds at the earliest. These will then be placed onto a queue, and executed one after another when no event handlers are running.

For istance, if you make a stack with a single button and set it script to

Code: Select all

on mouseUp
  answer "Hasta la vista, baby..." with "I'll be back"
  send "ComeBack" to me in 2 seconds
end mouseUp

on ComeBack
  answer "I told you I'd be back!"
end ComeBack
then click the button, you get the first answer dialog box, and when you close that, two seconds later you'll get another message.

The message that you send can also have parameters. Let's finetune the previous example a bit

Code: Select all

on mouseUp
  answer "Hasta la vista, baby..." with "I'll be back"
  put the long system time into tStartTime
  send "ComeBack tStartTime" to me in 2 seconds
end mouseUp

on ComeBack pStartTime
  answer "Around" && pStartTime && "I told you I'd be back!"
end ComeBack
when you click the button this time around, it will tell you when you closed the first answer dialog box. Admittedly a rather whimsical example, but it shows how you can pass parameters to a send message.

You can use this both for one time operations, and to schedule messages at a regular interval, like a timer service. Let me give you an example of a timer. Suppose we have a stack with a field "CurrentTime" and a single button "StartStop" that will handle "Start" and "Stop". Here's the script for our "StartStop" button.

Code: Select all

local sMessageID

on mouseUp
  if sMessageID is empty then
    -- start the timer
    send "ShowTime" to me in 500 milliseconds
    put the result into sMessageID
    set the label of me to "Stop"
  else
    -- stop the timer
    cancel sMessageID
    put empty into sMessageID
    set the label of me to "Start"
  end if
end mouseUp

on ShowTime
  put the long system time into field "CurrentTime"
  send "ShowTime" to me in 500 milliseconds
  put the result into sMessageID
end ShowTime
Click the button, and the field "CurrentTime" will be regularly updated with the long system time until you click the button again. If you want more information regarding the messages system, I highly recommend the Primer on Message Mechanics that Dar Scott put together back in 2003.

So my suggestion is to break down the repeat loops into events that you can 'send in time' to trigger their execution. Use parameters to keep track of the iteration(s) you're in, and take it from there.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

hamlynart
Posts: 101
Joined: Wed Mar 19, 2008 7:10 pm
Contact:

Post by hamlynart » Tue Dec 16, 2008 5:42 pm

Superb!

As is often the case RunRev turns out to be initially easy, then frighteningly complex and then, with a little patience, fantastically powerful.

I'm getting there.... slowly.

Thanks a million Jan.

Jim H

Post Reply