Skip to content

Commit

Permalink
Merge pull request #5 from splaspla-hacker/edge
Browse files Browse the repository at this point in the history
0.1.4
  • Loading branch information
jiikko authored Jul 10, 2021
2 parents 86dc83b + 0407364 commit 2f30852
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.1.4] - 2021-0
- ProconBypassMan.rootの定義を、gem rootからproject rootへ変更した
- 連打の頻度を変更できるようにした
- シグナルで設定ファイルを読み直すとpid_pathが消滅する不具合を修正した

## [0.1.3] - 2021-07-03
- 接続のしやすさ向上

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ gemspec
gem "rake", "~> 13.0"
gem "rspec"
gem "pry"
gem "timecop"
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
procon_bypass_man (0.1.3)
procon_bypass_man (0.1.4)

GEM
remote: https://rubygems.org/
Expand All @@ -26,6 +26,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
timecop (0.9.4)

PLATFORMS
arm-linux
Expand All @@ -37,6 +38,7 @@ DEPENDENCIES
pry
rake (~> 13.0)
rspec
timecop

BUNDLED WITH
2.2.20
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ Switch <-- (PBM): ZR連打
* 設定ファイルをwebから反映できる
* ラズパイのプロビジョニングを楽にしたい
* レコーディング機能(プロコンの入力をマクロとして登録ができる)
* webページから設定ファイルを変更できるようにする(sshしたくない)
* webサーバのデーモンとPBMはプロセスを分ける(NOTE)
* マクロにdelayを入れれるようにする

## 開発系
```ruby
ProconBypassMan.tap do |pbm|
pbm.logger = Logger.new("#{ProconBypassMan.root}/app.log", 5, 1024 * 1024 * 10)
pbm.logger.level = :debug
pbm.root = File.expand_path(__dir__)
end
```

Expand Down
1 change: 1 addition & 0 deletions examples/practical/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ProconBypassMan.tap do |pbm|
pbm.logger = Logger.new("#{ProconBypassMan.root}/app.log", 5, 1024 * 1024 * 10) # 5世代まで残して, 10MBでローテーション
pbm.logger.level = :debug
pbm.root = File.expand_path(__dir__)
end

ProconBypassMan.run(setting_path: "./setting.yml")
2 changes: 1 addition & 1 deletion examples/practical/setting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ setting: |-
prefix_keys_for_changing_layer [:zr, :r, :zl, :l]
layer :up, mode: :manual do
flip :zr, if_pressed: :zr, force_neutral: :zl
flip :zr, if_pressed: :zr, force_neutral: :zl, flip_interval: "8F"
flip :zl, if_pressed: [:y, :b, :zl]
flip :down, if_pressed: :down
macro fast_return.name, if_pressed: [:y, :b, :down]
Expand Down
15 changes: 11 additions & 4 deletions lib/procon_bypass_man.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def self.configure(setting_path: nil, &block)

def self.run(setting_path: nil, &block)
configure(setting_path: setting_path, &block)
at_exit { FileUtils.rm_rf(pid_path) }
File.write(pid_path, $$)
registry = ProconBypassMan::DeviceRegistry.new
Runner.new(gadget: registry.gadget, procon: registry.procon).run
rescue CouldNotLoadConfigError
ProconBypassMan.logger.error "設定ファイルが不正です。設定ファイルの読み込みに失敗しました"
puts "設定ファイルが不正です。設定ファイルの読み込みに失敗しました"
FileUtils.rm_rf(ProconBypassMan.pid_path)
exit 1
rescue FirstConnectionError
puts "接続を確立できませんでした。やりなおします。"
Expand All @@ -58,9 +58,8 @@ def self.logger
end
end

DEFAULT_PID_PATH = File.expand_path('../pbm_pid', __dir__).freeze
def self.pid_path
@@pid_path ||= DEFAULT_PID_PATH
@@pid_path ||= File.expand_path("#{root}/pbm_pid", __dir__).freeze
end

def self.reset!
Expand All @@ -72,6 +71,14 @@ def self.reset!
end

def self.root
File.expand_path('..', __dir__).freeze
if defined?(@@root)
@@root
else
File.expand_path('..', __dir__).freeze
end
end

def self.root=(path)
@@root = path
end
end
13 changes: 9 additions & 4 deletions lib/procon_bypass_man/configuration/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(mode: :manual, &block)
end

# @param [Symbol] button
def flip(button, if_pressed: false, channel: nil, force_neutral: nil)
def flip(button, if_pressed: false, force_neutral: nil, flip_interval: nil)
case if_pressed
when TrueClass
if_pressed = [button]
Expand All @@ -24,12 +24,17 @@ def flip(button, if_pressed: false, channel: nil, force_neutral: nil)
raise "not support class"
end
hash = { if_pressed: if_pressed }
if channel
hash[:channel] = channel
end
if force_neutral
hash[:force_neutral] = force_neutral
end
if flip_interval
if /\A(\d+)F\z/i =~ flip_interval
interval = ((frame = $1.to_i) / 60.0).floor(2)
else
raise "8F みたいなフォーマットで入力してください"
end
hash[:flip_interval] = interval
end
self.flips[button] = hash
end

Expand Down
15 changes: 11 additions & 4 deletions lib/procon_bypass_man/procon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ProconBypassMan::Procon
require "procon_bypass_man/procon/button_collection"
require "procon_bypass_man/procon/pressed_button_helper"
require "procon_bypass_man/procon/user_operation"
require "procon_bypass_man/procon/flip_cache"

attr_accessor :user_operation

Expand Down Expand Up @@ -51,15 +52,21 @@ def apply!
when :manual
@@status[:ongoing_mode] = ModeRegistry.load(:manual)
current_layer.flip_buttons.each do |button, options|
unless options[:if_pressed]
status[button] = !status[button]
if !options[:if_pressed]
FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
status[button] = !status[button]
end
next
end

if options[:if_pressed] && options[:if_pressed].all? { |b| user_operation.pressed_button?(b) }
status[button] = !status[button]
FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
status[button] = !status[button]
end
else
status[button] = false
FlipCache.fetch(key: button, expires_in: options[:flip_interval]) do
status[button] = false
end
end
end
else
Expand Down
22 changes: 22 additions & 0 deletions lib/procon_bypass_man/procon/flip_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class ProconBypassMan::Procon
class FlipCache
def self.fetch(key: , expires_in: , &block)
if expires_in.nil?
block.call
else
@@previous_flips_at_table[key] ||= Time.now
if @@previous_flips_at_table[key] < Time.now
@@previous_flips_at_table[key] = Time.now + expires_in
block.call
end
end
end

# for testing
def self.reset!
@@previous_flips_at_table = {}
end

reset!
end
end
11 changes: 8 additions & 3 deletions lib/procon_bypass_man/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def run
self_write.puts(sig)
end
rescue ArgumentError
ProconBypassMan.logger.info("Signal #{sig} not supported")
ProconBypassMan.logger.error("Signal #{sig} not supported")
end
end

Expand Down Expand Up @@ -53,6 +53,7 @@ def run
Process.wait
@gadget&.close
@procon&.close
FileUtils.rm_rf(ProconBypassMan.pid_path)
exit 1
end
end
Expand Down Expand Up @@ -179,10 +180,14 @@ def handle_signal(sig)
# @return [void]
def print_booted_message
booted_message = <<~EOF
----
RUBY_VERSION: #{RUBY_VERSION}
ProconBypassMan: #{ProconBypassMan::VERSION}
pid_path: #{ProconBypassMan.pid_path}
pid: #{$$}
project_path: #{ProconBypassMan.root}
root: #{ProconBypassMan.root}
pid_path: #{ProconBypassMan.pid_path}
setting_path: #{ProconBypassMan::Configuration.instance.setting_path}
----
EOF
ProconBypassMan.logger.info(booted_message)
puts booted_message
Expand Down
2 changes: 1 addition & 1 deletion lib/procon_bypass_man/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ProconBypassMan
VERSION = "0.1.3"
VERSION = "0.1.4"
end
15 changes: 13 additions & 2 deletions spec/lib/procon_bypass_man/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def self.binaries; ['a']; end
install_mode_plugin AModePlugin
layer :up do
flip :l, if_pressed: true
flip :r, channel: 1
flip :r
end
layer :down, mode: :manual do
flip :r, if_pressed: [:zr, :zl]
Expand All @@ -251,7 +251,7 @@ def self.binaries; ['a']; end
layer :left
end
expect(ProconBypassMan::Configuration.instance.layers[:up].flip_buttons[:l]).to eq(if_pressed: [:l])
expect(ProconBypassMan::Configuration.instance.layers[:up].flip_buttons[:r]).to eq(if_pressed: false, channel: 1)
expect(ProconBypassMan::Configuration.instance.layers[:up].flip_buttons[:r]).to eq(if_pressed: false)
expect(ProconBypassMan::Configuration.instance.layers[:up].flip_buttons.keys).to eq([:l, :r])
expect(ProconBypassMan::Configuration.instance.layers[:up].mode).to eq(:manual)
expect(ProconBypassMan::Configuration.instance.layers[:down].flip_buttons.keys).to eq([:r])
Expand Down Expand Up @@ -311,5 +311,16 @@ def self.binaries; ['a']; end
expect(ProconBypassMan::Configuration.instance.prefix_keys).to eq([:zr])
end
end

context 'flip_interval' do
it do
ProconBypassMan.configure do
layer :up do
flip :zr, flip_interval: "8F"
end
end
expect(ProconBypassMan::Configuration.instance.layers[:up].flip_buttons[:zr][:flip_interval]).to eq(0.13)
end
end
end
end
28 changes: 28 additions & 0 deletions spec/lib/procon_bypass_man/procon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
ProconBypassMan.reset!
end

context 'with flip_interval' do
let(:data) { "30f28100800078c77448287509550274ff131029001b0022005a0271ff191028001e00210064027cff1410280020002100000000000000000000000000000000" } # no_action
it do
Timecop.freeze(Time.parse("2011-11-11 10:00:00 +09:00")) do
ProconBypassMan::Procon::FlipCache.reset!
ProconBypassMan.configure do
prefix_keys_for_changing_layer [:zr]
layer :up do
flip :zr, flip_interval: "60F"
end
end
procon = ProconBypassMan::Procon.new(binary)
procon.apply!
expect(ProconBypassMan::Procon.new(procon.to_binary).pressed_zr?).to eq(false)

procon = ProconBypassMan::Procon.new(binary)
procon.apply!
expect(ProconBypassMan::Procon.new(procon.to_binary).pressed_zr?).to eq(false)
end

Timecop.freeze(Time.parse("2011-11-11 10:00:02 +09:00")) do
procon = ProconBypassMan::Procon.new(binary)
procon.apply!
expect(ProconBypassMan::Procon.new(procon.to_binary).pressed_zr?).to eq(true)
end
end
end

context 'with mode' do
let(:data) { "30f28100800078c77448287509550274ff131029001b0022005a0271ff191028001e00210064027cff1410280020002100000000000000000000000000000000" } # no_action
let(:pressed_y_and_b) { "30778105800099277344e86b0a7909f4f5a8f4b500c5ff8dff6c09cdf5b8f49a00c5ff92ff6a0979f5eef46500d5ff9bff000000000000000000000000000000" }
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "procon_bypass_man"
require "pry"
require "timecop"
require "bundler/setup"

# This file was generated by the `rspec --init` command. Conventionally, all
Expand Down

0 comments on commit 2f30852

Please sign in to comment.