Page 1 of 5 123 ... LastLast
Results 1 to 10 of 45

Thread: LIFX bulbs

  1. #1
    Join Date
    May 2004
    Posts
    2,589

    Default LIFX bulbs

    Has anyone considered developing a plugin for LIFX LED bulbs? They have an api available for developers.
    Beware of the robot.

  2. #2
    Join Date
    Jun 2012
    Location
    Wabash, IN
    Posts
    102

    Default

    I'm looking into it but I'm running into problems working with hex values in Lua. The LIFX Lan protocol is binary/hex and is difficult to deal with in Lua. It doesn't appear to me that there's any built in libraries for dealing with binary to hex conversion or big endian to little endian conversion. If anyone knows anything about this I'd appreciate some direction.

  3. #3
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,553

    Default

    There are a bunch of hex to binary functions under math. For example math.binaryToHexString and hexStringToBinary. There is no big-to-little or vice verse built in but that should be too hard. What is the binary you get and what is the format of it. We'll work out how to extract data from it together.
    Ron
    No support through PM

  4. #4
    Join Date
    Jun 2012
    Location
    Wabash, IN
    Posts
    102

    Default

    Hi Ron, I found those functions after posting however I'm extremely confused by them. The LIFX API walks you through building a binary packet then it shows it has to be converted to little endian hex.

    This is a partial example of a header built in binary
    Code:
    0011 0100 0000 0000 


    which should then convert to a hex value of (big endian)
    Code:
    34 00


    This would then need to be converted to little endian
    Code:
    00 34


    What I'm most confused about are the examples in the Girder manual relating to binary always seem to show text characters. For example binaryToHexString:

    Code:
    print( math.binaryToHexString( "Hello World" ) )
    Code:
    output: "48 65 6C 6C 6F 20 57 6F 72 6C 64"


    I have always known binary to be 1's and 0's so I'm extremely confused by the example given. Can you shed some light on this for me Ron?

    NOTE: The LIFX example shown comes from: https://lan.developer.lifx.com/docs/...-a-lifx-packet

    Thanks,
    Casey



  5. #5
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,553

    Default

    See attached, don't confuse the binary in the math functions with base2 numbers. It's meant to indicate that the string is treated as a opaque sequence of bytes without treating it as ascii. I just happened to pick a string of bytes "Hello World" that one can type, but it's used as a sequence of bytes with the first byte having value decimal 72 or or 0x48. Anyway here is a code snippet to extract a word out of a sequence of bytes. It also fixes the endianess in one go.

    Code:
    
     function getBigEndianWord( data, pos )
         local v1 = string.byte(data, pos)
         local v2 = string.byte(data, pos+1)
         return bit.lshift(v2,8) + v1
     end
     
    
     --[[ 
       the binary (base-2) number 00110100 is 54 in base-10 aka decimal
       Don't confuse binary in this context with math.**binary*** functions. Binary in
    
       that context simply means that the string can contain any values inside including
    
       zeros. So when you get data from a Lifx it probably is already binary trying to
       print that value will result in garbage. 
      --]]
     
    
     local bin = "\52\00" -- equal to 00110100 00000000
     print( string.format("%04X", getBigEndianWord(bin,1)))
    Ron
    No support through PM

  6. #6
    Join Date
    Jun 2012
    Location
    Wabash, IN
    Posts
    102

    Default

    Where does the "\52\00" value come from Ron? I ran your code and I got "0034" as expected, I'm just not sure how you came up with the value you entered. I have used an online binary to hex converter and when I enter "0011 0100 0000 0000" from the API example I get "3400" which is correct according to the example except for the fact it's bid endian. So how in girder/lua can I enter that value and get 3400? I've read your last post several times trying to get my head around it. Are you saying the built in math functions will not be useful for this? I apologize for my ignorance on this, I've never had to work in binary or hex so this is all new to me.

    Thanks,
    Casey

  7. #7
    Join Date
    Jun 2012
    Location
    Wabash, IN
    Posts
    102

    Default

    I did a little more digging Ron and I found the following:

    Code:
    local dec = tonumber("0011010000000000",2) 
     local hex = math.decimaltohex(dec)
     print(hex) -- outputs 3400
    I have to admit I don't understand your function "getBigEndianWord" but I thought I would run the result through it and I get "3433" which is not what I expected to get. Remember, my goal is 00 34. Thankfully, it looks to me like this is all the binary I'll have to deal with as I believe the rest of the packet is constructed solely of hex and is just appended to the end.

    The complete header hex in the example is: ?? ?? 00 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 00 00 00" The question marks designate a reserved space for packet size and will be changed once the packet is completely built.

    The final packet in the example is: 31 00 00 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 66 00 00 00 00 55 55 FF FF FF FF AC 0D 00 04 00 00

    This packet turns one or all lights green depending on which ones you send it to.

  8. #8
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,553

    Default

    "\52\00" -- equal to 00110100 00000000

    00110100 = 52 in decimal or 34 in hexadecimal
    00000000 = 0 in decimal or 0 in hexadecimal
    I'm just feeding the algorithm what you would get from Lifx.

    tonumber("0011010000000000",2) returns a number not a byte string. Hence putting it into getBigEndianWord fails. The value returnsed by tonumber is not equal to the byte string indicated in the lifx documents "0011010000000000"
    Ron
    No support through PM

  9. #9
    Join Date
    Jun 2012
    Location
    Wabash, IN
    Posts
    102

    Default

    I threw the following code together Ron, is it not correct? I know it's probably not very elegant as I'm not very good with Lua but I'm getting the result I expect to see.

    Code:
    function headerBinToHex(binStr)
        local dec = tonumber(binStr,2)     
        local hex = math.decimaltohex(dec)  
        local counter = 1
        local temp, val1, val2, finalHex = '', '', '', ''
        
        for i = 1, string.len(hex) do
            if counter == 1 or counter == 3 then
                temp = string.sub(hex,i,i)
            elseif counter == 2 then
                val1 = temp .. string.sub(hex,i,i)
            elseif counter == 4 then        
                val2 = temp .. string.sub(hex,i,i)
                counter = 0
                 
                if finalHex ~= '' then
                    finalHex = finalHex .. ' '
                end
                 
                if val1 > val2 then
                    finalHex = finalHex .. val2 .. ' ' .. val1
                else
                    finalHex = finalHex .. val1 .. ' ' .. val2
                end
            end 
            
            counter = counter + 1
        end
    
        return finalHex
    end
    
    print(headerBinToHex("0011010000000000")) -- outputs: 00 34, which is what the example shows

  10. #10
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,553

    Default

    The one thing I am worried about it that you are assuming that you will get the data in a base-2 string. You will most likely not. They are using that representation in the documentation for clarity (though it appears to confuse people more than anything). You will most likely get a string of bytes, for which you can use the function I gave you. So first thing is now to get a transport/connection up that actually gives you the data from the device. Then you can figure out how to process it.
    Ron
    No support through PM

Page 1 of 5 123 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •