Skip to content

Instantly share code, notes, and snippets.

@wrzasa
Created March 28, 2018 10:17
Show Gist options
  • Save wrzasa/62d63b570b01c76eb912ee67f7555567 to your computer and use it in GitHub Desktop.
Save wrzasa/62d63b570b01c76eb912ee67f7555567 to your computer and use it in GitHub Desktop.
My Raspberry PI notes (in PL) from my RRUG.pl talk

RRUG#12 notatki o RaspberryPi

pins = [ 14, 15, 18, 23, 24, 25, 8, 7 ]
  1. Sprzęt (obrazki pin outu!)
  2. (WiringPi)
  3. RPi_GPIO
  • IN piny on/off
  require 'rpi_gpio'
  RPi::GPIO.set_numbering :bcm
  RPi::GPIO.setup 14, as: :output
  RPi::GPIO.set_high 14
  RPi::GPIO.set_low 14
  • OUT button
  require 'rpi_gpio'
  RPi::GPIO.set_numbering :bcm

  RPi::GPIO.setup 10, as: :input
  ls RPi::GPIO
  RPi::GPIO.high? 10
  • czyszczenie pinów
    • RPi::GPIO.clean_up 14
    • RPi::GPIO.reset
  • PWM
  require 'rpi_gpio'
  RPi::GPIO.set_numbering :bcm

  RPi::GPIO.setup 14, as: :output
  pwm14 = RPi::GPIO::PWM.new 14, 10
  pwm14.start 50
  pwm14.frequency += 20
  pwm14.duty_cycle = 20
  require 'rpi_gpio'
  RPi::GPIO.set_numbering :bcm

  pins = [ 14, 15, 18, 23, 24, 25, 8, 7 ]
  pins.each { |p| RPi::GPIO.setup p, as: :output }
  pwms = pins.map { |p| RPi::GPIO::PWM.new p, 100 }
  pwms.each { |pwm| pwm.start 10 }
  pwms.each { |pwm| pwm.duty_cycle=50 }
  pwms.each.with_index { |pwm, i| pwm.duty_cycle=10 + i*10 }
  pwms.each.with_index { |pwm, i| pwm.duty_cycle=80-i*10 }

  def up(pwms)
    pwms.each.with_index { |pwm, i| pwm.duty_cycle=10 + i*10 }
  end

  def down(pwms)
    pwms.each.with_index { |pwm, i| pwm.duty_cycle=80-i*10 }
  end
  1. PiPiper
  • piny on/off
  require 'pi_piper'
  pins = [ 14, 15, 18, 23, 24, 25, 8, 7 ]
  leds = pins.map { |pin| PiPiper::Pin.new pin: pin, direction: :out}
  leds.each(&:on)
  leds.each(&:off)
  • OUT button
  require 'pi_piper'
  button = PiPiper::Pin.new pin: 10, direction: :in
  button.on?
  button.read;button.on?
  • watch
  require 'pi_piper'
  PiPiper::watch pin: 9 do |pin|
    pin.read
    puts "Pin 9: #{pin.value}"
  end
  PiPiper::after pin: 10, goes: :high do |pin|
    pin.read
    puts "Pin 10: #{pin.value}"
  end
  • powiedzieć o i2c i SPI

  • PWM (sprzętowy) "Supported pin are : 12, 13, 18, 19, 40, 41, 45, 52, 53 but only 18 is on the header.."

      require 'pi_piper'
      pwm = PiPiper::Pwm.new pin: 18
      pwm.value = 0.5
      pwm.value = 0.7
      pwm.value = 0.9
      pwm.off
      pwm.on
      pwm = PiPiper::Pwm.new pin: 18, clock: 100
      pwm.on
      pwm.value=10
    
      require 'pi_piper'
      led = PiPiper::Pin.new pin:14, direction: :out
      PiPiper::after pin: 10, goes: :low do
        led.read
        if led.on?
          led.off
        else
          led.on
        end
      end
    
  1. pi-blaster
def pwm(pin, fill)
    dev = "/dev/pi-blaster"
    unless File.exists?(dev) && File.ftype(dev) == 'fifo'
      raise "#{dev} not found or is not a FIFO. Did you start pi-blaster daemon?"
    end
    File.write(dev, "#{pin}=#{fill}\n")
end
def light_up(pin, vals)
  vals.each { |v| pwm pin, v; sleep 0.1 }
end

def light_down(pin, vals)
  vals.reverse.each { |v| pwm pin, v; sleep 0.1 }
end

def shine pin, vals
  light_up pin, vals
  light_down pin, vals
end

pins.each { |p| shine p, vals }
pins.each { |p| Thread.new { shine p, vals } }
pins.each { |p| Thread.new { shine p, vals }; sleep 0.1 }

def right(pins, vals)
  pins.each { |p| Thread.new { shine p, vals }; sleep  0.1 }

end

def left(pins, vals)
  pins.reverse.each { |p| Thread.new { shine p, vals }; sleep 0.1 }
end

def sweep(pins, vals)
  right pins, vals
  sleep 0.6
  left pins, vals
  sleep 0.6
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment