Class: CheekyPi::Animation::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cheeky-pi/animation/base.rb

Direct Known Subclasses

Cycle, Fade

Constant Summary collapse

TICKRATE =
0.033

Instance Method Summary collapse

Constructor Details

#initialize(duration, light, options = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
# File 'lib/cheeky-pi/animation/base.rb', line 11

def initialize(duration, light, options={})
  @duration = duration
  @light = light

  @deferred = EventMachine::Q.defer
  promise.then { handle_complete }

  start
end

Instance Method Details

#animateObject



64
65
# File 'lib/cheeky-pi/animation/base.rb', line 64

def animate
end

#completeObject



52
53
54
55
56
57
# File 'lib/cheeky-pi/animation/base.rb', line 52

def complete
  @timer.cancel
  @deferred.resolve

  self
end

#haltObject



45
46
47
48
49
50
# File 'lib/cheeky-pi/animation/base.rb', line 45

def halt
  @timer.cancel
  @deferred.reject

  self
end

#handle_completeObject



21
22
# File 'lib/cheeky-pi/animation/base.rb', line 21

def handle_complete
end

#percentObject



36
37
38
39
# File 'lib/cheeky-pi/animation/base.rb', line 36

def percent
  return 0 if @duration.nil?
  (Time.now.to_f - @start_time) / @duration
end

#promiseObject



24
25
26
# File 'lib/cheeky-pi/animation/base.rb', line 24

def promise
  @promise ||= @deferred.promise
end

#safe_percentObject



41
42
43
# File 'lib/cheeky-pi/animation/base.rb', line 41

def safe_percent
  [percent, 1].min
end

#sine(percent) ⇒ Object



72
73
74
# File 'lib/cheeky-pi/animation/base.rb', line 72

def sine(percent)
  Math::sin(percent *  2 * Math::PI - 0.5 * Math::PI) * 0.5 + 0.5
end

#startObject



28
29
30
31
32
33
34
# File 'lib/cheeky-pi/animation/base.rb', line 28

def start
  @timer.cancel if @timer
  @start_time = Time.now.to_f
  @timer = EventMachine.add_periodic_timer(TICKRATE) { tick }

  self
end

#tickObject



59
60
61
62
# File 'lib/cheeky-pi/animation/base.rb', line 59

def tick
  complete if percent >=1
  animate
end

#triangle(percent) ⇒ Object

convert [0-0.5-1] to [0-1-0]



68
69
70
# File 'lib/cheeky-pi/animation/base.rb', line 68

def triangle(percent)
  (0.5 - (percent % 1 - 0.5).abs) * 2
end