Sha256: dfc878061b3c92163685671b20edeb1a86c8c878a64a8a9701dc1a4240b9eadf

Contents?: true

Size: 1.14 KB

Versions: 32

Compression:

Stored size: 1.14 KB

Contents

# -*- coding: utf-8 -*-
require 'smalruby/hardware'

module Smalruby
  module Hardware
    # 汎用的なセンサーを表現するクラス
    class Sensor < Dino::Components::Sensor
      # デフォルトのセンサーの値に変化があったかどうかをチェックする閾値
      DEFAULT_THRESHOLD = 32

      attr_reader :value
      attr_accessor :threshold

      def initialize(options)
        super(board: world.board, pin: Pin.smalruby_to_dino(options[:pin]))
      end

      private

      def after_initialize(options = {})
        super(options)

        @value = 0
        @threshold = options[:threshold] || DEFAULT_THRESHOLD

        start_receiving_data
      end

      def start_receiving_data
        prev_data = 0
        when_data_received { |data|
          begin
            @value = data = data.to_i
            if (data - prev_data).abs >= @threshold ||
                (prev_data != data && (data == 0 || data >= 1023))
              world.sensor_changed(pin, data)
              prev_data = data
            end
          rescue
            Util.print_exception($ERROR_INFO)
          end
        }
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
smalruby-0.0.22-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.22 lib/smalruby/hardware/sensor.rb
smalruby-0.0.21-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.21 lib/smalruby/hardware/sensor.rb
smalruby-0.0.20-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.20 lib/smalruby/hardware/sensor.rb
smalruby-0.0.19-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.19 lib/smalruby/hardware/sensor.rb
smalruby-0.0.18-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.18 lib/smalruby/hardware/sensor.rb
smalruby-0.0.17-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.17 lib/smalruby/hardware/sensor.rb
smalruby-0.0.16-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.16 lib/smalruby/hardware/sensor.rb
smalruby-0.0.15-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.15 lib/smalruby/hardware/sensor.rb
smalruby-0.0.14-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.14 lib/smalruby/hardware/sensor.rb
smalruby-0.0.13-x86-mingw32 lib/smalruby/hardware/sensor.rb
smalruby-0.0.13 lib/smalruby/hardware/sensor.rb