Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'pp'
- require './rubybone.rb'
- include Rubybone
- SPI.setup(:SPI0)
- loop do
- # communicate with MCP3008
- # byte 1: start bit
- # byte 2: single(1)/diff(0),3 bites for channel, null pad
- # byte 3: don't care
- raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10000000, 0].pack("C*"))
- data = raw.unpack("C*")
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
- puts val
- raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10010000, 0].pack("C*"))
- data = raw.unpack("C*")
- val = ((data[1] & 0b00000011) << 8 ) | data[2]
- puts val
- sleep 0.25
- end
- exit
- #i2c testing
- I2C.setup(:I2C2)
- #put mag into continuous conversation mode
- I2C.write(:I2C2, 0x1e, [0x02, 0x00].pack("C*"))
- #enable temperatuer sensor, 15hz register update
- I2C.write(:I2C2, 0x1e, [0x00, "10010000".to_i(2)].pack("C*") )
- #delay for the settings to take effect
- sleep(0.1)
- # #two threads reading i2c data at the same time, thread safe
- # t1 = Thread.new do |t|
- # loop do
- # #read 2 bytes from temperature register
- # raw = I2C.read(:I2C2, 0x1e, 2, [0x31].pack("C*"))
- # #temperature is sent big endian, lsd last
- # temp = raw.unpack("S>").first
- # #temp is 12 bits, last 4 are unused
- # temp = temp >> 4
- #
- # #twos complement
- # temp -= 65535 if temp > 32767
- #
- # #each bit is 8c
- # temp /= 8
- #
- # #correction factor
- # temp += 19
- #
- # #convert to f
- # temp = (temp * 1.8 + 32).to_i
- #
- #
- # print "temp: #{temp} degrees f\n"
- # sleep 0.01
- # end
- # end
- #
- # t2 = Thread.new do |t|
- # loop do
- #
- # #read axis data
- # raw = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
- #
- # #coordinates are signed shorts in x,z,y order
- # x,z,y = raw.unpack("s>*")
- #
- # #calculate angle
- # degrees = (Math::atan2(y, x) * 180) / Math::PI
- # degrees += 360 if degrees < 0
- #
- # print "direction: #{degrees.to_i} degrees\n"
- # sleep 0.01
- # end
- #
- # end
- #
- #
- # sleep ( 40000 )
- # exit
- #or just read linearly
- loop do
- #read axis data
- raw = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
- #coordinates are signed shorts in x,z,y order
- x,z,y = raw.unpack("s>*")
- #calculate angle
- degrees = (Math::atan2(y, x) * 180) / Math::PI
- degrees += 360 if degrees < 0
- #read 2 bytes from temperature register
- raw = I2C.read(:I2C2, 0x1e, 2, [0x31].pack("C*"))
- #temperature is sent big endian, lsd last
- temp = raw.unpack("S>").first
- #temp is 12 bits, last 4 are unused
- temp = temp >> 4
- #twos complement
- temp -= 65535 if temp > 32767
- #each bit is 8c
- temp /= 8
- #correction factor
- temp += 19
- #convert to f
- temp = (temp * 1.8 + 32).to_i
- puts "#{Time.now.strftime("%H:%M")} temp: #{temp} degrees f direction: #{degrees.to_i} degrees"
- sleep 60
- end
- exit
- #raw i2c testing
- I2C_SLAVE = 0x0703
- MAG = 0x1e
- i2c = File.open("/dev/i2c-1", 'r+')
- #select device
- i2c.ioctl(I2C_SLAVE, MAG)
- #write 0x00 to register 0x02 to put device into continuous conversation mode
- i2c.syswrite([0x02, 0x00].pack("C*"))
- loop do
- #read mag data starting at 0x03
- i2c.syswrite([0x03].pack("C*"))
- raw = i2c.sysread(6)
- x,z,y = raw.unpack("S>*")
- x -= 65535 if x > 32767
- y -= 65535 if y > 32767
- z -= 65535 if z > 32767
- degrees = (Math::atan2(y, x) * 180) / Math::PI
- degrees += 360 if degrees < 0
- #get temp data at 0x31
- i2c.syswrite([0x31].pack("C*"))
- raw = i2c.sysread(2)
- temp = raw.unpack("S>").first
- #data is only 12 bits, shift 4 since we read 16
- temp = temp >> 4
- #twos complement
- temp -= 65535 if temp > 32767
- #each bit is 8c
- temp /= 8
- #correction factor
- temp += 19
- #convert to f
- temp = (temp * 1.8 + 32).to_i
- puts "DEGREES: #{degrees} TEMP: #{temp}"
- sleep(0.1)
- end
- exit
- #uart test
- UART.setup(:UART1, 9600)
- UART.write(:UART1, "test1")
- #puts UART.readchars(:UART1, 10)
- #puts UART.readchar(:UART1)
- #puts UART.readline(:UART1)
- callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
- #UART.run_on_each_chars(callback, :UART1, 3, 3)
- #UART.run_on_each_chars(callback, :UART1, 3)
- #UART.run_on_each_char(callback, :UART1, 3)
- #UART.run_once_on_each_chars(callback, :UART1, 3)
- #UART.run_once_on_each_char(callback, :UART1)
- #UART.run_on_each_chars(callback, :UART1, 2)
- UART.run_on_each_line(callback, :UART1)
- sleep(5)
- UART.stop_read_wait(:UART1)
- UART.each_chars(:UART1, 2) { |c| puts c }
- #UART.each_line(:UART1) { |line| puts line }
- UART.cleanup
- exit
- #shift register test
- GPIO.pin_mode(:P9_11, :OUT) #latch
- GPIO.pin_mode(:P9_13, :OUT) #clock
- GPIO.pin_mode(:P9_15, :OUT) #data
- data = 255
- GPIO.shift_out(:P9_11, :P9_13, :P9_15, data, nil)
- exit
- #background analog input
- callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
- AIN.run_on_change(callback, :P9_33, 10, 0.1)
- sleep 5
- AIN.stop_wait(:P9_33)
- #waiting for analog input change
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
- pp AIN.wait_for_change(:P9_33, 10, 0.01)
- exit
- #run when reaching a certain threshold
- callback = lambda { |pin, mv_last, mv, state_last, state, count|
- puts "[#{count}] #{pin} #{state_last} -> #{state} #{mv_last} -> #{mv}"
- }
- AIN.run_on_threshold(callback, :P9_33, 400, 1200, 5, 0.001)
- loop do sleep(4000);end
- exit
- #wait for analog input to hit a certrain threshold limit
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
- pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
- exit
- #analog read
- loop do
- puts AIN.read(:P9_33)
- sleep 0.01
- end
- exit
- #pwm into gpio with edge trigger
- PWM.start(:P9_14, 50, 50, :NORMAL)
- GPIO.pin_mode(:P9_11, :IN)
- GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
- sleep(5)
- PWM.cleanup
- exit
- #pwm setup
- PWM.start(:P9_14, 90, 10, :NORMAL)
- GPIO.pin_mode(:P9_11, :IN)
- sleep(1)
- PWM.set_frequency(:P9_14, 20)
- sleep(1)
- PWM.set_duty_cycle(:P9_14, 95)
- sleep(1)
- PWM.set_duty_cycle(:P9_14, 50)
- sleep(1)
- PWM.set_frequency(:P9_14, 2)
- sleep(1)
- PWM.stop(:P9_14)
- sleep(1)
- PWM.start(:P9_14, 90, 10, :NORMAL)
- sleep(1)
- PWM.set_frequency(:P9_14, 32)
- sleep(1)
- PWM.set_duty_cycle(:P9_14, 94)
- sleep(1)
- PWM.set_period_ns(:P9_14, 31250000)
- PWM.set_duty_cycle_ns(:P9_14, 31250000)
- PWM.set_period_ns(:P9_14, 31249999)
- PWM.set_duty_cycle(:P9_14, 10)
- PWM.set_polarity(:P9_14, :INVERTED)
- sleep(1)
- PWM.cleanup
- exit
- #gpio edge trigger callback
- GPIO.pin_mode(:P9_12, :OUT)
- GPIO.pin_mode(:P9_11, :IN)
- GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
- leds = [ :USR0, :USR1, :USR2, :USR3 ]
- leds.each do |ledpin|
- GPIO.pin_mode(ledpin, :OUT)
- end
- x = 0
- loop do
- #gpio write to led pins
- leds.each do |ledpin|
- GPIO.digital_write(ledpin, :LOW)
- sleep 0.25
- GPIO.digital_write(ledpin, :HIGH)
- end
- x += 1
- if x == 10
- GPIO.stop_edge_wait(:P9_11)
- puts "U STOP"
- end
- if x == 15
- GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
- puts "OK GO AGAIN"
- x = 0
- end
- end
- exit
- #gpio edge trigger
- GPIO.pin_mode(:P9_12, :OUT)
- GPIO.pin_mode(:P9_11, :IN)
- loop do
- edge = GPIO.wait_for_edge(:P9_11, :RISING)
- puts "OMG TRIGGERED ON #{edge}"
- end
- #standard gpio setup and testing
- GPIO.pin_mode(:P9_12, :OUT)
- puts GPIO.enabled?(:P9_12)
- puts GPIO.read_gpio_direction(:P9_12)
- #gpio setup for led pins
- leds = [ :USR0, :USR1, :USR2, :USR3 ]
- leds.each do |ledpin|
- GPIO.pin_mode(ledpin, :OUT)
- end
- #gpio write to led pins
- leds.each do |ledpin|
- GPIO.digital_write(ledpin, :LOW)
- end
- #gpio write
- loop do
- GPIO.digital_write(:P9_12, :HIGH)
- sleep 0.25
- GPIO.digital_write(:P9_12, :LOW)
- sleep 0.25
- end
- GPIO.cleanup
- exit
- # gpio read
- GPIO.pin_mode(:P9_12, :IN)
- loop do
- puts GPIO.digital_read(:P9_12)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement