Skip to content

Commit

Permalink
fix i2cd
Browse files Browse the repository at this point in the history
  • Loading branch information
avaldebe committed Dec 31, 2015
1 parent 6400d29 commit 336d1db
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions lua_modules/i2cd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,6 @@ local M={
}
_G[M.name]=M

local gpio_pin={5,4,0,2,14,12,13}--,15,3,1,9,10}
function M.init(SDA,SCL,volatile,scan)
-- Usage: require('i2cd').init(sda,scl,true,true)
-- Scan add addresses on sda,scl and free module from memory after completetion
-- volatile module
if volatile==true then
_G[M.name],package.loaded[M.name]=nil,nil
end
-- init i2c bus
i2c.setup(0,SDA,SCL,i2c.SLOW)
-- scan i2c bus
if scan==true then
local addr,try
for addr=0x08,0x77 do -- 7bit addresses; skip invalid: 0x00..0x07,0x78..0x7F
for try=1,3 do -- try each address 3 times
if M.io(addr) then -- device responds with ACK?
print(('%s: Device found at address 0x%02X,')
..' SDA %d (GPIO%02d), SCL %d (GPIO%02d) on try %d.')
:format(M.name,addr,SDA,gpio_pin[SDA],SCL,gpio_pin[SCL],try))
break
end
end
end
end
end
function M.autoscan(volatile)
-- Usage: require('i2cd').io(true)
-- Scan all pins and addresses and free module from memory after completetion
print(('%s: Scanning all pins for I2C devices'):format(M.name))
local sda,scl
for scl=1,#gpio_pin do
for sda=1,#gpio_pin do
tmr.wdclr() -- pat the (watch)dog!
if sda~=scl then -- if the pins are the same then skip this round
M.init(sda,scl,volatile,true)
end
end
end
print(('%s: Scanning completed'):format(M.name))
end

function M.io(addr,...)
-- Usage:
-- wake-up device: i2cd.io(addr)
Expand Down Expand Up @@ -138,4 +97,45 @@ function M.int(c,t)
return nil
end

local gpio_pin={5,4,0,2,14,12,13}--,15,3,1,9,10}
function M.init(SDA,SCL,volatile,scan)
-- Usage: require('i2cd').init(sda,scl,true,true)
-- Scan add addresses on sda,scl and free module from memory after completetion
-- volatile module
if volatile==true then
_G[M.name],package.loaded[M.name]=nil,nil
end
-- init i2c bus
i2c.setup(0,SDA,SCL,i2c.SLOW)
-- scan i2c bus
if scan==true then
local addr,try
for addr=0x08,0x77 do -- 7bit addresses; skip invalid: 0x00..0x07,0x78..0x7F
for try=1,3 do -- try each address 3 times
if M.io(addr) then -- device responds with ACK?
print(('%s: Device found at address 0x%02X,')
..' SDA %d (GPIO%02d), SCL %d (GPIO%02d) on try %d.')
:format(M.name,addr,SDA,gpio_pin[SDA],SCL,gpio_pin[SCL],try))
break
end
end
end
end
end
function M.autoscan(volatile)
-- Usage: require('i2cd').io(true)
-- Scan all pins and addresses and free module from memory after completetion
print(('%s: Scanning all pins for I2C devices'):format(M.name))
local sda,scl
for scl=1,#gpio_pin do
for sda=1,#gpio_pin do
tmr.wdclr() -- pat the (watch)dog!
if sda~=scl then -- if the pins are the same then skip this round
M.init(sda,scl,volatile,true)
end
end
end
print(('%s: Scanning completed'):format(M.name))
end

return M

0 comments on commit 336d1db

Please sign in to comment.