Skip to content

Commit

Permalink
mcp23017: inline constants
Browse files Browse the repository at this point in the history
Saves nearly half a kilobyte of heap.
  • Loading branch information
nwf committed Oct 25, 2020
1 parent 5dedff4 commit a3fe975
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lua_modules/mcp23017/mcp23017.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@ local i2c, string, issetBit, setBit, clearBit =
i2c, string, bit.isset, bit.set, bit.clear

-- registers (not used registers are commented out)
local MCP23017_IODIRA = 0x00
local MCP23017_IODIRB = 0x01
local MCP23017_DEFVALA = 0x06
local MCP23017_DEFVALB = 0x07
local MCP23017_GPIOA = 0x12
local MCP23017_GPIOB = 0x13
--[[
local MCP23017_IPOLA = 0x02
local MCP23017_IPOLB = 0x03
local MCP23017_GPINTENA = 0x04
local MCP23017_GPINTENB = 0x05
local MCP23017_DEFVALA = 0x06
local MCP23017_DEFVALB = 0x07
local MCP23017_INTCONA = 0x08
local MCP23017_INTCONB = 0x09
local MCP23017_IOCON = 0x0A
Expand Down Expand Up @@ -100,30 +92,30 @@ end

function mcp23017:writeIODIR(bReg, newByte)
writeByte(self.address, self.i2cId,
bReg and MCP23017_IODIRB or MCP23017_IODIRA, newByte)
bReg and 0x1 --[[const: MCP23017_IODIRB]] or 0x0 --[[const: MCP23017_IODIRA]], newByte)
end

function mcp23017:writeGPIO(bReg, newByte)
writeByte(self.address, self.i2cId,
bReg and MCP23017_GPIOB or MCP23017_GPIOA, newByte)
bReg and 0x13 --[[const: MCP23017_GPIOB]] or 0x12 --[[const: MCP23017_GPIOA]], newByte)
end

function mcp23017:readGPIO(bReg)
return readByte(self.address, self.i2cId,
bReg and MCP23017_GPIOB or MCP23017_GPIOA)
bReg and 0x13 --[[const: MCP23017_GPIOB]] or 0x12 --[[const: MCP23017_GPIOA]])
end

-- read pin input
function mcp23017:getPinState(bReg, pin)
return issetBit(readByte(self.address, self.i2cId,
bReg and MCP23017_GPIOB or MCP23017_GPIOA),
bReg and 0x13 --[[const: MCP23017_GPIOB]] or 0x12 --[[const: MCP23017_GPIOA]]),
checkPinIsInRange(pin))
end

-- set pin to low or high
function mcp23017:setPin(bReg, pin, state)
local a, i = self.address, self.i2cId
local inReq = bReg and MCP23017_GPIOB or MCP23017_GPIOA
local inReq = bReg and 0x13 --[[const: MCP23017_GPIOB]] or 0x12 --[[const: MCP23017_GPIOA]]
local inPin = checkPinIsInRange(pin)
local response = readByte(a, i, inReq)
writeByte(a, i, inReq,
Expand All @@ -134,7 +126,7 @@ end
-- set mode for a pin
function mcp23017:setMode(bReg, pin, mode)
local a, i = self.address, self.i2cId
local inReq = bReg and MCP23017_IODIRB or MCP23017_IODIRA
local inReq = bReg and 0x1 --[[const: MCP23017_IODIRB]] or 0x0 --[[const: MCP23017_IODIRA]]
local inPin = checkPinIsInRange(pin)
local response = readByte(a, i, inReq)
writeByte(a, i, inReq,
Expand All @@ -145,14 +137,14 @@ end
-- reset gpio mode
function mcp23017:reset()
local a, i = self.address, self.i2cId
writeByte(a, i, MCP23017_IODIRA, 0xFF)
writeByte(a, i, MCP23017_IODIRB, 0xFF)
writeByte(a, i, 0x0 --[[const: MCP23017_IODIRA]], 0xFF)
writeByte(a, i, 0x1 --[[const: MCP23017_IODIRB]], 0xFF)
end

-- setup internal pullup
function mcp23017:setInternalPullUp(bReg, iByte)
writeByte(self.address, self.i2cId,
bReg and MCP23017_DEFVALB or MCP23017_DEFVALA, iByte)
bReg and 0x7 --[[const: MCP23017_DEFVALB]] or 0x6 --[[const: MCP23017_DEFVALA]], iByte)
end

return function(address, i2cId)
Expand Down

0 comments on commit a3fe975

Please sign in to comment.