<commit_before>#! /usr/bin/ruby

require 'pi_piper'

$last_pulse = Time.now
$current_digit = 0
$dialed = ""
LIMIT = 0.25 # two digits are separated by at least 0.25 seconds

def check_last_digit
  if Time.now - $last_pulse > LIMIT and $current_digit > 0
    $dialed << ($current_digit % 10).to_s
    $current_digit = 0
  end
end

PiPiper.after :pin => 18, :goes => :high do
  check_last_digit
  $last_pulse = Time.now
  $current_digit += 1
end

Thread.new {
  loop {
    sleep 0.5
    check_last_digit
    if Time.now - $last_pulse > 5 and not $dialed.empty?
      puts "dialed #{$dialed}"
      $dialed = ""
    end
  }
}

PiPiper.wait
<commit_msg>put into class, multithreaded<commit_after>#! /usr/bin/ruby

require 'pi_piper'

$end = false

class Dial
  LIMIT = 0.25 # two digits are separated by at least 0.25 seconds

  def initialize
    @last_pulse = Time.now
    @current_digit = 0
    @dialed = ""

    # need lambda bound to local variable for correct scope
    getpulse = lambda {
      check_last_digit
      @last_pulse = Time.now
      @current_digit += 1
    }

    PiPiper.after :pin => 18, :goes => :high do getpulse.call end

    Thread.new {
      loop {
        sleep 0.5
        check_last_digit
        if Time.now - @last_pulse > 5 and not @dialed.empty?
          puts "dialed #{@dialed}"
          $end = true if @dialed == "0000"
          @dialed = ""
        end
      }
    }

  end

private
  def check_last_digit
    if Time.now - @last_pulse > LIMIT and @current_digit > 0
      @dialed << (@current_digit % 10).to_s
      @current_digit = 0
    end
  end
end

Dial.new

while !$end
  sleep 2
end
{'fraction_non_alphanumeric': 0.10626535626535627, 'fraction_numerical': 0.023955773955773956, 'mean_word_length': 2.372670807453416, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4237176', 'n_tokens_mistral': 659, 'n_tokens_neox': 582, 'n_words': 166}