Advertisement
Guest User

Untitled

a guest
Apr 11th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 7.95 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'pp'
  3. require './rubybone.rb'
  4.  
  5. include  Rubybone
  6.  
  7. SPI.setup(:SPI0)
  8.  
  9. loop do
  10.   # communicate with MCP3008
  11.   # byte 1: start bit
  12.   # byte 2: single(1)/diff(0),3 bites for channel, null pad
  13.   # byte 3: don't care
  14.   raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10000000, 0].pack("C*"))
  15.   data = raw.unpack("C*")
  16.  
  17.   val = ((data[1] & 0b00000011) << 8 ) | data[2]
  18.   puts val
  19.  
  20.  
  21.   raw = SPI.xfer(:SPI0, [ 0b00000001, 0b10010000, 0].pack("C*"))
  22.   data = raw.unpack("C*")
  23.  
  24.   val = ((data[1] & 0b00000011) << 8 ) | data[2]
  25.   puts val
  26.  
  27.  
  28.   sleep 0.25
  29. end
  30.  
  31. exit
  32.  
  33. #i2c testing
  34. I2C.setup(:I2C2)
  35.  
  36.  
  37. #put mag into continuous conversation mode
  38. I2C.write(:I2C2, 0x1e, [0x02, 0x00].pack("C*"))
  39. #enable temperatuer sensor, 15hz register update
  40. I2C.write(:I2C2, 0x1e, [0x00, "10010000".to_i(2)].pack("C*") )
  41. #delay for the settings to take effect
  42. sleep(0.1)
  43.  
  44. # #two threads reading i2c data at the same time, thread safe
  45. # t1 = Thread.new do |t|
  46. #   loop do
  47. #     #read 2 bytes from temperature register
  48. #     raw = I2C.read(:I2C2, 0x1e, 2, [0x31].pack("C*"))
  49. #     #temperature is sent big endian, lsd last
  50. #     temp = raw.unpack("S>").first
  51. #     #temp is 12 bits, last 4 are unused
  52. #     temp = temp >> 4
  53. #
  54. #     #twos complement
  55. #     temp -= 65535 if temp > 32767
  56. #
  57. #     #each bit is 8c
  58. #     temp /= 8
  59. #
  60. #     #correction factor
  61. #     temp += 19
  62. #
  63. #     #convert to f
  64. #     temp = (temp * 1.8 + 32).to_i
  65. #
  66. #
  67. #     print "temp: #{temp} degrees f\n"
  68. #     sleep 0.01
  69. #   end
  70. # end
  71. #
  72. # t2 = Thread.new do |t|
  73. #   loop do
  74. #
  75. #     #read axis data
  76. #     raw = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
  77. #
  78. #     #coordinates are signed shorts in x,z,y order
  79. #     x,z,y = raw.unpack("s>*")
  80. #
  81. #     #calculate angle
  82. #     degrees = (Math::atan2(y, x) * 180) / Math::PI
  83. #     degrees += 360 if degrees < 0
  84. #
  85. #     print "direction: #{degrees.to_i} degrees\n"
  86. #     sleep 0.01
  87. #   end
  88. #
  89. # end
  90. #
  91. #
  92. # sleep ( 40000 )
  93. # exit
  94.  
  95. #or just read linearly
  96.  
  97. loop do
  98.  
  99.   #read axis data
  100.   raw = I2C.read(:I2C2, 0x1e, 6, [0x03].pack("C*"))
  101.  
  102.   #coordinates are signed shorts in x,z,y order
  103.   x,z,y = raw.unpack("s>*")
  104.  
  105.   #calculate angle
  106.   degrees = (Math::atan2(y, x) * 180) / Math::PI
  107.   degrees += 360 if degrees < 0
  108.  
  109.   #read 2 bytes from temperature register
  110.   raw = I2C.read(:I2C2, 0x1e, 2, [0x31].pack("C*"))
  111.   #temperature is sent big endian, lsd last
  112.   temp = raw.unpack("S>").first
  113.   #temp is 12 bits, last 4 are unused
  114.   temp = temp >> 4
  115.  
  116.   #twos complement
  117.   temp -= 65535 if temp > 32767
  118.  
  119.   #each bit is 8c
  120.   temp /= 8
  121.  
  122.   #correction factor
  123.   temp += 19
  124.  
  125.   #convert to f
  126.   temp = (temp * 1.8 + 32).to_i
  127.  
  128.  
  129.   puts "#{Time.now.strftime("%H:%M")}  temp: #{temp} degrees f        direction: #{degrees.to_i} degrees"
  130.   sleep 60
  131. end
  132.  
  133. exit
  134.  
  135. #raw i2c testing
  136. I2C_SLAVE = 0x0703
  137. MAG = 0x1e
  138. i2c = File.open("/dev/i2c-1", 'r+')
  139.  
  140. #select device
  141. i2c.ioctl(I2C_SLAVE, MAG)
  142.  
  143. #write 0x00 to register 0x02 to put device into continuous conversation mode
  144. i2c.syswrite([0x02, 0x00].pack("C*"))
  145.  
  146.  
  147. loop do
  148.   #read mag data starting at 0x03
  149.   i2c.syswrite([0x03].pack("C*"))
  150.  
  151.   raw = i2c.sysread(6)
  152.  
  153.   x,z,y = raw.unpack("S>*")
  154.  
  155.   x -= 65535 if x > 32767
  156.   y -= 65535 if y > 32767
  157.   z -= 65535 if z > 32767
  158.  
  159.   degrees = (Math::atan2(y, x) * 180) / Math::PI
  160.   degrees += 360 if degrees < 0
  161.  
  162.  
  163.   #get temp data at 0x31
  164.   i2c.syswrite([0x31].pack("C*"))
  165.   raw = i2c.sysread(2)
  166.   temp = raw.unpack("S>").first
  167.   #data is only 12 bits, shift 4 since we read 16
  168.   temp = temp >> 4
  169.  
  170.   #twos complement
  171.   temp -= 65535 if temp > 32767
  172.  
  173.   #each bit is 8c
  174.   temp /= 8
  175.  
  176.   #correction factor
  177.   temp += 19
  178.  
  179.   #convert to f
  180.   temp = (temp * 1.8 + 32).to_i
  181.  
  182.   puts "DEGREES: #{degrees} TEMP: #{temp}"
  183.   sleep(0.1)
  184.  
  185. end
  186.  
  187. exit
  188.  
  189. #uart test
  190. UART.setup(:UART1, 9600)
  191.  
  192. UART.write(:UART1, "test1")
  193.  
  194. #puts UART.readchars(:UART1, 10)
  195. #puts UART.readchar(:UART1)
  196. #puts UART.readline(:UART1)
  197.  
  198. callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
  199. #UART.run_on_each_chars(callback, :UART1, 3, 3)
  200. #UART.run_on_each_chars(callback, :UART1, 3)
  201. #UART.run_on_each_char(callback, :UART1, 3)
  202. #UART.run_once_on_each_chars(callback, :UART1, 3)
  203. #UART.run_once_on_each_char(callback, :UART1)
  204. #UART.run_on_each_chars(callback, :UART1, 2)
  205. UART.run_on_each_line(callback, :UART1)
  206.  
  207. sleep(5)
  208. UART.stop_read_wait(:UART1)
  209.  
  210.  
  211. UART.each_chars(:UART1, 2) { |c| puts c }
  212. #UART.each_line(:UART1) { |line| puts line }
  213.  
  214. UART.cleanup
  215.  
  216. exit
  217.  
  218. #shift register test
  219.  
  220. GPIO.pin_mode(:P9_11, :OUT) #latch
  221. GPIO.pin_mode(:P9_13, :OUT) #clock
  222. GPIO.pin_mode(:P9_15, :OUT) #data
  223. data = 255
  224. GPIO.shift_out(:P9_11, :P9_13, :P9_15, data, nil)
  225.  
  226. exit
  227.  
  228. #background analog input
  229. callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
  230. AIN.run_on_change(callback, :P9_33, 10, 0.1)
  231. sleep 5
  232. AIN.stop_wait(:P9_33)
  233.  
  234. #waiting for analog input change
  235. pp AIN.wait_for_change(:P9_33, 10, 0.01)
  236. pp AIN.wait_for_change(:P9_33, 10, 0.01)
  237. pp AIN.wait_for_change(:P9_33, 10, 0.01)
  238.  
  239. exit
  240.  
  241.  
  242. #run when reaching a certain threshold
  243. callback = lambda { |pin, mv_last, mv, state_last, state, count|
  244.   puts "[#{count}] #{pin} #{state_last} -> #{state}     #{mv_last} -> #{mv}"
  245. }
  246. AIN.run_on_threshold(callback, :P9_33, 400, 1200, 5, 0.001)
  247. loop do sleep(4000);end
  248.  
  249. exit
  250.  
  251.  
  252. #wait for analog input to hit a certrain threshold limit
  253. pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
  254. pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
  255. pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
  256. pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
  257. pp AIN.wait_for_threshold(:P9_33, 200, 1600, 100, 0.01)
  258. exit
  259.  
  260. #analog read
  261.  
  262. loop do
  263.   puts AIN.read(:P9_33)
  264.   sleep 0.01
  265. end
  266.  
  267. exit
  268. #pwm into gpio with edge trigger
  269. PWM.start(:P9_14, 50, 50, :NORMAL)
  270. GPIO.pin_mode(:P9_11, :IN)
  271. GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
  272. sleep(5)
  273. PWM.cleanup
  274. exit
  275.  
  276. #pwm setup
  277. PWM.start(:P9_14, 90, 10, :NORMAL)
  278. GPIO.pin_mode(:P9_11, :IN)
  279. sleep(1)
  280. PWM.set_frequency(:P9_14, 20)
  281. sleep(1)
  282. PWM.set_duty_cycle(:P9_14, 95)
  283. sleep(1)
  284. PWM.set_duty_cycle(:P9_14, 50)
  285. sleep(1)
  286. PWM.set_frequency(:P9_14, 2)
  287. sleep(1)
  288. PWM.stop(:P9_14)
  289. sleep(1)
  290. PWM.start(:P9_14, 90, 10, :NORMAL)
  291. sleep(1)
  292. PWM.set_frequency(:P9_14, 32)
  293. sleep(1)
  294. PWM.set_duty_cycle(:P9_14, 94)
  295. sleep(1)
  296. PWM.set_period_ns(:P9_14, 31250000)
  297. PWM.set_duty_cycle_ns(:P9_14, 31250000)
  298. PWM.set_period_ns(:P9_14, 31249999)
  299. PWM.set_duty_cycle(:P9_14, 10)
  300. PWM.set_polarity(:P9_14, :INVERTED)
  301. sleep(1)
  302. PWM.cleanup
  303. exit
  304.  
  305. #gpio edge trigger callback
  306. GPIO.pin_mode(:P9_12, :OUT)
  307. GPIO.pin_mode(:P9_11, :IN)
  308. GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
  309.  
  310. leds = [ :USR0, :USR1, :USR2, :USR3 ]
  311. leds.each do |ledpin|
  312.   GPIO.pin_mode(ledpin, :OUT)
  313. end
  314.  
  315. x = 0
  316. loop do
  317. #gpio write to led pins
  318.   leds.each do |ledpin|
  319.     GPIO.digital_write(ledpin, :LOW)
  320.     sleep 0.25
  321.     GPIO.digital_write(ledpin, :HIGH)
  322.   end
  323.     x += 1
  324.  
  325.     if x == 10
  326.         GPIO.stop_edge_wait(:P9_11)
  327.         puts "U STOP"
  328.     end
  329.   if x == 15
  330.     GPIO.run_on_edge(lambda { |x,y,z| puts "#{x} -- #{y} -- #{z}" }, :P9_11, :BOTH)
  331.         puts "OK GO AGAIN"
  332.         x = 0
  333.     end
  334.  
  335. end
  336.  
  337.  
  338. exit
  339.  
  340. #gpio edge trigger
  341. GPIO.pin_mode(:P9_12, :OUT)
  342. GPIO.pin_mode(:P9_11, :IN)
  343.  
  344. loop do
  345.     edge = GPIO.wait_for_edge(:P9_11, :RISING)
  346.     puts "OMG TRIGGERED ON #{edge}"
  347. end
  348.  
  349.  
  350. #standard gpio setup and testing
  351. GPIO.pin_mode(:P9_12, :OUT)
  352. puts GPIO.enabled?(:P9_12)
  353. puts GPIO.read_gpio_direction(:P9_12)
  354.  
  355. #gpio setup for led pins
  356. leds = [ :USR0, :USR1, :USR2, :USR3 ]
  357. leds.each do |ledpin|
  358.     GPIO.pin_mode(ledpin, :OUT)
  359. end
  360.  
  361. #gpio write to led pins
  362. leds.each do |ledpin|
  363.     GPIO.digital_write(ledpin, :LOW)
  364. end
  365.  
  366.  
  367. #gpio write
  368. loop do
  369.     GPIO.digital_write(:P9_12, :HIGH)
  370.     sleep 0.25
  371.     GPIO.digital_write(:P9_12, :LOW)
  372.     sleep 0.25
  373. end
  374.  
  375.  
  376. GPIO.cleanup
  377. exit
  378.  
  379. # gpio read
  380. GPIO.pin_mode(:P9_12, :IN)
  381. loop do
  382.     puts GPIO.digital_read(:P9_12)
  383. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement