Page 50 - Hacking Roomba
P. 50

Chapter 2 — Interfacing Basics            31




                                                  Setting and Clearing Bits

                          Many parts of the ROI represent multiple binary values within a single byte. For command
                          data bytes, these bits are like mini-commands within the larger command, telling Roomba
                          to do one small thing or another. In the MOTORS command, for instance, you can turn on or
                          off any combination of the three cleaning motors.
                          Bytes are divided into 8 binary digits called bits. Each bit has a position in a byte
                          and value equal to 2 position . Bit positions are numbered from 0 to 7. The corresponding bit
                          values are 1,2,4,8,16,32,64,128, more commonly represented in hexadecimal as
                          0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80. Sometimes a set of bits is called a bit field. A
                          byte is a bit field of 8 bits.

                          The act of turning on a bit (and thus turning on a motor in the case of the MOTORS com-
                          mand) is called setting a bit. Turning off a bit is called clearing a bit. Most of the time, you
                          can start with all bits cleared and then set the ones you want. In practice this means start-
                          ing with a value of zero and then joining together the bit values of the bits you want
                          to set.
                          For example, in the MOTORS command if you want to turn on only the main brush (bit 2, bit
                          value 0x04) and the side brush (bit 0, bit value 0x01), you can do this:
                              databyte = 0x04 | 0x01

                          The vertical bar (|) is the logical OR operator and works sort of like addition in this context.
                          You can also build up a byte incrementally using the “|=” operator:
                              databyte = 0
                              databyte |= 0x04
                              databate |= 0x01

                          The result is the same as before, but now you can add code to easily pick which statement
                          to run. Setting bits is similar but a little more complex, by using the logical NOT and logical
                          AND operators on a bit’s value. For example, to clear the side brush bit (and thus turn it
                          off), you would do this:
                              databyte &= ~0x01
                          This type of complex setting and clearing of bits isn’t used much in the ROI. Usually the
                          very first method of setting bits is used. For completeness, the general definitions in a C-like
                          language for setting and clearing a bit in a byte given the bit’s position are as follows:

                              #define setbit(byte,bit) (byte) |= (1 << (bit))
                              #define clrbit(byte,bit) (byte) &= ~(1 << (bit))
   45   46   47   48   49   50   51   52   53   54   55