Skip to content

Instantly share code, notes, and snippets.

@izawa
Created August 18, 2017 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izawa/049fee08173206aec582fba575fd79d5 to your computer and use it in GitHub Desktop.
Save izawa/049fee08173206aec582fba575fd79d5 to your computer and use it in GitHub Desktop.
Splatoon2 picture drawing application (requires raspberrypi + UFB)
# coding: utf-8
require 'rmagick'
require 'optparse'
require 'pi_piper'
include PiPiper
class Gpio
DELAY = 0.04
DOT_PIN = 12
RIGHT_PIN = 16
LEFT_PIN = 20
DOWN_PIN = 21
def initialize
@dot = Pin.new pin: DOT_PIN, direction: :out
@dot.on
@right = Pin.new pin: RIGHT_PIN, direction: :out
@right.on
@left = Pin.new pin: LEFT_PIN, direction: :out
@left.on
@down = Pin.new pin: DOWN_PIN, direction: :out
@down.on
end
def DOT
@dot.off
sleep DELAY
@dot.on
sleep DELAY
end
def RIGHT
@right.off
sleep DELAY
@right.on
sleep DELAY
end
def LEFT
@left.off
sleep DELAY
@left.on
sleep DELAY
end
def DOWN
@down.off
sleep DELAY
@down.on
sleep DELAY
end
end
# parsing options
params = ARGV.getopts('f:')
unless params['f']
puts '-f required.'
exit 1
end
filename = params['f']
unless FileTest.readable?(filename)
puts "Error reading file #{filename}"
exit 1
end
gpio = Gpio.new
include Magick
img = Magick::ImageList.new(filename)
(0..img.rows - 1).each do |row|
# 偶数行なら正順で。カーソルも正順で
if row.even?
(0..img.columns - 1).each do |col|
gpio.DOT if img.pixel_color(col, row).red.zero?
gpio.RIGHT if col != img.columns - 1
end
else # 奇数行なら逆順で。カーソルも逆順で
(0..img.columns - 1).reverse_each do |col|
gpio.DOT if img.pixel_color(col, row).red.zero?
gpio.LEFT if col != 0
end
end
gpio.DOWN
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment