Sha256: 18e768d9762a9d5ff0dc232dafa94aa62245e5d20c7e22f4ace62640056e0f73

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'pi_piper'
require 'tamashii/agent/device/buzzer/base'

module Tamashii
  module Agent
    module Device
      module Buzzer
        class GpioBuzzer < Base

          DEFAULT_PIN = 18
          SHORT_PLAY_TIME = 0.2
          LONG_PLAY_TIME = 0.5
          REPEAT_INTERVAL = 0.1
          LOW_FREQ = 0.7

          def initialize(*args)
            super
            setup_pwm
          end

          def shutdown
            stop
          end

          def play_ok
            play_repeat_short(1)
          end

          def play_no
            play_repeat_short(3)
          end

          def play_error
            play_repeat_long(3)
          end

          private

          def stop
            @pwm.off
          end

          def setup_pwm
            unless pin = @options[:pin]
              logger.warn "No GPIO pin given. Using default: #{DEFAULT_PIN}"
              pin = DEFAULT_PIN
            end
            @pwm = PiPiper::Pwm.new pin: pin #, mode: :markspace
            @pwm.off
            @pwm.value = 1.0
          end

          def play(value = 1.0)
            @pwm.value = value
            @pwm.on
          end

          def play_short(value = 1.0)
            play_time(SHORT_PLAY_TIME,value)
          end

          def play_long(value = 1.0)
            play_time(LONG_PLAY_TIME,value)
          end

          def play_time(time, value = 1.0)
            play(value)
            sleep time
            stop
          end

          def play_repeat_short(repeat, repeat_interval = REPEAT_INTERVAL)
            repeat.times do
              play_short
              sleep repeat_interval
            end
          end

          def play_repeat_long(repeat, repeat_interval = REPEAT_INTERVAL)
            repeat.times do
              play_long
              sleep repeat_interval
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tamashii-agent-0.3.1 lib/tamashii/agent/device/buzzer/gpio_buzzer.rb