This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Emergency Light, decoding of status
#6
You can access any value up to 4 bytes the same way. Second argument for getbit function is bit number (starting from 0, up to 31).

You can also access bytes separately by using the code from the first example:
Code:
hex = event.datahex
bytes = {}

for i = 1, #hex, 2 do
  bytes[ #bytes + 1 ] = tonumber(hex:sub(i, i + 1), 16)
end

function getbit(value, nr)
  value = bit.rshift(value, nr)
  return bit.band(value, 1)
end

-- first byte
grp.checkupdate('32/1/1', getbit(bytes[ 1 ], 0))
grp.checkupdate('32/1/2', getbit(bytes[ 1 ], 1))
grp.checkupdate('32/1/3', getbit(bytes[ 1 ], 2))
grp.checkupdate('32/1/4', getbit(bytes[ 1 ], 3))
grp.checkupdate('32/1/5', getbit(bytes[ 1 ], 4))
grp.checkupdate('32/1/6', getbit(bytes[ 1 ], 5))
grp.checkupdate('32/1/7', getbit(bytes[ 1 ], 6))
grp.checkupdate('32/1/8', getbit(bytes[ 1 ], 7))

-- second byte
grp.checkupdate('32/2/1', getbit(bytes[ 2 ], 0))
grp.checkupdate('32/2/2', getbit(bytes[ 2 ], 1))
grp.checkupdate('32/2/3', getbit(bytes[ 2 ], 2))
grp.checkupdate('32/2/4', getbit(bytes[ 2 ], 3))
grp.checkupdate('32/2/5', getbit(bytes[ 2 ], 4))
grp.checkupdate('32/2/6', getbit(bytes[ 2 ], 5))
grp.checkupdate('32/2/7', getbit(bytes[ 2 ], 6))
grp.checkupdate('32/2/8', getbit(bytes[ 2 ], 7))
Reply


Messages In This Thread
RE: Emergency Light, decoding of status - by admin - 21.09.2018, 10:54

Forum Jump: