Scheudle display

I have made a scheduler for lights that uses a labjack. I would like to
have an easier to read display for any created schedule.

Currently I am just using Tk listbox that displays like
<hour>:<minute> <Weekday> <Action>

It would be nice if I could generate a 24 hour clock (meaning a circle
with 24 divisions) and fill it in where the appropriate action is.

I have no idea how or even if this is possible with ruby.

I am open to other suggestions though as to an effective way of
displaying a complex schedule.

Message-ID: <1131392781.003984.245710@z14g2000cwz.googlegroups.com>

Currently I am just using Tk listbox that displays like
<hour>:<minute> <Weekday> <Action>

It would be nice if I could generate a 24 hour clock (meaning a circle
with 24 divisions) and fill it in where the appropriate action is.

Maybe, I don't understand your problem.
Can the following script give you a hint to solve the problem ?

···

From: "csjasnoch@wisc.edu" <csjasnoch@wisc.edu>
Subject: Scheudle display
Date: Tue, 8 Nov 2005 04:47:11 +0900
---------------------------------------------------------
require 'tk'

c = TkCanvas.new(:width=>400, :height=>400,
                 :scrollregion=>[-200, -200, 200, 200]).pack
oval = TkcOval.new(c, [-150, -150], [150, 150], :fill=>'white')
TkcOval.new(c, [-5, -5], [5, 5], :fill=>'black')

def coords_to_time(x, y)
  return ((y < 0)? [0, 0]: [12, 0]) if x == 0
  hh, mm = (Math.atan(-y/-x.to_f) + Math::PI/2).divmod(Math::PI/12)
  [((x >= 0)? hh.to_i: (hh.to_i + 12)), (60 * mm).to_i]
end

l = TkLabel.new(:text=>'00:00').pack
oval.bind('Motion',
          proc{|x, y|
            l.text = '%02d:%02d' % coords_to_time(c.canvasx(x), c.canvasy(y))
          }, '%x %y')

Tk.mainloop
---------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Yes it helps but just in the right direction.

rather than the small oval drawn I would want to fill in many sectional
areas to represent lights on or off for those time periods.

I will play with it and see what I come up with but if anyone has
anything closer to what I need please point me in the right direction.

Thank you.

Collin Jasnoch

Oh and the minutes conversion isn't working? It only goes up to 15 (I
am not sur why though)..

Looking at some other stuff this is what I came up with but I am stuck
on the usage of TkcPolygon.new

I do not understand what the coordianates are and how big the vector
should/can be.
I will need to have access to be able to call the addPoly function
anytime a new event is set in my scheduler. So I would like to put a
cone type shape over that time period.

Example:

Someone wants a schedule that is trigger on at 00:00 and off at 6:00
then on again at 12:00 then off again at 18:00.

Each time they add these seperate events to the schedule a the canvas
needs to adjusst its picutre accordingly. With this example in the end
it would be divided evenly into 4 sections 0degrees to 90degrees would
be red (or whatever color), as well as 180 to 270 degrees. The
remaining sections would remain white (or whatever color is
initialized).

As i said i do not understand how TkcPolygon.new works though so I am
initializing it with values that seem to be meaningless.

I hope someone can help.

Here is my adjusted code for adding a poly.

require 'tk'

class Clock
  def initialize
    @c = TkCanvas.new(:width=>400, :height=>400,
    :scrollregion=>[-200, -200, 200, 200]).pack
    oval = TkcOval.new(@c, [-150, -150], [150, 150], :fill=>'white')
    TkcOval.new(@c, [-5, -5], [5, 5], :fill=>'black')

    l = TkLabel.new(:text=>'00:00').pack
    oval.bind('Motion',
    proc{|x, y|
      l.text = '%02d:%02d' % coords_to_time(@c.canvasx(x), @c.canvasy(y))
    }, '%x %y')
  end

  def coords_to_time(x, y)
    return ((y < 0)? [0, 0]: [12, 0]) if x == 0
    hh, mm = (Math.atan(-y/-x.to_f) + Math::PI/2).divmod(Math::PI/12)
    [((x >= 0)? hh.to_i: (hh.to_i + 12)), (60 * mm).to_i]
  end

  def addPoly(coords)
    @coords = coords
    @poly = TkcPolygon.new(@c, @coords, :fill=>'red')
  end

end

coords = [[-5, -5], [5, 5], [100, 200],[100,100]]
my_clock = Clock.new

my_clock.addPoly(coords)
Tk.mainloop

Message-ID: <1131657574.017316.243080@o13g2000cwo.googlegroups.com>

Example:

Someone wants a schedule that is trigger on at 00:00 and off at 6:00
then on again at 12:00 then off again at 18:00.

Each time they add these seperate events to the schedule a the canvas
needs to adjusst its picutre accordingly. With this example in the end
it would be divided evenly into 4 sections 0degrees to 90degrees would
be red (or whatever color), as well as 180 to 270 degrees. The
remaining sections would remain white (or whatever color is
initialized).

Hmm... How about the following?

···

From: "csjasnoch@wisc.edu" <csjasnoch@wisc.edu>
Subject: Re: Scheudle display
Date: Fri, 11 Nov 2005 06:22:14 +0900
-------------------------------------------------------------------------
require 'tk'

class Sched24
  def initialize
    @c = TkCanvas.new(:width=>400, :height=>400,
                     :scrollregion=>[-200, -200, 200, 200]
                     ).pack(:fill=>:both, :expand=>true)

    @tag = TkcTag.new(@c)

    @circle_coords = [[-180, -180], [180, 180]]
    @oval = TkcOval.new(@c, @circle_coords, :fill=>'white', :tags=>[@tag])

    @center_dot = TkcOval.new(@c, [-5, -5], [5, 5], :fill=>'black')

    @l = TkLabel.new(:text=>'00:00').pack

    cmd = proc{|x, y|
      @l.text = '%02d:%02d' % coords_to_time(@c.canvasx(x), @c.canvasy(y))
    }
    @c.bind('Motion', cmd, '%x %y')
    @tag.bind('Motion', cmd, '%x %y')
  end

  def coords_to_time(x, y)
    return ((y < 0)? [0, 0]: [12, 0]) if x == 0
    h, m = (720.0*Math.atan2(y,x)/Math::PI).divmod(60)
    hh = (x<0 && y<0)? h+30:h+6
    mm = m.round
    if mm >= 60
      hh += 1
      mm -= 60
    end
    [hh, mm]
  end

  def create_pie(hh, mm, span, color='red')
    start = 90.0 - (hh*60 + mm)/4.0 # 360.0*(hh*60+mm)/(24*60)
    extent = -span/4.0

    pie = TkcArc.new(@c, @circle_coords, :tags=>[@tag],
                     :outline=>'black', 'fill'=>color,
                     :start=>start, :extent=>extent)
    @center_dot.raise
    pie
  end
end

sched = Sched24.new
sched.create_pie(0,0, 60) # 60 minutes from 00:00
sched.create_pie(6,30, 280, 'green') # 280 minutes from 06:30
sched.create_pie(15,20, 90, 'blue') # 90 minutes from 15:20

Tk.mainloop
-------------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hidetoshi NAGAI wrote:

Hmm... How about the following?
-------------------------------------------------------------------------
require 'tk'
[snip]
Tk.mainloop
-------------------------------------------------------------------------

It was interesting to me - thanks.

I prefer the trigonometry in this improved version :wink:

daz

Message-ID: <20051114.005918.74727279.nagai@ai.kyutech.ac.jp>

Hmm... How about the following?

The following is a improved version of the previous example. :slight_smile:

# A little long. Sorry.

···

From: Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
Subject: Re: Scheudle display
Date: Mon, 14 Nov 2005 00:59:19 +0900

-------------------------------------------------------------------
require 'tk'

class Clock
  def initialize(clock24 = true)
    @clock = (clock24)? 24: 12

    @size = 200
    @cdot_size = 5

    @cdot_color = 'black'
    @hour_hand_color = 'black'
    @minute_hand_color = 'gray25'
    @second_hand_color = 'gray50'

    @mark_font = 'Helvetica -14'
    @mark_width = 3
    @mark_color = 'black'
    @submark_color = 'gray50'

    @c = TkCanvas.new(:width=>2*@size, :height=>2*@size,
                     :scrollregion=>[-@size, -@size, @size, @size]
                     ).pack(:fill=>:both, :expand=>true)

    @tag = TkcTag.new(@c)
    @hand_tag = TkcTag.new(@c)

    @circle_coords = [[-0.9*@size, -0.9*@size], [0.9*@size, 0.9*@size]]
    @oval = TkcOval.new(@c, @circle_coords, :fill=>'white', :tags=>[@tag])

    f = TkFrame.new.pack
    TkLabel.new(f, :text=>'CURRENT:').pack(:side=>:left)
    @now = TkLabel.new(f, :text=>'00:00:00').pack(:side=>:left, :padx=>2)
    TkLabel.new(f, :text=>' ').pack(:side=>:left)
    TkLabel.new(f, :text=>' ').pack(:side=>:right)
    @l = TkLabel.new(f, :text=>'00:00').pack(:side=>:right, :padx=>2)
    TkLabel.new(f, :text=>'POINTER:').pack(:side=>:right)

    cmd = proc{|x, y|
      @l.text = '%02d:%02d' % coords_to_time(@c.canvasx(x), @c.canvasy(y))
    }
    @c.bind('Motion', cmd, '%x %y')
    @tag.bind('Motion', cmd, '%x %y')

    _create_hands
    _create_marks

    timer_proc = proc{
      t = Time.now
      @now.text = '%02d:%02d:%02d' % [t.hour, t.min, t.sec]
      set_hands(t.hour, t.min, t.sec)
    }

    timer_proc.call
    @timer = TkRTTimer.start(1000, -1, timer_proc)
  end

  def _create_marks
    @mark_tag = TkcTag.new(@c)

    TkcLine.new(@c, 0, -0.90*@size, 0, -0.85*@size,
                :tags=>[@tag, @mark_tag],
                :width=>@mark_width, :fill=>@mark_color)
    TkcLine.new(@c, 0.90*@size, 0, 0.85*@size, 0,
                :tags=>[@tag, @mark_tag],
                :width=>@mark_width, :fill=>@mark_color)
    TkcLine.new(@c, 0, 0.90*@size, 0, 0.85*@size,
                :tags=>[@tag, @mark_tag],
                :width=>@mark_width, :fill=>@mark_color)
    TkcLine.new(@c, -0.90*@size, 0, -0.85*@size, 0,
                :tags=>[@tag, @mark_tag],
                :width=>@mark_width, :fill=>@mark_color)

    TkcText.new(@c, [0, -0.92*@size], :text=>0,
                :anchor=>'s', :fill=>@mark_color)
    TkcText.new(@c, [0.92*@size, 0], :text=>@clock.div(4),
                :anchor=>'w', :fill=>@mark_color)
    TkcText.new(@c, [0, 0.92*@size], :text=>@clock.div(2),
                :anchor=>'n', :fill=>@mark_color)
    TkcText.new(@c, [-0.92*@size, 0], :text=>@clock.div(4)*3,
                :anchor=>'e', :fill=>@mark_color)

    [30.0, 60.0].each{|angle|
      rad = Math::PI * angle / 180.0
      x_base = @size*Math::sin(rad)
      y_base = @size*Math::cos(rad)

      x1 = 0.90*x_base
      y1 = 0.90*y_base

      x2 = 0.85*x_base
      y2 = 0.85*y_base

      TkcLine.new(@c, x1, y1, x2, y2,
                  :tags=>[@tag, @mark_tag],
                  :width=>@mark_width, :fill=>@mark_color)
      TkcLine.new(@c, x1, -y1, x2, -y2,
                  :tags=>[@tag, @mark_tag],
                  :width=>@mark_width, :fill=>@mark_color)
      TkcLine.new(@c, -x1, y1, -x2, y2,
                  :tags=>[@tag, @mark_tag],
                  :width=>@mark_width, :fill=>@mark_color)
      TkcLine.new(@c, -x1, -y1, -x2, -y2,
                  :tags=>[@tag, @mark_tag],
                  :width=>@mark_width, :fill=>@mark_color)

      x3 = 0.92*x_base
      y3 = 0.92*y_base

      if @clock == 24
        dh = angle.to_i/15
      else # @clock == 12
        dh = angle.to_i/30
      end

      TkcText.new(@c, x3, -y3, :text=>dh,
                  :anchor=>'sw', :fill=>@mark_color)
      TkcText.new(@c, x3, y3, :text=>@clock.div(2)-dh,
                  :anchor=>'nw', :fill=>@mark_color)
      TkcText.new(@c, -x3, y3, :text=>@clock.div(2)+dh,
                  :anchor=>'ne', :fill=>@mark_color)
      TkcText.new(@c, -x3, -y3, :text=>@clock-dh,
                  :anchor=>'se', :fill=>@mark_color)
    }

    if @clock == 24
      [15.0, 45.0, 75.0].each{|angle|
        rad = Math::PI * angle / 180.0
        x_base = @size*Math::sin(rad)
        y_base = @size*Math::cos(rad)

        x1 = 0.90*x_base
        y1 = 0.90*y_base

        x2 = 0.875*x_base
        y2 = 0.875*y_base

        TkcLine.new(@c, x1, y1, x2, y2,
                    :tags=>[@tag, @mark_tag],
                    :width=>@mark_width, :fill=>@submark_color)
        TkcLine.new(@c, x1, -y1, x2, -y2,
                    :tags=>[@tag, @mark_tag],
                    :width=>@mark_width, :fill=>@submark_color)
        TkcLine.new(@c, -x1, y1, -x2, y2,
                    :tags=>[@tag, @mark_tag],
                    :width=>@mark_width, :fill=>@submark_color)
        TkcLine.new(@c, -x1, -y1, -x2, -y2,
                    :tags=>[@tag, @mark_tag],
                    :width=>@mark_width, :fill=>@submark_color)
      }
    end
  end

  def _create_hands
    hour_hand_len = 0.55*@size
    minute_hand_len = 0.85*@size
    second_hand_len = 0.88*@size

    hour_hand_width = 1.8*@cdot_size
    minute_hand_width = 1.0*@cdot_size
    second_hand_width = 1 # 0.4*@cdot_size

    @hour_hand_coords = [
      [0, -0.5*@cdot_size],
      [hour_hand_width, -0.5*@cdot_size-hour_hand_width],
      [hour_hand_width, -hour_hand_len+hour_hand_width],
      [0, -hour_hand_len],
      [-hour_hand_width, -hour_hand_len+hour_hand_width],
      [-hour_hand_width, -0.5*@cdot_size-hour_hand_width],
    ]
    @minute_hand_coords = [
      [0, -0.5*@cdot_size],
      [minute_hand_width, -0.5*@cdot_size - minute_hand_width],
      [minute_hand_width, -minute_hand_len+minute_hand_width],
      [0, -minute_hand_len],
      [-minute_hand_width, -minute_hand_len+minute_hand_width],
      [-minute_hand_width, -0.5*@cdot_size-minute_hand_width],
    ]
    @second_hand_coords = [
      [0, -0.5*@cdot_size],
      [second_hand_width, -0.5*@cdot_size - second_hand_width],
      [second_hand_width, -second_hand_len+second_hand_width],
      [0, -second_hand_len],
      [-second_hand_width, -second_hand_len+second_hand_width],
      [-second_hand_width, -0.5*@cdot_size-second_hand_width],
    ]

    @hour_hand = TkcPolygon.new(@c, @hour_hand_coords,
                                :tags=>[@tag, @hand_tag],
                                :outline=>@hour_hand_color,
                                :fill=>@hour_hand_color)

    @minute_hand = TkcPolygon.new(@c, @minute_hand_coords,
                                  :tags=>[@tag, @hand_tag],
                                  :outline=>@minute_hand_color,
                                  :fill=>@minute_hand_color)

    @second_hand = TkcPolygon.new(@c, @second_hand_coords,
                                  :tags=>[@tag, @hand_tag],
                                  :outline=>@second_hand_color,
                                  :fill=>@second_hand_color)

    @center_dot = TkcOval.new(@c,
                              [-@cdot_size, -@cdot_size],
                              [@cdot_size, @cdot_size],
                              :outline=>@cdot_color, :fill=>@cdot_color)
  end
  private :_create_hands

  def _raise_hands
    @hour_hand.raise
    @minute_hand.raise
    @second_hand.raise
    @center_dot.raise
  end
  private :_raise_hands

  def _raise_marks
    @mark_tag.raise
  end
  private :_raise_marks

  def set_hands(hh, mm, ss)
    ss_angle = Math::PI * ss / 30.0
    mm_angle = Math::PI * (mm + ss/60.0) / 30.0
    hh_angle = Math::PI * (hh + (mm + ss/60.0)/60.0) / (@clock.div(2))

    @second_hand.coords = @second_hand_coords.collect{|x, y|
      r = Math::hypot(y, x)
      a = Math::atan2(y, x) + ss_angle
      [Math::cos(a) * r, Math::sin(a) * r]
    }

    @minute_hand.coords = @minute_hand_coords.collect{|x, y|
      r = Math::hypot(y, x)
      a = Math::atan2(y, x) + mm_angle
      [Math::cos(a) * r, Math::sin(a) * r]
    }

    @hour_hand.coords = @hour_hand_coords.collect{|x, y|
      r = Math::hypot(y, x)
      a = Math::atan2(y, x) + hh_angle
      [Math::cos(a) * r, Math::sin(a) * r]
    }

    _raise_hands
    _raise_marks
  end

  def coords_to_time(x, y)
    return ((y < 0)? [0, 0]: [@clock.div(2), 0]) if x == 0
    if @clock == 24
      offset = (x<0&&y<0)? 1800.0: 360.0
      m_half = 720.0
    else # @clock == 12
      offset = (x<0&&y<0)? 900.0: 180.0
      m_half = 360.0
    end
    (offset + m_half*Math.atan2(y,x)/Math::PI).round.divmod(60)
  end

  def create_pie(hh, mm, span, color='red')
    if @clock == 24
      start = 90.0 - (hh*60 + mm)/4.0 # 360.0*(hh*60+mm)/(24*60)
      extent = -span/4.0
    else # @clock == 12
      start = 90.0 - (hh*60 + mm)/2.0 # 360.0*(hh*60+mm)/(12*60)
      extent = -span/2.0
    end

    pie = TkcArc.new(@c, @circle_coords, :tags=>[@tag],
                     :outline=>'black', 'fill'=>color,
                     :start=>start, :extent=>extent)
    _raise_hands
    _raise_marks
    pie
  end
end

sched = Clock.new
sched.create_pie(0,0, 60) # 60 minutes from 00:00
sched.create_pie(6,30, 280, 'green') # 280 minutes from 06:30
sched.create_pie(15,20, 90, 'blue') # 90 minutes from 15:20

Tk.mainloop
-------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hidetoshi NAGAI wrote:

The following is a improved version of the previous example. :slight_smile:

This is a c-r-a-z-y clock :-)))

The one-click Windows installer is a little old for it.
Here are the steps if folks want to run the script:

1) RENAME lib\ruby\1.8\tk\timer.rb to timer_ORIG.rb

2) Download revision 1.1.2.15 of tk/timer.rb from:
    qurl.net - This website is for sale! - qurl Resources and Information. (same as URL below [*1])
    - save as lib\ruby\1.8\tk\timer.rb

3) Add require 'tk/timer' to the clock script [*2]

    > require 'tk'
      require 'tk/timer' # <--- ADD
    >
    > class Clock
    > [...]

Thanks,

daz

[*1] <http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/ext/tk/lib/tk/timer.rb?only_with_tag=ruby_1_8&gt;

[*2] In the full version, tk/timer autoload is triggered by use of TkRTTimer