Chinaunix首页 | 论坛 | 博客
  • 博客访问: 156262
  • 博文数量: 55
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 17
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-19 09:09
文章分类

全部博文(55)

文章存档

2015年(17)

2014年(38)

我的朋友

分类: 项目管理

2014-12-17 20:34:54

1. Shell中实现, 比如对GPIO 26的操作
echo 26 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio26/direction
echo in > /sys/class/gpio/gpio26/direction
echo 1 > /sys/class/gpio/gpio26/value
cat /sys/class/gpio/gpio26/value


2. LUA中的操作可以直接引用 Ewelina, Rafa, Rafa写的例程
gpio.lua

点击(此处)折叠或打开

  1. --@author: Ewelina, Rafa, Rafa
  2. --GPIO utilities

  3. --Writes 'what' to 'where'
  4. function writeToFile (where,what)
  5.     local fileToWrite=io.open(where, 'w')
  6.     fileToWrite:write(what)
  7.     fileToWrite:close()    
  8. end
  9. --Reads a character from file 'where' and returns the string
  10. function readFromFile (where)
  11.     local fileToRead=io.open(where, 'r')
  12.     fileStr = fileToRead:read(1)
  13.     fileToRead:close()    
  14.     return fileStr
  15. end

  16. --Returns true if file exists
  17. function file_exists(name)
  18.    local f=io.open(name,"r")
  19.    if f~=nil then io.close(f) return true else return false end
  20. end

  21. --Exports gpio ID to use as an output pin
  22. function configureOutGPIO (id)
  23.     if not file_exists('/sys/class/gpio/gpio'..id..'/direction') then
  24.         writeToFile('/sys/class/gpio/export',id)
  25.     end
  26.     writeToFile('/sys/class/gpio/gpio'..id..'/direction','out')
  27. end

  28. --Exports gpio ID to use as an input pin
  29. function configureInGPIO (id)
  30.     if not file_exists('/sys/class/gpio/gpio'..id..'/direction') then
  31.         writeToFile('/sys/class/gpio/export',id)
  32.     end
  33.     writeToFile('/sys/class/gpio/gpio'..id..'/direction','in')
  34. end

  35. --Reads GPIO 'id' and returns it's value
  36. --@Pre: GPIO 'id' must be exported with configureInGPIO
  37. function readGPIO(id)
  38.     gpioVal = readFromFile('/sys/class/gpio/gpio'..id..'/value')
  39.     return gpioVal
  40. end

  41. --Writes a value to GPIO 'id'
  42. --@Pre: GPIO 'id' must be exported with configureOutGPIO
  43. function writeGPIO(id, val)
  44.     gpioVal = writeToFile('/sys/class/gpio/gpio'..id..'/value

inputtest.lua

点击(此处)折叠或打开

  1. --Read the state of the buttons connected to pins 139, 138 and 137 and print it to the console

  2. require ("gpio")

  3. local buttons = {}
  4. buttons[1]=139
  5. buttons[2]=138
  6. buttons[3]=137

  7. for i,v in ipairs(buttons) do
  8.     configureInGPIO (v)
  9. end

  10. while true do
  11.     for i,v in ipairs(buttons) do
  12.         local val = readGPIO(v)
  13.         if val.."" == '1' then
  14.             print ('Button '..i .. ' pressed')
  15.         end
  16.     end
  17.     
  18. end

outputtest.lua

点击(此处)折叠或打开

  1. --Read the state of a button and turn on a led connected to a different gpio as follows:
  2. --button 139 turns on led 158
  3. --button 138 turns on led 162
  4. --button 137 turns on led 161

  5. require ("gpio")

  6. local buttons = {}
  7. buttons[1]=139
  8. buttons[2]=138
  9. buttons[3]=137

  10. local leds = {}
  11. leds[1]=158
  12. leds[2]=162
  13. leds[3]=161

  14. for i,v in ipairs(buttons) do
  15.     --configure v as input gpio
  16.     configureInGPIO(v)
  17. end
  18. for i,v in ipairs(leds) do
  19.     --configure v as output gpio
  20.     configureOutGPIO(v)
  21. end

  22. while true do
  23.     for i,v in ipairs(buttons) do
  24.         local val = readGPIO(v)
  25.         if val.."" == '1' then
  26.             print ('button '..i .. ' pressed')
  27.             writeGPIO(leds[i],1)
  28.         else
  29.             writeGPIO(leds[i],0)
  30.         end
  31.     end
  32.     
  33. end


阅读(2224) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~