Menu

Script Example Digital Counter

Peter Maersk-Moller
Attachments
digi.png (18280 bytes)

Script Example Digital Clock Counter

Updated for Snowmix 0.5.0 and 0.5.1

This page explains and demonstrates how to use basic Snowmix Scripting to create a digital clock counter. The digital clock counter will display hours, minutes and seconds. We could use text strings, but will in this case load graphic images with the numbers 0 through 9 and a colon as separator between hours, minutes and seconds.

clock

The image above shows the clock.

Initially we need to a minimum setup for Snowmix to run as shown below:

    require version 0.5.0
    system control port 9999
    system geometry 768 576 BGRA
    system frame rate 25
    system socket /tmp/mixer1

To show and test the clock we need no video feeds and we can use the sytem feed 0 as background. By default the system feed is black. Now we need to load the images to use as numbers for the clock.

    image load 10 ../images/digital-0-105x151.png
    image load 11 ../images/digital-1-105x151.png
    image load 12 ../images/digital-2-105x151.png
    image load 13 ../images/digital-3-105x151.png
    image load 14 ../images/digital-4-105x151.png
    image load 15 ../images/digital-5-105x151.png
    image load 16 ../images/digital-6-105x151.png
    image load 17 ../images/digital-7-105x151.png
    image load 18 ../images/digital-8-105x151.png
    image load 19 ../images/digital-9-105x151.png
    image load 20 ../images/digital-colon-37x151.png

Then we need to place images as hours, minutes and seconds. We will also in this case scale down the images to 1/4 size. In a real case scenario, the images should be scaled perfectly outside Snowmix first before loaded to minimize running load of a running Snowmix system. But in this case, the we do not need to worry about that.

    # HH:MM:SS placed as 8 7 6 5 4 3 2 1
    image place 1 10 450 226 center middle
    image place 2 10 425 226 center middle
    image place 3 20 407 226 center middle
    image place 4 10 390 226 center middle
    image place 5 10 365 226 center middle
    image place 6 20 347 226 center middle
    image place 7 10 330 226 center middle
    image place 8 10 305 226 center middle
    image scale 1 0.25 0.25
    image scale 2 0.25 0.25
    image scale 3 0.25 0.25
    image scale 4 0.25 0.25
    image scale 5 0.25 0.25
    image scale 6 0.25 0.25
    image scale 7 0.25 0.25
    image scale 8 0.25 0.25

Now we need ensure that the images are overlayed during each frame mix.

    # By default the stack will have the system feed 0 as the bottom background
    # stack 0

    command create Show
      image overlay 1..8
      loop
    command end
    overlay finish Show

With the configuration so far, we can now test it and should see hours, minutes and seconds separated by colons displayed as 00:00:00. The above commands is first saved into the file ini/ExampleDigitalCounter.ini.

    cd src
    ./snowmix ../ini/ExampleDigitalCounter.ini

    and in another window

    cd scripts
    ./output2screen

However to have the clock running, we need to create a state-machine setting the state of the clock as time progress. To do that, we use the overlay pre command.

    command create PreShow
      FrameUpdate25
      loop
    command end
    overlay pre PreShow

    command create FrameUpdate25
      next 24
      SecondUpdate
      loop
    command end

Here we know the command macro set with the command overlay pre is called at frame rate whether or not a shmsrc is connected to Snowmix. We also know that we have set the framerate to be 25. The result is that the command macro SecondUpdate is called every second. This can then be used to create a counter as part of the state-machine. First we create the second counter that will count from 00 to 59 and then call the minute counter and reset back to 00.

    command create SecondUpdate
      image image 1 11
      next
      image image 1 12
      next
      image image 1 13
      next
      image image 1 14
      next
      image image 1 15
      next
      image image 1 16
      next
      image image 1 17
      next
      image image 1 18
      next
      image image 1 19
      next
      image image 1 10
      Second10Update
      loop
    command end
    command create Second10Update
      image image 2 11
      next
      image image 2 12
      next
      image image 2 13
      next
      image image 2 14
      next
      image image 2 15
      next
      image image 2 10
      MinuteUpdate
      loop
command end

The minute counter is nearly identical to the second counter.

    command create MinuteUpdate
      image image 4 11
      next
      image image 4 12
      next
      image image 4 13
      next
      image image 4 14
      next
      image image 4 15
      next
      image image 4 16
      next
      image image 4 17
      next
      image image 4 18
      next
      image image 4 19
      next
      image image 4 10
      Minute10Update
      loop
    command end
    command create Minute10Update
      image image 5 11
      next
      image image 5 12
      next
      image image 5 13
      next
      image image 5 14
      next
      image image 5 15
      next
      image image 5 10
      Hour24Update
      loop
command end

Now the hour counter is a bit special as we need to count from 00 to 23 and then reset to 00.

    command create Hour24Update
      HourUpdate
      loop 23
      command restart HourUpdate
      command restart Hour10Update
      loop
    command end
    command create HourUpdate
      image image 7 11
      next
      image image 7 12
      next
      image image 7 13
      next
      image image 7 14
      next
      image image 7 15
      next
      image image 7 16
      next
      image image 7 17
      next
      image image 7 18
      next
      image image 7 19
      next
      image image 7 10
      Hour10Update
      loop
    command end

    command create Hour10Update
      image image 8 11
      next
      image image 8 12
      loop
    command end

Now saving all the above commands into an ini file, we can now test the clock as described earlier. The clock will count from 00:00:00 with a 1 second interval.

If we want to set the clock to 09:23:17, we can execute the following commands

    # 7*2 -1 = 13, 1*2 -1 = 1, 3*2 -1 = 5, 2*2 -1 = 3, 9*Hour24Update
    command pointer atline SecondUpdate 13
    SecondUpdate
    command pointer atline Second10Update 1
    Second10Update
    command pointer atline MinuteUpdate 5
    MinuteUpdate
    command pointer atline Minute10Update 3
    Minute10Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update
    Hour24Update

Setting the clock manually is as shown a bit difficult although using an external script makes is easier. Another way would be to use the embedded Tcl interpreter and advanced scripting.

The ini file for this example is available as an attachment to this page found at the top of the page. Download the file and use it as ini file for Snowmix to test the example.


Related

Wiki: Advanced Scripting
Wiki: Scripting
Wiki: Tcl Interpreter

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.