Page 5 of 5 FirstFirst ... 345
Results 41 to 45 of 45

Thread: LIFX bulbs

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

    Default

    I think you're right, I believe it is ms since epoch. To be honest, I don't know if I need it or not. I guess I just had it in my mind that I would decode everything but maybe I don't need to worry about it. I'm not sure how I'd truncate it but for now I'll just skip over it and come back to it if needed.
    Casey P.

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

    Default

    Ron, in a previous post you provided the following code and said it took care of the endianess. How does it take care of the endianess? Is it simply reversing the bytes without any regard to their value or does it look at the value and order them from least to greatest? If it's just reversing then how could it work on longer integers?

    Code:
     function getLittleEndianWord( data, pos )
         local v1 = string.byte(data, pos)
         local v2 = string.byte(data, pos+1)
         return bit.lshift(v2,8) + v1
     end

    I've been playing around with it and feeding in the following and it works great but what happens when I have to feed in a longer value?
    (actually I'm feeding in the raw binary packet but I'm showing the hex equivalent for illustration purposes)

    Code:
    29 00 00 54 00 00 00 00 D0 73 D5 14 A3 B2 00 00 4C 49 46 58 56 32 00 00 78 38 9C A0 96 0B 98 14 03 00 00 00 01 7C DD 00 00
    
    The last 4 bytes are the port number: 7C DD 00 00
    
    print( tonumber(string.format("%04X", getBigEndianWord(data,38)), 16)) -- prints 56700 (Correct)
    As I've found, only the numeric portions of the packet are in little endian. From what I understand the rest is in the given order. What I don't understand is if it's already little endian and I'm using a windows computer which stores values in little endian then why is it out of order at all? I just don't really understand endianess and what bytes get reversed and how and it's driving me nuts.
    Last edited by Ron; January 9th, 2017 at 09:50 AM. Reason: RON: fixed code so noone grabs this and is confused.
    Casey P.

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

    Default

    I just read this Wikipedia article and I think I've got a better understanding of endianess. It looks to me like it's about numerical position and not value as I thought. So in the number 123 one is the greatest significance because it's 100 while I was thinking of it as 1. So 123 in little endian would be 321. Is that correct? In my previous thinking 513 would be reordered to 135 which I think is wrong. It would be 315, 3 being the least significant and 5 the most.
    Casey P.

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

    Default

    Correct. Endianess is reversing the bytes, not sorting them! (Strictly your examples are not correct but I think you got the idea).

    To manually do the endianess go via hexadecimal representation. 2 hex digits is one byte.

    123 decimal is 7B in hex -> Which doesn't change! 7B if stored as 8 bit. If stored as 16 bit in little endian it becomes 7B 00, if stored as 16 bit big endian becomes 00 7B
    513 decimal is 201 in hex if stored as big endian it would be 0201, if stored in little endian it would be 0102

    bonus:

    Code:
    function getBigEndian32( data, pos )
        local v1 = string.byte(data, pos)
        local v2 = string.byte(data, pos+1)
        local v3 = string.byte(data, pos+2)
        local v4 = string.byte(data, pos+3)
    
        return bit.lshift(v1,24) + bit.lshift(v2,16) + bit.lshift(v3,8) + v4
    end
    
    function getBigEndian16( data, pos )
        local v1 = string.byte(data, pos)
        local v2 = string.byte(data, pos+1)
    
        return bit.lshift(v1,8) + v2
    end
    I think the code I gave before was wrong. This is the right one. Big endian stores the high bytes first.
    Last edited by Ron; January 9th, 2017 at 09:48 AM.
    Ron
    No support through PM

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

    Default

    The numbers I gave were for illustration purposes. I didn't mean for them to be converted to hex. My point was 1=100 and 3=3 so 1 is the more significant numeral in this case. I didn't realize until I read that article that the entire value gets flipped or stays the same. This makes a ton more sense to me as I've been trying like heck to figure out how the computer knows which order to put them back in. It's basically like this... In English we write in big endian, left to write, most significant value first, in Chinese I believe they write from right to left which would look like little endian to us but big endian to them. By George, I think I've got it! Lol
    Casey P.

Page 5 of 5 FirstFirst ... 345

Posting Permissions

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