-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonion-gpio.rb
311 lines (276 loc) · 9.38 KB
/
onion-gpio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
64
65
66
67
68
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
############################################################################
# onion_gpio.rb : binding for gpio acces in ruby
#--------------------------------------------------------------------------
# In this version, acess are done via filefs.
#
# benchmark :
# 0.4 ms seconds for writing a value
# 3 ms if export/unexport on each access
#
# TODO : access par direct read/write n MIPS register
#--------------------------------------------------------------------------
# A filefs documentation:
# http://elixir.free-electrons.com/linux/latest/source/Documentation/gpio/sysfs.txt
#
###########################################################################
########################################################################
# Constants, for Onin/Omega architectures
########################################################################
class ConstOnionGpio
VERSION = "1"
GPIO_BASE_PATH= '/sys/class/gpio'
GPIO_EXPORT = GPIO_BASE_PATH + '/export'
GPIO_UNEXPORT = GPIO_BASE_PATH + '/unexport'
GPIO_PATH = GPIO_BASE_PATH + '/gpio%d'
GPIO_VALUE_FILE ='value'
GPIO_DIRECTION_FILE = 'direction'
GPIO_ACTIVE_LOW_FILE = 'active_low'
GPIO_INPUT_DIRECTION = 'in'
GPIO_OUTPUT_DIRECTION = 'out'
GPIO_OUTPUT_DIRECTION_LOW = 'low'
GPIO_OUTPUT_DIRECTION_HIGH = 'high'
GPIO_ACTIVE_HIGH = 0
GPIO_ACTIVE_LOW = 1
end
###############" Tools ror read/write files, run process
module V1Tools
def fwrite(*args)
fn=args[0..-2].join("/")
value=args.last.to_s
puts("%-40s ==> %s" % [fn,value]) if @verbose
open(fn,"w") {|f| f.puts(value)}
end
def fread(*args)
fn=args.join("/")
open(fn,"r") {|f| return(f.read().strip) }
end
def async_spawn(*args)
puts "async_spawn : > #{args.join(' ')}" if @verbose
pid=spawn(*(args.flatten),:chdir=>"/tmp")
Process.detach(pid)
end
def sync_spawn(*args)
puts "sync_spawn : > #{args.join(' ')}" if @verbose
pid=spawn(*(args.flatten),:chdir=>"/tmp")
Process.wait(pid)
end
def exec(*args)
puts "exec : > #{args.join(' ')}" if @verbose
system( *( args.map {|a| a.to_s} ))
end
end
########################################################################
# OnionGpio : digital io access : read/write/pwm
########################################################################
class OnionGpio < ConstOnionGpio
include V1Tools
def initialize(gpio, verbose=false)
@gpio = gpio
@path = GPIO_PATH % [ gpio ]
@verbose = verbose
puts 'GPIO%d path: %s' % [@gpio, @path] if @verbose
@getted=0
end
def started()
_initGpio()
end
def release()
@getted=1
_freeGpio()
end
# Write to the gpio export to make the gpio available in sysfs
def _initGpio()
( fwrite(GPIO_EXPORT,@gpio) rescue _freeGpio() ) if @getted==0
@getted+=1
end
# Write to the gpio unexport to release the gpio sysfs instance
def _freeGpio()
fwrite(GPIO_UNEXPORT,@gpio) if @getted==1
@getted-=1
end
def getValue()
_initGpio()
ret=fread(@path,GPIO_VALUE_FILE)
_freeGpio()
ret
end
def setValue(value)
_initGpio()
ret=fwrite(@path,GPIO_VALUE_FILE,value)
_freeGpio()
end
# direction functions
def getDirection()
_initGpio()
ret=fread(@path,GPIO_DIRECTION_FILE)
_freeGpio()
ret
end
def _setDirection(direction)
# check the direction argument
if direction != GPIO_INPUT_DIRECTION and direction != GPIO_OUTPUT_DIRECTION and direction != GPIO_OUTPUT_DIRECTION_LOW and direction != GPIO_OUTPUT_DIRECTION_HIGH
raise("setDirection() : invalide value for direction: '#{direction}'")
end
_initGpio()
fwrite(@path,GPIO_DIRECTION_FILE,direction)
setValue(0) if direction == GPIO_OUTPUT_DIRECTION
_freeGpio()
end
def setInputDirection() _setDirection(GPIO_INPUT_DIRECTION) end
def setOutputDirection(initial=-1)
argument = GPIO_OUTPUT_DIRECTION
argument = case initial
when 0 then GPIO_OUTPUT_DIRECTION_LOW
when 1 then GPIO_OUTPUT_DIRECTION_HIGH
else
GPIO_OUTPUT_DIRECTION
end
_setDirection(argument)
end
=begin
# active-low functions
def getActiveLow()
_initGpio()
activeLow=fread(@path,GPIO_ACTIVE_LOW_FILE)
fwrite(@path,GPIO_DIRECTION_FILE,direction)
setValue(0) if direction == GPIO_OUTPUT_DIRECTION
_freeGpio()
end
def _setActiveLow(activeLow)
raise("") if activeLow != GPIO_ACTIVE_HIGH && activeLow != GPIO_ACTIVE_LOW
_initGpio()
fwrite(@path,GPIO_ACTIVE_LOW_FILE)
_freeGpio()
end
def setActiveHigh() _setActiveLow(GPIO_ACTIVE_HIGH) end
def setActiveLow() _setActiveLow(GPIO_ACTIVE_LOW) end
=end
# period = (1.0f/freq) * 1000;
# dutyCycle = duty / 100.0f;
# periodHigh = period * dutyCycle;
# periodLow = period - periodHigh; //can also be: period * (1.0f - dutyCycle);
def pwm_set(frequency,on_ratio)
r=on_ratio.to_i
puts "pwm #{@gpio} => set to f=#{frequency} pulse=#{r}%" if @verbose
async_spawn( "fast-gpio","pwm",@gpio.to_s,frequency.to_s,(r < 0 ? 0 : (r > 100 ? 100 : r)).to_s)
end
def pwm_reset()
puts "pwm #{@gpio} => pwm reset" if @verbose
async_spawn( "fast-gpio",@gpio.to_s,"0")
end
end
########################################################################
# I2C : commands line I2C via i2cget i2cset i2cdetect
########################################################################
class OnionI2C < ConstOnionGpio
include V1Tools
def initialize(gpio, verbose=false)
@gpio = gpio
@verbose = verbose
puts 'I2C%d path: %s' % [@gpio, @path] if @verbose
@getted=0
end
def show_detect(no=3)
puts exec("i2cdetect -y #{@gpio}")
end
def show_registers_bytes(no=3)
puts exec("i2cdump -y #{@gpio} #{no} b")
end
def show_registers_words(no=3)
puts exec("i2cdump -y #{@gpio} #{no} w")
end
def read_register_byte(no,address)
ret=exec("i2cget","-y",@gpio.to_s,no.to_s,"0x%02X" % address,"b")
end
def read_register_word(no,address)
ret=exec("i2cget","-y",@gpio.to_s,no.to_s,"0x%02X" % address,"w")
end
end
########################################################################
# Oled
# Seem to work with any SSD1308 Oled ship (I2C Oled driver 128xN)
# Tested with a 128x64 Oled Goove (seedStudio)
# Should work with Onion OledExp
# Draw file format :
# See http://www.seeedstudio.com/forum/viewtopic.php?f=10&t=3274
########################################################################
class OnionOled < ConstOnionGpio
include V1Tools
def initialize(i2c,noi2c, verbose=false)
@i2c = i2c
@noi2c = noi2c
@verbose = verbose
@opt=["-q"]
@opt=["-v"] if verbose
puts('OLED(%d,%d) ' % [@i2c,@noi2c]) if @verbose
end
def reset() sync_spawn("oled-exp","-c","power","on") end
def clear() sync_spawn("oled-exp","-c","cursor","1,1") end
# set cursar and wwirte text a this position
# position starts at 1,1 , max are 20,5 for a 128x64 OLED
def pos(x,y,t="") sync_spawn("oled-exp",@opt,"cursor","#{y+1},#{x+1}","write",t.to_s) end
def write_at(x,y,text="") sync_spawn("oled-exp",@opt,"cursor","#{y+1},#{x+1}","write",text.to_s) end
def write_text(text) sync_spawn("oled-exp",@opt,"write",text) end
def write(text) sync_spawn("oled-exp",@opt,"write",text) end
def write_byte(value)sync_spawn("oled-exp",@opt,"writeByte",value.to_s) end
def invert(on) sync_spawn("oled-exp",@opt,"invert", on ? "on" : "off") end
def bright(on) sync_spawn("oled-exp",@opt,"dim", on ? "off" : "on") end
# cascad: chain several command in one shot
# "write","txt"
# "writeByte",v
# "invert","on/of"
# "dim","on/off"
# "curser","y,x"
def cascad(lcmd) sync_spawn("oled-exp",@opt,"cascad",*(lcmd.map {|a|i a.to_s}) ) end
# file should be a lcd formated raster image :
# create image,
# convert it to ppm with image magick,
# then convert it to oled format using ppm2oled.rb from here
# convert x.png x.ppm ; ruby ppm2oled.rb x.ppm x.bin
def write_raster_file(fn)
raise("File not exist") unless File.file?(fn)
puts "> oled-exp draw #{fn}" if @verbose
system("oled-exp","draw",fn)
#sync_spawn("oled-exp",@opt,"draw",fn) >> issue with file location...
end
end
############################ T e s t s ################################
if $0 == __FILE__
def chrono(nb=1)
start=Time.now
nb.times {|i| yield(i) }
eend=Time.now
puts "Duration: #{((eend.to_f-start.to_f)*1000)/nb} ms"
end
def mp(*args)
puts "\n\n#{'#'*40}" if args.size>0 && args.first[0]=="#"
args.each {|s| puts s.to_s }
puts "#{'#'*40}\n" if args.size>0 && args.first[0]=="#"
end
mp("# OLED Test")
oled=OnionOled.new(0,0,true)
oled.reset
oled.write_text("CouCou") ; sleep(1)
oled.write_raster_file("oled.bin"); sleep(1)
oled.pos(0,1)
3.times { oled.write_text(Time.now.to_s.split(' ')[1]) ; sleep(1) }
20.times {|x|
oled.pos(x,2+x%2,'X')
sleep(1)
}
mp("# PWM Test")
pwm=OnionGpio.new( (ARGV.shift || "3").to_i ,true)
pwm.pwm_set(2,20)
sleep(5)
pwm.pwm_reset()
chrono(100) { |i| pwm.pwm_set(2,i) }
pwm.pwm_reset()
mp("# Gpio digital io Test")
gpio=OnionGpio.new( (ARGV.shift || "3").to_i, false)
gpio.setOutputDirection()
10.times { |i| gpio.setValue(i%2) ; sleep 0.1}
chrono(100) { |i| gpio.setValue(i%2) }
gpio.started
chrono(100) { |i| gpio.setValue(i%2) }
gpio.release
end