Jump to: navigation, search

Mihini/gpio

< Mihini
Revision as of 08:29, 10 March 2015 by Niranjan.babu.in.bosch.com (Talk | contribs) (Created page with "== GPIO == '''Variable declaration''' local sched = require 'sched' local log = require 'log' local gpio = require 'gpio' '''gpio.availablelist() : Lists available GPI...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

GPIO

Variable declaration

 local sched = require 'sched'
 local log = require 'log'
 local gpio = require 'gpio'

gpio.availablelist() : Lists available GPIO on the System

local function getGpioList()
 local res=gpio.availablelist();
  for k,v in pairs(res) do
   print(k,v)
end
end

gpio.configure(id, config) : Configures the GPIO parameters.

local function configureGpio()
   local val = gpio.configure(2,{direction="in", edge="both", activelow="1"})    --Gpio 0 as input
   local val2 = gpio.configure(17,{direction="out", edge="both", activelow="0"}) -- GPIO 17 as output
     print(val..val2)
end

gpio.getconfig(id) : Retrieves GPIO configuration

 local function getConfig()
   res=gpio.getconfig(2) --get config of gpio2
     for k,v in pairs(res) do
        print(k,v)
    end
  end


gpio.read(id) : Reads a GPIO value.

 local function readGpio()
    val = gpio.read(2)
     print(val) ---(val=1 if input =5v and val=0 if input=0 (note floating point gives you 1))
 end

gpio.register(id, userhook) : Registers a GPIO for monitoring it for changes.

 local function hookUp(id,value)
     print("gpio %d has changed ! new value =%s",id,value)
  end
local function registerGpio()
  gpio.register(17,hookUp)
 end

gpio.write(id, value) : Writes a GPIO value.

 local function writeGpio()
    gpio.write(17,0)
     sched.wait(2)
    gpio.write(17,1)
  end

Main function to sequentially call all the above API's

local function main()
  getGpioList()
  getGpioList()
  configureGpio()
  getConfig()
  registerGpio()
  readGpio()
  writeGpio()
  readGpio()
 end