Class: BitWizard::Board
- Inherits:
-
Object
- Object
- BitWizard::Board
- Defined in:
- lib/bitwizard.rb
Direct Known Subclasses
Defined Under Namespace
Classes: NullLogger
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#bus ⇒ Object
readonly
Returns the value of attribute bus.
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
-
.detect(options) ⇒ Object
Detects the type of board on the given address and creates the correct handler class for it.
Instance Method Summary collapse
-
#initialize(options) ⇒ Board
constructor
Creates a generic board handle for reading and writing directly.
-
#read(reg, count) ⇒ Object
Reads a value from the board.
-
#valid? ⇒ Boolean
Returns if the board has a valid communication address.
-
#write(reg, value) ⇒ Object
Writes a value to the board, either a single byte or several in the form of a string.
Constructor Details
#initialize(options) ⇒ Board
Creates a generic board handle for reading and writing directly.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bitwizard.rb', line 47 def initialize() = { address: -1, type: :auto_detect, bus: :spi, skip_check: false, clock: 45000, logger: NullLogger.new }.merge() raise ArgumentError.new "Bus must be :spi or :i2c." unless [:bus] == :spi or [:bus] == :i2c @logger = [:logger] @address = [:address] @type = [:type] @bus = [:bus] self_check! unless [:skip_check] end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
35 36 37 |
# File 'lib/bitwizard.rb', line 35 def address @address end |
#bus ⇒ Object (readonly)
Returns the value of attribute bus.
35 36 37 |
# File 'lib/bitwizard.rb', line 35 def bus @bus end |
#features ⇒ Object (readonly)
Returns the value of attribute features.
35 36 37 |
# File 'lib/bitwizard.rb', line 35 def features @features end |
#logger ⇒ Object
Returns the value of attribute logger.
36 37 38 |
# File 'lib/bitwizard.rb', line 36 def logger @logger end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
35 36 37 |
# File 'lib/bitwizard.rb', line 35 def type @type end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
35 36 37 |
# File 'lib/bitwizard.rb', line 35 def version @version end |
Class Method Details
.detect(options) ⇒ Object
Detects the type of board on the given address and creates the correct handler class for it.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bitwizard.rb', line 13 def Board.detect() = { address: -1, bus: :spi, logger: NullLogger.new }.merge().merge({ type: :auto_detect, skip_check: false }) [:logger] = NullLogger.new unless [:logger] temp = BitWizard::Board.new correct = temp.instance_variable_get(:@constructor).call(.merge({skip_check: true})) if temp.valid? correct.instance_variable_set(:@type, temp.type) correct.instance_variable_set(:@version, temp.version) correct.instance_variable_set(:@features, temp.features) correct end |
Instance Method Details
#read(reg, count) ⇒ Object
Reads a value from the board
90 91 92 93 94 95 |
# File 'lib/bitwizard.rb', line 90 def read(reg, count) raise ArgumentError.new "#{reg} is not a valid register, must be a number between 0x00..0xff" unless reg.is_a? Fixnum and (0..255).include? reg return spi_read(reg, count) if @bus == :spi return i2c_read(reg, count) if @bus == :i2c end |
#valid? ⇒ Boolean
Returns if the board has a valid communication address
68 69 70 71 |
# File 'lib/bitwizard.rb', line 68 def valid? return false if @address == -1 or @type == :auto_detect true end |
#write(reg, value) ⇒ Object
Writes a value to the board, either a single byte or several in the form of a string
77 78 79 80 81 82 83 84 |
# File 'lib/bitwizard.rb', line 77 def write(reg, value) raise ArgumentError.new "#{reg} is not a valid register, must be a number between 0x00..0xff" unless reg.is_a? Fixnum and (0..255).include? reg raise ArgumentError.new "#{value} is not a valid value, must be a single byte or a string" unless (value.is_a? Fixnum and (0..255).include? value) or (value.is_a? String) value = value.unpack("C*") if value.is_a? String return spi_write(reg, value) if @bus == :spi return i2c_write(reg, value) if @bus == :i2c end |