Class: BitWizard::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/bitwizard.rb

Defined Under Namespace

Classes: NullLogger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Board

Creates a generic board handle for reading and writing directly.

Parameters:

  • options (Hash)

    A hash of options.

Options Hash (options):

  • :address (Fixnum)

    The address of the board. (0x00..0xff)

  • :type (Symbol)

    The board type, defaults to auto detecting. (identifier)

  • :bus (Symbol)

    The bus it’s connected to. (:spi or :i2c)

  • :skip_check (Boolean)

    Skip the self check that runs on creation.

  • :clock (Number)

    The clockrate you want to run the communication with.

  • :logger (Logger)

    Add a logger here to log data that’s sent and received.



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(options)
	options = {
		address: -1,
		type: :auto_detect,
		bus: :spi,
		skip_check: false,
		clock: 45000,
		logger: NullLogger.new
	}.merge(options)

	raise ArgumentError.new "Bus must be :spi or :i2c." unless options[:bus] == :spi or options[:bus] == :i2c

	@logger = options[:logger]
	@address = options[:address]
	@type = options[:type]
	@bus = options[:bus]

	self_check! unless options[:skip_check]
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



35
36
37
# File 'lib/bitwizard.rb', line 35

def address
  @address
end

#busObject (readonly)

Returns the value of attribute bus.



35
36
37
# File 'lib/bitwizard.rb', line 35

def bus
  @bus
end

#featuresObject (readonly)

Returns the value of attribute features.



35
36
37
# File 'lib/bitwizard.rb', line 35

def features
  @features
end

#loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/bitwizard.rb', line 36

def logger
  @logger
end

#typeObject (readonly)

Returns the value of attribute type.



35
36
37
# File 'lib/bitwizard.rb', line 35

def type
  @type
end

#versionObject (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.

Parameters:

  • address (Number)

    The address to check.

  • options (optional, Hash)

    A Hash of options.

Options Hash (options):

  • :bus (Symbol)

    The type of bus the board is connected on. (:spi or :i2c)

  • :logger (Logger)

    A logger you want to attach to the board.



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(options)
	options = {
		address: -1,
		bus: :spi,
		logger: NullLogger.new
	}.merge(options).merge({
		type: :auto_detect,
		skip_check: false
	})

	options[:logger] = NullLogger.new unless options[:logger]

	temp = BitWizard::Board.new options
	correct = temp.instance_variable_get(:@constructor).call(options.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

Parameters:

  • reg (Number)

    The registry address to read from

  • count (Number)

    The number of bytes to read



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

Returns:

  • (Boolean)


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

Parameters:

  • reg (Number)

    The registry address to write to

  • value (Number|String)

    The data to write to the board



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