pi_driver 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pi_driver/pin.rb +35 -87
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc90f84fe49ba85fc4c6e0f5b3af5e6396952733
4
- data.tar.gz: 30d3039abade3868c00a0872176b243452e3a39b
3
+ metadata.gz: dfc021b1477c6a5a8dee6afc2f5550dcc7eeb510
4
+ data.tar.gz: e6bd262304d0f0a689bc600710d8180e46ade1c9
5
5
  SHA512:
6
- metadata.gz: d7c63839480a43a98c4a26e7e9aa20bd4d5e196b697f2eb6db48098997ffb21e2f8fd97db4cff0e5cbe01c140d3837fa8f170bdcd1f986eb2d16ec42d25c4725
7
- data.tar.gz: '0635499eee9f47a2479e2759d72399231bce35417ccac7a9d673c858d0c3bb93ed3e018482ece97be0fc04290164f64a363e5b90cdeb708a86e245f22143f582'
6
+ metadata.gz: 7339065022461a951bd697926a746c1925bc93cc3e193a540dd93697f375caefdd28425b9cd36ab1163c7d97c01f04fdca83adbe5229e475538876b5d63835b8
7
+ data.tar.gz: b94b27a6d86ae44c991729e764f7c177f480886ae3e2f91a6ff6ed0c965394cc00429608522f1185056fee493804133a469f075a3f9784454251ecb73a6c8647
data/lib/pi_driver/pin.rb CHANGED
@@ -1,46 +1,37 @@
1
+ require 'pi_driver/pin/argument_helper'
2
+ require 'pi_driver/pin/board'
3
+ require 'pi_driver/pin/direction'
4
+ require 'pi_driver/pin/edge'
5
+ require 'pi_driver/pin/file_helper'
6
+ require 'pi_driver/pin/value'
7
+
1
8
  module PiDriver
2
9
  class Pin
3
- # delete this and below
4
- DIR_BASE = File.expand_path '~/pi/gpio/sys/class'
5
- # # uncomment below, delete this
6
- # DIR_BASE = '/sys/class'
7
-
8
- DIR_GPIO = "#{DIR_BASE}/gpio"
9
-
10
10
  attr_reader :gpio_number
11
11
 
12
- class Direction
13
- INPUT = :in
14
- OUTPUT = :out
15
- VALID_DIRECTIONS = [INPUT, OUTPUT]
16
- end
12
+ def initialize(gpio_number, options = {})
13
+ @gpio_number = gpio_number
17
14
 
18
- class Edge
19
- RISING = :rising
20
- FALLING = :falling
21
- BOTH = :both
22
- NONE = :none
23
- VALID_EDGES = [RISING, FALLING, BOTH, NONE]
24
- end
15
+ @argument_helper = ArgumentHelper.new @gpio_number
16
+ @argument_helper.check(:gpio_number, gpio_number, Board::VALID_NUMBERS)
25
17
 
26
- class Value
27
- LOW = 0
28
- HIGH = 1
29
- VALID_VALUES = [LOW, HIGH]
30
- end
18
+ @direction = options[:direction] || Direction::INPUT
19
+ @value = options[:value] || Value::LOW
31
20
 
32
- def initialize(gpio_number, options = {})
33
- direction = options[:direction] || Direction::INPUT
34
- value = options[:value] || Value::LOW
21
+ @file_helper = FileHelper.new @gpio_number
35
22
 
36
- write_export gpio_number
23
+ @file_helper.write_export
37
24
 
38
- write_direction(direction)
39
- input? ? read_value : write_value(value)
25
+ @argument_helper.check(:direction, @direction, Direction::VALID_DIRECTIONS)
26
+ @file_helper.write_direction(@direction)
27
+
28
+ @argument_helper.check(:value, @value, Value::VALID_VALUES)
29
+ input? ? @file_helper.read_value : @file_helper.write_value(@value)
40
30
  end
41
31
 
42
32
  def input
43
- write_direction(Direction::INPUT) unless input?
33
+ @direction = Direction::INPUT
34
+ @file_helper.write_direction(@direction)
44
35
  end
45
36
 
46
37
  def input?
@@ -48,8 +39,11 @@ module PiDriver
48
39
  end
49
40
 
50
41
  def output(value = Value::LOW)
51
- write_direction(Direction::OUTPUT) unless output?
52
- write_value(value)
42
+ @argument_helper.check(:value, value, Value::VALID_VALUES)
43
+ @value = value
44
+ @direction = Direction::OUTPUT
45
+ @file_helper.write_direction(@direction)
46
+ @file_helper.write_value(@value)
53
47
  end
54
48
 
55
49
  def output?
@@ -58,7 +52,8 @@ module PiDriver
58
52
 
59
53
  def clear
60
54
  return unless output?
61
- write_value(Value::LOW) unless clear?
55
+ @value = Value::LOW
56
+ @file_helper.write_value(@value)
62
57
  @value
63
58
  end
64
59
 
@@ -68,7 +63,8 @@ module PiDriver
68
63
 
69
64
  def set
70
65
  return unless output?
71
- write_value(Value::HIGH) unless set?
66
+ @value = Value::HIGH
67
+ @file_helper.write_value(@value)
72
68
  @value
73
69
  end
74
70
 
@@ -77,18 +73,17 @@ module PiDriver
77
73
  end
78
74
 
79
75
  def value
80
- input? ? read_value : @value
76
+ input? ? @file_helper.read_value : @value
81
77
  end
82
78
 
83
79
  def interrupt(edge = Edge::RISING)
84
- check_arg(:edge, edge, Edge::VALID_EDGES)
85
-
80
+ @argument_helper.check(:edge, edge, Edge::VALID_EDGES)
86
81
  @edge = edge
87
82
 
88
83
  @interrupt_thread = Thread.new do
89
- last_value = read_value
84
+ last_value = @file_helper.read_value
90
85
  loop do
91
- new_value = read_value
86
+ new_value = @file_helper.read_value
92
87
  yield if block_given? && interrupted?(new_value, last_value)
93
88
  last_value = new_value
94
89
  end
@@ -101,16 +96,6 @@ module PiDriver
101
96
 
102
97
  private
103
98
 
104
- def check_arg(type, arg, valid_options)
105
- valid_options_for_message = valid_options.map { |value| ":#{value}"}.join(', ')
106
- message = "Pin #{@gpio_number} invalid #{type}: #{arg} expected to be one of #{valid_options_for_message}"
107
- raise ArgumentError, message unless valid_options.include?(arg)
108
- end
109
-
110
- def dir_pin
111
- "#{DIR_GPIO}/gpio#{@gpio_number}"
112
- end
113
-
114
99
  def interrupted?(new_value, last_value)
115
100
  rising_edge = new_value == Value::HIGH && last_value == Value::LOW
116
101
  falling_edge = new_value == Value::LOW && last_value == Value::HIGH
@@ -124,42 +109,5 @@ module PiDriver
124
109
  rising_edge || falling_edge
125
110
  end
126
111
  end
127
-
128
- def path_direction
129
- "#{dir_pin}/direction"
130
- end
131
-
132
- def path_export
133
- "#{DIR_GPIO}/export"
134
- end
135
-
136
- def path_value
137
- "#{dir_pin}/value"
138
- end
139
-
140
- def read_direction
141
- @direction = File.read(path_direction).to_sym
142
- end
143
-
144
- def read_value
145
- @value = File.read(path_value).to_i
146
- end
147
-
148
- def write_direction(direction)
149
- check_arg(:direction, direction, Direction::VALID_DIRECTIONS)
150
- @direction = direction
151
- File.write(path_direction, @direction)
152
- end
153
-
154
- def write_export(gpio_number)
155
- @gpio_number = gpio_number
156
- File.write(path_export, @gpio_number)
157
- end
158
-
159
- def write_value(value)
160
- check_arg(:value, value, Value::VALID_VALUES)
161
- @value = value
162
- File.write(path_value, @value)
163
- end
164
112
  end
165
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pi_driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Winningham