Class: ChipGPIO::SoftSPI

Inherits:
Object
  • Object
show all
Defined in:
lib/chip-gpio/SoftSpi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clock_pin: nil, input_pin: nil, output_pin: nil, polarity: 1, phase: 0, word_size: 8, lsb_first: false) ⇒ SoftSPI

Returns a new instance of SoftSPI.

Raises:

  • (ArgumentError)


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
64
65
# File 'lib/chip-gpio/SoftSpi.rb', line 32

def initialize(clock_pin: nil, input_pin: nil, output_pin: nil, polarity: 1, phase: 0, word_size: 8, lsb_first: false)
  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
  @lsb_first = lsb_first
end

Instance Attribute Details

#clock_pinObject (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_pinObject (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

#lsb_firstObject (readonly)

Returns the value of attribute lsb_first.



30
31
32
# File 'lib/chip-gpio/SoftSpi.rb', line 30

def lsb_first
  @lsb_first
end

#output_pinObject (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

#phaseObject (readonly)

Returns the value of attribute phase.



28
29
30
# File 'lib/chip-gpio/SoftSpi.rb', line 28

def phase
  @phase
end

#polarityObject (readonly)

Returns the value of attribute polarity.



27
28
29
# File 'lib/chip-gpio/SoftSpi.rb', line 27

def polarity
  @polarity
end

#word_sizeObject (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_wordObject



67
68
69
# File 'lib/chip-gpio/SoftSpi.rb', line 67

def max_word
  ((2**@word_size) - 1)
end

#write(words: []) ⇒ Object



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
98
# File 'lib/chip-gpio/SoftSpi.rb', line 71

def write(words: [])
  raise "An output_pin must be specified to write" if !@output_pin

  #you can't make reverse ranges so this logic is gross
  #the key point is that 0 is the MSB so we only want it first if 
  #@lsb_first is set
  bits = Array (0..(@word_size - 1))
  bits = bits.reverse() if !@lsb_first 
  
  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