Class: ChipGPIO::SoftSPI
- Inherits:
-
Object
- Object
- ChipGPIO::SoftSPI
- Defined in:
- lib/chip-gpio/SoftSpi.rb
Instance Attribute Summary collapse
-
#clock_pin ⇒ Object
readonly
Returns the value of attribute clock_pin.
-
#input_pin ⇒ Object
readonly
Returns the value of attribute input_pin.
-
#output_pin ⇒ Object
readonly
Returns the value of attribute output_pin.
-
#phase ⇒ Object
readonly
Returns the value of attribute phase.
-
#polarity ⇒ Object
readonly
Returns the value of attribute polarity.
-
#word_size ⇒ Object
readonly
Returns the value of attribute word_size.
Instance Method Summary collapse
-
#initialize(clock_pin: nil, input_pin: nil, output_pin: nil, polarity: 1, phase: 0, word_size: 8) ⇒ SoftSPI
constructor
A new instance of SoftSPI.
- #max_word ⇒ Object
- #write(words: [], reverse_output: true) ⇒ Object
Constructor Details
#initialize(clock_pin: nil, input_pin: nil, output_pin: nil, polarity: 1, phase: 0, word_size: 8) ⇒ SoftSPI
Returns a new instance of SoftSPI.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/chip-gpio/SoftSpi.rb', line 31 def initialize(clock_pin: nil, input_pin: nil, output_pin: nil, polarity: 1, phase: 0, word_size: 8) raise ArgumentError, "clock_pin is required" if clock_pin == nil raise ArgumentError, "At least input_pin or output_pin must be specified" if ((input_pin == nil) && (output_pin == nil)) raise ArgumentError, "polarity must be either 0 or 1" if ((polarity != 0) && (polarity != 1)) raise ArgumentError, "phase must be either 0 or 1" if ((phase != 0) && (phase != 1)) pins = ChipGPIO.get_pins() @clock_pin = nil @input_pin = nil @output_pin = nil @clock_pin = pins[clock_pin] @input_pin = pins[input_pin] if (input_pin) @output_pin = pins[output_pin] if (output_pin) @clock_pin.export if not @clock_pin.available? @input_pin.export if input_pin && (not @input_pin.available?) @output_pin.export if output_pin && (not @output_pin.available?) @clock_pin.direction = :output @input_pin.direction = :output if (input_pin) @output_pin.direction = :output if (output_pin) @clock_pin.value = 0 @input_pin.value = 0 if (input_pin) @output_pin.value = 0 if (output_pin) @polarity = polarity @phase = phase @word_size = word_size end |
Instance Attribute Details
#clock_pin ⇒ Object (readonly)
Returns the value of attribute clock_pin.
23 24 25 |
# File 'lib/chip-gpio/SoftSpi.rb', line 23 def clock_pin @clock_pin end |
#input_pin ⇒ Object (readonly)
Returns the value of attribute input_pin.
24 25 26 |
# File 'lib/chip-gpio/SoftSpi.rb', line 24 def input_pin @input_pin end |
#output_pin ⇒ Object (readonly)
Returns the value of attribute output_pin.
25 26 27 |
# File 'lib/chip-gpio/SoftSpi.rb', line 25 def output_pin @output_pin end |
#phase ⇒ Object (readonly)
Returns the value of attribute phase.
28 29 30 |
# File 'lib/chip-gpio/SoftSpi.rb', line 28 def phase @phase end |
#polarity ⇒ Object (readonly)
Returns the value of attribute polarity.
27 28 29 |
# File 'lib/chip-gpio/SoftSpi.rb', line 27 def polarity @polarity end |
#word_size ⇒ Object (readonly)
Returns the value of attribute word_size.
29 30 31 |
# File 'lib/chip-gpio/SoftSpi.rb', line 29 def word_size @word_size end |
Instance Method Details
#max_word ⇒ Object
65 66 67 |
# File 'lib/chip-gpio/SoftSpi.rb', line 65 def max_word ((2**@word_size) - 1) end |
#write(words: [], reverse_output: true) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/chip-gpio/SoftSpi.rb', line 69 def write(words: [], reverse_output: true) raise "An output_pin must be specified to write" if !@output_pin bits = Array (0..(@word_size - 1)) if reverse_output words = words.reverse() bits = bits.reverse() end words.each do |w| w = 0 if w < 0 w = max_word if w > max_word bits.each do |b| @clock_pin.value = (1 - @polarity) if (w & (1 << b)) > 0 @output_pin.value = 1 else @output_pin.value = 0 end @clock_pin.value = @polarity end @clock_pin.value = (1 - @polarity) end end |