-- read = function (this, reg) -- I2C i2c.start(this.ID) -- if not i2c.address(this.ID, this.ADDR, i2c.TRANSMITTER) then return nil end -- ( , ) i2c.write(this.ID, reg) -- i2c.stop(this.ID) -- I2C i2c.start(this.ID) -- if not i2c.address(this.ID, this.ADDR, i2c.RECEIVER) then return nil end -- 1 c = i2c.read(this.ID, 1) -- i2c.stop(this.ID) -- return c:byte(1) end, -- write = function (this, reg, ...) i2c.start(this.ID) if not i2c.address(this.ID, this.ADDR, i2c.TRANSMITTER) then return nil end i2c.write(this.ID, reg) len = i2c.write(this.ID, ...) i2c.stop(this.ID) return len end,
-- bit.rshift(3271, 8) -- 00001100 11000111 -> 00001100 -- -- 00001100 -- bit.band(3271, 0xFF) -- 00001100 11000111 -- 11111111 -- -- 00000000 11000111
getFq = function(this) local fq = this:read(this.PRE_SCALE) return math.floor(25000000 / ( fq + 1) / 4096) end, setFq = function(this, fq) local fq = math.floor(25000000 / ( fq * 4096 ) - 1) local oldm1 = this:read(0x00); this:setMode1(bit.bor(oldm1, this.SLEEP)) this:write(this.PRE_SCALE, fq) this:setMode1(oldm1) return nil end
getMode1 = function(this) return this:read(0x00) end, setMode1 = function(this, data) return this:write(0x00, data) end, getMode2 = function(this) return this:read(0x01) end, setMode2 = function(this, data) return this:write(0x01, data) end, getChan = function(this, chan) return 6 + chan * 4 end,
-- MODE 1 reset = function(this) local mode1 = this:getMode1() mode1 = bit.set(mode1, 7) this:setMode1(mode1) mode1 = bit.clear(mode1, 7) this:setMode1(mode1) end, getExt = function(this) return bit.isset(this:getMode1(), 6) end, setExt = function(this, ext) local mode1 = this:getMode1() if (ext) then mode1 = bit.clear(mode1, 6) else mode1 = bit.set(mode1, 6) end this:setMode1(mode1) end, getAi = function(this) return bit.isset(this:getMode1(), 5) end, setAi = function(this, ai) local mode1 = this:geMode1() if (ai) then mode1 = bit.clear(mode1, 5) else mode1 = bit.set(mode1, 5) end this:setMode1(mode1) end, getSleep = function(this) return bit.isset(this:getMode1(), 4) end, setSleep = function(this, sleep) local mode1 = this:geMode1() if (sleep) then mode1 = bit.clear(mode1, 4) else mode1 = bit.set(mode1, 4) end this:setMode1(mode1) end, getAC = function(this) return bit.isset(this:getMode1(), 0) end, setAC = function(this, ac) local mode1 = this:geMode1() if (ac) then mode1 = bit.clear(mode1, 0) else mode1 = bit.set(mode1, 0) end this:setMode1(mode1) end,
-- MODE 2 getInvrt = function(this) return bit.isset(this:getMode2(), 4) end, setInvrt = function(this, invrt) local mode2 = this:geMode2() if (invrt) then mode2 = bit.clear(mode1, 4) else mode2 = bit.set(mode1, 4) end this:setMode2(mode2) end, getInvrt = function(this) return bit.isset(this:getMode2(), 4) end, setInvrt = function(this, invrt) local mode2 = this:geMode2() if (invrt) then mode2 = bit.clear(mode2, 4) else mode2 = bit.set(mode2, 4) end this:setMode2(mode2) end, getOch = function(this) return bit.isset(this:getMode2(), 3) end, setOch = function(this, och) local mode2 = this:geMode2() if (och) then mode2 = bit.clear(mode2, 3) else mode2 = bit.set(mode2, 3) end this:setMode2(mode2) end, getOutDrv = function(this) return bit.isset(this:getMode2(), 2) end, setOutDrv = function(this, outDrv) local mode2 = this:geMode2() if (outDrv) then mode2 = bit.clear(mode2, 2) else mode2 = bit.set(mode2, 2) end this:setMode2(mode2) end, getOutNe = function(this) return bit.band(this:getMode2(), 3) end, setOutNe = function(this, outne) local mode2 = this:geMode2() this:setMode2(bit.bor(mode2, bit.band(outne, 3))) end, getMode2Table = function(this) return { invrt = this:getInvrt(), och = this:getOch(), outDrv = this:getOutDrv(), outNe = this:getOutNe(), } end,
-- CNAHEL setOn = function(this, chan, data) this:write(this:getChan(chan), bit.band(data, 0xFF)) this:write(this:getChan(chan) + 1, bit.rshift(data, 8)) end, setOff = function(this, chan, data) this:write(this:getChan(chan) + 2, bit.band(data, 0xFF)) this:write(this:getChan(chan) + 3, bit.rshift(data, 8)) end, setOnOf = function(this, chan, dataStart, dataEdn) this:setOn(chan, dataStart) this:setOff(chan, dataEdn) end,
-- require('pca9685') -- , i2c pca = pca9685.create(0, 0x40) -- GPIO c SDA SCL pca:init(1, 2) -- pca:setMode1(0x01) pca:setMode2(0x04) -- pca:setFq(50) -- pca:setOnOf(0, 200, 600)
Source: https://habr.com/ru/post/320006/
All Articles