Method: Request::Choregraphy#set

Defined in:
lib/violet/request.rb

#set(command) ⇒ Object

Raises:



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/violet/request.rb', line 404

def set command
  raise BadChorDesc.new('wrong Choregraphy description')    unless command.is_a?(LedCommandStruct)
  raise BadChorDesc.new('need an element')                  unless command.elements
  command.elements.each do |e|
    raise BadChorDesc.new('wrong element')                  unless e == :all or e.between?(Leds::Positions::BOTTOM,Leds::Positions::TOP)
  end
  raise BadChorDesc.new('need a time')                      unless command.time
  raise BadChorDesc.new('time must be >= than zero')        unless command.time.to_i >= 0
  raise BadChorDesc.new('need a color')                     unless command.color
  raise BadChorDesc.new('wrong size for rgb color array')   unless command.color.size == 3
  command.color.collect! do |c|
    raise BadChorDesc.new('color code must be betwen 0 and 255') unless c.to_i.between?(0,255)
    c.to_i
  end

  template = '%s,led,%s,%s'
  command.color = command.color.join(',')

  # remove trailling element if all is set
  command.elements.uniq!
  command.elements = [:all] if command.elements.include?(:all)

  command.elements.each do |e|
    if e == :all
      (Leds::Positions.constants - ['ALL']).each do |cste_name|
        cste = Helpers.constantize "#{self.class}::Leds::Positions::#{cste_name}"
        @chor << template % [ command.time.to_i, cste, command.color ]
      end
    else
        @chor << template % [ command.time.to_i,  e, command.color ]
    end
  end
end