<file_sep>require "wiringpi_kb_adapter/version"

module WiringpiKbAdapter
  begin
    require 'wiringpi'
  rescue LoadError
    require File.expand_path('../wiring_pi', __FILE__)
  end
end
<file_sep>require 'spec_helper'

module WiringPi
  describe GPIO do
    let(:input_pins)  { {goal_a: 0, goal_b: 3, start: 4} }
    let(:output_pins) { {led: 7} }
    let(:subject) { GPIO.new(:input_pins => input_pins, :output_pins => output_pins) }

    # attr_readers
    %w[input_pins output_pins mode_pins].each do |method|
      it { should respond_to method }
    end

    %w[mode write readAll].each do |method|
      it { should respond_to method}
    end

    describe '#readAll' do
      context 'when no key is pressed' do
        before { subject.stub :getch => nil }

        it { subject.readAll.should be_a Hash }

        it 'returns a hash with default values set to 1' do
          subject.readAll[:something].should == 1
        end
      end

      context 'when no pin associated character is pressed' do
        before { subject.stub :getch => 'A' }

        it 'adds no pair to the hash' do
          subject.readAll.size.should be_zero
        end
      end

      context 'when a pin associated character is pressed' do
        before { subject.stub :getch => '4' }

        it 'adds the expected pair to the hash' do
          subject.readAll.size.should_not be_zero
        end

        it 'sets the pin value in the hash to zero' do
          h = subject.readAll
          puts h.inspect
          h[4].should be_zero
        end
      end
    end
  end
end<file_sep>source 'https://rubygems.org'

# Specify your gem's dependencies in wiringpi_kb_adapter.gemspec
gemspec
<file_sep>require_relative '../lib/wiringpi_kb_adapter'
<file_sep>INPUT         = $stdin
OUTPUT        = $stdout
WPI_MODE_PINS = Object.new

require 'curses'

module WiringPi
  def puts(message)
    Curses.addstr message
  end

  class Serial
    def initialize(*args)
      Curses.timeout = 0
      Curses.noecho # do not show typed keys
      Curses.init_screen
      Curses.stdscr.keypad(true) # enable keypad
      Curses.setpos(0, 0)
      @serial_not_read = true
      @serial_count = 0
    end

    def serialDataAvail
      if @finished and !@serial_not_read
        @finished = false
        @serial_count = 0
        @serial_not_read = true
      end
      @serial_not_read ? 1 : 0
    end

    def serialGetchar
      char = (48..57).to_a.sample
      if @serial_count > 4
        @serial_not_read = false
        @finished = true
      end
      @serial_count += 1
      char
    end
  end

  class GPIO
    attr_reader :mode_pins, :input_pins, :output_pins

    def initialize(opts={})
      @input_pins  = opts[:input_pins] or Table::INPUT_PINS
      @output_pins = opts[:output_pins] or Table::OUTPUT_PINS
    end

    def mode(pin_number, io)
      # only once
      # set the mode operation of the pin, if input or output
    end

    def readAll
      # the returned hash has 1 as value for all its keys by default
      hash = Hash.new {|h, k| h[k] = 1}
      char = getch
      if char =~ /\d/
        value = char.to_i
        hash[value] = 0 if key = input_pins.key(value)
      end
      hash
    end

    def write(pin_number, value)
      # puts "pin #{pin_number} is now #{value}"
    end

    private

    def getch
      Curses.getch
    end
  end
end
<file_sep>require 'spec_helper'

module WiringPi
  describe GPIO do
    let(:input_pins)  { {goal_a: 0, goal_b: 3, start: 4} }
    let(:output_pins) { {led: 7} }
    let(:subject) { GPIO.new(:input_pins => input_pins, :output_pins => output_pins) }

    %w[input_pins output_pins mode_pins mode_pins=].each do |method|
      it { should respond_to method }
    end
  end
end