-
-
Save takkanm/2fdb652dc2bb88b91d4ac2b97ef60e7b to your computer and use it in GitHub Desktop.
Cassette42 CubeTimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kbd = Keyboard.new | |
kbd.split = false | |
kbd.init_direct_pins( | |
[ 8, 27, 28, 29, 9, 26 ] | |
) | |
rgb = RGB.new( | |
0, # pin number | |
5, # size of underglow pixel | |
0, # size of backlight pixel | |
false # 32bit data will be sent to a pixel if true while 24bit if false | |
) | |
kbd.append rgb | |
class Array | |
def drop(val) | |
new_arr = [] | |
self.each do |v| | |
new_arr << v if v != val | |
end | |
new_arr | |
end | |
end | |
class CubeTimer | |
def initialize(rgb) | |
puts 'CubeTimer initialize..' | |
@in_progress = false | |
@start_time = nil | |
@stop_time = nil | |
@history = [] | |
@rgb = rgb | |
end | |
def start_and_stop | |
if @in_progress | |
timer_stop | |
else | |
timer_start | |
end | |
end | |
def latest | |
if @history.size > 0 | |
sprintf_time(@history[-1]) | |
else | |
'No result.' | |
end | |
end | |
def ao5 | |
texts = '' | |
if @history.size == 5 | |
min = @history.min | |
max = @history.max | |
target_times = @history.drop(min).drop(max) | |
sum = 0 | |
target_times.each { |t| sum += t } | |
ao5_result = sum / target_times.size | |
texts << "AO5 : #{sprintf_time(ao5_result)}" | |
else | |
texts = "Can't calculate it yet" | |
end | |
count = 1 | |
@history.each do |time| | |
texts << ", " | |
texts << "#{count} : #{sprintf_time(time)}" | |
count += 1 | |
end | |
puts texts | |
return texts | |
end | |
def reset_session | |
@history = [] | |
'Reset results.' | |
end | |
def timer_start | |
puts "CubeTimer Start" | |
@rgb.effect = :rainbow_mood | |
@in_progress = true | |
@stop_time = nil | |
@start_time = board_millis | |
end | |
def timer_stop | |
@stop_time = board_millis | |
@in_progress = false | |
time = @stop_time - @start_time | |
@history << time | |
@history.shift if @history.size > 5 | |
@rgb.turn_off | |
puts sprintf_time(time) | |
end | |
def sprintf_time(time) | |
min = time / (60 * 1000) | |
sec = (time - min) / 1000 | |
msec = time - min - sec | |
str = '' | |
str << "#{min}m " if min > 0 | |
str << "#{sec}.#{msec}s" | |
end | |
end | |
timer = CubeTimer.new(rgb) | |
kbd.add_layer :default, %i[ | |
TIMER_RESET AO5 LAST TIMER_START_STOP KC_ENT KC_ENT | |
] | |
kbd.define_mode_key :TIMER_START_STOP, [ Proc.new { timer.start_and_stop }, :KC_NO, 300, nil ] | |
kbd.define_mode_key :AO5, [ Proc.new { kbd.macro timer.ao5 }, :KC_NO, 300, nil ] | |
kbd.define_mode_key :LAST, [ Proc.new { kbd.macro timer.latest }, :KC_NO, 300, nil ] | |
kbd.define_mode_key :TIMER_RESET, [ Proc.new { kbd.macro timer.reset_session }, :KC_NO, 300, nil ] | |
kbd.start! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment