[C.CC USERS] 3 Blinkin lights - other solutions

Matt Jadud matt at jadud.com
Mon Feb 28 22:08:07 GMT 2011


Hi all,

On Mon, Feb 28, 2011 at 16:38, Steve Pretty
<steve.g.pretty at btinternet.com> wrote:
> to see the kind of complex issues that can arise in concurrent code coming
> up in just three lines of code.  Using "Blink" on three LEDs is causing 18
> (at least) occam processes to compete for processing cycles ( each Blink
> starts a tick (which starts a delay) and a pin.toggle (which starts a toggle
> and a digital.output).

Steve is exactly right. blink(...,...) was written in a completely
compositional style, which leads to lots of processes. Further, we run
smack into Lar's timing issues.

> Of course, if you just want three LEDs to flash in syncronism, you might be
> best just to use a single timer and distribute the output to three output
> functions - e.g.:

> All the lights run in synch using this approach. Well - actually, I put a
> logic analyser on it - becasue of the concurrency, the LEDs come on one
> after another, with the third coming on typically 5ms after the first.
>
> The engineering side of me says that the simplest way to get three LEDS to
> blick in sync is to wire them in parallel!

Yep. And, a variation on this approach (compiles, but untested):

--- BEGIN CODE ---
#INCLUDE "plumbing.module"

VAL []INT LEDS IS [9, 10, 11]:
VAL INT NUM.LEDS IS (SIZE LEDS):

PROC pinger (CHAN SIGNAL in?, []CHAN SIGNAL out!)
  WHILE TRUE
    SEQ
      in ? SIGNAL
      PAR i = 0 FOR NUM.LEDS
        out[i] ! SIGNAL
:


PROC main ()
  CHAN SIGNAL trigger:
  [NUM.LEDS]CHAN SIGNAL s:
  PAR
    tick (100, trigger!)
    pinger(trigger?, s!)
    PAR i = 0 FOR NUM.LEDS
      pin.toggle(LEDS[i], LOW, s[i]?)
:
--- END CODE ---

This demonstrates replication in a PAR. This way, we can have an array
of channels, and an array of LED pins, and compress some of this down
in terms of lines-of-code. It also makes it easier to add another
LED... simply grow the LED array, and the number of pins you're
toggling changes as well. Like Steve said, though, we still only have
one processor, but the timing will be much closer.

I think I'd like a logic analyzer... that would be a very useful tool to have.

Cheers,
Matt



More information about the users mailing list