ropod7 / pyboard_drive

22 stars 11 forks source link

Want to learn/understand about packing #8

Closed mchobby closed 8 years ago

mchobby commented 8 years ago

I'm trying to understand the role of the following line of code in _write(self, word, dc, recv, recvsize=2) method. Someone could help me to fill the missing part in my knowledge.

        if recv:
            fmt = '>B{0}'.format('B' * recvsize)
            recv = bytearray(1+recvsize)
            data = self.spi.send_recv(struct.pack(fmt, word), recv=recv)
            self._csx.high()
            return data

Let's say that I want to send the value (a word as I guess) 65535 and receiving 2 bytes. The pack formatting string is '>BBB' so big-endians (ok) with 24 bits (3 unsigned chars).

Question1: As I guess, if I want to receive 2 bytes over the SPI, I do need to send 2 bytes. Am I right?

Question 2: why do we have on byte more? (recvsize +1)

Question 3: If I do want to send the value 65536 (the word value) and receive 2 bytes, the format string is '>BBB' AND struct.pack( '>BBB', 65535 ) returns the following binary response b'\xff\x00\x00' (difficult for me to see how/where the 65535 is encoded/hidden). More over, if I do struct.unpack( '>BBB', b'\xff\x00\x00' ) I do get (255, 0, 0) as result. I'm getting wrong there because my initial value is completely lost. Please, tell me where I'm wrong?

Thank for your help or guiding for proper reading. Dominique

ropod7 commented 8 years ago

I think, there is between send byte and recv byte 1 null byte. Look datasheet command data tables.

It might be buggy, because there nothing to recieve in this driver.

Try look forward to test this feature. )

I am still far away from my lab.

ropod7 commented 8 years ago

There is an mistake in my previous message. I am creating now Read Memory feature for automatic font bgcolor applying. Now it's near of good condition. Tomorrow it should be in gthub as well.

ropod7 commented 8 years ago

There added new method, where writed:

For example:

    #    1. recieving sets 5 bytes
    #    2. firs 2 of them are useless (or null bytes)
    #    3. and just 3 last of them having a useful data:
    #        - those 3 bytes are RGB bytes (if we are reading from memory)
    #        - those 3 bytes have a 7 useful bits (and doesn't matter which color is)
    #        - we must get from them:
    #            \* just 5 largest bits for R and B colors
    #            \* just 6 largest bits for G color
ropod7 commented 8 years ago

And you may try it by running:

d = LCD() s = d.initCh(font=Heydings_23, color=WHITE) i=0 for color in [BLACK, RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, DARKGREY, LIGHTGREY, OLIVE, MAROON, DARKGREEN, DARKCYAN, NAVY, GREENYELLOW]: d.fillMonocolor(color) s.printChar(chr(64+i), 100, 100, scale=3) pyb.delay(500) i+=1

ropod7 commented 8 years ago

Memory reading feature give us amazing scope: p60416-202850

mchobby commented 8 years ago

I didn't realize immediately the advantages. Now it ring a bell!