Skip to content

Commit 4eddca7

Browse files
committed
Shell.get_device
1 parent 3723034 commit 4eddca7

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

mrbgems/picoruby-ble/mrblib/ble.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def heartbeat_callback
110110
end
111111

112112
def blink_led
113-
@led ||= CYW43::GPIO.new(CYW43::GPIO::LED_PIN)
113+
@led ||= Shell.get_device(:gpio, 'LED_BLE')
114114
@led.write(@led.low? ? 1 : 0)
115115
end
116116

mrbgems/picoruby-shell/mrbgem.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MRuby::Gem::Specification.new('picoruby-shell') do |spec|
1414
spec.add_dependency 'picoruby-env'
1515
spec.add_dependency 'picoruby-crc'
1616
spec.add_dependency 'picoruby-machine'
17+
spec.add_dependency 'picoruby-yaml'
1718
if build.posix?
1819
if build.vm_mrubyc?
1920
spec.add_dependency('picoruby-dir')

mrbgems/picoruby-shell/mrblib/shell.rb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require "env"
2+
require 'gpio'
23
require "metaprog"
34
require "picorubyvm"
45
require "sandbox"
56
require "crc"
67
require "machine"
8+
require 'yaml'
79
begin
810
require "filesystem-fat"
911
require "vfs"
@@ -17,6 +19,25 @@
1719

1820

1921
class Shell
22+
23+
DeviceInstances = {}
24+
25+
def self.get_device(type, name)
26+
key = "#{type}_#{name}".upcase
27+
DeviceInstances[key] ||= case key
28+
when 'GPIO_TRIGGER_NMBLE'
29+
GPIO.new((ENV[key] || 22).to_i, GPIO::IN|GPIO::PULL_UP)
30+
when 'GPIO_LED_BLE', 'GPIO_LED_WIFI'
31+
if ENV[key].nil? || ENV[key] == 'cyw43_led'
32+
CYW43::GPIO.new(CYW43::GPIO::LED_PIN)
33+
else
34+
GPIO.new(ENV[key].to_i, GPIO::OUT)
35+
end
36+
else
37+
raise "Unknown GPIO key: #{key}"
38+
end
39+
end
40+
2041
def self.setup_root_volume(device, label: "PicoRuby")
2142
sleep 1 if device == :sd
2243
return if VFS.volume_index("/")
@@ -89,6 +110,38 @@ def self.setup_system_files(root = nil, force: false)
89110
self.ensure_system_file(path, Machine.unique_id, nil)
90111
end
91112
Dir.chdir ENV['HOME']
113+
114+
config_file = "/etc/config.yml"
115+
# example of `config.yml`:
116+
#
117+
# device:
118+
# gpio:
119+
# trigger_nmble: 22
120+
# led_ble: cyw43_led
121+
# led_wifi: 23
122+
begin
123+
config = YAML.load_file(config_file)
124+
# @type var config: Hash[String, untyped]
125+
device = config['device']
126+
if device&.respond_to?(:each)
127+
device.each do |type, values|
128+
values&.each do |key, value|
129+
ENV["#{type}_#{key}".upcase] = value.to_s
130+
end
131+
end
132+
end
133+
rescue => e
134+
puts "Failed to load config file: #{config_file}"
135+
puts " #{e.message} (#{e.class})"
136+
end
137+
138+
begin
139+
require "cyw43"
140+
if CYW43.respond_to?(:enable_sta_mode)
141+
ENV['WIFI_MODULE'] = "cwy43"
142+
end
143+
rescue
144+
end
92145
end
93146

94147
def self.bootstrap(file)
@@ -178,6 +231,7 @@ def initialize(clean: false)
178231
'|_| |_|\___\___/|_| \_\\___,_|_.__/ \__, |',
179232
" #{AUTHOR_COLOR}by hasumikin#{LOGO_COLOR} |___/"
180233
]
234+
SHORT_LOGO_LINES = ["PicoRuby", " by", "hasumikin"]
181235
elsif RUBY_ENGINE == "mruby"
182236
LOGO_LINES = [
183237
' __ __ _ ____ _',
@@ -187,8 +241,8 @@ def initialize(clean: false)
187241
'|_| |_|_|\___|_| \___/|_| \_\\\\__,_|_.__/ \__, |',
188242
" #{AUTHOR_COLOR}by hasumikin#{LOGO_COLOR} |___/"
189243
]
244+
SHORT_LOGO_LINES = ["MicroRuby", " by", "hasumikin"]
190245
end
191-
SHORT_LOGO_LINES = ["PicoRuby", " by", "hasumikin"]
192246

193247
def show_logo
194248
return nil if ENV['TERM'] == "dumb"

mrbgems/picoruby-shell/shell_executables/r2p2.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
begin
2-
require "cyw43"
3-
if CYW43.respond_to?(:enable_sta_mode)
4-
ENV['WIFI_MODULE'] = "cwy43"
5-
require 'gpio'
6-
pin = GPIO.new(22, GPIO::IN|GPIO::PULL_UP)
7-
if pin.low?
8-
system "nmble"
9-
end
10-
system "wifi_connect --check-auto-connect"
1+
if ENV['WIFI_MODULE'] == "cwy43"
2+
if Shell.get_device(:gpio, 'TRIGGER_NMBLE').low?
3+
system "nmble"
114
end
12-
rescue => e
13-
puts "No WiFi module. Ignore: #{e.message}"
5+
system "wifi_connect --check-auto-connect"
146
end
157

168
if File.exist?("#{ENV['HOME']}/app.mrb")

mrbgems/picoruby-shell/sig/shell.rbs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ $LOAD_PATH: Array[String]
99
ARGV: Array[String]
1010

1111
class Shell
12+
type rtc_t = PCF8523
13+
14+
type device_t = GPIO | ADC | I2C | SPI | UART | PWM
15+
DeviceInstances: Hash[String, device_t]
16+
def self.get_device: (:gpio, String | Symbol name) -> GPIO
17+
| (:adc, String | Symbol name) -> ADC
18+
| (:i2c, String | Symbol name) -> I2C
19+
| (:spi, String | Symbol name) -> SPI
20+
| (:uart, String | Symbol name) -> UART
21+
| (:pwm, String | Symbol name) -> PWM
22+
1223
LOGO_LINES: [String]
1324
SHORT_LOGO_LINES: [String]
1425
LOGO_COLOR: String

0 commit comments

Comments
 (0)